diff --git a/api/konfig/general.go b/api/konfig/general.go index 712bfe7894..c465492451 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,17 @@ func DefaultKustomizationFileName() string { return RecognizedKustomizationFileNames()[0] } +func DefaultKustomizationMetadata() *types.ObjectMeta { + configAnnotation := map[string]string{ + filters.LocalConfigAnnotation: "true", + } + defaultMetadata := &types.ObjectMeta{ + Annotations: configAnnotation, + } + + 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