From db3dd9f49053a1dc69c95d1fed92e63c14df053f Mon Sep 17 00:00:00 2001 From: Jason Madigan Date: Wed, 15 Nov 2023 12:48:52 +0000 Subject: [PATCH] copy labels, rules --- cmd/generate_gatewayapi_httproute.go | 16 +++++- pkg/gatewayapi/http_route.go | 76 ++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/cmd/generate_gatewayapi_httproute.go b/cmd/generate_gatewayapi_httproute.go index fea188d..1cd75d7 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,18 @@ 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 + } + + // Extract and set rules + extractedRules, err := gatewayapi.ExtractRulesFromOAS(doc) + if err == nil { + httpRoute.Spec.Rules = append(httpRoute.Spec.Rules, extractedRules...) + } + + return httpRoute } diff --git a/pkg/gatewayapi/http_route.go b/pkg/gatewayapi/http_route.go index dd5438a..40b7918 100644 --- a/pkg/gatewayapi/http_route.go +++ b/pkg/gatewayapi/http_route.go @@ -1,6 +1,9 @@ package gatewayapi import ( + "encoding/json" + "fmt" + "github.com/getkin/kin-openapi/openapi3" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/utils/ptr" @@ -113,6 +116,79 @@ func HTTPRouteRulesFromOAS(doc *openapi3.T) []gatewayapiv1beta1.HTTPRouteRule { return rules } +func ConvertOASLabelsAndRulesToHTTPRoute(doc *openapi3.T) (*gatewayapiv1beta1.HTTPRoute, error) { + if doc.Info == nil || doc.Info.Extensions == nil { + return nil, fmt.Errorf("no extensions found in OAS info") + } + + var httpRoute gatewayapiv1beta1.HTTPRoute + + // Extract and set labels + if labels, ok := ExtractLabelsFromOAS(doc); ok { + httpRoute.ObjectMeta.Labels = labels + } + + // Extract and set rules + if rules, err := ExtractRulesFromOAS(doc); err == nil { + httpRoute.Spec.Rules = rules + } else { + return nil, err + } + + return &httpRoute, nil +} + +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 ExtractRulesFromOAS(doc *openapi3.T) ([]gatewayapiv1beta1.HTTPRouteRule, error) { + if doc.Info == nil || doc.Info.Extensions == nil { + return nil, fmt.Errorf("no extensions found in OAS info") + } + + 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 rulesInterface, ok := route["rules"]; ok { + rulesBytes, err := json.Marshal(rulesInterface) + if err != nil { + return nil, fmt.Errorf("error marshaling rules: %w", err) + } + + var rules []gatewayapiv1beta1.HTTPRouteRule + err = json.Unmarshal(rulesBytes, &rules) + if err != nil { + return nil, fmt.Errorf("error unmarshaling rules into HTTPRouteRules: %w", err) + } + + return rules, nil + } + } + } + } + + return nil, fmt.Errorf("rules not found in kuadrant extension") +} + func buildHTTPRouteRule(path string, pathItem *openapi3.PathItem, verb string, op *openapi3.Operation, backendRefs []gatewayapiv1beta1.HTTPBackendRef) gatewayapiv1beta1.HTTPRouteRule { match := utils.OpenAPIMatcherFromOASOperations(path, pathItem, verb, op)