Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource generation - default to yaml, add option for output formats (JSON or YAML) #66

Merged
merged 4 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ Generate Gateway API resources from an OpenAPI 3.x specification

| Subcommand | Description | Flags |
| ---------- | ------------------------------------------------ | --------------------------------- |
| `httproute`| Generate Gateway API HTTPRoute from OpenAPI 3.0.X| `--oas string` Path or URL to OpenAPI spec (required) |
| `httproute`| Generate Gateway API HTTPRoute from OpenAPI 3.0.X| `--oas string` Path to OpenAPI spec file (in JSON or YAML format), URL, or '-' to read from standard input (required). `-o` Output format: 'yaml' or 'json'. Default: yaml |

##### `generate kuadrant`

Generate Kuadrant resources from an OpenAPI 3.x specification

| Subcommand | Description | Flags |
| ---------------- | ------------------------------------------------- | --------------------------------- |
| `authpolicy` | Generate a [Kuadrant AuthPolicy](https://docs.kuadrant.io/kuadrant-operator/doc/auth/) from an OpenAPI 3.0.x specification | `--oas string` Path or URL to OpenAPI spec (required) |
| `ratelimitpolicy`| Generate [Kuadrant RateLimitPolicy](https://docs.kuadrant.io/kuadrant-operator/doc/rate-limiting/) from an OpenAPI 3.0.x specification | `--oas string` Path or URL to OpenAPI spec (required) |
| `authpolicy` | Generate a [Kuadrant AuthPolicy](https://docs.kuadrant.io/kuadrant-operator/doc/auth/) from an OpenAPI 3.0.x specification | `--oas string` Path to OpenAPI spec file (in JSON or YAML format), URL, or '-' to read from standard input (required). `-o` Output format: 'yaml' or 'json'. Default: yaml |
| `ratelimitpolicy`| Generate [Kuadrant RateLimitPolicy](https://docs.kuadrant.io/kuadrant-operator/doc/rate-limiting/) from an OpenAPI 3.0.x specification | `--oas string` Path to OpenAPI spec file (in JSON or YAML format), URL, or '-' to read from standard input (required). `-o` Output format: 'yaml' or 'json'. Default: yaml |


#### `install`
Expand Down
16 changes: 12 additions & 4 deletions cmd/generate_gatewayapi_httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
"github.com/kuadrant/kuadrantctl/pkg/gatewayapi"
"github.com/kuadrant/kuadrantctl/pkg/utils"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
gatewayapiv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
)

var (
generateGatewayAPIHTTPRouteOAS string
generateGatewayAPIHTTPRouteOAS string
generateGatewayAPIHTTPRouteFormat string
)

//kuadrantctl generate gatewayapi httproute --oas [OAS_FILE_PATH | OAS_URL | @]
Expand All @@ -27,7 +29,8 @@ func generateGatewayApiHttpRouteCommand() *cobra.Command {
}

// OpenAPI ref
cmd.Flags().StringVar(&generateGatewayAPIHTTPRouteOAS, "oas", "", "/path/to/file.[json|yaml|yml] OR http[s]://domain/resource/path.[json|yaml|yml] OR @ (required)")
cmd.Flags().StringVar(&generateGatewayAPIHTTPRouteOAS, "oas", "", "Path to OpenAPI spec file (in JSON or YAML format), URL, or '-' to read from standard input (required)")
cmd.Flags().StringVarP(&generateGatewayAPIHTTPRouteFormat, "output-format", "o", "yaml", "Output format: 'yaml' or 'json'. Default: yaml")
err := cmd.MarkFlagRequired("oas")
if err != nil {
panic(err)
Expand Down Expand Up @@ -55,12 +58,17 @@ func runGenerateGatewayApiHttpRoute(cmd *cobra.Command, args []string) error {

httpRoute := buildHTTPRoute(doc)

jsonData, err := json.Marshal(httpRoute)
var outputBytes []byte
if generateGatewayAPIHTTPRouteFormat == "json" {
outputBytes, err = json.Marshal(httpRoute)
} else { // default to YAML if not explicitly JSON
outputBytes, err = yaml.Marshal(httpRoute)
}
if err != nil {
return err
}

fmt.Fprintln(cmd.OutOrStdout(), string(jsonData))
fmt.Fprintln(cmd.OutOrStdout(), string(outputBytes))
return nil
}

Expand Down
20 changes: 16 additions & 4 deletions cmd/generate_kuadrant_authpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/getkin/kin-openapi/openapi3"
kuadrantapiv1beta2 "github.com/kuadrant/kuadrant-operator/api/v1beta2"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayapiv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
Expand All @@ -16,6 +17,11 @@ import (
"github.com/kuadrant/kuadrantctl/pkg/utils"
)

var (
generateAuthPolicyOAS string
generateAuthPolicyFormat string
)

//kuadrantctl generate kuadrant authpolicy --oas [OAS_FILE_PATH | OAS_URL | @]

func generateKuadrantAuthPolicyCommand() *cobra.Command {
Expand All @@ -27,7 +33,8 @@ func generateKuadrantAuthPolicyCommand() *cobra.Command {
}

// OpenAPI ref
cmd.Flags().StringVar(&generateGatewayAPIHTTPRouteOAS, "oas", "", "/path/to/file.[json|yaml|yml] OR http[s]://domain/resource/path.[json|yaml|yml] OR @ (required)")
cmd.Flags().StringVar(&generateAuthPolicyOAS, "oas", "", "Path to OpenAPI spec file (in JSON or YAML format), URL, or '-' to read from standard input (required)")
cmd.Flags().StringVarP(&generateAuthPolicyFormat, "output-format", "o", "yaml", "Output format: 'yaml' or 'json'. Default: yaml")
err := cmd.MarkFlagRequired("oas")
if err != nil {
panic(err)
Expand All @@ -37,7 +44,7 @@ func generateKuadrantAuthPolicyCommand() *cobra.Command {
}

func runGenerateKuadrantAuthPolicy(cmd *cobra.Command, args []string) error {
oasDataRaw, err := utils.ReadExternalResource(generateGatewayAPIHTTPRouteOAS)
oasDataRaw, err := utils.ReadExternalResource(generateAuthPolicyOAS)
if err != nil {
return err
}
Expand All @@ -55,12 +62,17 @@ func runGenerateKuadrantAuthPolicy(cmd *cobra.Command, args []string) error {

ap := buildAuthPolicy(doc)

jsonData, err := json.Marshal(ap)
var outputBytes []byte
if generateAuthPolicyFormat == "json" {
outputBytes, err = json.Marshal(ap)
} else { // default to YAML if not explicitly JSON
outputBytes, err = yaml.Marshal(ap)
}
if err != nil {
return err
}

fmt.Fprintln(cmd.OutOrStdout(), string(jsonData))
fmt.Fprintln(cmd.OutOrStdout(), string(outputBytes))
return nil
}

Expand Down
33 changes: 23 additions & 10 deletions cmd/generate_kuadrant_ratelimitpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package cmd
import (
"encoding/json"
"fmt"
"os"

"github.com/getkin/kin-openapi/openapi3"
kuadrantapiv1beta2 "github.com/kuadrant/kuadrant-operator/api/v1beta2"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
gatewayapiv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2"
gatewayapiv1beta1 "sigs.k8s.io/gateway-api/apis/v1beta1"
Expand All @@ -18,26 +20,32 @@ import (

//kuadrantctl generate kuadrant ratelimitpolicy --oas [OAS_FILE_PATH | OAS_URL | @]

var (
generateRateLimitPolicyOAS string
generateRateLimitPolicyFormat string
)

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",
Short: "Generate Kuadrant Rate Limit Policy from OpenAPI 3.0.X",
Long: "Generate Kuadrant Rate Limit Policy 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)
cmd.Flags().StringVar(&generateRateLimitPolicyOAS, "oas", "", "Path to OpenAPI spec file (in JSON or YAML format), URL, or '-' to read from standard input (required)")
cmd.Flags().StringVarP(&generateRateLimitPolicyFormat, "output-format", "o", "yaml", "Output format: 'yaml' or 'json'. Default: yaml")

if err := cmd.MarkFlagRequired("oas"); err != nil {
fmt.Println("Error setting 'oas' flag as required:", err)
os.Exit(1)
}

return cmd
}

func runGenerateKuadrantRateLimitPolicy(cmd *cobra.Command, args []string) error {
oasDataRaw, err := utils.ReadExternalResource(generateGatewayAPIHTTPRouteOAS)
oasDataRaw, err := utils.ReadExternalResource(generateRateLimitPolicyOAS)
if err != nil {
return err
}
Expand All @@ -55,12 +63,17 @@ func runGenerateKuadrantRateLimitPolicy(cmd *cobra.Command, args []string) error

rlp := buildRateLimitPolicy(doc)

jsonData, err := json.Marshal(rlp)
var outputBytes []byte
if generateRateLimitPolicyFormat == "json" {
outputBytes, err = json.Marshal(rlp)
} else { // default to YAML if not explicitly JSON
outputBytes, err = yaml.Marshal(rlp)
}
if err != nil {
return err
}

fmt.Fprintln(cmd.OutOrStdout(), string(jsonData))
fmt.Fprintln(cmd.OutOrStdout(), string(outputBytes))
return nil
}

Expand Down
5 changes: 3 additions & 2 deletions doc/generate-gateway-api-httproute.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ Usage:
kuadrantctl generate gatewayapi httproute [flags]

Flags:
-h, --help help for httproute
--oas string /path/to/file.[json|yaml|yml] OR http[s]://domain/resource/path.[json|yaml|yml] OR @ (required)
-h, --help help for httproute
--oas string Path to OpenAPI spec file (in JSON or YAML format), URL, or '-' to read from standard input (required)
-o Output format: 'yaml' or 'json'. Default: yaml

Global Flags:
-v, --verbose verbose output
Expand Down
3 changes: 2 additions & 1 deletion doc/generate-kuadrant-auth-policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ Usage:

Flags:
-h, --help help for authpolicy
--oas string /path/to/file.[json|yaml|yml] OR http[s]://domain/resource/path.[json|yaml|yml] OR @ (required)
--oas string Path to OpenAPI spec file (in JSON or YAML format), URL, or '-' to read from standard input (required)
-o Output format: 'yaml' or 'json'. Default: yaml

Global Flags:
-v, --verbose verbose output
Expand Down
3 changes: 2 additions & 1 deletion doc/generate-kuadrant-rate-limit-policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ Usage:

Flags:
-h, --help help for ratelimitpolicy
--oas string /path/to/file.[json|yaml|yml] OR http[s]://domain/resource/path.[json|yaml|yml] OR @ (required)
--oas string Path to OpenAPI spec file (in JSON or YAML format), URL, or '-' to read from standard input (required)
-o Output format: 'yaml' or 'json'. Default: yaml

Global Flags:
-v, --verbose verbose output
Expand Down
4 changes: 2 additions & 2 deletions pkg/utils/external_resource_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
)

// ReadExternalResource reads data streams from external resources. Currently implemented:
// - '@' for STDIN
// - '-' or '@' for STDIN
jasonmadigan marked this conversation as resolved.
Show resolved Hide resolved
// - URLs (HTTP[S])
// - Files
func ReadExternalResource(resource string) ([]byte, error) {
if resource == "@" {
if resource == "-" || resource == "@" {
return io.ReadAll(os.Stdin)
}

Expand Down
Loading