diff --git a/cmd/generate_gatewayapi_httproute.go b/cmd/generate_gatewayapi_httproute.go index fea188d..93b7960 100644 --- a/cmd/generate_gatewayapi_httproute.go +++ b/cmd/generate_gatewayapi_httproute.go @@ -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", @@ -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 } diff --git a/pkg/gatewayapi/http_route.go b/pkg/gatewayapi/http_route.go index dd5438a..b58470e 100644 --- a/pkg/gatewayapi/http_route.go +++ b/pkg/gatewayapi/http_route.go @@ -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" @@ -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)