-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generation of applyconfiguration
- Loading branch information
Showing
15 changed files
with
642 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
This helper module allows us to: | ||
1. Avoid adding openapi generator dependencies to the main CAPO module | ||
1. Import a specific k8s.io/code-generator commit without messing up the main | ||
CAPO module or tools dependencies | ||
|
||
It may be possible to simplify this configuration in the future when CAPO is | ||
using at least k/k v0.31. | ||
|
||
We are very specifically pulling: | ||
``` | ||
k8s.io/code-generator 030791bd8d60de2141f3b7f57c751787ee468ac9 | ||
``` | ||
|
||
This commit contains a fix to openapi-gen which prevents it running when | ||
imported as a module. Later commits pull in changes which prevent the generated | ||
applyconfiguration from building against k/k v0.29, so we can't pull those in | ||
yet. | ||
|
||
Do not bump the version of code-generator from this specific commit until we | ||
also bump CAPO to k/k v0.31. At this point we should: | ||
* Delete cmd/magnet from this directory | ||
* Run go mod tidy | ||
|
||
With these changes, the go.work in this module will result in us pulling the | ||
version of code-generator corresponding to the version of k/k used by the main | ||
CAPO module. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
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 | ||
|
||
// This is just an import magnet so 'go mod tidy' doesn't remove k8s.io/code-generator | ||
import ( | ||
_ "k8s.io/code-generator" | ||
) | ||
|
||
func main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
Copyright 2024 The Kubernetes Authors. | ||
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 ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"k8s.io/kube-openapi/pkg/common" | ||
"k8s.io/kube-openapi/pkg/validation/spec" | ||
|
||
"sigs.k8s.io/cluster-api-provider-openstack/hack/codegen/openapi" | ||
) | ||
|
||
// Outputs openAPI schema JSON containing the schema definitions in zz_generated.openapi.go. | ||
// pulled from model_schema command of k/k | ||
func main() { | ||
err := output() | ||
if err != nil { | ||
os.Stderr.WriteString(fmt.Sprintf("Failed: %v", err)) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
func output() error { | ||
refFunc := func(name string) spec.Ref { | ||
return spec.MustCreateRef(fmt.Sprintf("#/definitions/%s", friendlyName(name))) | ||
} | ||
defs := openapi.GetOpenAPIDefinitions(refFunc) | ||
schemaDefs := make(map[string]spec.Schema, len(defs)) | ||
for k, v := range defs { | ||
// Replace top-level schema with v2 if a v2 schema is embedded | ||
// so that the output of this program is always in OpenAPI v2. | ||
// This is done by looking up an extension that marks the embedded v2 | ||
// schema, and, if the v2 schema is found, make it the resulting schema for | ||
// the type. | ||
if schema, ok := v.Schema.Extensions[common.ExtensionV2Schema]; ok { | ||
if v2Schema, isOpenAPISchema := schema.(spec.Schema); isOpenAPISchema { | ||
schemaDefs[friendlyName(k)] = v2Schema | ||
continue | ||
} | ||
} | ||
|
||
schemaDefs[friendlyName(k)] = v.Schema | ||
} | ||
data, err := json.Marshal(&spec.Swagger{ | ||
SwaggerProps: spec.SwaggerProps{ | ||
Definitions: schemaDefs, | ||
Info: &spec.Info{ | ||
InfoProps: spec.InfoProps{ | ||
Title: "Kubernetes", | ||
Version: "unversioned", | ||
}, | ||
}, | ||
Swagger: "2.0", | ||
}, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("error serializing api definitions: %w", err) | ||
} | ||
os.Stdout.Write(data) | ||
return nil | ||
} | ||
|
||
// From vendor/k8s.io/apiserver/pkg/endpoints/openapi/openapi.go | ||
func friendlyName(name string) string { | ||
nameParts := strings.Split(name, "/") | ||
// Reverse first part. e.g., io.k8s... instead of k8s.io... | ||
if len(nameParts) > 0 && strings.Contains(nameParts[0], ".") { | ||
parts := strings.Split(nameParts[0], ".") | ||
for i, j := 0, len(parts)-1; i < j; i, j = i+1, j-1 { | ||
parts[i], parts[j] = parts[j], parts[i] | ||
} | ||
nameParts[0] = strings.Join(parts, ".") | ||
} | ||
return strings.Join(nameParts, ".") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module sigs.k8s.io/cluster-api-provider-openstack/hack/codegen | ||
|
||
go 1.22.2 | ||
|
||
require ( | ||
k8s.io/apimachinery v0.30.2 | ||
k8s.io/code-generator v0.30.0-alpha.3.0.20240618021310-030791bd8d60 | ||
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 | ||
) | ||
|
||
require ( | ||
github.com/emicklei/go-restful/v3 v3.12.0 // indirect | ||
github.com/go-logr/logr v1.4.1 // indirect | ||
github.com/go-openapi/jsonpointer v0.19.6 // indirect | ||
github.com/go-openapi/jsonreference v0.20.2 // indirect | ||
github.com/go-openapi/swag v0.22.4 // indirect | ||
github.com/gogo/protobuf v1.3.2 // 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 | ||
github.com/josharian/intern v1.0.0 // indirect | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/mailru/easyjson v0.7.7 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/onsi/ginkgo/v2 v2.17.2 // indirect | ||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect | ||
github.com/spf13/pflag v1.0.5 // indirect | ||
golang.org/x/mod v0.17.0 // indirect | ||
golang.org/x/net v0.25.0 // indirect | ||
golang.org/x/sync v0.7.0 // indirect | ||
golang.org/x/text v0.15.0 // indirect | ||
golang.org/x/tools v0.21.0 // indirect | ||
google.golang.org/protobuf v1.33.0 // 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/gengo/v2 v2.0.0-20240228010128-51d4e06bde70 // indirect | ||
k8s.io/klog/v2 v2.130.1 // indirect | ||
k8s.io/utils v0.0.0-20231127182322-b307cd553661 // indirect | ||
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect | ||
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect | ||
sigs.k8s.io/yaml v1.4.0 // indirect | ||
) |
Oops, something went wrong.