Skip to content

Commit

Permalink
copy labels, rules
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmadigan committed Nov 15, 2023
1 parent 0379791 commit db3dd9f
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
16 changes: 15 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,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
}
76 changes: 76 additions & 0 deletions pkg/gatewayapi/http_route.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit db3dd9f

Please sign in to comment.