Skip to content

✨ New kubebuilder:schemaModifier CRD marker #1140

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ hack/tools/bin

junit-report.xml
/artifacts
tmp
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit, missing the new line char at the end

3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/gobuffalo/flect v1.0.3
github.com/google/go-cmp v0.7.0
github.com/onsi/ginkgo v1.16.5
github.com/onsi/ginkgo/v2 v2.23.3
github.com/onsi/gomega v1.37.0
github.com/spf13/cobra v1.9.1
github.com/spf13/pflag v1.0.6
Expand All @@ -29,8 +30,10 @@ require (
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/gnostic-models v0.6.9 // indirect
github.com/google/pprof v0.0.0-20241210010833-40e02aabc2ad // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ github.com/go-openapi/jsonreference v0.20.2/go.mod h1:Bl1zwGIM8/wsvqjsOQLJ/SH+En
github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+GrE=
github.com/go-openapi/swag v0.23.0/go.mod h1:esZ8ITTYEsH1V2trKHjAN8Ai7xHb8RV+YSZ577vPjgQ=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/go-task/slim-sprig/v3 v3.0.0 h1:sUs3vkvUymDpBKi3qH1YSqBQk9+9D/8M2mN1vB6EwHI=
github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZiAzKg9hl15HA8=
Expand Down
187 changes: 187 additions & 0 deletions pkg/crd/markers/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ package markers

import (
"fmt"
"path/filepath"
"regexp"
"strings"

apiext "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/utils/ptr"

"sigs.k8s.io/controller-tools/pkg/markers"
)

Expand Down Expand Up @@ -57,6 +61,9 @@ var CRDMarkers = []*definitionWithHelp{

must(markers.MakeDefinition("kubebuilder:selectablefield", markers.DescribesType, SelectableField{})).
WithHelp(SelectableField{}.Help()),

must(markers.MakeDefinition("kubebuilder:schemaModifier", markers.DescribesType, SchemaModifier{})).
WithHelp(SchemaModifier{}.Help()),
}

// TODO: categories and singular used to be annotations types
Expand Down Expand Up @@ -419,3 +426,183 @@ func (s SelectableField) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, ve

return nil
}

// +controllertools:marker:generateHelp:category=CRD

// SchemaModifier allows modifying JSONSchemaProps for CRDs.
//
// The PathPattern field defines the rule for selecting target fields within the CRD structure.
// This rule is specified as a path in a JSONPath-like format and supports special wildcard characters:
// - `*`: matches any single field name (e.g., `/spec/*/field`).
// - `**`: matches fields at any depth, across multiple levels of nesting (e.g., `/spec/**/field`).
Comment on lines +434 to +437
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the use case for patterns? This seems like it could have unintended consequences/people may not realise quite how much impact it might have

//
// Example:
// +kubebuilder:schemaModifier:pathPattern=/spec/exampleField/*,description=""
Copy link
Contributor

Choose a reason for hiding this comment

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

What would an example of this with something that is not just a simple string look like? E.g. required?

//
// In this example, all fields matching the path `/spec/exampleField/*` will have the empty description applied.
//
// Any specified values (e.g., Description, Format, Maximum, etc.) will be applied to all schemas matching the given path.
type SchemaModifier struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the list of fields comprehensive, and, if so, how can we ensure in the future, if additional fields are needed, that we do not forget to add them?

// PathPattern defines the path for selecting JSON schemas.
// Supports `*` and `**` for matching nested fields.
PathPattern string `marker:"pathPattern"`

// Description sets a new value for JSONSchemaProps.Description.
Description *string `marker:",optional"`
// Format sets a new value for JSONSchemaProps.Format.
Format *string `marker:",optional"`
// Maximum sets a new value for JSONSchemaProps.Maximum.
Maximum *float64 `marker:",optional"`
// ExclusiveMaximum sets a new value for JSONSchemaProps.ExclusiveMaximum.
ExclusiveMaximum *bool `marker:",optional"`
// Minimum sets a new value for JSONSchemaProps.Minimum.
Minimum *float64 `marker:",optional"`
// ExclusiveMinimum sets a new value for JSONSchemaProps.ExclusiveMinimum.
ExclusiveMinimum *bool `marker:",optional"`
// MaxLength sets a new value for JSONSchemaProps.MaxLength.
MaxLength *int `marker:",optional"`
// MinLength sets a new value for JSONSchemaProps.MinLength.
MinLength *int `marker:",optional"`
// Pattern sets a new value for JSONSchemaProps.Pattern.
Pattern *string `marker:",optional"`
// MaxItems sets a new value for JSONSchemaProps.MaxItems.
MaxItems *int `marker:",optional"`
// MinItems sets a new value for JSONSchemaProps.MinItems.
MinItems *int `marker:",optional"`
// UniqueItems sets a new value for JSONSchemaProps.UniqueItems.
UniqueItems *bool `marker:",optional"`
// MultipleOf sets a new value for JSONSchemaProps.MultipleOf.
MultipleOf *float64 `marker:",optional"`
// MaxProperties sets a new value for JSONSchemaProps.MaxProperties.
MaxProperties *int `marker:",optional"`
// MinProperties sets a new value for JSONSchemaProps.MinProperties.
MinProperties *int `marker:",optional"`
// Required sets a new value for JSONSchemaProps.Required.
Required *[]string `marker:",optional"`
// Nullable sets a new value for JSONSchemaProps.Nullable.
Nullable *bool `marker:",optional"`
}

func (s SchemaModifier) ApplyToCRD(crd *apiext.CustomResourceDefinitionSpec, _ string) error {
ruleRegex, err := s.ParsePattern()
if err != nil {
return fmt.Errorf("failed to parse rule: %w", err)
}

for i := range crd.Versions {
ver := &crd.Versions[i]
if err = s.applyRuleToSchema(ver.Schema.OpenAPIV3Schema, ruleRegex, "/"); err != nil {
return err
}
}
return nil
}

func (s SchemaModifier) applyRuleToSchema(schema *apiext.JSONSchemaProps, ruleRegex *regexp.Regexp, path string) error {
if schema == nil {
return nil
}

if ruleRegex.MatchString(path) {
s.applyToSchema(schema)
}

if schema.Properties != nil {
for key := range schema.Properties {
prop := schema.Properties[key]

newPath := filepath.Join(path, key)

if err := s.applyRuleToSchema(&prop, ruleRegex, newPath); err != nil {
return err
}
schema.Properties[key] = prop
}
}

if schema.Items != nil {
if schema.Items.Schema != nil {
if err := s.applyRuleToSchema(schema.Items.Schema, ruleRegex, path+"/items"); err != nil {
return err
}
} else if len(schema.Items.JSONSchemas) > 0 {
for i, item := range schema.Items.JSONSchemas {
newPath := fmt.Sprintf("%s/items/%d", path, i)
if err := s.applyRuleToSchema(&item, ruleRegex, newPath); err != nil {
return err
}
}
}
}

return nil
}

func (s SchemaModifier) applyToSchema(schema *apiext.JSONSchemaProps) {
if schema == nil {
return
}
if s.Description != nil {
schema.Description = *s.Description
}
if s.Format != nil {
schema.Format = *s.Format
}
if s.Maximum != nil {
schema.Maximum = s.Maximum
}
if s.ExclusiveMaximum != nil {
schema.ExclusiveMaximum = *s.ExclusiveMaximum
}
if s.Minimum != nil {
schema.Minimum = s.Minimum
}
if s.ExclusiveMinimum != nil {
schema.ExclusiveMinimum = *s.ExclusiveMinimum
}
if s.MaxLength != nil {
schema.MaxLength = ptr.To(int64(*s.MaxLength))
}
if s.MinLength != nil {
schema.MinLength = ptr.To(int64(*s.MinLength))
}
if s.Pattern != nil {
schema.Pattern = *s.Pattern
}
if s.MaxItems != nil {
schema.MaxItems = ptr.To(int64(*s.MaxItems))
}
if s.MinItems != nil {
schema.MinItems = ptr.To(int64(*s.MinItems))
}
if s.UniqueItems != nil {
schema.UniqueItems = *s.UniqueItems
}
if s.MultipleOf != nil {
schema.MultipleOf = s.MultipleOf
}
if s.MaxProperties != nil {
schema.MaxProperties = ptr.To(int64(*s.MaxProperties))
}
if s.MinProperties != nil {
schema.MinProperties = ptr.To(int64(*s.MinProperties))
}
if s.Required != nil {
schema.Required = *s.Required
}
if s.Nullable != nil {
schema.Nullable = *s.Nullable
}
}

func (s SchemaModifier) ParsePattern() (*regexp.Regexp, error) {
pattern := strings.NewReplacer("**", ".*", "*", "[^/]+").Replace(s.PathPattern)
regexStr := "^" + pattern + "$"

compiledRegex, err := regexp.Compile(regexStr)
if err != nil {
return nil, fmt.Errorf("invalid rule: %w", err)
}

return compiledRegex, nil
}
Loading