From 84982bc66ab3b333ab2186e5157fc5543a3f06f0 Mon Sep 17 00:00:00 2001 From: Jason Madigan Date: Wed, 15 Nov 2023 12:48:52 +0000 Subject: [PATCH] copy labels --- cmd/generate_gatewayapi_httproute.go | 10 +++++++++- pkg/gatewayapi/http_route.go | 24 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) 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)