From e079927bda9af4ccbfee53600e866c5eae1c9874 Mon Sep 17 00:00:00 2001 From: Michal Budzyn Date: Sun, 18 Aug 2024 09:54:50 +0200 Subject: [PATCH] Add selectablefield marker --- pkg/crd/markers/crd.go | 32 ++++++++++++++++++++++ pkg/crd/markers/zz_generated.markerhelp.go | 28 +++++++++++++++---- pkg/crd/zz_generated.markerhelp.go | 6 ++-- pkg/genall/zz_generated.markerhelp.go | 2 +- 4 files changed, 58 insertions(+), 10 deletions(-) diff --git a/pkg/crd/markers/crd.go b/pkg/crd/markers/crd.go index 1b1cf5ede..36f3fcec3 100644 --- a/pkg/crd/markers/crd.go +++ b/pkg/crd/markers/crd.go @@ -55,6 +55,9 @@ var CRDMarkers = []*definitionWithHelp{ must(markers.MakeDefinition("kubebuilder:metadata", markers.DescribesType, Metadata{})). WithHelp(Metadata{}.Help()), + + must(markers.MakeDefinition("kubebuilder:selectablefield", markers.DescribesType, SelectableField{})). + WithHelp(SelectableField{}.Help()), } // TODO: categories and singular used to be annotations types @@ -388,3 +391,32 @@ func (s Metadata) ApplyToCRD(crd *apiext.CustomResourceDefinition, _ string) err return nil } + +// +controllertools:marker:generateHelp:category=CRD + +// SelectableField adds a field that may be used with field selectors. +type SelectableField struct { + // JSONPath specifies the jsonpath expression which is used to produce a field selector value. + JSONPath string `marker:"JSONPath"` +} + +func (s SelectableField) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, version string) error { + var selectableFields *[]apiext.SelectableField + for i := range crd.Versions { + ver := &crd.Versions[i] + if ver.Name != version { + continue + } + selectableFields = &ver.SelectableFields + break + } + if selectableFields == nil { + return fmt.Errorf("selectable field applied to version %q not in CRD", version) + } + + *selectableFields = append(*selectableFields, apiext.SelectableField{ + JSONPath: s.JSONPath, + }) + + return nil +} diff --git a/pkg/crd/markers/zz_generated.markerhelp.go b/pkg/crd/markers/zz_generated.markerhelp.go index 7fb1b2164..e2db2b990 100644 --- a/pkg/crd/markers/zz_generated.markerhelp.go +++ b/pkg/crd/markers/zz_generated.markerhelp.go @@ -148,7 +148,7 @@ func (ListType) Help() *markers.DefinitionHelp { Category: "CRD processing", DetailedHelp: markers.DetailedHelp{ Summary: "specifies the type of data-structure that the list", - Details: "represents (map, set, atomic).\n\n\nPossible data-structure types of a list are:\n\n\n - \"map\": it needs to have a key field, which will be used to build an\n associative list. A typical example is a the pod container list,\n which is indexed by the container name.\n\n\n - \"set\": Fields need to be \"scalar\", and there can be only one\n occurrence of each.\n\n\n - \"atomic\": All the fields in the list are treated as a single value,\n are typically manipulated together by the same actor.", + Details: "represents (map, set, atomic).\n\nPossible data-structure types of a list are:\n\n - \"map\": it needs to have a key field, which will be used to build an\n associative list. A typical example is a the pod container list,\n which is indexed by the container name.\n\n - \"set\": Fields need to be \"scalar\", and there can be only one\n occurrence of each.\n\n - \"atomic\": All the fields in the list are treated as a single value,\n are typically manipulated together by the same actor.", }, FieldHelp: map[string]markers.DetailedHelp{}, } @@ -159,7 +159,7 @@ func (MapType) Help() *markers.DefinitionHelp { Category: "CRD processing", DetailedHelp: markers.DetailedHelp{ Summary: "specifies the level of atomicity of the map;", - Details: "i.e. whether each item in the map is independent of the others,\nor all fields are treated as a single unit.\n\n\nPossible values:\n\n\n - \"granular\": items in the map are independent of each other,\n and can be manipulated by different actors.\n This is the default behavior.\n\n\n - \"atomic\": all fields are treated as one unit.\n Any changes have to replace the entire map.", + Details: "i.e. whether each item in the map is independent of the others,\nor all fields are treated as a single unit.\n\nPossible values:\n\n - \"granular\": items in the map are independent of each other,\n and can be manipulated by different actors.\n This is the default behavior.\n\n - \"atomic\": all fields are treated as one unit.\n Any changes have to replace the entire map.", }, FieldHelp: map[string]markers.DetailedHelp{}, } @@ -385,6 +385,22 @@ func (Schemaless) Help() *markers.DefinitionHelp { } } +func (SelectableField) Help() *markers.DefinitionHelp { + return &markers.DefinitionHelp{ + Category: "CRD", + DetailedHelp: markers.DetailedHelp{ + Summary: "adds a field that may be used with field selectors.", + Details: "", + }, + FieldHelp: map[string]markers.DetailedHelp{ + "JSONPath": { + Summary: "specifies the jsonpath expression which is used to produce a field selector value.", + Details: "", + }, + }, + } +} + func (SkipVersion) Help() *markers.DefinitionHelp { return &markers.DefinitionHelp{ Category: "CRD", @@ -412,7 +428,7 @@ func (StructType) Help() *markers.DefinitionHelp { Category: "CRD processing", DetailedHelp: markers.DetailedHelp{ Summary: "specifies the level of atomicity of the struct;", - Details: "i.e. whether each field in the struct is independent of the others,\nor all fields are treated as a single unit.\n\n\nPossible values:\n\n\n - \"granular\": fields in the struct are independent of each other,\n and can be manipulated by different actors.\n This is the default behavior.\n\n\n - \"atomic\": all fields are treated as one unit.\n Any changes have to replace the entire struct.", + Details: "i.e. whether each field in the struct is independent of the others,\nor all fields are treated as a single unit.\n\nPossible values:\n\n - \"granular\": fields in the struct are independent of each other,\n and can be manipulated by different actors.\n This is the default behavior.\n\n - \"atomic\": all fields are treated as one unit.\n Any changes have to replace the entire struct.", }, FieldHelp: map[string]markers.DetailedHelp{}, } @@ -502,7 +518,7 @@ func (XIntOrString) Help() *markers.DefinitionHelp { Category: "CRD validation", DetailedHelp: markers.DetailedHelp{ Summary: "IntOrString marks a fields as an IntOrString.", - Details: "This is required when applying patterns or other validations to an IntOrString\nfield. Knwon information about the type is applied during the collapse phase\nand as such is not normally available during marker application.", + Details: "This is required when applying patterns or other validations to an IntOrString\nfield. Known information about the type is applied during the collapse phase\nand as such is not normally available during marker application.", }, FieldHelp: map[string]markers.DetailedHelp{}, } @@ -513,7 +529,7 @@ func (XPreserveUnknownFields) Help() *markers.DefinitionHelp { Category: "CRD processing", DetailedHelp: markers.DetailedHelp{ Summary: "PreserveUnknownFields stops the apiserver from pruning fields which are not specified.", - Details: "By default the apiserver drops unknown fields from the request payload\nduring the decoding step. This marker stops the API server from doing so.\nIt affects fields recursively, but switches back to normal pruning behaviour\nif nested properties or additionalProperties are specified in the schema.\nThis can either be true or undefined. False\nis forbidden.\n\n\nNB: The kubebuilder:validation:XPreserveUnknownFields variant is deprecated\nin favor of the kubebuilder:pruning:PreserveUnknownFields variant. They function\nidentically.", + Details: "By default the apiserver drops unknown fields from the request payload\nduring the decoding step. This marker stops the API server from doing so.\nIt affects fields recursively, but switches back to normal pruning behaviour\nif nested properties or additionalProperties are specified in the schema.\nThis can either be true or undefined. False\nis forbidden.\n\nNB: The kubebuilder:validation:XPreserveUnknownFields variant is deprecated\nin favor of the kubebuilder:pruning:PreserveUnknownFields variant. They function\nidentically.", }, FieldHelp: map[string]markers.DetailedHelp{}, } @@ -524,7 +540,7 @@ func (XValidation) Help() *markers.DefinitionHelp { Category: "CRD validation", DetailedHelp: markers.DetailedHelp{ Summary: "marks a field as requiring a value for which a given", - Details: "expression evaluates to true.\n\n\nThis marker may be repeated to specify multiple expressions, all of\nwhich must evaluate to true.", + Details: "expression evaluates to true.\n\nThis marker may be repeated to specify multiple expressions, all of\nwhich must evaluate to true.", }, FieldHelp: map[string]markers.DetailedHelp{ "Rule": { diff --git a/pkg/crd/zz_generated.markerhelp.go b/pkg/crd/zz_generated.markerhelp.go index c1aa3853f..bf480ded9 100644 --- a/pkg/crd/zz_generated.markerhelp.go +++ b/pkg/crd/zz_generated.markerhelp.go @@ -38,7 +38,7 @@ func (Generator) Help() *markers.DefinitionHelp { }, "AllowDangerousTypes": { Summary: "allows types which are usually omitted from CRD generation", - Details: "because they are not recommended.\n\n\nCurrently the following additional types are allowed when this is true:\nfloat32\nfloat64\n\n\nLeft unspecified, the default is false", + Details: "because they are not recommended.\n\nCurrently the following additional types are allowed when this is true:\nfloat32\nfloat64\n\nLeft unspecified, the default is false", }, "MaxDescLen": { Summary: "specifies the maximum description length for fields in CRD's OpenAPI schema.", @@ -46,7 +46,7 @@ func (Generator) Help() *markers.DefinitionHelp { }, "CRDVersions": { Summary: "specifies the target API versions of the CRD type itself to", - Details: "generate. Defaults to v1.\n\n\nCurrently, the only supported value is v1.\n\n\nThe first version listed will be assumed to be the \"default\" version and\nwill not get a version suffix in the output filename.\n\n\nYou'll need to use \"v1\" to get support for features like defaulting,\nalong with an API server that supports it (Kubernetes 1.16+).", + Details: "generate. Defaults to v1.\n\nCurrently, the only supported value is v1.\n\nThe first version listed will be assumed to be the \"default\" version and\nwill not get a version suffix in the output filename.\n\nYou'll need to use \"v1\" to get support for features like defaulting,\nalong with an API server that supports it (Kubernetes 1.16+).", }, "GenerateEmbeddedObjectMeta": { Summary: "specifies if any embedded ObjectMeta in the CRD should be generated", @@ -62,7 +62,7 @@ func (Generator) Help() *markers.DefinitionHelp { }, "DeprecatedV1beta1CompatibilityPreserveUnknownFields": { Summary: "indicates whether", - Details: "or not we should turn off field pruning for this resource.\n\n\nSpecifies spec.preserveUnknownFields value that is false and omitted by default.\nThis value can only be specified for CustomResourceDefinitions that were created with\n`apiextensions.k8s.io/v1beta1`.\n\n\nThe field can be set for compatiblity reasons, although strongly discouraged, resource\nauthors should move to a structural OpenAPI schema instead.\n\n\nSee https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#field-pruning\nfor more information about field pruning and v1beta1 resources compatibility.", + Details: "or not we should turn off field pruning for this resource.\n\nSpecifies spec.preserveUnknownFields value that is false and omitted by default.\nThis value can only be specified for CustomResourceDefinitions that were created with\n`apiextensions.k8s.io/v1beta1`.\n\nThe field can be set for compatiblity reasons, although strongly discouraged, resource\nauthors should move to a structural OpenAPI schema instead.\n\nSee https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#field-pruning\nfor more information about field pruning and v1beta1 resources compatibility.", }, }, } diff --git a/pkg/genall/zz_generated.markerhelp.go b/pkg/genall/zz_generated.markerhelp.go index 998061b9b..b33c2392e 100644 --- a/pkg/genall/zz_generated.markerhelp.go +++ b/pkg/genall/zz_generated.markerhelp.go @@ -40,7 +40,7 @@ func (OutputArtifacts) Help() *markers.DefinitionHelp { Category: "", DetailedHelp: markers.DetailedHelp{ Summary: "outputs artifacts to different locations, depending on", - Details: "whether they're package-associated or not.\n\n\nNon-package associated artifacts\nare output to the Config directory, while package-associated ones are output\nto their package's source files' directory, unless an alternate path is\nspecified in Code.", + Details: "whether they're package-associated or not.\n\nNon-package associated artifacts\nare output to the Config directory, while package-associated ones are output\nto their package's source files' directory, unless an alternate path is\nspecified in Code.", }, FieldHelp: map[string]markers.DetailedHelp{ "Config": {