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 3 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
31 changes: 25 additions & 6 deletions cmd/generate_gatewayapi_httproute.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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"
)
Expand All @@ -23,11 +24,24 @@ func generateGatewayApiHttpRouteCommand() *cobra.Command {
Use: "httproute",
Short: "Generate Gateway API HTTPRoute from OpenAPI 3.0.X",
Long: "Generate Gateway API HTTPRoute from OpenAPI 3.0.X",
RunE: runGenerateGatewayApiHttpRoute,
RunE: func(cmd *cobra.Command, args []string) error {
outputFormat, err := cmd.Flags().GetString("output-format")
if err != nil {
return err
}

oasPath, err := cmd.Flags().GetString("oas")
jasonmadigan marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}

return runGenerateGatewayApiHttpRoute(cmd, oasPath, outputFormat)
},
}

// 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().StringP("output-format", "o", "yaml", "Output format: 'yaml' or 'json'. Default: yaml")
err := cmd.MarkFlagRequired("oas")
if err != nil {
panic(err)
Expand All @@ -36,8 +50,8 @@ func generateGatewayApiHttpRouteCommand() *cobra.Command {
return cmd
}

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

httpRoute := buildHTTPRoute(doc)

jsonData, err := json.Marshal(httpRoute)
var outputBytes []byte
if outputFormat == "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
31 changes: 25 additions & 6 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 @@ -23,11 +24,24 @@ func generateKuadrantAuthPolicyCommand() *cobra.Command {
Use: "authpolicy",
Short: "Generate Kuadrant AuthPolicy from OpenAPI 3.0.X",
Long: "Generate Kuadrant AuthPolicy from OpenAPI 3.0.X",
RunE: runGenerateKuadrantAuthPolicy,
RunE: func(cmd *cobra.Command, args []string) error {
outputFormat, err := cmd.Flags().GetString("output-format")
if err != nil {
return err
}

oasPath, err := cmd.Flags().GetString("oas")
if err != nil {
return err
}

return runGenerateKuadrantAuthPolicy(cmd, oasPath, outputFormat)
},
}

// 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().StringP("output-format", "o", "yaml", "Output format: 'yaml' or 'json'. Default: yaml")
err := cmd.MarkFlagRequired("oas")
if err != nil {
panic(err)
Expand All @@ -36,8 +50,8 @@ func generateKuadrantAuthPolicyCommand() *cobra.Command {
return cmd
}

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

ap := buildAuthPolicy(doc)

jsonData, err := json.Marshal(ap)
var outputBytes []byte
if outputFormat == "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
44 changes: 32 additions & 12 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 @@ -21,23 +23,36 @@ import (
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,
Short: "Generate Kuadrant Rate Limit Policy from OpenAPI 3.0.X",
Long: "Generate Kuadrant Rate Limit Policy from OpenAPI 3.0.X",
RunE: func(cmd *cobra.Command, args []string) error {
outputFormat, err := cmd.Flags().GetString("output-format")
if err != nil {
return err
}

oasPath, err := cmd.Flags().GetString("oas")
if err != nil {
return err
}

return runGenerateKuadrantRateLimitPolicy(cmd, oasPath, outputFormat)
},
}

// 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().String("oas", "", "Path to OpenAPI spec file (in JSON or YAML format) or URL (required)")
cmd.Flags().StringP("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)
func runGenerateKuadrantRateLimitPolicy(cmd *cobra.Command, oasPath, outputFormat string) error {
oasDataRaw, err := utils.ReadExternalResource(oasPath)
if err != nil {
return err
}
Expand All @@ -55,12 +70,17 @@ func runGenerateKuadrantRateLimitPolicy(cmd *cobra.Command, args []string) error

rlp := buildRateLimitPolicy(doc)

jsonData, err := json.Marshal(rlp)
var outputBytes []byte
if outputFormat == "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