Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for selectable fields for custom resources #1039

Closed
everesio opened this issue Aug 17, 2024 · 9 comments
Closed

Support for selectable fields for custom resources #1039

everesio opened this issue Aug 17, 2024 · 9 comments
Labels
kind/feature Categorizes issue or PR as related to a new feature.

Comments

@everesio
Copy link

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
Copy link
Author

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()),
  1. 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
}
  1. Code generated by helpgen in the zz_generated.markerhelp.go would be
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
Copy link

/kind feature

@k8s-ci-robot k8s-ci-robot added the kind/feature Categorizes issue or PR as related to a new feature. label Sep 4, 2024
@sbueringer
Copy link
Member

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

@everesio
Copy link
Author

everesio commented Sep 6, 2024

PR #1050

@everesio
Copy link
Author

PR was merged #1050

@everesio
Copy link
Author

@sbueringer Thank you !

@sbueringer
Copy link
Member

Thank you as well! :)

@pmalek
Copy link
Contributor

pmalek commented Oct 10, 2024

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
Copy link
Member

@pmalek Just created the v0.16.4 release.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature Categorizes issue or PR as related to a new feature.
Projects
None yet
Development

No branches or pull requests

5 participants