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

✨ Add selectablefield marker #1050

Merged
merged 2 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions pkg/crd/markers/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a few of these to

// +kubebuilder:metadata:annotations="api-approved.kubernetes.io=https://github.com/kubernetes-sigs/controller-tools";"cert-manager.io/inject-ca-from-secret=cert-manager/cert-manager-webhook-ca"

IIRC they then also have to be added here: https://github.com/kubernetes-sigs/controller-tools/blob/7ef21aae5452047af82f020774639c94aa659eca/pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml

(but you can just check the failing test after the marker was added to the type)

WithHelp(SelectableField{}.Help()),
}

// TODO: categories and singular used to be annotations types
Expand Down Expand Up @@ -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 {
sbueringer marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("selectable field applied to version %q not in CRD", version)
}

*selectableFields = append(*selectableFields, apiext.SelectableField{
JSONPath: s.JSONPath,
})

return nil
}
28 changes: 22 additions & 6 deletions pkg/crd/markers/zz_generated.markerhelp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pkg/crd/testdata/cronjob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,9 @@ type CronJobSpec struct {
// +optional
// +default="TCP"
Protocol corev1.Protocol `json:"protocol,omitempty" protobuf:"bytes,4,opt,name=protocol,casttype=Protocol"`

// This tests that selectable field.
SelectableFieldString string `json:"selectableFieldString,omitempty"`
}

type ContainsNestedMap struct {
Expand Down Expand Up @@ -634,6 +637,7 @@ type CronJobStatus struct {
// +kubebuilder:resource:singular=mycronjob
// +kubebuilder:storageversion
// +kubebuilder:metadata:annotations="api-approved.kubernetes.io=https://github.com/kubernetes-sigs/controller-tools";"cert-manager.io/inject-ca-from-secret=cert-manager/cert-manager-webhook-ca"
// +kubebuilder:selectablefield:JSONPath=`.spec.selectableFieldString`

// CronJob is the Schema for the cronjobs API
type CronJob struct {
Expand Down
5 changes: 5 additions & 0 deletions pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8966,6 +8966,9 @@ spec:
type: string
schemaless:
description: This tests that the schemaless marker works
selectableFieldString:
description: This tests that selectable field.
type: string
startingDeadlineSeconds:
description: |-
Optional deadline in seconds for starting the job if it misses scheduled
Expand Down Expand Up @@ -9203,6 +9206,8 @@ spec:
type: string
type: object
type: object
selectableFields:
- jsonPath: .spec.selectableFieldString
served: true
storage: true
subresources:
Expand Down
6 changes: 3 additions & 3 deletions pkg/crd/zz_generated.markerhelp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/genall/zz_generated.markerhelp.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.