From 58b950e80f5acf2778c61d11c90b9b754ba0451b Mon Sep 17 00:00:00 2001 From: Maximilian Blatt Date: Tue, 21 May 2024 12:47:51 +0200 Subject: [PATCH] feat(schema): Add marker to skip fields Signed-off-by: Maximilian Blatt --- pkg/crd/markers/validation.go | 3 +++ pkg/crd/schema.go | 5 +++++ pkg/crd/testdata/gen/foo_types.go | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/pkg/crd/markers/validation.go b/pkg/crd/markers/validation.go index 7ed2c97b8..b9f3f16a0 100644 --- a/pkg/crd/markers/validation.go +++ b/pkg/crd/markers/validation.go @@ -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()), diff --git a/pkg/crd/schema.go b/pkg/crd/schema.go index 2eaadb675..a6b990d59 100644 --- a/pkg/crd/schema.go +++ b/pkg/crd/schema.go @@ -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) diff --git a/pkg/crd/testdata/gen/foo_types.go b/pkg/crd/testdata/gen/foo_types.go index 089904770..73ebb824e 100644 --- a/pkg/crd/testdata/gen/foo_types.go +++ b/pkg/crd/testdata/gen/foo_types.go @@ -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{}