Skip to content

Commit

Permalink
feat(openapi): go openapi schema + json openapi schema generation
Browse files Browse the repository at this point in the history
The generator is capable of parsing go types to generate
a zz_generated.openapi.go file containing the OpenAPI schema definitions.
The file is generated using kube-openapi openapi-gen programmatically
as a go-generator script.

The file is further processed to generate the openshift-generated.json file
that contains the final OpenAPI schema definitions augmented with x-fabric8-info
that includes the additional information to be able to infer group, version, kind,
and so on.

Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa authored Oct 2, 2024
1 parent 50608f5 commit fa135b9
Show file tree
Hide file tree
Showing 16 changed files with 35,816 additions and 6,530 deletions.
16 changes: 3 additions & 13 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,6 @@ hs_err_pid*
.idea/
# if you remove the above rule, at least ignore the following:

# User-specific stuff:
# .idea/workspace.xml
# .idea/tasks.xml
# .idea/dictionaries

# Sensitive or high-churn files:
# .idea/dataSources.ids
# .idea/dataSources.xml
# .idea/sqlDataSources.xml
# .idea/dynamic.xml
# .idea/uiDesigner.xml

# Gradle:
.gradle
# .idea/gradle.xml
Expand Down Expand Up @@ -81,11 +69,13 @@ com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties

# Ignore generator binary output file and vendor directory
# Ignore generator binary output file, vendor directory, and generated files
kubernetes-model-generator/openapi/generator/generator
kubernetes-model-generator/openapi/generator/vendor
kubernetes-model-generator/*/generate
extensions/*/generator/generate
zz_generated.openapi.go
zz_generated.openapi.go.report.txt

# Formatter and Impsort cache directory
.cache
Expand Down
11 changes: 6 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ clean-java:
.PHONY: clean
clean: clean-java

.PHONY: generate-openapi
generate-openapi:
.PHONY: openapi-generate-schema
openapi-generate-schema:
cd $(OPENAPI_GENERATOR_DIR) && go generate ./...
cd $(OPENAPI_GENERATOR_DIR) && go build -o $(OPENAPI_GENERATOR_BINARY_NAME) ./cmd
$(OPENAPI_GENERATOR_BINARY) reflection $(OPENAPI_SCHEMAS_DIR)
# To be able to access the go sources and mod information must be run from the root of the (Go) project
cd $(OPENAPI_GENERATOR_DIR) && ./$(OPENAPI_GENERATOR_BINARY_NAME) open-api $(OPENAPI_SCHEMAS_DIR)

.PHONY: generate-openapi-classes
generate-openapi-classes:
.PHONY: openapi-generate-java-classes
openapi-generate-java-classes:
# TODO: Do for all modules once they've all been migrated
# cd kubernetes-model-generator && mvn -Pgenerate clean install
cd kubernetes-model-generator/kubernetes-model-common && mvn clean install
Expand Down Expand Up @@ -79,7 +80,7 @@ generate-model-legacy:
cd kubernetes-model-generator && ./generateModel.sh

.PHONY: generate-model
generate-model: generate-openapi generate-openapi-classes generate-model-legacy
generate-model: openapi-generate-schema openapi-generate-java-classes generate-model-legacy

.PHONY: sonar
sonar:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
<schema>${project.basedir}/../openapi/schemas/admission-registration.json</schema>
</schemas>
<packageMappings combine.self="append">
<io.k8s.api>io.fabric8.kubernetes.api.model</io.k8s.api>
<io.k8s.api.authentication.v1>io.fabric8.kubernetes.api.model.authentication</io.k8s.api.authentication.v1>
</packageMappings>
<includeGenerationRegexes>
Expand Down
3 changes: 0 additions & 3 deletions kubernetes-model-generator/kubernetes-model-apps/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@
<schemas>
<schema>${openapi.schema.kubernetes-latest}</schema>
</schemas>
<packageMappings combine.self="append">
<io.k8s.api.apps.v1>io.fabric8.kubernetes.api.model.apps</io.k8s.api.apps.v1>
</packageMappings>
<includeGenerationRegexes>
<includeGenerationRegex>^io\.k8s\.api\.apps\..*$</includeGenerationRegex>
</includeGenerationRegexes>
Expand Down
13 changes: 10 additions & 3 deletions kubernetes-model-generator/openapi/generator/cmd/openapi.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:generate go run ../tools/generator/openapi.go
/**
* Copyright (C) 2015 Red Hat, Inc.
*
Expand All @@ -17,9 +18,13 @@ package main

import (
"fmt"
"github.com/fabric8io/kubernetes-client/kubernetes-model-generator/openapi/generator/cmd/generated_openapi"
"github.com/fabric8io/kubernetes-client/kubernetes-model-generator/openapi/generator/pkg/openapi"
"github.com/fabric8io/kubernetes-client/kubernetes-model-generator/openapi/generator/pkg/openshift"
"github.com/fabric8io/kubernetes-client/kubernetes-model-generator/openapi/generator/pkg/parser"
"github.com/openshift/api/openapi/generated_openapi"
"time"

//"github.com/openshift/api/openapi/generated_openapi"
"github.com/spf13/cobra"
"k8s.io/kube-openapi/pkg/common"
"k8s.io/kube-openapi/pkg/validation/spec"
Expand All @@ -36,14 +41,16 @@ func init() {
}

var openApiRun = func(cobraCmd *cobra.Command, args []string) {
startTime := time.Now()
fmt.Printf("OpenAPI JSON schema generation started...\n")
var targetDirectory string
if len(args) > 0 {
targetDirectory = args[0]
} else {
targetDirectory = "."
}
openApiGenerator := openapi.NewGenerator(targetDirectory, "openshift-generated")
openShiftModule := parser.NewModule("github.com/openshift/api")
openShiftModule := parser.NewModule(openshift.PackagePatterns...)
/////////////////////////////////////////////////////////////////////////////////
// Ported from github.com/openshift/api/openapi/cmd/models-schema/main.go
refFunc := func(name string) spec.Ref {
Expand Down Expand Up @@ -71,8 +78,8 @@ var openApiRun = func(cobraCmd *cobra.Command, args []string) {
}
openApiGenerator.PutDefinition(openShiftModule.ApiName(k), v.Schema)
}

if err := openApiGenerator.WriteDefinitions(); err != nil {
panic(fmt.Errorf("error writing OpenAPI schema: %w", err))
}
fmt.Printf("OpenAPI JSON schema generation completed in %v\n", time.Since(startTime))
}
31 changes: 21 additions & 10 deletions kubernetes-model-generator/openapi/generator/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ require (
github.com/getkin/kin-openapi v0.125.0
// Match latest 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
k8s.io/client-go v0.30.2
github.com/openshift/cluster-network-operator v0.0.0-20240912134426-a3188633549d
github.com/spf13/cobra v1.8.1
k8s.io/api v0.31.1
k8s.io/apiextensions-apiserver v0.31.1
k8s.io/apimachinery v0.31.1
k8s.io/client-go v0.31.1
k8s.io/gengo/v2 v2.0.0-20240911193312-2b36238f13e9
k8s.io/kube-openapi v0.0.0-20240903163716-9e1beecbcb38
k8s.io/metrics v0.30.2
sigs.k8s.io/gateway-api v1.1.0
sigs.k8s.io/kustomize/api v0.17.2
Expand All @@ -20,22 +24,23 @@ require (
// TODO: WIP while this gets merged or fixed https://github.com/openshift/api/pull/2050
replace github.com/openshift/api => github.com/marcnuri-forks/api v0.0.0-20240930125604-62d5277244a4

require (
github.com/openshift/cluster-network-operator v0.0.0-20240912134426-a3188633549d
github.com/spf13/cobra v1.8.1
k8s.io/gengo/v2 v2.0.0-20240911193312-2b36238f13e9
k8s.io/kube-openapi v0.0.0-20240903163716-9e1beecbcb38
)
// TODO: WIP while this gets merged or fixed https://github.com/operator-framework/api/pull/365
replace github.com/operator-framework/api => github.com/marcnuri-forks/operator-framework-api v0.0.0-20241001140003-cf4aa8da1ffa

require github.com/operator-framework/api v0.0.0-00010101000000-000000000000

require (
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.12.0 // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
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
Expand All @@ -48,19 +53,25 @@ 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/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/pflag v1.0.6-0.20210604193023-d5e0c0615ace // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/mod v0.20.0 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/oauth2 v0.21.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.23.0 // indirect
golang.org/x/term v0.23.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.24.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.130.1 // indirect
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
sigs.k8s.io/controller-runtime v0.19.0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/kyaml v0.17.1 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
Expand Down
Loading

0 comments on commit fa135b9

Please sign in to comment.