Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 17 additions & 0 deletions tools/openapi2crd/.mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
all: true
dir: '{{.InterfaceDir}}'
filename: '{{base (trimSuffix ".go" .InterfaceFile)}}_mock.go'
force-file-write: true
formatter: goimports
log-level: info
structname: '{{.InterfaceName}}{{.Mock}}'
pkgname: '{{.SrcPackageName}}'
recursive: false
template: testify
packages:
tools/openapi2crd/pkg/config:
config:
all: true
tools/openapi2crd/pkg/plugins:
config:
all: true
116 changes: 116 additions & 0 deletions tools/openapi2crd/cmd/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package cmd

import (
"context"
"fmt"
"os"
"strings"

"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/spf13/viper"

configv1alpha1 "tools/openapi2crd/pkg/apis/config/v1alpha1"
"tools/openapi2crd/pkg/config"
"tools/openapi2crd/pkg/exporter"
"tools/openapi2crd/pkg/generator"
"tools/openapi2crd/pkg/plugins"
)

const (
outputOption = "output"
configOption = "config"
forceOption = "force"

readOnly = os.O_RDONLY
)

func initConfig() {
viper.AutomaticEnv()
}

func RunCmd(ctx context.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "openapi2crd SPEC_FILE",
Short: "Generate CustomResourceDefinition from OpenAPI 3.0 document",
SilenceErrors: true,
SilenceUsage: true,
PreRun: func(cmd *cobra.Command, args []string) {
_ = viper.BindPFlags(cmd.Flags())
},
RunE: func(cmd *cobra.Command, args []string) error {
fs := afero.NewOsFs()

outputOptionValue := viper.GetString(outputOption)
forceOptionValue := viper.GetBool(forceOption)
fsExporter, err := exporter.New(fs, outputOptionValue, forceOptionValue)
if err != nil {
return err
}

err = fsExporter.Start()
if err != nil {
return fmt.Errorf("error starting the exporter: %w", err)
}

configPath := viper.GetString(configOption)
file, err := fs.OpenFile(configPath, readOnly, 0o644)
if err != nil {
return fmt.Errorf("error opening the file %s: %w", configPath, err)
}

configData, err := afero.ReadAll(file)
if err != nil {
return fmt.Errorf("error reading the file %s: %w", configPath, err)
}

cfg, err := config.Parse(configData)
if err != nil {
return fmt.Errorf("error parsing config: %w", err)
}

definitionsMap := map[string]configv1alpha1.OpenAPIDefinition{}
for _, def := range cfg.Spec.OpenAPIDefinitions {
definitionsMap[def.Name] = def
}

catalog := plugins.NewCatalog()
pluginSets, err := catalog.BuildSets(cfg.Spec.PluginSets)
if err != nil {
return fmt.Errorf("error creating plugin set: %w", err)
}

openapiLoader := config.NewKinOpeAPI(fs)

for _, crdConfig := range cfg.Spec.CRDConfig {
pluginSet, err := plugins.GetPluginSet(pluginSets, crdConfig.PluginSet)
if err != nil {
return fmt.Errorf("error getting plugin set %q: %w", crdConfig.PluginSet, err)
}

g := generator.NewGenerator(definitionsMap, pluginSet, openapiLoader)
crd, err := g.Generate(ctx, &crdConfig)
if err != nil {
return err
}

err = fsExporter.Export(crd)
if err != nil {
return err
}
}

return fsExporter.Close()
},
}

cmd.Flags().StringP(outputOption, "o", "", "Path to output file (required)")
_ = cmd.MarkFlagRequired(outputOption)
cmd.Flags().StringP(configOption, "c", "", "Path to the config file (required)")
cmd.Flags().BoolP(forceOption, "f", false, "Force overwrite the output file if it exists")
cobra.OnInitialize(initConfig)

viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))

return cmd
}
15 changes: 10 additions & 5 deletions tools/openapi2crd/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ spec:
- name: atlas
default: true
plugins:
- base
- major_version
- parameters
- parameter
- entry
- status
- sensitive_properties
- skipped_properties
- read_only_properties
- read_write_only_properties
- sensitive_property
- skipped_property
- read_only_property
- read_write_property
- reference
- reference_metadata
- mutual_exclusive_major_versions
- atlas_sdk_version

openapi:
- name: v20250312
Expand Down
6 changes: 5 additions & 1 deletion tools/openapi2crd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ toolchain go1.24.7
require (
github.com/getkin/kin-openapi v0.131.0
github.com/goccy/go-yaml v1.18.0
github.com/google/go-cmp v0.6.0
github.com/pkg/errors v0.9.1
github.com/spf13/afero v1.6.0
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.9.0
github.com/stoewer/go-strcase v1.3.0
github.com/stretchr/testify v1.10.0
go.mongodb.org/atlas-sdk/v20250312005 v20250312005.0.0
k8s.io/api v0.32.3
k8s.io/apiextensions-apiserver v0.32.3
Expand Down Expand Up @@ -60,14 +63,15 @@ require (
github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/spf13/afero v1.6.0 // indirect
github.com/spf13/cast v1.4.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions tools/openapi2crd/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@ github.com/stoewer/go-strcase v1.3.0/go.mod h1:fAH5hQ5pehh+j3nZfvwdk2RgEgQjAoM8w
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand Down
74 changes: 2 additions & 72 deletions tools/openapi2crd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,82 +19,12 @@ import (
"context"
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"tools/openapi2crd/pkg/config"
"tools/openapi2crd/pkg/exporter"
"tools/openapi2crd/pkg/generator"
"tools/openapi2crd/cmd"
)

const (
outputOption = "output"
configOption = "config"
)

// RootCmd defines the root cli command
func RootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "openapi2crd SPEC_FILE",
Short: "Generate CustomResourceDefinition from OpenAPI 3.0 document",
SilenceErrors: true,
SilenceUsage: true,
PreRun: func(cmd *cobra.Command, args []string) {
_ = viper.BindPFlags(cmd.Flags())
},
RunE: func(cmd *cobra.Command, args []string) error {
outputOptionValue := viper.GetString(outputOption)
exporter, err := exporter.New(outputOptionValue)
if err != nil {
return err
}

configPath := viper.GetString(configOption)
raw, err := os.ReadFile(configPath)
if err != nil {
return fmt.Errorf("error reading config: %w", err)
}

cfg, err := config.Parse(raw)
if err != nil {
return fmt.Errorf("error parsing config: %w", err)
}

ctx := context.Background()
for _, crdConfig := range cfg.Spec.CRDConfig {
g := generator.NewGenerator(crdConfig, cfg.Spec.OpenAPIDefinitions)
crd, err := g.Generate(ctx)
if err != nil {
return err
}

err = exporter.Export(crd)
if err != nil {
return err
}
}

return nil
},
}

cmd.Flags().StringP(outputOption, "o", "", "Path to output file (required)")
_ = cmd.MarkFlagRequired(outputOption)
cmd.Flags().StringP(configOption, "c", "", "Path to the config file (required)")
cobra.OnInitialize(initConfig)

viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
return cmd
}

func initConfig() {
viper.AutomaticEnv()
}

func main() {
// Run the cli
if err := RootCmd().Execute(); err != nil {
if err := cmd.RunCmd(context.Background()).Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
Expand Down
9 changes: 9 additions & 0 deletions tools/openapi2crd/pkg/apis/config/v1alpha1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,18 @@ type Config struct {
}

type Spec struct {
PluginSets []PluginSet `json:"pluginSets,omitempty"`
CRDConfig []CRDConfig `json:"crd,omitempty"`
OpenAPIDefinitions []OpenAPIDefinition `json:"openapi,omitempty"`
}

type PluginSet struct {
Name string `json:"name"`
Default bool `json:"default,omitempty"`
InheritFrom string `json:"inheritFrom,omitempty"`
Plugins []string
}

type OpenAPIDefinition struct {
Name string `json:"name"`
Path string `json:"path"`
Expand All @@ -55,6 +63,7 @@ type CRDConfig struct {
Categories []string `json:"categories,omitempty"`
Mappings []CRDMapping `json:"mappings,omitempty"`
ShortNames []string `json:"shortNames,omitempty"`
PluginSet string `json:"pluginSet,omitempty"`
}

type CRDMapping struct {
Expand Down
56 changes: 46 additions & 10 deletions tools/openapi2crd/pkg/config/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,40 +32,76 @@ package config
import (
"fmt"
"net/url"
"os"
"path/filepath"

"github.com/getkin/kin-openapi/openapi3"
"github.com/goccy/go-yaml"
"github.com/spf13/afero"
)

func LoadOpenAPI(filePath string) (*openapi3.T, error) {
type Loader interface {
Load(path string) (*openapi3.T, error)
}

type KinOpeAPI struct {
fs afero.Fs
}

func NewKinOpeAPI(fs afero.Fs) *KinOpeAPI {
return &KinOpeAPI{
fs: fs,
}
}

func (a *KinOpeAPI) Load(path string) (*openapi3.T, error) {
loader := &openapi3.Loader{
IsExternalRefsAllowed: true,
}

uri, err := url.Parse(filePath)
if err == nil && uri.Scheme != "" && uri.Host != "" {
if uri, ok := isURI(path); ok {
return loader.LoadFromURI(uri)
}

filePath = filepath.Clean(filePath)
b, err := os.ReadFile(filePath)
data, err := a.transform(path)
if err != nil {
return nil, fmt.Errorf("failed to read file %s: %w", filePath, err)
return nil, fmt.Errorf("failed to transform the file %s: %w", path, err)
}

return loader.LoadFromData(data)
}

func (a *KinOpeAPI) transform(path string) ([]byte, error) {
filePath := filepath.Clean(path)

data, err := afero.ReadFile(a.fs, filePath)
if err != nil {
return nil, fmt.Errorf("failed to read the file %s: %w", filePath, err)
}

result := make(map[string]interface{})
err = yaml.Unmarshal(b, &result)
err = yaml.Unmarshal(data, &result)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal file %s: %w", filePath, err)
}

removeXGenChangelog(result)
b, err = yaml.Marshal(result)

data, err = yaml.Marshal(result)
if err != nil {
return nil, fmt.Errorf("failed to marshal yaml: %w", err)
}

return loader.LoadFromData(b)
return data, nil
}

func isURI(path string) (*url.URL, bool) {
uri, err := url.Parse(path)

if err == nil && uri.Scheme != "" && uri.Host != "" {
return uri, true
}

return nil, false
}

func removeXGenChangelog(m map[string]interface{}) {
Expand Down
Loading