Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug in Generator options #5117

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/krusty/configmaps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ resources:
configMapGenerator:
- name: project
behavior: merge
options:
disableNameSuffixHash: true
literals:
- ANOTHER_ENV_VARIABLE="bar"
`)
Expand Down
135 changes: 135 additions & 0 deletions api/krusty/generatorplugin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

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 <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
name: cmap
annotations:
kustomize.config.k8s.io/needs-hash: "true"
kustomize.config.k8s.io/behavior: "merge"
data:
c: d
EOF
`)
assert.NoError(t, os.Chmod(filepath.Join(tmpDir.String(), generatorFilename), 0777))
m := th.Run(tmpDir.String(), o)
assert.NoError(t, err)
yml, err := m.AsYaml()
assert.NoError(t, err)
assert.Equal(t, `apiVersion: v1
data:
a: b
c: d
kind: ConfigMap
metadata:
name: cmap-fh478f99mk
`, string(yml))
}

func TestGeneratorHashSuffixWithReplaceBehavior(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 <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
name: cmap
annotations:
kustomize.config.k8s.io/needs-hash: "true"
kustomize.config.k8s.io/behavior: "replace"
data:
c: d
EOF
`)
assert.NoError(t, os.Chmod(filepath.Join(tmpDir.String(), generatorFilename), 0777))
m := th.Run(tmpDir.String(), o)
assert.NoError(t, err)
yml, err := m.AsYaml()
assert.NoError(t, err)
assert.Equal(t, `apiVersion: v1
data:
c: d
kind: ConfigMap
metadata:
name: cmap-gbdtcf54mt
`, string(yml))
}
11 changes: 11 additions & 0 deletions api/resmap/reswrangler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
assert.NoError(t, expected.AnnotateAll(utils.BuildAnnotationsGenAddHashSuffix, "enabled"))
assert.NoError(t, w.ErrorIfNotEqualLists(expected))
}

func TestToRNodeSlice(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions api/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,10 @@ 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 {
koba1t marked this conversation as resolved.
Show resolved Hide resolved
continue
}
if v, ok := maps[0][BuildAnnotations[i]]; ok {
result[BuildAnnotations[i]] = v
continue
Expand Down
Loading