Skip to content

Commit

Permalink
✨ Add selectablefield marker (#1050)
Browse files Browse the repository at this point in the history
* Add selectablefield marker

* Selectable field marker parser test
  • Loading branch information
Michal Budzyn authored Sep 24, 2024
1 parent 3b70a40 commit f79cfef
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 10 deletions.
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{})).
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 {
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 @@ -352,6 +352,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 StringAlias = string
Expand Down Expand Up @@ -639,6 +642,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 @@ -9206,6 +9209,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.

0 comments on commit f79cfef

Please sign in to comment.