Skip to content

Commit

Permalink
init controller-gen support for oneOf and not (kyverno#10776)
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Jogeleit <[email protected]>
Co-authored-by: Charles-Edouard Brétéché <[email protected]>
  • Loading branch information
fjogeleit and eddycharly authored Aug 2, 2024
1 parent 2ead7fb commit cfef8a0
Show file tree
Hide file tree
Showing 19 changed files with 184 additions and 28 deletions.
24 changes: 10 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ USE_CONFIG ?= standard
TOOLS_DIR ?= $(PWD)/.tools
KIND ?= $(TOOLS_DIR)/kind
KIND_VERSION ?= v0.23.0
CONTROLLER_GEN ?= $(TOOLS_DIR)/controller-gen
CONTROLLER_GEN_VERSION ?= v0.15.0
CLIENT_GEN ?= $(TOOLS_DIR)/client-gen
LISTER_GEN ?= $(TOOLS_DIR)/lister-gen
Expand All @@ -61,7 +60,7 @@ HELM_DOCS_VERSION ?= v1.11.0
KO ?= $(TOOLS_DIR)/ko
KO_VERSION ?= v0.14.1
KUBE_VERSION ?= v1.25.0
TOOLS := $(KIND) $(CONTROLLER_GEN) $(CLIENT_GEN) $(LISTER_GEN) $(INFORMER_GEN) $(OPENAPI_GEN) $(REGISTER_GEN) $(DEEPCOPY_GEN) $(DEFAULTER_GEN) $(APPLYCONFIGURATION_GEN) $(GEN_CRD_API_REFERENCE_DOCS) $(GENREF) $(GO_ACC) $(GOIMPORTS) $(HELM) $(HELM_DOCS) $(KO)
TOOLS := $(KIND) $(CLIENT_GEN) $(LISTER_GEN) $(INFORMER_GEN) $(OPENAPI_GEN) $(REGISTER_GEN) $(DEEPCOPY_GEN) $(DEFAULTER_GEN) $(APPLYCONFIGURATION_GEN) $(GEN_CRD_API_REFERENCE_DOCS) $(GENREF) $(GO_ACC) $(GOIMPORTS) $(HELM) $(HELM_DOCS) $(KO)
ifeq ($(GOOS), darwin)
SED := gsed
else
Expand All @@ -73,10 +72,6 @@ $(KIND):
@echo Install kind... >&2
@GOBIN=$(TOOLS_DIR) go install sigs.k8s.io/kind@$(KIND_VERSION)

$(CONTROLLER_GEN):
@echo Install controller-gen... >&2
@GOBIN=$(TOOLS_DIR) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_GEN_VERSION)

$(CLIENT_GEN):
@echo Install client-gen... >&2
@GOBIN=$(TOOLS_DIR) go install k8s.io/code-generator/cmd/client-gen@$(CODE_GEN_VERSION)
Expand Down Expand Up @@ -503,28 +498,28 @@ codegen-client-all: codegen-client-informers
codegen-client-all: codegen-client-wrappers

.PHONY: codegen-crds-kyverno
codegen-crds-kyverno: $(CONTROLLER_GEN) ## Generate kyverno CRDs
codegen-crds-kyverno: ## Generate kyverno CRDs
@echo Generate kyverno crds... >&2
@rm -rf $(CRDS_PATH)/kyverno && mkdir -p $(CRDS_PATH)/kyverno
@$(CONTROLLER_GEN) paths=./api/kyverno/... crd:crdVersions=v1 output:dir=$(CRDS_PATH)/kyverno
@go run ./hack/controller-gen -- paths=./api/kyverno/... crd:crdVersions=v1,ignoreUnexportedFields=true,generateEmbeddedObjectMeta=false output:dir=$(CRDS_PATH)/kyverno

.PHONY: codegen-crds-policyreport
codegen-crds-policyreport: $(CONTROLLER_GEN) ## Generate policy reports CRDs
codegen-crds-policyreport: ## Generate policy reports CRDs
@echo Generate policy reports crds... >&2
@rm -rf $(CRDS_PATH)/policyreport && mkdir -p $(CRDS_PATH)/policyreport
@$(CONTROLLER_GEN) paths=./api/policyreport/... crd:crdVersions=v1 output:dir=$(CRDS_PATH)/policyreport
@go run ./hack/controller-gen -- paths=./api/policyreport/... crd:crdVersions=v1,ignoreUnexportedFields=true,generateEmbeddedObjectMeta=false output:dir=$(CRDS_PATH)/policyreport

.PHONY: codegen-crds-reports
codegen-crds-reports: $(CONTROLLER_GEN) ## Generate reports CRDs
codegen-crds-reports: ## Generate reports CRDs
@echo Generate reports crds... >&2
@rm -rf $(CRDS_PATH)/reports && mkdir -p $(CRDS_PATH)/reports
@$(CONTROLLER_GEN) paths=./api/reports/... crd:crdVersions=v1 output:dir=$(CRDS_PATH)/reports
@go run ./hack/controller-gen -- paths=./api/reports/... crd:crdVersions=v1,ignoreUnexportedFields=true,generateEmbeddedObjectMeta=false output:dir=$(CRDS_PATH)/reports

.PHONY: codegen-crds-cli
codegen-crds-cli: $(CONTROLLER_GEN) ## Generate CLI CRDs
codegen-crds-cli: ## Generate CLI CRDs
@echo Generate cli crds... >&2
@rm -rf ${PWD}/cmd/cli/kubectl-kyverno/config/crds && mkdir -p ${PWD}/cmd/cli/kubectl-kyverno/config/crds
@$(CONTROLLER_GEN) paths=./cmd/cli/kubectl-kyverno/apis/... crd:crdVersions=v1 output:dir=${PWD}/cmd/cli/kubectl-kyverno/config/crds
@go run ./hack/controller-gen -- paths=./cmd/cli/kubectl-kyverno/apis/... crd:crdVersions=v1,ignoreUnexportedFields=true,generateEmbeddedObjectMeta=false output:dir=${PWD}/cmd/cli/kubectl-kyverno/config/crds

.PHONY: codegen-crds-all
codegen-crds-all: codegen-crds-kyverno codegen-crds-policyreport codegen-crds-reports codegen-cli-crds ## Generate all CRDs
Expand Down Expand Up @@ -601,6 +596,7 @@ define generate_crd
| $(SED) -e '/^ annotations:/a \ \ \ \ {{- with .Values.annotations }}' \
| $(SED) -e '/^ annotations:/i \ \ labels:' \
| $(SED) -e '/^ labels:/a \ \ \ \ {{- include "kyverno.crds.labels" . | nindent 4 }}' \
| $(SED) -e 's/(devel)/$(CONTROLLER_GEN_VERSION)/' \
>> ./charts/kyverno/charts/crds/templates/$(3)/$(1)
@echo "{{- end }}" >> ./charts/kyverno/charts/crds/templates/$(3)/$(1)
endef
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.15.0
controller-gen.kubebuilder.io/version: (devel)
name: clusterpolicies.kyverno.io
spec:
group: kyverno.io
Expand Down
2 changes: 1 addition & 1 deletion cmd/cli/kubectl-kyverno/data/crds/kyverno.io_policies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.15.0
controller-gen.kubebuilder.io/version: (devel)
name: policies.kyverno.io
spec:
group: kyverno.io
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.15.0
controller-gen.kubebuilder.io/version: (devel)
name: policyexceptions.kyverno.io
spec:
group: kyverno.io
Expand Down
2 changes: 1 addition & 1 deletion config/crds/kyverno/kyverno.io_cleanuppolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.15.0
controller-gen.kubebuilder.io/version: (devel)
name: cleanuppolicies.kyverno.io
spec:
group: kyverno.io
Expand Down
2 changes: 1 addition & 1 deletion config/crds/kyverno/kyverno.io_clustercleanuppolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.15.0
controller-gen.kubebuilder.io/version: (devel)
name: clustercleanuppolicies.kyverno.io
spec:
group: kyverno.io
Expand Down
2 changes: 1 addition & 1 deletion config/crds/kyverno/kyverno.io_clusterpolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.15.0
controller-gen.kubebuilder.io/version: (devel)
name: clusterpolicies.kyverno.io
spec:
group: kyverno.io
Expand Down
2 changes: 1 addition & 1 deletion config/crds/kyverno/kyverno.io_globalcontextentries.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.15.0
controller-gen.kubebuilder.io/version: (devel)
name: globalcontextentries.kyverno.io
spec:
group: kyverno.io
Expand Down
2 changes: 1 addition & 1 deletion config/crds/kyverno/kyverno.io_policies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.15.0
controller-gen.kubebuilder.io/version: (devel)
name: policies.kyverno.io
spec:
group: kyverno.io
Expand Down
2 changes: 1 addition & 1 deletion config/crds/kyverno/kyverno.io_policyexceptions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.15.0
controller-gen.kubebuilder.io/version: (devel)
name: policyexceptions.kyverno.io
spec:
group: kyverno.io
Expand Down
2 changes: 1 addition & 1 deletion config/crds/kyverno/kyverno.io_updaterequests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.15.0
controller-gen.kubebuilder.io/version: (devel)
name: updaterequests.kyverno.io
spec:
group: kyverno.io
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.15.0
controller-gen.kubebuilder.io/version: (devel)
name: clusterpolicyreports.wgpolicyk8s.io
spec:
group: wgpolicyk8s.io
Expand Down
2 changes: 1 addition & 1 deletion config/crds/policyreport/wgpolicyk8s.io_policyreports.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.15.0
controller-gen.kubebuilder.io/version: (devel)
name: policyreports.wgpolicyk8s.io
spec:
group: wgpolicyk8s.io
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.15.0
controller-gen.kubebuilder.io/version: (devel)
name: clusterephemeralreports.reports.kyverno.io
spec:
group: reports.kyverno.io
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.15.0
controller-gen.kubebuilder.io/version: (devel)
name: ephemeralreports.reports.kyverno.io
spec:
group: reports.kyverno.io
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ require (
k8s.io/pod-security-admission v0.30.1
k8s.io/utils v0.0.0-20240502163921-fe8a2dddb1d0
sigs.k8s.io/controller-runtime v0.18.4
sigs.k8s.io/controller-tools v0.15.0
sigs.k8s.io/kubectl-validate v0.0.4
sigs.k8s.io/kustomize/api v0.17.3
sigs.k8s.io/kustomize/kyaml v0.17.2
Expand Down Expand Up @@ -216,6 +217,7 @@ require (
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-openapi/validate v0.24.0 // indirect
github.com/go-piv/piv-go v1.11.0 // indirect
github.com/gobuffalo/flect v1.0.2 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
Expand Down Expand Up @@ -366,6 +368,7 @@ require (
golang.org/x/sys v0.22.0 // indirect
golang.org/x/term v0.22.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.23.0 // indirect
google.golang.org/api v0.183.0 // indirect
google.golang.org/genproto v0.0.0-20240604185151-ef581f913117 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240604185151-ef581f913117 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,8 @@ github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1v
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
github.com/go-test/deep v1.1.0 h1:WOcxcdHcvdgThNXjw0t76K42FXTU7HpNQWHpA2HHNlg=
github.com/go-test/deep v1.1.0/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
github.com/gobuffalo/flect v1.0.2 h1:eqjPGSo2WmjgY2XlpGwo2NXgL3RucAKo4k4qQMNA5sA=
github.com/gobuffalo/flect v1.0.2/go.mod h1:A5msMlrHtLqh9umBSnvabjsMrCcCpAyzglnDvkbYKHs=
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
Expand Down Expand Up @@ -1236,6 +1238,8 @@ sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.30.3 h1:2770sDpzrjjsA
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.30.3/go.mod h1:Ve9uj1L+deCXFrPOk1LpFXqTg7LCFzFso6PA48q/XZw=
sigs.k8s.io/controller-runtime v0.18.4 h1:87+guW1zhvuPLh1PHybKdYFLU0YJp4FhJRmiHvm5BZw=
sigs.k8s.io/controller-runtime v0.18.4/go.mod h1:TVoGrfdpbA9VRFaRnKgk9P5/atA0pMwq+f+msb9M8Sg=
sigs.k8s.io/controller-tools v0.15.0 h1:4dxdABXGDhIa68Fiwaif0vcu32xfwmgQ+w8p+5CxoAI=
sigs.k8s.io/controller-tools v0.15.0/go.mod h1:8zUSS2T8Hx0APCNRhJWbS3CAQEbIxLa07khzh7pZmXM=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0=
sigs.k8s.io/kubectl-validate v0.0.4 h1:tGKuv0awYHn11Cb6KPsZKxUmHgavF46K3NvVH0Nse9U=
Expand Down
116 changes: 116 additions & 0 deletions hack/controller-gen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package main

import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
"sigs.k8s.io/controller-tools/pkg/crd"
"sigs.k8s.io/controller-tools/pkg/genall"
"sigs.k8s.io/controller-tools/pkg/markers"
)

var (
allGenerators = map[string]genall.Generator{
"crd": crd.Generator{},
}
allOutputRules = map[string]genall.OutputRule{
"dir": genall.OutputToDirectory(""),
"none": genall.OutputToNothing,
"stdout": genall.OutputToStdout,
"artifacts": genall.OutputArtifacts{},
}
optionsRegistry = &markers.Registry{}
)

func init() { //nolint:gochecknoinits
for genName, gen := range allGenerators {
// make the generator options marker itself
defn := markers.Must(markers.MakeDefinition(genName, markers.DescribesPackage, gen))
if err := optionsRegistry.Register(defn); err != nil {
panic(err)
}
if helpGiver, hasHelp := gen.(genall.HasHelp); hasHelp {
if help := helpGiver.Help(); help != nil {
optionsRegistry.AddHelp(defn, help)
}
}
// make per-generation output rule markers
for ruleName, rule := range allOutputRules {
ruleMarker := markers.Must(markers.MakeDefinition(fmt.Sprintf("output:%s:%s", genName, ruleName), markers.DescribesPackage, rule))
if err := optionsRegistry.Register(ruleMarker); err != nil {
panic(err)
}
if helpGiver, hasHelp := rule.(genall.HasHelp); hasHelp {
if help := helpGiver.Help(); help != nil {
optionsRegistry.AddHelp(ruleMarker, help)
}
}
}
}
// make "default output" output rule markers
for ruleName, rule := range allOutputRules {
ruleMarker := markers.Must(markers.MakeDefinition("output:"+ruleName, markers.DescribesPackage, rule))
if err := optionsRegistry.Register(ruleMarker); err != nil {
panic(err)
}
if helpGiver, hasHelp := rule.(genall.HasHelp); hasHelp {
if help := helpGiver.Help(); help != nil {
optionsRegistry.AddHelp(ruleMarker, help)
}
}
}
// add in the common options markers
if err := genall.RegisterOptionsMarkers(optionsRegistry); err != nil {
panic(err)
}
}

type noUsageError struct{ error }

func main() {
cmd := &cobra.Command{
Use: "controller-gen",
RunE: func(c *cobra.Command, rawOpts []string) error {
oneOf, err := markers.MakeAnyTypeDefinition("kubebuilder:oneOf", markers.DescribesType, OneOf{})
if err != nil {
return err
}
not, err := markers.MakeAnyTypeDefinition("kubebuilder:not", markers.DescribesType, Not{})
if err != nil {
return err
}
// otherwise, set up the runtime for actually running the generators
rt, err := genall.FromOptions(optionsRegistry, rawOpts)
if err != nil {
return err
}
if err := rt.Collector.Registry.Register(oneOf); err != nil {
return err
}
if err := rt.Collector.Registry.Register(not); err != nil {
return err
}
if len(rt.Generators) == 0 {
return fmt.Errorf("no generators specified")
}
if hadErrs := rt.Run(); hadErrs {
// don't obscure the actual error with a bunch of usage
return noUsageError{fmt.Errorf("not all generators ran successfully")}
}
return nil
},
SilenceUsage: true, // silence the usage, then print it out ourselves if it wasn't suppressed
}
if err := cmd.Execute(); err != nil {
if _, noUsage := err.(noUsageError); !noUsage {
// print the usage unless we suppressed it
if err := cmd.Usage(); err != nil {
panic(err)
}
}
fmt.Fprintf(cmd.OutOrStderr(), "run `%[1]s %[2]s -w` to see all available markers, or `%[1]s %[2]s -h` for usage\n", cmd.CalledAs(), strings.Join(os.Args[1:], " "))
os.Exit(1)
}
}
37 changes: 37 additions & 0 deletions hack/controller-gen/markers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import (
"encoding/json"

apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
)

type OneOf struct {
Value any
}

func (m OneOf) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
var props apiext.JSONSchemaProps
if data, err := json.Marshal(m.Value); err != nil {
return err
} else if err := json.Unmarshal(data, &props); err != nil {
return err
}
schema.OneOf = append(schema.OneOf, props)
return nil
}

type Not struct {
Value any
}

func (m Not) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
var props apiext.JSONSchemaProps
if data, err := json.Marshal(m.Value); err != nil {
return err
} else if err := json.Unmarshal(data, &props); err != nil {
return err
}
schema.Not = &props
return nil
}

0 comments on commit cfef8a0

Please sign in to comment.