Skip to content

Commit

Permalink
feat(schema): Add marker to skip fields
Browse files Browse the repository at this point in the history
  • Loading branch information
MisterMX committed May 21, 2024
1 parent 85686cb commit 9064e8c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/crd/markers/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ var FieldOnlyMarkers = []*definitionWithHelp{
must(markers.MakeDefinition("optional", markers.DescribesField, struct{}{})).
WithHelp(markers.SimpleHelp("CRD validation", "specifies that this field is optional.")),

must(markers.MakeDefinition("kubebuilder:skip", markers.DescribesField, struct{}{})).
WithHelp(markers.SimpleHelp("CRD generation", "specifies that this field should be skipped for schema generation.")),

must(markers.MakeDefinition("nullable", markers.DescribesField, Nullable{})).
WithHelp(Nullable{}.Help()),

Expand Down
5 changes: 5 additions & 0 deletions pkg/crd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,11 @@ func structToSchema(ctx *schemaContext, structType *ast.StructType) *apiext.JSON
continue
}

if field.Markers.Get("kubebuilder:skip") != nil {
// Explicitly ignore this field for schema generation
continue
}

jsonTag, hasTag := field.Tag.Lookup("json")
if !hasTag {
// if the field doesn't have a JSON tag, it doesn't belong in output (and shouldn't exist in a serialized type)
Expand Down
4 changes: 4 additions & 0 deletions pkg/crd/testdata/gen/foo_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ type FooSpec struct {
// +kubebuilder:default=fooDefaultString
// +kubebuilder:example=fooExampleString
DefaultedString string `json:"defaultedString"`

// This field is explicitly ignored for schema generation.
// +kubebuilder:skip
Skipped string `json:"skipped"`
}
type FooStatus struct{}

Expand Down

0 comments on commit 9064e8c

Please sign in to comment.