-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
368 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/getkin/kin-openapi/openapi3" | ||
kuadrantapiv1beta2 "github.com/kuadrant/kuadrant-operator/api/v1beta2" | ||
"github.com/spf13/cobra" | ||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2" | ||
gatewayapiv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1" | ||
|
||
"github.com/kuadrant/kuadrantctl/pkg/gatewayapi" | ||
"github.com/kuadrant/kuadrantctl/pkg/kuadrantapi" | ||
"github.com/kuadrant/kuadrantctl/pkg/utils" | ||
) | ||
|
||
//kuadrantctl generate kuadrant httproute --oas [OAS_FILE_PATH | OAS_URL | @] | ||
|
||
func generateKuadrantRateLimitPolicyCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "ratelimitpolicy", | ||
Short: "Generate Kuadrant RateLimitPolicy from OpenAPI 3.0.X", | ||
Long: "Generate Kuadrant RateLimitPolicy from OpenAPI 3.0.X", | ||
RunE: runGenerateKuadrantRateLimitPolicy, | ||
} | ||
|
||
// OpenAPI ref | ||
cmd.Flags().StringVar(&generateGatewayAPIHTTPRouteOAS, "oas", "", "/path/to/file.[json|yaml|yml] OR http[s]://domain/resource/path.[json|yaml|yml] OR @ (required)") | ||
err := cmd.MarkFlagRequired("oas") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return cmd | ||
} | ||
|
||
func runGenerateKuadrantRateLimitPolicy(cmd *cobra.Command, args []string) error { | ||
oasDataRaw, err := utils.ReadExternalResource(generateGatewayAPIHTTPRouteOAS) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
openapiLoader := openapi3.NewLoader() | ||
doc, err := openapiLoader.LoadFromData(oasDataRaw) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = doc.Validate(openapiLoader.Context) | ||
if err != nil { | ||
return fmt.Errorf("OpenAPI validation error: %w", err) | ||
} | ||
|
||
rlp := buildRateLimitPolicy(doc) | ||
|
||
jsonData, err := json.Marshal(rlp) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Fprintln(cmd.OutOrStdout(), string(jsonData)) | ||
return nil | ||
} | ||
|
||
func buildRateLimitPolicy(doc *openapi3.T) *kuadrantapiv1beta2.RateLimitPolicy { | ||
routeMeta := gatewayapi.HTTPRouteObjectMetaFromOAS(doc) | ||
|
||
rlp := &kuadrantapiv1beta2.RateLimitPolicy{ | ||
TypeMeta: v1.TypeMeta{ | ||
APIVersion: "kuadrant.io/v1beta2", | ||
Kind: "RateLimitPolicy", | ||
}, | ||
ObjectMeta: kuadrantapi.RateLimitPolicyObjectMetaFromOAS(doc), | ||
Spec: kuadrantapiv1beta2.RateLimitPolicySpec{ | ||
TargetRef: gatewayapiv1alpha2.PolicyTargetReference{ | ||
Group: gatewayapiv1beta1.Group("gateway.networking.k8s.io"), | ||
Kind: gatewayapiv1beta1.Kind("HTTPRoute"), | ||
Name: gatewayapiv1beta1.ObjectName(routeMeta.Name), | ||
}, | ||
Limits: kuadrantapi.RateLimitPolicyLimitsFromOAS(doc), | ||
}, | ||
} | ||
|
||
if routeMeta.Namespace != "" { | ||
rlp.Spec.TargetRef.Namespace = &[]gatewayapiv1beta1.Namespace{ | ||
gatewayapiv1beta1.Namespace(routeMeta.Namespace), | ||
}[0] | ||
} | ||
|
||
return rlp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## OpenAPI 3.0.X Kuadrant Extensions | ||
|
||
TODO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.