diff --git a/api/internal/accumulator/namereferencetransformer_test.go b/api/internal/accumulator/namereferencetransformer_test.go index e162f193b..41327acfc 100644 --- a/api/internal/accumulator/namereferencetransformer_test.go +++ b/api/internal/accumulator/namereferencetransformer_test.go @@ -721,7 +721,7 @@ func TestNameReferenceNamespace(t *testing.T) { t.Fatalf("unexpected error: %v", err) } - m.RemoveIdAnnotations() + m.RemoveBuildAnnotations() if err = expected.ErrorIfNotEqualLists(m); err != nil { t.Fatalf("actual doesn't match expected: %v", err) } @@ -883,7 +883,7 @@ func TestNameReferenceClusterWide(t *testing.T) { t.Fatalf("unexpected error: %v", err) } - m.RemoveIdAnnotations() + m.RemoveBuildAnnotations() if err = expected.ErrorIfNotEqualLists(m); err != nil { t.Fatalf("actual doesn't match expected: %v", err) } @@ -1010,7 +1010,7 @@ func TestNameReferenceNamespaceTransformation(t *testing.T) { t.Fatalf("unexpected error: %v", err) } - m.RemoveIdAnnotations() + m.RemoveBuildAnnotations() if err = expected.ErrorIfNotEqualLists(m); err != nil { t.Fatalf("actual doesn't match expected: %v", err) } @@ -1047,7 +1047,7 @@ func TestNameReferenceCandidateSelection(t *testing.T) { t.Fatalf("unexpected error: %v", err) } - m.RemoveIdAnnotations() + m.RemoveBuildAnnotations() if err = expected.ErrorIfNotEqualLists(m); err != nil { t.Fatalf("actual doesn't match expected: %v", err) } diff --git a/api/internal/plugins/execplugin/execplugin_test.go b/api/internal/plugins/execplugin/execplugin_test.go index 16822cd73..f49e02d5f 100644 --- a/api/internal/plugins/execplugin/execplugin_test.go +++ b/api/internal/plugins/execplugin/execplugin_test.go @@ -45,7 +45,7 @@ s/$BAR/bar baz/g "argsFromFile": "sed-input.txt", }) - pluginConfig.RemoveIdAnnotations() + pluginConfig.RemoveBuildAnnotations() p := NewExecPlugin( pLdr.AbsolutePluginPath( konfig.DisabledPluginConfig(), diff --git a/api/internal/target/kusttarget_test.go b/api/internal/target/kusttarget_test.go index a0a279e38..7a0240094 100644 --- a/api/internal/target/kusttarget_test.go +++ b/api/internal/target/kusttarget_test.go @@ -244,7 +244,7 @@ metadata: t.Fatalf("unexpected error %v", err) } } - expected.RemoveIdAnnotations() + expected.RemoveBuildAnnotations() expYaml, err := expected.AsYaml() assert.NoError(t, err) @@ -252,7 +252,7 @@ metadata: assert.NoError(t, kt.Load()) actual, err := kt.MakeCustomizedResMap() assert.NoError(t, err) - actual.RemoveIdAnnotations() + actual.RemoveBuildAnnotations() actYaml, err := actual.AsYaml() assert.NoError(t, err) assert.Equal(t, expYaml, actYaml) diff --git a/api/internal/wrappy/factory.go b/api/internal/wrappy/factory.go index 89008fd95..b155ed3c6 100644 --- a/api/internal/wrappy/factory.go +++ b/api/internal/wrappy/factory.go @@ -58,7 +58,7 @@ func (k *WNodeFactory) SliceFromBytes(bs []byte) ([]ifc.Kunstructured, error) { // shouldDropObject returns true if the resource should not be accumulated. func shouldDropObject(m yaml.ResourceMeta) bool { - _, y := m.ObjectMeta.Annotations[konfig.IgnoredByKustomizeResourceAnnotation] + _, y := m.ObjectMeta.Annotations[konfig.IgnoredByKustomizeAnnotation] return y } diff --git a/api/k8sdeps/kunstruct/factory.go b/api/k8sdeps/kunstruct/factory.go index ec1333565..b592e9394 100644 --- a/api/k8sdeps/kunstruct/factory.go +++ b/api/k8sdeps/kunstruct/factory.go @@ -125,7 +125,7 @@ func (kf *KunstructuredFactoryImpl) skipResource(u unstructured.Unstructured) bo return false } // check if the Resource has opt-ed out of kustomize - _, found := an[konfig.IgnoredByKustomizeResourceAnnotation] + _, found := an[konfig.IgnoredByKustomizeAnnotation] return found } diff --git a/api/konfig/general.go b/api/konfig/general.go index 5d75c2aca..7c095cd35 100644 --- a/api/konfig/general.go +++ b/api/konfig/general.go @@ -56,8 +56,11 @@ const ( // A program name, for use in help, finding the XDG_CONFIG_DIR, etc. ProgramName = "kustomize" + // ConfigAnnoDomain is configuration-related annotation namespace. + ConfigAnnoDomain = "config.kubernetes.io" + // If a resource has this annotation, kustomize will drop it. - IgnoredByKustomizeResourceAnnotation = "config.kubernetes.io/local-config" + IgnoredByKustomizeAnnotation = ConfigAnnoDomain + "/local-config" // Label key that indicates the resources are built from Kustomize ManagedbyLabelKey = "app.kubernetes.io/managed-by" diff --git a/api/krusty/kustomizer.go b/api/krusty/kustomizer.go index 6e78628b8..3995b7f01 100644 --- a/api/krusty/kustomizer.go +++ b/api/krusty/kustomizer.go @@ -93,6 +93,6 @@ func (b *Kustomizer) Run(path string) (resmap.ResMap, error) { } t.Transform(m) } - m.RemoveIdAnnotations() + m.RemoveBuildAnnotations() return m, nil } diff --git a/api/resmap/resmap.go b/api/resmap/resmap.go index ad97212b7..4b977907b 100644 --- a/api/resmap/resmap.go +++ b/api/resmap/resmap.go @@ -246,6 +246,6 @@ type ResMap interface { ApplySmPatch( selectedSet *resource.IdSet, patch *resource.Resource) error - // Remove annotations used exclusively by the kustomize build process. - RemoveIdAnnotations() + // RemoveBuildAnnotations removes annotations created by the build process. + RemoveBuildAnnotations() } diff --git a/api/resmap/reswrangler.go b/api/resmap/reswrangler.go index d0ad72c9c..48e2185bb 100644 --- a/api/resmap/reswrangler.go +++ b/api/resmap/reswrangler.go @@ -621,8 +621,8 @@ func (m *resWrangler) ApplySmPatch( return nil } -func (m *resWrangler) RemoveIdAnnotations() { +func (m *resWrangler) RemoveBuildAnnotations() { for _, r := range m.Resources() { - r.RemoveIdAnnotations() + r.RemoveBuildAnnotations() } } diff --git a/api/resmap/reswrangler_test.go b/api/resmap/reswrangler_test.go index 317d09505..57f618cbb 100644 --- a/api/resmap/reswrangler_test.go +++ b/api/resmap/reswrangler_test.go @@ -998,7 +998,7 @@ spec: return } assert.False(t, tc.errorExpected) - m.RemoveIdAnnotations() + m.RemoveBuildAnnotations() yml, err := m.AsYaml() assert.NoError(t, err) assert.Equal(t, strings.Join(tc.expected, "---\n"), string(yml)) @@ -1111,7 +1111,7 @@ $patch: delete assert.NoError(t, err, name) assert.NoError(t, m.ApplySmPatch(idSet, p), name) assert.Equal(t, tc.finalMapSize, m.Size(), name) - m.RemoveIdAnnotations() + m.RemoveBuildAnnotations() yml, err := m.AsYaml() assert.NoError(t, err, name) assert.Equal(t, tc.expected, string(yml), name) diff --git a/api/resource/resource.go b/api/resource/resource.go index 4a20246d6..a2703300b 100644 --- a/api/resource/resource.go +++ b/api/resource/resource.go @@ -11,6 +11,7 @@ import ( "sigs.k8s.io/kustomize/api/filters/patchstrategicmerge" "sigs.k8s.io/kustomize/api/ifc" "sigs.k8s.io/kustomize/api/internal/wrappy" + "sigs.k8s.io/kustomize/api/konfig" "sigs.k8s.io/kustomize/api/resid" "sigs.k8s.io/kustomize/api/types" "sigs.k8s.io/kustomize/kyaml/filtersutil" @@ -30,10 +31,10 @@ type Resource struct { } const ( - nameAnnotation = "config.kubernetes.io/originalName" - prefixAnnotation = "config.kubernetes.io/prefixes" - suffixAnnotation = "config.kubernetes.io/suffixes" - namespaceAnnotation = "config.kubernetes.io/originalNs" + buildAnnotationOriginalName = konfig.ConfigAnnoDomain + "/originalName" + buildAnnotationPrefixes = konfig.ConfigAnnoDomain + "/prefixes" + buildAnnotationSuffixes = konfig.ConfigAnnoDomain + "/suffixes" + buildAnnotationOriginalNamespace = konfig.ConfigAnnoDomain + "/originalNs" ) func (r *Resource) ResetPrimaryData(incoming *Resource) { @@ -244,12 +245,12 @@ func copyStringSlice(s []string) []string { // Implements ResCtx AddNamePrefix func (r *Resource) AddNamePrefix(p string) { - r.addAdditiveAnnotation(prefixAnnotation, p) + r.addAdditiveAnnotation(buildAnnotationPrefixes, p) } // Implements ResCtx AddNameSuffix func (r *Resource) AddNameSuffix(s string) { - r.addAdditiveAnnotation(suffixAnnotation, s) + r.addAdditiveAnnotation(buildAnnotationSuffixes, s) } func (r *Resource) addAdditiveAnnotation(name, value string) { @@ -309,19 +310,19 @@ func sameEndingSubarray(a, b []string) bool { // Implements ResCtx GetNamePrefixes func (r *Resource) GetNamePrefixes() []string { annotations := r.GetAnnotations() - if _, ok := annotations[prefixAnnotation]; !ok { + if _, ok := annotations[buildAnnotationPrefixes]; !ok { return nil } - return strings.Split(annotations[prefixAnnotation], ",") + return strings.Split(annotations[buildAnnotationPrefixes], ",") } // Implements ResCtx GetNameSuffixes func (r *Resource) GetNameSuffixes() []string { annotations := r.GetAnnotations() - if _, ok := annotations[suffixAnnotation]; !ok { + if _, ok := annotations[buildAnnotationSuffixes]; !ok { return nil } - return strings.Split(annotations[suffixAnnotation], ",") + return strings.Split(annotations[buildAnnotationSuffixes], ",") } // OutermostPrefixSuffixEquals returns true if both resources @@ -345,21 +346,24 @@ func (r *Resource) PrefixesSuffixesEquals(o ResCtx) bool { return sameEndingSubarray(r.GetNamePrefixes(), o.GetNamePrefixes()) && sameEndingSubarray(r.GetNameSuffixes(), o.GetNameSuffixes()) } -func (r *Resource) RemoveIdAnnotations() { +// RemoveBuildAnnotations removes annotations created by the build process. +// These are internal-only to kustomize, added to the data pipeline to +// track name changes so name references can be fixed. +func (r *Resource) RemoveBuildAnnotations() { annotations := r.GetAnnotations() if len(annotations) == 0 { return } - delete(annotations, nameAnnotation) - delete(annotations, prefixAnnotation) - delete(annotations, suffixAnnotation) - delete(annotations, namespaceAnnotation) + delete(annotations, buildAnnotationOriginalName) + delete(annotations, buildAnnotationPrefixes) + delete(annotations, buildAnnotationSuffixes) + delete(annotations, buildAnnotationOriginalNamespace) r.SetAnnotations(annotations) } func (r *Resource) GetOriginalName() string { annotations := r.GetAnnotations() - if name, ok := annotations[nameAnnotation]; ok { + if name, ok := annotations[buildAnnotationOriginalName]; ok { return name } return r.kunStr.GetName() @@ -370,8 +374,8 @@ func (r *Resource) SetOriginalName(n string, overwrite bool) *Resource { if annotations == nil { annotations = make(map[string]string) } - if _, ok := annotations[nameAnnotation]; !ok || overwrite { - annotations[nameAnnotation] = n + if _, ok := annotations[buildAnnotationOriginalName]; !ok || overwrite { + annotations[buildAnnotationOriginalName] = n } r.kunStr.SetAnnotations(annotations) return r @@ -379,7 +383,7 @@ func (r *Resource) SetOriginalName(n string, overwrite bool) *Resource { func (r *Resource) GetOriginalNs() string { annotations := r.GetAnnotations() - if ns, ok := annotations[namespaceAnnotation]; ok { + if ns, ok := annotations[buildAnnotationOriginalNamespace]; ok { return ns } ns := r.GetNamespace() @@ -397,8 +401,8 @@ func (r *Resource) SetOriginalNs(n string, overwrite bool) *Resource { if annotations == nil { annotations = make(map[string]string) } - if _, ok := annotations[namespaceAnnotation]; !ok || overwrite { - annotations[namespaceAnnotation] = n + if _, ok := annotations[buildAnnotationOriginalNamespace]; !ok || overwrite { + annotations[buildAnnotationOriginalNamespace] = n } r.SetAnnotations(annotations) return r diff --git a/api/testutils/kusttest/harness.go b/api/testutils/kusttest/harness.go index f39b035dd..8ff164e0b 100644 --- a/api/testutils/kusttest/harness.go +++ b/api/testutils/kusttest/harness.go @@ -127,7 +127,7 @@ func (th Harness) AssertActualEqualsExpected( } func (th Harness) AssertActualEqualsExpectedNoIdAnnotations(m resmap.ResMap, expected string) { - m.RemoveIdAnnotations() + m.RemoveBuildAnnotations() th.AssertActualEqualsExpectedWithTweak(m, nil, expected) } diff --git a/api/testutils/kusttest/harnessenhanced.go b/api/testutils/kusttest/harnessenhanced.go index 8216e5a83..03bdb2574 100644 --- a/api/testutils/kusttest/harnessenhanced.go +++ b/api/testutils/kusttest/harnessenhanced.go @@ -109,7 +109,7 @@ func (th *HarnessEnhanced) LoadAndRunGenerator( if err != nil { th.t.Fatalf("Err: %v", err) } - rm.RemoveIdAnnotations() + rm.RemoveBuildAnnotations() return rm }