Skip to content

Commit

Permalink
Add generation of applyconfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
mdbooth committed Jul 15, 2024
1 parent 0c95f00 commit 1c297ef
Show file tree
Hide file tree
Showing 15 changed files with 642 additions and 6 deletions.
6 changes: 1 addition & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ kubernetes.tar.gz
# generated files in any directory
# TODO(thockin): uncomment this when we stop committing the generated files.
#zz_generated.*
zz_generated.openapi.go
#zz_generated.openapi.go

# make-related metadata
/.make/
Expand Down Expand Up @@ -183,7 +183,3 @@ docs/book/book/

# CAPO doesn't use vendorings
vendor/

# Ignore go workspaces
go.work
go.work.sum
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ modules: ## Runs go mod to ensure proper vendoring.
cd $(TOOLS_DIR); go mod tidy

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

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

generate-codegen: generate-controller-gen
./hack/update-codegen.sh

.PHONY: generate-conversion-gen
capo_module := sigs.k8s.io/cluster-api-provider-openstack
generate-conversion-gen: $(CONVERSION_GEN)
Expand Down
1 change: 1 addition & 0 deletions api/v1alpha1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ limitations under the License.
// package v1alpha1 contains API Schema definitions for the infrastructure v1alpha1 API group
// +kubebuilder:object:generate=true
// +groupName=infrastructure.cluster.x-k8s.io
// +k8s:openapi-gen=true
package v1alpha1
1 change: 1 addition & 0 deletions api/v1alpha6/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ limitations under the License.
// +kubebuilder:object:generate=true
// +groupName=infrastructure.cluster.x-k8s.io
// +k8s:conversion-gen=sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1
// +k8s:openapi-gen=true
package v1alpha6
1 change: 1 addition & 0 deletions api/v1alpha7/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ limitations under the License.
// +kubebuilder:object:generate=true
// +groupName=infrastructure.cluster.x-k8s.io
// +k8s:conversion-gen=sigs.k8s.io/cluster-api-provider-openstack/api/v1beta1
// +k8s:openapi-gen=true
package v1alpha7
1 change: 1 addition & 0 deletions api/v1beta1/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ limitations under the License.
// Package v1beta1 contains API Schema definitions for the infrastructure v1beta1 API group.
// +kubebuilder:object:generate=true
// +groupName=infrastructure.cluster.x-k8s.io
// +k8s:openapi-gen=true
package v1beta1
26 changes: 26 additions & 0 deletions hack/codegen/README.md
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.
24 changes: 24 additions & 0 deletions hack/codegen/cmd/magnet/main.go
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() {}
93 changes: 93 additions & 0 deletions hack/codegen/cmd/models-schema/main.go
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, ".")
}
44 changes: 44 additions & 0 deletions hack/codegen/go.mod
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
)
Loading

0 comments on commit 1c297ef

Please sign in to comment.