diff --git a/Makefile b/Makefile index a39bcea0c9e3..3a50e8fe0f8e 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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 @@ -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) @@ -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 @@ -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 diff --git a/cmd/cli/kubectl-kyverno/data/crds/kyverno.io_clusterpolicies.yaml b/cmd/cli/kubectl-kyverno/data/crds/kyverno.io_clusterpolicies.yaml index b116a76ef36e..34900db2737e 100644 --- a/cmd/cli/kubectl-kyverno/data/crds/kyverno.io_clusterpolicies.yaml +++ b/cmd/cli/kubectl-kyverno/data/crds/kyverno.io_clusterpolicies.yaml @@ -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 diff --git a/cmd/cli/kubectl-kyverno/data/crds/kyverno.io_policies.yaml b/cmd/cli/kubectl-kyverno/data/crds/kyverno.io_policies.yaml index 87ee0cefdbc1..7b3de058c523 100644 --- a/cmd/cli/kubectl-kyverno/data/crds/kyverno.io_policies.yaml +++ b/cmd/cli/kubectl-kyverno/data/crds/kyverno.io_policies.yaml @@ -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 diff --git a/cmd/cli/kubectl-kyverno/data/crds/kyverno.io_policyexceptions.yaml b/cmd/cli/kubectl-kyverno/data/crds/kyverno.io_policyexceptions.yaml index de3ba8b35bc4..1477cb5907f4 100644 --- a/cmd/cli/kubectl-kyverno/data/crds/kyverno.io_policyexceptions.yaml +++ b/cmd/cli/kubectl-kyverno/data/crds/kyverno.io_policyexceptions.yaml @@ -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 diff --git a/config/crds/kyverno/kyverno.io_cleanuppolicies.yaml b/config/crds/kyverno/kyverno.io_cleanuppolicies.yaml index f5af429ba604..42debd9feaba 100644 --- a/config/crds/kyverno/kyverno.io_cleanuppolicies.yaml +++ b/config/crds/kyverno/kyverno.io_cleanuppolicies.yaml @@ -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 diff --git a/config/crds/kyverno/kyverno.io_clustercleanuppolicies.yaml b/config/crds/kyverno/kyverno.io_clustercleanuppolicies.yaml index cef0014072d4..44bcedc58050 100644 --- a/config/crds/kyverno/kyverno.io_clustercleanuppolicies.yaml +++ b/config/crds/kyverno/kyverno.io_clustercleanuppolicies.yaml @@ -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 diff --git a/config/crds/kyverno/kyverno.io_clusterpolicies.yaml b/config/crds/kyverno/kyverno.io_clusterpolicies.yaml index b116a76ef36e..34900db2737e 100644 --- a/config/crds/kyverno/kyverno.io_clusterpolicies.yaml +++ b/config/crds/kyverno/kyverno.io_clusterpolicies.yaml @@ -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 diff --git a/config/crds/kyverno/kyverno.io_globalcontextentries.yaml b/config/crds/kyverno/kyverno.io_globalcontextentries.yaml index f9dfb1b3885a..c4c743acb294 100644 --- a/config/crds/kyverno/kyverno.io_globalcontextentries.yaml +++ b/config/crds/kyverno/kyverno.io_globalcontextentries.yaml @@ -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 diff --git a/config/crds/kyverno/kyverno.io_policies.yaml b/config/crds/kyverno/kyverno.io_policies.yaml index 87ee0cefdbc1..7b3de058c523 100644 --- a/config/crds/kyverno/kyverno.io_policies.yaml +++ b/config/crds/kyverno/kyverno.io_policies.yaml @@ -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 diff --git a/config/crds/kyverno/kyverno.io_policyexceptions.yaml b/config/crds/kyverno/kyverno.io_policyexceptions.yaml index de3ba8b35bc4..1477cb5907f4 100644 --- a/config/crds/kyverno/kyverno.io_policyexceptions.yaml +++ b/config/crds/kyverno/kyverno.io_policyexceptions.yaml @@ -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 diff --git a/config/crds/kyverno/kyverno.io_updaterequests.yaml b/config/crds/kyverno/kyverno.io_updaterequests.yaml index 489881aba73b..a4e549d44c99 100644 --- a/config/crds/kyverno/kyverno.io_updaterequests.yaml +++ b/config/crds/kyverno/kyverno.io_updaterequests.yaml @@ -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 diff --git a/config/crds/policyreport/wgpolicyk8s.io_clusterpolicyreports.yaml b/config/crds/policyreport/wgpolicyk8s.io_clusterpolicyreports.yaml index db9914ac82bd..ca69f1e25521 100644 --- a/config/crds/policyreport/wgpolicyk8s.io_clusterpolicyreports.yaml +++ b/config/crds/policyreport/wgpolicyk8s.io_clusterpolicyreports.yaml @@ -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 diff --git a/config/crds/policyreport/wgpolicyk8s.io_policyreports.yaml b/config/crds/policyreport/wgpolicyk8s.io_policyreports.yaml index 0dc72f8dcff8..6229552adccd 100644 --- a/config/crds/policyreport/wgpolicyk8s.io_policyreports.yaml +++ b/config/crds/policyreport/wgpolicyk8s.io_policyreports.yaml @@ -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 diff --git a/config/crds/reports/reports.kyverno.io_clusterephemeralreports.yaml b/config/crds/reports/reports.kyverno.io_clusterephemeralreports.yaml index 3f9b9abaacfd..a9aea77d6f7c 100644 --- a/config/crds/reports/reports.kyverno.io_clusterephemeralreports.yaml +++ b/config/crds/reports/reports.kyverno.io_clusterephemeralreports.yaml @@ -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 diff --git a/config/crds/reports/reports.kyverno.io_ephemeralreports.yaml b/config/crds/reports/reports.kyverno.io_ephemeralreports.yaml index 8fbed3a19229..eca3c51fa63c 100644 --- a/config/crds/reports/reports.kyverno.io_ephemeralreports.yaml +++ b/config/crds/reports/reports.kyverno.io_ephemeralreports.yaml @@ -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 diff --git a/go.mod b/go.mod index 854b8e922f55..49cfd65d1c59 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 @@ -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 diff --git a/go.sum b/go.sum index a9d9ab7f7670..299c2b097c1f 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/hack/controller-gen/main.go b/hack/controller-gen/main.go new file mode 100644 index 000000000000..77b40ee7d91f --- /dev/null +++ b/hack/controller-gen/main.go @@ -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) + } +} diff --git a/hack/controller-gen/markers.go b/hack/controller-gen/markers.go new file mode 100644 index 000000000000..eaf8f049217d --- /dev/null +++ b/hack/controller-gen/markers.go @@ -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 +}