From aa573f300a04927b2d77cbced4ad473eee4ac25f 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 | 15 +++++++++++++++ kustomize/commands/create/create.go | 1 + kustomize/commands/create/create_test.go | 17 +++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/api/konfig/general.go b/api/konfig/general.go index 712bfe7894..6680d47f17 100644 --- a/api/konfig/general.go +++ b/api/konfig/general.go @@ -3,6 +3,11 @@ package konfig +import ( + "sigs.k8s.io/kustomize/api/types" + "sigs.k8s.io/kustomize/kyaml/kio/filters" +) + // RecognizedKustomizationFileNames is a list of file names // that kustomize recognizes. // To avoid ambiguity, a kustomization directory may not @@ -19,6 +24,16 @@ func DefaultKustomizationFileName() string { return RecognizedKustomizationFileNames()[0] } +func DefaultKustomizationMetadata() *types.ObjectMeta { + defaultMetadata := &types.ObjectMeta{ + Annotations: map[string]string{ + filters.LocalConfigAnnotation: "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..5f58f421a5 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,22 @@ 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) + if err != nil { + t.Errorf("unexpected cmd error: %v", err) + } + m := readKustomizationFS(t, fSys) + expected := konfig.DefaultKustomizationMetadata() + if !reflect.DeepEqual(m.MetaData, expected) { + t.Fatalf("expected %+v but got %+v", expected, m.Resources) + } +} + func writeDetectContent(fSys filesys.FileSystem) { fSys.WriteFile("/test.yaml", []byte(` apiVersion: v1