Closed everesio closed 1 month ago
Since adding a new CRD marker is very simple, I could create a PR. I would propose the following changes.
New marker in the crd.go
must(markers.MakeDefinition("kubebuilder:selectablefield", markers.DescribesType, SelectableField{})).
WithHelp(SelectableField{}.Help()),
SelectableField in the crd.go
// +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
}
3. Code generated by helpgen in the zz_generated.markerhelp.go would be
```golang
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: "",
},
},
}
}
/kind feature
I look into the details on the PR, but feel free to open a PR for it
@sbueringer Thank you !
Thank you as well! :)
Thanks for implementing this 🙇 This will greatly improve operations on some of the CRDs that we use (and kubebuilder's lack of support for this code marker has been the only missing point for us).
One thing that I believe should still be added is a mention about this in https://book.kubebuilder.io/reference/markers/crd, right? Is this just a matter of pulling in the latest controller tools to kubebuilder or we need to update https://github.com/kubernetes-sigs/kubebuilder/tree/master/docs/book/src/reference/markers ?
@pmalek Just created the v0.16.4 release.
Not sure what is needed on the kubebuilder side (cc @camilamacedo86)
Kubernetes 1.31 introduces the ability to define field selectors for custom resources. You can find more details in the official documentation crd-selectable-fields
This issue is a feature request to add support for this functionality to controller-tools, including the addition markers and extending the documentation to explain how the feature should be used.