Skip to content
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
31 changes: 31 additions & 0 deletions tools/openapi2crd/.mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Copyright 2025 MongoDB Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

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
137 changes: 137 additions & 0 deletions tools/openapi2crd/cmd/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Copyright 2025 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

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 {
configPath := viper.GetString(configOption)
outputPath := viper.GetString(outputOption)
forceOverwrite := viper.GetBool(forceOption)

fs := afero.NewOsFs()

return runOpenapi2crd(ctx, fs, configPath, outputPath, forceOverwrite)
},
}

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
}

func runOpenapi2crd(ctx context.Context, fs afero.Fs, input, output string, overwrite bool) error {
file, err := fs.OpenFile(input, readOnly, 0o644)
if err != nil {
return fmt.Errorf("error opening the file %s: %w", input, err)
}

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

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

fsExporter, err := exporter.New(fs, output, overwrite)
if err != nil {
return fmt.Errorf("error creating the exporter: %w", err)
}

err = fsExporter.Start()
if err != nil {
return fmt.Errorf("error starting the exporter: %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)
atlasLoader := config.NewAtlas(openapiLoader)

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, atlasLoader)
crd, err := g.Generate(ctx, &crdConfig)
if err != nil {
return err
}

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

return fsExporter.Close()
}
130 changes: 130 additions & 0 deletions tools/openapi2crd/cmd/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
// Copyright 2025 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package cmd

import (
"context"
"testing"

"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestRunOpenapi2crd(t *testing.T) {
tests := map[string]struct {
input string
output string
overwrite bool
expectedErr error
}{
"generates CRD successfully": {
input: "./testdata/config.yaml",
output: "./testdata/output.yaml",
overwrite: true,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
fs := afero.NewMemMapFs()
_, err := fs.Create(tt.input)
require.NoError(t, err)
err = afero.WriteFile(fs, tt.input, []byte(configFile()), 0o644)
require.NoError(t, err)
err = afero.WriteFile(fs, "./testdata/openapi.yaml", []byte(openapiFile()), 0o644)
require.NoError(t, err)

err = runOpenapi2crd(context.Background(), fs, tt.input, tt.output, tt.overwrite)
assert.Equal(t, tt.expectedErr, err)
})
}
}

func configFile() string {
return `kind: Config
apiVersion: atlas2crd.mongodb.com/v1alpha1
spec:
pluginSets:
- name: example
default: true
plugins:
- base
- major_version
- parameters
- entry
- status
openapi:
- name: v1
path: ./testdata/openapi.yaml
crd:
- gvk:
version: v1
kind: Example
group: example.generated.mongodb.com
categories:
- example
shortNames:
- ex
mappings:
- majorVersion: v1
openAPIRef:
name: v1
entry:
schema: "ExampleRequest"
status:
schema: "ExampleResponse"`
}

func openapiFile() string {
return `openapi: 3.0.0
info:
title: Example API
version: 1.0.0
components:
schemas:
ExampleRequest:
type: object
properties:
name:
type: string
description:
type: string
ExampleResponse:
type: object
properties:
id:
type: string
name:
type: string
description:
type: string
paths:
/example:
post:
summary: Create an example
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/ExampleRequest'
responses:
'200':
description: Successful response
content:
application/json:
schema:
$ref: '#/components/schemas/ExampleResponse'`
}
7 changes: 6 additions & 1 deletion 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
- entry
- status
- sensitive_properties
- skipped_properties
- read_only_properties
- read_write_only_properties
- read_write_properties
- references
- references_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
12 changes: 0 additions & 12 deletions tools/openapi2crd/hack/tools/boilerplate.go.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1 @@
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
6 changes: 2 additions & 4 deletions tools/openapi2crd/hack/tools/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ package tools
import (
// linter(s)
_ "github.com/golangci/golangci-lint/cmd/golangci-lint"

// kubernetes code generators
_ "k8s.io/code-generator/cmd/deepcopy-gen"

// test runner
_ "gotest.tools/gotestsum"
// kubernetes code generators
_ "k8s.io/code-generator/cmd/deepcopy-gen"
)
Loading