kubernetes-sigs / controller-tools

Tools to use with the controller-runtime libraries
Apache License 2.0
727 stars 418 forks source link

Support for selectable fields for custom resources #1039

Closed everesio closed 1 month ago

everesio commented 2 months ago

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.

everesio commented 2 months ago

Since adding a new CRD marker is very simple, I could create a PR. I would propose the following changes.

  1. New marker in the crd.go

    must(markers.MakeDefinition("kubebuilder:selectablefield", markers.DescribesType, SelectableField{})).
        WithHelp(SelectableField{}.Help()),
  2. 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: "",
            },
        },
    }
}
liangyuanpeng commented 1 month ago

/kind feature

sbueringer commented 1 month ago

I look into the details on the PR, but feel free to open a PR for it

everesio commented 1 month ago

PR https://github.com/kubernetes-sigs/controller-tools/pull/1050

everesio commented 1 month ago

PR was merged https://github.com/kubernetes-sigs/controller-tools/pull/1050

everesio commented 1 month ago

@sbueringer Thank you !

sbueringer commented 1 month ago

Thank you as well! :)

pmalek commented 2 weeks ago

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 ?

sbueringer commented 2 weeks ago

@pmalek Just created the v0.16.4 release.

Not sure what is needed on the kubebuilder side (cc @camilamacedo86)