From b4be2bd7f0775bf408dee42a599e4889621ec1ea Mon Sep 17 00:00:00 2001 From: Mason Cole Date: Fri, 8 Nov 2024 08:37:05 -0800 Subject: [PATCH] feat(cmd): add local-config annotation on create this adds a DefaultMetadata object in konfig that can be used when creating a new kustomization yaml that conforms with the best practice recommendations in the kustomize docs. > The `Kustomization` config in a `kustomization.yaml` > **SHOULD** contain this annotation so that tools know it is not intended to be sent to > the Kubernetes api server. --- api/konfig/general.go | 16 ++++++++++++++++ kustomize/commands/create/create.go | 1 + kustomize/commands/create/create_test.go | 16 ++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/api/konfig/general.go b/api/konfig/general.go index 712bfe7894..10ce6e8784 100644 --- a/api/konfig/general.go +++ b/api/konfig/general.go @@ -3,6 +3,10 @@ package konfig +import ( + "sigs.k8s.io/kustomize/api/types" +) + // RecognizedKustomizationFileNames is a list of file names // that kustomize recognizes. // To avoid ambiguity, a kustomization directory may not @@ -19,6 +23,18 @@ func DefaultKustomizationFileName() string { return RecognizedKustomizationFileNames()[0] } +// DefaultKustomizationMetadata returns the default metadata +// for a kustomization configuration. +func DefaultKustomizationMetadata() *types.ObjectMeta { + defaultMetadata := &types.ObjectMeta{ + Annotations: map[string]string{ + IgnoredByKustomizeAnnotation: "true", + }, + } + + return defaultMetadata +} + const ( // An environment variable to consult for kustomization // configuration data. See: diff --git a/kustomize/commands/create/create.go b/kustomize/commands/create/create.go index 6b867c9089..1683d64af8 100644 --- a/kustomize/commands/create/create.go +++ b/kustomize/commands/create/create.go @@ -133,6 +133,7 @@ func runCreate(opts createFlags, fSys filesys.FileSystem, rf *resource.Factory) if err != nil { return err } + m.MetaData = konfig.DefaultKustomizationMetadata() m.Resources = resources m.Namespace = opts.namespace m.NamePrefix = opts.prefix diff --git a/kustomize/commands/create/create_test.go b/kustomize/commands/create/create_test.go index adcf56f691..4594f0ae50 100644 --- a/kustomize/commands/create/create_test.go +++ b/kustomize/commands/create/create_test.go @@ -9,6 +9,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "sigs.k8s.io/kustomize/api/konfig" "sigs.k8s.io/kustomize/api/provider" "sigs.k8s.io/kustomize/api/types" "sigs.k8s.io/kustomize/kustomize/v5/commands/internal/kustfile" @@ -134,6 +135,21 @@ func TestCreateWithNameSuffix(t *testing.T) { } } +func TestCreateHasDefaultMetadata(t *testing.T) { + fSys := filesys.MakeEmptyDirInMemory() + fSys.WriteFile("foo.yaml", []byte("")) + fSys.WriteFile("bar.yaml", []byte("")) + opts := createFlags{resources: "foo.yaml,bar.yaml"} + err := runCreate(opts, fSys, factory) + + require.NoErrorf(t, err, "unexpected cmd error: %v") + + m := readKustomizationFS(t, fSys) + expected := konfig.DefaultKustomizationMetadata() + + require.Truef(t, reflect.DeepEqual(m.MetaData, expected), "expected %v but got %v", expected, m.MetaData) +} + func writeDetectContent(fSys filesys.FileSystem) { fSys.WriteFile("/test.yaml", []byte(` apiVersion: v1