Skip to content

Commit

Permalink
refactor: openapi generator restructuring before adding further tooling
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <marc@marcnuri.com>
  • Loading branch information
manusa authored Sep 27, 2024
1 parent cccc3df commit f2cecfa
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 52 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties

# Ignore generator binary output file
# Ignore generator binary output file and vendor directory
kubernetes-model-generator/openapi/generator/generator
kubernetes-model-generator/openapi/generator/vendor
kubernetes-model-generator/*/generate
extensions/*/generator/generate

Expand Down
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
#

MAVEN_OPTIONS ?=
OPENAPI_DIR=./kubernetes-model-generator/openapi
OPENAPI_GENERATOR_DIR=$(OPENAPI_DIR)/generator
OPENAPI_GENERATOR_BINARY_NAME=generator
OPENAPI_GENERATOR_BINARY=$(OPENAPI_GENERATOR_DIR)/$(OPENAPI_GENERATOR_BINARY_NAME)
OPENAPI_SCHEMAS_DIR=$(OPENAPI_DIR)/schemas

.PHONY: clean-java
clean-java:
Expand All @@ -25,8 +30,9 @@ clean: clean-java

.PHONY: generate-openapi
generate-openapi:
cd kubernetes-model-generator/openapi/generator && go build
./kubernetes-model-generator/openapi/generator/generator ./kubernetes-model-generator/openapi/schemas
cd $(OPENAPI_GENERATOR_DIR) && go build -o $(OPENAPI_GENERATOR_BINARY_NAME) ./cmd
$(OPENAPI_GENERATOR_BINARY) reflection $(OPENAPI_SCHEMAS_DIR)
$(OPENAPI_GENERATOR_BINARY) open-api $(OPENAPI_SCHEMAS_DIR)

.PHONY: generate-openapi-classes
generate-openapi-classes:
Expand Down
2 changes: 1 addition & 1 deletion kubernetes-model-generator/openapi/generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Many of these types are used for WebHooks.
## Usage

```shell
./generator $pathToTargetDirectory
./generator reflection $pathToTargetDirectory
```

## Additional notes and resources
Expand Down
35 changes: 35 additions & 0 deletions kubernetes-model-generator/openapi/generator/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Copyright (C) 2015 Red Hat, 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 main

import (
"flag"
"fmt"
"os"
)

func main() {
defer func() {
if r := recover(); r != nil {
fmt.Println("Error:", r)
os.Exit(1)
}
}()
flag.Parse()
if err := rootCmd.Execute(); err != nil {
panic(err)
}
}
32 changes: 32 additions & 0 deletions kubernetes-model-generator/openapi/generator/cmd/openapi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (C) 2015 Red Hat, 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 main

import "github.com/spf13/cobra"

var openApi = &cobra.Command{
Use: "open-api [targetDirectory]",
Short: "Generate OpenAPI definitions from OpenAPI/Swagger schema definitions in codegen and openapi-gen zz_generated files",
Run: openApiRun,
}

func init() {
rootCmd.AddCommand(openApi)
}

var openApiRun = func(cobraCmd *cobra.Command, args []string) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
openshiftmachinev1 "github.com/openshift/api/machine/v1"
openshiftmachinev1alpha1 "github.com/openshift/api/machine/v1alpha1"
openshiftsecurityv1 "github.com/openshift/api/security/v1"
"github.com/spf13/cobra"
admissionV1 "k8s.io/api/admission/v1"
admissionV1Beta1 "k8s.io/api/admission/v1beta1"
admissionregistrationV1 "k8s.io/api/admissionregistration/v1"
Expand All @@ -40,39 +41,21 @@ import (
"time"
)

type ApiVersion struct {
List bool
GroupVersion string
Plural string
Namespaced bool
var reflection = &cobra.Command{
Use: "reflection [targetDirectory]",
Short: "Generate OpenAPI definitions from Go type reflection",
Args: cobra.MaximumNArgs(1),
Run: reflectionRun,
}

type Schema struct {
Types []reflect.Type
Name string
Paths map[reflect.Type]ApiVersion
func init() {
rootCmd.AddCommand(reflection)
}

var mappingOverrides = map[reflect.Type]string{
reflect.TypeOf(kustomize.ObjectMeta{}): "#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta",
}

func NewTypeSchema(types []reflect.Type, name string) Schema {
return Schema{types, name, make(map[reflect.Type]ApiVersion)}
}

func NewPathSchema(paths map[reflect.Type]ApiVersion, name string) Schema {
schema := Schema{make([]reflect.Type, 0), name, paths}
for t := range paths {
schema.Types = append(schema.Types, t)
}
return schema
}

func main() {
var reflectionRun = func(cmd *cobra.Command, args []string) {
var targetDirectory string
if len(os.Args) > 1 {
targetDirectory = os.Args[1]
if len(args) > 0 {
targetDirectory = args[0]
} else {
targetDirectory = "."
}
Expand Down
23 changes: 23 additions & 0 deletions kubernetes-model-generator/openapi/generator/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (C) 2015 Red Hat, 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 main

import "github.com/spf13/cobra"

var rootCmd = &cobra.Command{
Use: "generator",
Short: "Kubernetes and OpenShift model generator",
}
50 changes: 50 additions & 0 deletions kubernetes-model-generator/openapi/generator/cmd/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Copyright (C) 2015 Red Hat, 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 main

import (
"reflect"
kustomize "sigs.k8s.io/kustomize/api/types"
)

type ApiVersion struct {
List bool
GroupVersion string
Plural string
Namespaced bool
}

type Schema struct {
Types []reflect.Type
Name string
Paths map[reflect.Type]ApiVersion
}

var mappingOverrides = map[reflect.Type]string{
reflect.TypeOf(kustomize.ObjectMeta{}): "#/components/schemas/io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta",
}

func NewTypeSchema(types []reflect.Type, name string) Schema {
return Schema{types, name, make(map[reflect.Type]ApiVersion)}
}

func NewPathSchema(paths map[reflect.Type]ApiVersion, name string) Schema {
schema := Schema{make([]reflect.Type, 0), name, paths}
for t := range paths {
schema.Types = append(schema.Types, t)
}
return schema
}
10 changes: 8 additions & 2 deletions kubernetes-model-generator/openapi/generator/go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
module github.com/fabric8io/kubernetes-client/kubernetes-model-generator/openapi/generator

go 1.22.0
go 1.23

toolchain go1.23.1

require (
github.com/getkin/kin-openapi v0.125.0
github.com/openshift/api v0.0.0-20240911192208-3e5de946111c
// Match lastest commit in the version branch (e.g. release-4.17)
github.com/openshift/api v0.0.0-20240912201240-0a8800162826
k8s.io/api v0.30.2
k8s.io/apiextensions-apiserver v0.30.2
k8s.io/apimachinery v0.30.2
Expand All @@ -25,6 +28,7 @@ require (
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/invopop/yaml v0.2.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand All @@ -33,6 +37,8 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/spf13/cobra v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
10 changes: 8 additions & 2 deletions kubernetes-model-generator/openapi/generator/go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
Expand Down Expand Up @@ -32,6 +33,8 @@ github.com/google/gofuzz v1.2.0 h1:xRy4A+RhZaiKjJ1bPfwQ8sedCA+YS2YcCHW6ec7JMi0=
github.com/google/gofuzz v1.2.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/invopop/yaml v0.2.0 h1:7zky/qH+O0DwAyoobXUqvVBwgBFRxKoQ/3FjcVpjTMY=
github.com/invopop/yaml v0.2.0/go.mod h1:2XuRLgs/ouIrW3XNzuNj7J3Nvu/Dig5MXvbCEdiBN3Q=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
Expand All @@ -55,15 +58,18 @@ github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/openshift/api v0.0.0-20240911192208-3e5de946111c h1:46hH/7XmmaPmeJWTyrzh8TRB6I7TCwzJdxxWeyK8blM=
github.com/openshift/api v0.0.0-20240911192208-3e5de946111c/go.mod h1:OOh6Qopf21pSzqNVCB5gomomBXb8o5sGKZxG2KNpaXM=
github.com/openshift/api v0.0.0-20240912201240-0a8800162826 h1:A8D9SN/hJUwAbdO0rPCVTqmuBOctdgurr53gK701SYo=
github.com/openshift/api v0.0.0-20240912201240-0a8800162826/go.mod h1:OOh6Qopf21pSzqNVCB5gomomBXb8o5sGKZxG2KNpaXM=
github.com/perimeterx/marshmallow v1.1.5 h1:a2LALqQ1BlHM8PZblsDdidgv1mWi1DgC2UmX50IvK2s=
github.com/perimeterx/marshmallow v1.1.5/go.mod h1:dsXbUu8CRzfYP5a87xpp0xq9S3u0Vchtcl8we9tYaXw=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public class SchemaUtils {
TYPE_MAP.put("int32", "Integer");
TYPE_MAP.put("integer", "Integer");
TYPE_MAP.put("int64", "Long");
TYPE_MAP.put("float", "Float");
TYPE_MAP.put("double", "Double");
TYPE_MAP.put("number", "Number");
TYPE_MAP.put("object", OBJECT_PRIMITIVE);
Expand Down
Loading

0 comments on commit f2cecfa

Please sign in to comment.