Skip to content

Commit 1c297ef

Browse files
committed
Add generation of applyconfiguration
1 parent 0c95f00 commit 1c297ef

File tree

15 files changed

+642
-6
lines changed

15 files changed

+642
-6
lines changed

.gitignore

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ kubernetes.tar.gz
146146
# generated files in any directory
147147
# TODO(thockin): uncomment this when we stop committing the generated files.
148148
#zz_generated.*
149-
zz_generated.openapi.go
149+
#zz_generated.openapi.go
150150

151151
# make-related metadata
152152
/.make/
@@ -183,7 +183,3 @@ docs/book/book/
183183

184184
# CAPO doesn't use vendorings
185185
vendor/
186-
187-
# Ignore go workspaces
188-
go.work
189-
go.work.sum

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ modules: ## Runs go mod to ensure proper vendoring.
268268
cd $(TOOLS_DIR); go mod tidy
269269

270270
.PHONY: generate
271-
generate: templates generate-controller-gen generate-conversion-gen generate-go generate-manifests generate-api-docs ## Generate all generated code
271+
generate: templates generate-controller-gen generate-codegen generate-conversion-gen generate-go generate-manifests generate-api-docs ## Generate all generated code
272272

273273
.PHONY: generate-go
274274
generate-go: $(MOCKGEN)
@@ -280,6 +280,9 @@ generate-controller-gen: $(CONTROLLER_GEN)
280280
paths=./api/... \
281281
object:headerFile=./hack/boilerplate/boilerplate.generatego.txt
282282

283+
generate-codegen: generate-controller-gen
284+
./hack/update-codegen.sh
285+
283286
.PHONY: generate-conversion-gen
284287
capo_module := sigs.k8s.io/cluster-api-provider-openstack
285288
generate-conversion-gen: $(CONVERSION_GEN)

api/v1alpha1/doc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ limitations under the License.
1717
// package v1alpha1 contains API Schema definitions for the infrastructure v1alpha1 API group
1818
// +kubebuilder:object:generate=true
1919
// +groupName=infrastructure.cluster.x-k8s.io
20+
// +k8s:openapi-gen=true
2021
package v1alpha1

api/v1alpha6/doc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ limitations under the License.
1818
// +kubebuilder:object:generate=true
1919
// +groupName=infrastructure.cluster.x-k8s.io
2020
// +k8s:conversion-gen=sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1
21+
// +k8s:openapi-gen=true
2122
package v1alpha6

api/v1alpha7/doc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ limitations under the License.
1818
// +kubebuilder:object:generate=true
1919
// +groupName=infrastructure.cluster.x-k8s.io
2020
// +k8s:conversion-gen=sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1
21+
// +k8s:openapi-gen=true
2122
package v1alpha7

api/v1beta1/doc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ limitations under the License.
1717
// Package v1beta1 contains API Schema definitions for the infrastructure v1beta1 API group.
1818
// +kubebuilder:object:generate=true
1919
// +groupName=infrastructure.cluster.x-k8s.io
20+
// +k8s:openapi-gen=true
2021
package v1beta1

hack/codegen/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
This helper module allows us to:
2+
1. Avoid adding openapi generator dependencies to the main CAPO module
3+
1. Import a specific k8s.io/code-generator commit without messing up the main
4+
CAPO module or tools dependencies
5+
6+
It may be possible to simplify this configuration in the future when CAPO is
7+
using at least k/k v0.31.
8+
9+
We are very specifically pulling:
10+
```
11+
k8s.io/code-generator 030791bd8d60de2141f3b7f57c751787ee468ac9
12+
```
13+
14+
This commit contains a fix to openapi-gen which prevents it running when
15+
imported as a module. Later commits pull in changes which prevent the generated
16+
applyconfiguration from building against k/k v0.29, so we can't pull those in
17+
yet.
18+
19+
Do not bump the version of code-generator from this specific commit until we
20+
also bump CAPO to k/k v0.31. At this point we should:
21+
* Delete cmd/magnet from this directory
22+
* Run go mod tidy
23+
24+
With these changes, the go.work in this module will result in us pulling the
25+
version of code-generator corresponding to the version of k/k used by the main
26+
CAPO module.

hack/codegen/cmd/magnet/main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
// This is just an import magnet so 'go mod tidy' doesn't remove k8s.io/code-generator
20+
import (
21+
_ "k8s.io/code-generator"
22+
)
23+
24+
func main() {}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package main
18+
19+
import (
20+
"encoding/json"
21+
"fmt"
22+
"os"
23+
"strings"
24+
25+
"k8s.io/kube-openapi/pkg/common"
26+
"k8s.io/kube-openapi/pkg/validation/spec"
27+
28+
"sigs.k8s.io/cluster-api-provider-openstack/hack/codegen/openapi"
29+
)
30+
31+
// Outputs openAPI schema JSON containing the schema definitions in zz_generated.openapi.go.
32+
// pulled from model_schema command of k/k
33+
func main() {
34+
err := output()
35+
if err != nil {
36+
os.Stderr.WriteString(fmt.Sprintf("Failed: %v", err))
37+
os.Exit(1)
38+
}
39+
}
40+
41+
func output() error {
42+
refFunc := func(name string) spec.Ref {
43+
return spec.MustCreateRef(fmt.Sprintf("#/definitions/%s", friendlyName(name)))
44+
}
45+
defs := openapi.GetOpenAPIDefinitions(refFunc)
46+
schemaDefs := make(map[string]spec.Schema, len(defs))
47+
for k, v := range defs {
48+
// Replace top-level schema with v2 if a v2 schema is embedded
49+
// so that the output of this program is always in OpenAPI v2.
50+
// This is done by looking up an extension that marks the embedded v2
51+
// schema, and, if the v2 schema is found, make it the resulting schema for
52+
// the type.
53+
if schema, ok := v.Schema.Extensions[common.ExtensionV2Schema]; ok {
54+
if v2Schema, isOpenAPISchema := schema.(spec.Schema); isOpenAPISchema {
55+
schemaDefs[friendlyName(k)] = v2Schema
56+
continue
57+
}
58+
}
59+
60+
schemaDefs[friendlyName(k)] = v.Schema
61+
}
62+
data, err := json.Marshal(&spec.Swagger{
63+
SwaggerProps: spec.SwaggerProps{
64+
Definitions: schemaDefs,
65+
Info: &spec.Info{
66+
InfoProps: spec.InfoProps{
67+
Title: "Kubernetes",
68+
Version: "unversioned",
69+
},
70+
},
71+
Swagger: "2.0",
72+
},
73+
})
74+
if err != nil {
75+
return fmt.Errorf("error serializing api definitions: %w", err)
76+
}
77+
os.Stdout.Write(data)
78+
return nil
79+
}
80+
81+
// From vendor/k8s.io/apiserver/pkg/endpoints/openapi/openapi.go
82+
func friendlyName(name string) string {
83+
nameParts := strings.Split(name, "/")
84+
// Reverse first part. e.g., io.k8s... instead of k8s.io...
85+
if len(nameParts) > 0 && strings.Contains(nameParts[0], ".") {
86+
parts := strings.Split(nameParts[0], ".")
87+
for i, j := 0, len(parts)-1; i < j; i, j = i+1, j-1 {
88+
parts[i], parts[j] = parts[j], parts[i]
89+
}
90+
nameParts[0] = strings.Join(parts, ".")
91+
}
92+
return strings.Join(nameParts, ".")
93+
}

hack/codegen/go.mod

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module sigs.k8s.io/cluster-api-provider-openstack/hack/codegen
2+
3+
go 1.22.2
4+
5+
require (
6+
k8s.io/apimachinery v0.30.2
7+
k8s.io/code-generator v0.30.0-alpha.3.0.20240618021310-030791bd8d60
8+
k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340
9+
)
10+
11+
require (
12+
github.com/emicklei/go-restful/v3 v3.12.0 // indirect
13+
github.com/go-logr/logr v1.4.1 // indirect
14+
github.com/go-openapi/jsonpointer v0.19.6 // indirect
15+
github.com/go-openapi/jsonreference v0.20.2 // indirect
16+
github.com/go-openapi/swag v0.22.4 // indirect
17+
github.com/gogo/protobuf v1.3.2 // indirect
18+
github.com/golang/protobuf v1.5.4 // indirect
19+
github.com/google/gnostic-models v0.6.8 // indirect
20+
github.com/google/gofuzz v1.2.0 // indirect
21+
github.com/josharian/intern v1.0.0 // indirect
22+
github.com/json-iterator/go v1.1.12 // indirect
23+
github.com/mailru/easyjson v0.7.7 // indirect
24+
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
25+
github.com/modern-go/reflect2 v1.0.2 // indirect
26+
github.com/onsi/ginkgo/v2 v2.17.2 // indirect
27+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
28+
github.com/spf13/pflag v1.0.5 // indirect
29+
golang.org/x/mod v0.17.0 // indirect
30+
golang.org/x/net v0.25.0 // indirect
31+
golang.org/x/sync v0.7.0 // indirect
32+
golang.org/x/text v0.15.0 // indirect
33+
golang.org/x/tools v0.21.0 // indirect
34+
google.golang.org/protobuf v1.33.0 // indirect
35+
gopkg.in/inf.v0 v0.9.1 // indirect
36+
gopkg.in/yaml.v2 v2.4.0 // indirect
37+
gopkg.in/yaml.v3 v3.0.1 // indirect
38+
k8s.io/gengo/v2 v2.0.0-20240228010128-51d4e06bde70 // indirect
39+
k8s.io/klog/v2 v2.130.1 // indirect
40+
k8s.io/utils v0.0.0-20231127182322-b307cd553661 // indirect
41+
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
42+
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
43+
sigs.k8s.io/yaml v1.4.0 // indirect
44+
)

0 commit comments

Comments
 (0)