From 73962072ac316983929f63d16d6fc3e2b19342d9 Mon Sep 17 00:00:00 2001 From: yutaroyamanaka Date: Sat, 8 Apr 2023 21:39:58 +0900 Subject: [PATCH 1/7] not delete kustomize.config.k8s.io/needs-hash --- api/krusty/configmaps_test.go | 2 ++ api/resource/resource.go | 3 +++ 2 files changed, 5 insertions(+) diff --git a/api/krusty/configmaps_test.go b/api/krusty/configmaps_test.go index b161d88b35..8c375e2237 100644 --- a/api/krusty/configmaps_test.go +++ b/api/krusty/configmaps_test.go @@ -291,6 +291,8 @@ resources: configMapGenerator: - name: project behavior: merge + options: + disableNameSuffixHash: true literals: - ANOTHER_ENV_VARIABLE="bar" `) diff --git a/api/resource/resource.go b/api/resource/resource.go index ae1a98be0e..b14395498e 100644 --- a/api/resource/resource.go +++ b/api/resource/resource.go @@ -523,6 +523,9 @@ func mergeStringMapsWithBuildAnnotations(maps ...map[string]string) map[string]s result := mergeStringMaps(maps...) for i := range BuildAnnotations { if len(maps) > 0 { + if BuildAnnotations[i] == utils.BuildAnnotationsGenAddHashSuffix { + continue + } if v, ok := maps[0][BuildAnnotations[i]]; ok { result[BuildAnnotations[i]] = v continue From 9bea36e562f9842bf86d270d5d667deebc61da19 Mon Sep 17 00:00:00 2001 From: yutaroyamanaka Date: Sat, 8 Apr 2023 21:47:57 +0900 Subject: [PATCH 2/7] add a unit test for BuildAnnotationsGenAddHashSuffix --- api/resmap/reswrangler_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/api/resmap/reswrangler_test.go b/api/resmap/reswrangler_test.go index d576560868..bcf51a7980 100644 --- a/api/resmap/reswrangler_test.go +++ b/api/resmap/reswrangler_test.go @@ -12,6 +12,7 @@ import ( "github.com/stretchr/testify/assert" "sigs.k8s.io/kustomize/api/filters/labels" + "sigs.k8s.io/kustomize/api/internal/utils" "sigs.k8s.io/kustomize/api/provider" . "sigs.k8s.io/kustomize/api/resmap" "sigs.k8s.io/kustomize/api/resource" @@ -895,6 +896,16 @@ func TestAbsorbAll(t *testing.T) { assert.Error(t, err) assert.True( t, strings.Contains(err.Error(), "behavior must be merge or replace")) + + // Assure BuildAnnotationsGenAddHashSuffix is not deleted after AbsorbAll + w = makeMap1() + w.RemoveBuildAnnotations() + w2 = makeMap2(types.BehaviorReplace) + assert.NoError(t, w.AbsorbAll(w2)) + expected = makeMap2(types.BehaviorUnspecified) + expected.RemoveBuildAnnotations() + expected.AnnotateAll(utils.BuildAnnotationsGenAddHashSuffix, "enabled") + assert.NoError(t, w.ErrorIfNotEqualLists(expected)) } func TestToRNodeSlice(t *testing.T) { From 84ca53ea12ccd69b7eabc8d5729c43688effdb23 Mon Sep 17 00:00:00 2001 From: yutaroyamanaka Date: Sat, 8 Apr 2023 22:18:02 +0900 Subject: [PATCH 3/7] add test cases for generators and kustomize.config.k8s.io annotations --- api/krusty/generatorplugin_test.go | 132 +++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 api/krusty/generatorplugin_test.go diff --git a/api/krusty/generatorplugin_test.go b/api/krusty/generatorplugin_test.go new file mode 100644 index 0000000000..68d5b3ec61 --- /dev/null +++ b/api/krusty/generatorplugin_test.go @@ -0,0 +1,132 @@ +package krusty_test + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest" + "sigs.k8s.io/kustomize/kyaml/filesys" +) + +func TestGeneratorHashSuffixWithMergeBehavior(t *testing.T) { + fSys := filesys.MakeFsOnDisk() + generatorFilename := "generateWithHashRequest.sh" + + th := kusttest_test.MakeHarnessWithFs(t, fSys) + o := th.MakeOptionsPluginsEnabled() + o.PluginConfig.FnpLoadingOptions.EnableExec = true + + tmpDir, err := filesys.NewTmpConfirmedDir() + assert.NoError(t, err) + th.WriteK(tmpDir.String(), ` +resources: +- configmap.yaml +generators: +- |- + kind: Executable + metadata: + name: demo + annotations: + config.kubernetes.io/function: | + exec: + path: ./`+generatorFilename+` +`) + + th.WriteF(filepath.Join(tmpDir.String(), "configmap.yaml"), ` +apiVersion: v1 +kind: ConfigMap +metadata: + name: cmap +data: + a: b +`) + th.WriteF(filepath.Join(tmpDir.String(), generatorFilename), `#!/bin/sh + +cat < Date: Wed, 5 Jul 2023 22:08:12 +0900 Subject: [PATCH 4/7] add a comment --- api/resource/resource.go | 1 + 1 file changed, 1 insertion(+) diff --git a/api/resource/resource.go b/api/resource/resource.go index b14395498e..53c869da2b 100644 --- a/api/resource/resource.go +++ b/api/resource/resource.go @@ -523,6 +523,7 @@ func mergeStringMapsWithBuildAnnotations(maps ...map[string]string) map[string]s result := mergeStringMaps(maps...) for i := range BuildAnnotations { if len(maps) > 0 { + // not delete BuildAnnotationsGenAddHashSuffix to work with kustomize.config.k8s.io/needs-hash annotation in generator options if BuildAnnotations[i] == utils.BuildAnnotationsGenAddHashSuffix { continue } From 0b23711422fed7e13db8b5de6d004771c50ac8ee Mon Sep 17 00:00:00 2001 From: yutaroyamanaka Date: Mon, 16 Oct 2023 21:04:49 +0900 Subject: [PATCH 5/7] use assert.NoError to handle linter error --- api/resmap/reswrangler_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/resmap/reswrangler_test.go b/api/resmap/reswrangler_test.go index bcf51a7980..0ea6b327a3 100644 --- a/api/resmap/reswrangler_test.go +++ b/api/resmap/reswrangler_test.go @@ -904,7 +904,7 @@ func TestAbsorbAll(t *testing.T) { assert.NoError(t, w.AbsorbAll(w2)) expected = makeMap2(types.BehaviorUnspecified) expected.RemoveBuildAnnotations() - expected.AnnotateAll(utils.BuildAnnotationsGenAddHashSuffix, "enabled") + assert.NoError(t, expected.AnnotateAll(utils.BuildAnnotationsGenAddHashSuffix, "enabled")) assert.NoError(t, w.ErrorIfNotEqualLists(expected)) } From f0f0cf354c40b20cbc589977899085a2f8db7f9d Mon Sep 17 00:00:00 2001 From: yutaroyamanaka Date: Fri, 20 Oct 2023 07:48:14 +0900 Subject: [PATCH 6/7] lint error fix --- api/resource/resource.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/resource/resource.go b/api/resource/resource.go index 53c869da2b..874c632ed2 100644 --- a/api/resource/resource.go +++ b/api/resource/resource.go @@ -523,7 +523,7 @@ func mergeStringMapsWithBuildAnnotations(maps ...map[string]string) map[string]s result := mergeStringMaps(maps...) for i := range BuildAnnotations { if len(maps) > 0 { - // not delete BuildAnnotationsGenAddHashSuffix to work with kustomize.config.k8s.io/needs-hash annotation in generator options + // not delete BuildAnnotationsGenAddHashSuffix to work with kustomize.config.k8s.io/needs-hash annotation in generator options if BuildAnnotations[i] == utils.BuildAnnotationsGenAddHashSuffix { continue } From 89647160f7b52ff74203e812b11b60a374515e04 Mon Sep 17 00:00:00 2001 From: yutaroyamanaka Date: Sat, 4 Nov 2023 14:16:15 +0800 Subject: [PATCH 7/7] add license --- api/krusty/generatorplugin_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/api/krusty/generatorplugin_test.go b/api/krusty/generatorplugin_test.go index 68d5b3ec61..d04c8dd2ba 100644 --- a/api/krusty/generatorplugin_test.go +++ b/api/krusty/generatorplugin_test.go @@ -1,3 +1,6 @@ +// Copyright 2019 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 + package krusty_test import (