Skip to content

Commit

Permalink
copy labels
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmadigan committed Nov 22, 2023
1 parent 0379791 commit 84982bc
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
10 changes: 9 additions & 1 deletion cmd/generate_gatewayapi_httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func runGenerateGatewayApiHttpRoute(cmd *cobra.Command, args []string) error {
}

func buildHTTPRoute(doc *openapi3.T) *gatewayapiv1beta1.HTTPRoute {
return &gatewayapiv1beta1.HTTPRoute{
httpRoute := &gatewayapiv1beta1.HTTPRoute{
TypeMeta: v1.TypeMeta{
APIVersion: "gateway.networking.k8s.io/v1beta1",
Kind: "HTTPRoute",
Expand All @@ -79,4 +79,12 @@ func buildHTTPRoute(doc *openapi3.T) *gatewayapiv1beta1.HTTPRoute {
Rules: gatewayapi.HTTPRouteRulesFromOAS(doc),
},
}

// Extract and set labels
labels, ok := gatewayapi.ExtractLabelsFromOAS(doc)
if ok {
httpRoute.ObjectMeta.Labels = labels
}

return httpRoute
}
24 changes: 24 additions & 0 deletions pkg/gatewayapi/http_route.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gatewayapi

import (
"fmt"

"github.com/getkin/kin-openapi/openapi3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
Expand Down Expand Up @@ -113,6 +115,28 @@ func HTTPRouteRulesFromOAS(doc *openapi3.T) []gatewayapiv1beta1.HTTPRouteRule {
return rules
}

func ExtractLabelsFromOAS(doc *openapi3.T) (map[string]string, bool) {
if doc.Info == nil || doc.Info.Extensions == nil {
return nil, false
}

if extension, ok := doc.Info.Extensions["x-kuadrant"]; ok {
if extensionMap, ok := extension.(map[string]interface{}); ok {
if route, ok := extensionMap["route"].(map[string]interface{}); ok {
if labelsInterface, ok := route["labels"]; ok {
labels := make(map[string]string)
for key, value := range labelsInterface.(map[string]interface{}) {
labels[key] = fmt.Sprint(value)
}
return labels, true
}
}
}
}

return nil, false
}

func buildHTTPRouteRule(path string, pathItem *openapi3.PathItem, verb string, op *openapi3.Operation, backendRefs []gatewayapiv1beta1.HTTPBackendRef) gatewayapiv1beta1.HTTPRouteRule {
match := utils.OpenAPIMatcherFromOASOperations(path, pathItem, verb, op)

Expand Down

0 comments on commit 84982bc

Please sign in to comment.