Skip to content

Commit

Permalink
replace Resource.options with annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
natasha41575 committed Jul 20, 2021
1 parent 259fcfc commit a7f1ad0
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 16 deletions.
2 changes: 1 addition & 1 deletion api/internal/target/kusttarget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,5 +255,5 @@ metadata:
actual.RemoveBuildAnnotations()
actYaml, err := actual.AsYaml()
assert.NoError(t, err)
assert.Equal(t, expYaml, actYaml)
assert.Equal(t, string(expYaml), string(actYaml))
}
3 changes: 2 additions & 1 deletion api/internal/utils/makeResIds.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import (
const (
BuildAnnotationPreviousKinds = konfig.ConfigAnnoDomain + "/previousKinds"
BuildAnnotationPreviousNames = konfig.ConfigAnnoDomain + "/previousNames"
BuildAnnotationPreviousNamespaces = konfig.ConfigAnnoDomain + "/previousNamespaces"
BuildAnnotationPrefixes = konfig.ConfigAnnoDomain + "/prefixes"
BuildAnnotationSuffixes = konfig.ConfigAnnoDomain + "/suffixes"
BuildAnnotationPreviousNamespaces = konfig.ConfigAnnoDomain + "/previousNamespaces"
BuildAnnotationsRefBy = konfig.ConfigAnnoDomain + "/refBy"
BuildAnnotationsGenOptions = konfig.InternalConfigAnnoDomain + "/generatorOptions"

// the following are only for patches, to specify whether they can change names
// and kinds of their targets
Expand Down
3 changes: 3 additions & 0 deletions api/konfig/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ const (
// ConfigAnnoDomain is configuration-related annotation namespace.
ConfigAnnoDomain = "config.kubernetes.io"

// InternalConfigAnnoDomain is internal configuration-related annotation namespace.
InternalConfigAnnoDomain = "internal.config.kubernetes.io"

// If a resource has this annotation, kustomize will drop it.
IgnoredByKustomizeAnnotation = ConfigAnnoDomain + "/local-config"

Expand Down
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 @@ configMapGenerator:
behavior: merge
literals:
- ANOTHER_ENV_VARIABLE="bar"
options:
disableNameSuffixHash: true
`)
th.WriteF("cm.yaml", `
apiVersion: v1
Expand Down
3 changes: 3 additions & 0 deletions api/resmap/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,10 @@ BAR=baz
}
r, err := rmF.NewResMapFromConfigMapArgs(kvLdr, tc.input)
assert.NoError(t, err, tc.description)
r.RemoveBuildAnnotations()
rYaml, err := r.AsYaml()
assert.NoError(t, err, tc.description)
tc.expected.RemoveBuildAnnotations()
expYaml, err := tc.expected.AsYaml()
assert.NoError(t, err, tc.description)
assert.Equal(t, expYaml, rYaml)
Expand Down Expand Up @@ -252,6 +254,7 @@ func TestNewResMapFromSecretArgs(t *testing.T) {
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
actual.RemoveBuildAnnotations()
actYaml, err := actual.AsYaml()
assert.NoError(t, err)

Expand Down
3 changes: 3 additions & 0 deletions api/resmap/reswrangler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,8 @@ func TestAbsorbAll(t *testing.T) {
}))
w := makeMap1()
assert.NoError(t, w.AbsorbAll(makeMap2(types.BehaviorMerge)))
expected.RemoveBuildAnnotations()
w.RemoveBuildAnnotations()
assert.NoError(t, expected.ErrorIfNotEqualLists(w))
w = makeMap1()
assert.NoError(t, w.AbsorbAll(nil))
Expand All @@ -853,6 +855,7 @@ func TestAbsorbAll(t *testing.T) {
w = makeMap1()
w2 := makeMap2(types.BehaviorReplace)
assert.NoError(t, w.AbsorbAll(w2))
w2.RemoveBuildAnnotations()
assert.NoError(t, w2.ErrorIfNotEqualLists(w))
w = makeMap1()
w2 = makeMap2(types.BehaviorUnspecified)
Expand Down
4 changes: 3 additions & 1 deletion api/resource/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ func (rf *Factory) makeOne(rn *yaml.RNode, o *types.GenArgs) *Resource {
if o == nil {
o = types.NewGenArgs(nil)
}
return &Resource{RNode: *rn, options: o}
resource := &Resource{RNode: *rn}
resource.SetOptions(o)
return resource
}

// SliceFromPatches returns a slice of resources given a patch path
Expand Down
28 changes: 22 additions & 6 deletions api/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
// paired with metadata used by kustomize.
type Resource struct {
kyaml.RNode
options *types.GenArgs
refVarNames []string
}

Expand All @@ -35,6 +34,7 @@ var BuildAnnotations = []string{
utils.BuildAnnotationAllowNameChange,
utils.BuildAnnotationAllowKindChange,
utils.BuildAnnotationsRefBy,
utils.BuildAnnotationsGenOptions,
}

func (r *Resource) ResetRNode(incoming *Resource) {
Expand Down Expand Up @@ -101,7 +101,6 @@ func (r *Resource) CopyMergeMetaDataFieldsFrom(other *Resource) error {
}

func (r *Resource) copyKustomizeSpecificFields(other *Resource) {
r.options = other.options
r.refVarNames = copyStringSlice(other.refVarNames)
}

Expand Down Expand Up @@ -274,7 +273,7 @@ func (r *Resource) String() string {
if err != nil {
return "<" + err.Error() + ">"
}
return strings.TrimSpace(string(bs)) + r.options.String()
return strings.TrimSpace(string(bs))
}

// AsYAML returns the resource in Yaml form.
Expand All @@ -296,20 +295,37 @@ func (r *Resource) MustYaml() string {
return string(yml)
}

func (r *Resource) getOptions() *types.GenArgs {
annotations := r.GetAnnotations()
if genOptsAnno, ok := annotations[utils.BuildAnnotationsGenOptions]; ok {
var genOpts types.GeneratorArgs
yaml.Unmarshal([]byte(genOptsAnno), &genOpts)
return types.NewGenArgs(&genOpts)
}
return &types.GenArgs{}
}

// SetOptions updates the generator options for the resource.
func (r *Resource) SetOptions(o *types.GenArgs) {
r.options = o
if o.IsNilOrEmpty() {
return
}
annotations := r.GetAnnotations()
b, _ := yaml.Marshal(o.GetArgs())
annotations[utils.BuildAnnotationsGenOptions] = string(b)
r.SetAnnotations(annotations)
}

// Behavior returns the behavior for the resource.
func (r *Resource) Behavior() types.GenerationBehavior {
return r.options.Behavior()
return r.getOptions().Behavior()
}

// NeedHashSuffix returns true if a resource content
// hash should be appended to the name of the resource.
func (r *Resource) NeedHashSuffix() bool {
return r.options != nil && r.options.ShouldAddHashSuffixToName()
options := r.getOptions()
return options != nil && options.ShouldAddHashSuffixToName()
}

// OrgId returns the original, immutable ResId for the resource.
Expand Down
67 changes: 63 additions & 4 deletions api/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ var testConfigMap = factory.FromMap(
},
})

const genArgOptions = "{nsfx:false,beh:unspecified}"

//nolint:gosec
const configMapAsString = `{"apiVersion":"v1","kind":"ConfigMap","metadata":{"name":"winnie","namespace":"hundred-acre-wood"}}`
const configMapAsStringWithOptions = "{\"apiVersion\":\"v1\",\"kind\":\"ConfigMap\",\"metadata\":{\"annotations\":" +
"{\"internal.config.kubernetes.io/generatorOptions\":\"{}\\n\"},\"name\":\"winnie\",\"namespace\":\"hundred-acre-wood\"}}"

var testDeployment = factory.FromMap(
map[string]interface{}{
Expand All @@ -43,6 +43,9 @@ var testDeployment = factory.FromMap(
})

const deploymentAsString = `{"apiVersion":"apps/v1","kind":"Deployment","metadata":{"name":"pooh"}}`
const deploymentAsStringWithOptions = "{\"apiVersion\":\"apps/v1\",\"kind\":\"Deployment" +
"\",\"metadata\":{\"annotations\":{\"internal.config.kubernetes.io/generatorOptions\":\"{}\\n\"}" +
",\"name\":\"pooh\"}}"

func TestAsYAML(t *testing.T) {
expected := `apiVersion: apps/v1
Expand All @@ -66,14 +69,38 @@ func TestResourceString(t *testing.T) {
}{
{
in: testConfigMap,
s: configMapAsString + genArgOptions,
s: configMapAsString,
},
{
in: testDeployment,
s: deploymentAsString,
},
}
for _, test := range tests {
if test.in.String() != test.s {
t.Fatalf("Expected %s == %s", test.in.String(), test.s)
}
}
}

func TestResourceStringWithOptionsAnnotations(t *testing.T) {
tests := []struct {
in *Resource
s string
}{
{
in: testConfigMap,
s: configMapAsStringWithOptions,
},
{
in: testDeployment,
s: deploymentAsString + genArgOptions,
s: deploymentAsStringWithOptions,
},
}
for _, test := range tests {
args := &types.GeneratorArgs{}
options := types.NewGenArgs(args)
test.in.SetOptions(options)
if test.in.String() != test.s {
t.Fatalf("Expected %s == %s", test.in.String(), test.s)
}
Expand Down Expand Up @@ -1171,3 +1198,35 @@ spec:
resid.FromString("gr2_ver2_knd2|ns2|name2"),
})
}

func TestOptions(t *testing.T) {
r, err := factory.FromBytes([]byte(`
apiVersion: v1
kind: ConfigMap
metadata:
name: example-configmap-test
`))
assert.NoError(t, err)

args := &types.GeneratorArgs{
Behavior: "merge",
Options: &types.GeneratorOptions{
DisableNameSuffixHash: true,
},
}

options := types.NewGenArgs(args)
r.SetOptions(options)
assert.Equal(t, r.RNode.MustString(), `apiVersion: v1
kind: ConfigMap
metadata:
name: example-configmap-test
annotations:
internal.config.kubernetes.io/generatorOptions: |
behavior: merge
options:
disableNameSuffixHash: true
`)
assert.Equal(t, r.Behavior(), types.BehaviorMerge)
assert.Equal(t, r.NeedHashSuffix(), !args.Options.DisableNameSuffixHash)
}
18 changes: 18 additions & 0 deletions api/testutils/kusttest/harnessenhanced.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,24 @@ func (th *HarnessEnhanced) LoadAndRunGenerator(
return rm
}

func (th *HarnessEnhanced) LoadAndRunGeneratorWithBuildAnnotations(
config string) resmap.ResMap {
res, err := th.rf.RF().FromBytes([]byte(config))
if err != nil {
th.t.Fatalf("Err: %v", err)
}
g, err := th.pl.LoadGenerator(
th.ldr, valtest_test.MakeFakeValidator(), res)
if err != nil {
th.t.Fatalf("Err: %v", err)
}
rm, err := g.Generate()
if err != nil {
th.t.Fatalf("generate err: %v", err)
}
return rm
}

func (th *HarnessEnhanced) LoadAndRunTransformer(
config, input string) resmap.ResMap {
resMap, err := th.RunTransformer(config, input)
Expand Down
10 changes: 10 additions & 0 deletions api/types/genargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,13 @@ func (g *GenArgs) Behavior() GenerationBehavior {
}
return NewGenerationBehavior(g.args.Behavior)
}

// IsNilOrEmpty returns true if g is nil or if the args are empty
func (g *GenArgs) IsNilOrEmpty() bool {
return g == nil || g.args == nil
}

// GetArgs returns a copy of the underlying GeneratorArgs
func (g *GenArgs) GetArgs() GeneratorArgs {
return *g.args
}
2 changes: 1 addition & 1 deletion cmd/config/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ require (
sigs.k8s.io/kustomize/kyaml v0.11.0
)

replace sigs.k8s.io/kustomize/kyaml => ../../kyaml
replace sigs.k8s.io/kustomize/kyaml => ../../kyaml
2 changes: 1 addition & 1 deletion kustomize/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ replace sigs.k8s.io/kustomize/api => ../api

replace sigs.k8s.io/kustomize/cmd/config => ../cmd/config

replace sigs.k8s.io/kustomize/kyaml => ../kyaml
replace sigs.k8s.io/kustomize/kyaml => ../kyaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestBashedConfigMapPlugin(t *testing.T) {
PrepExecPlugin("someteam.example.com", "v1", "BashedConfigMap")
defer th.Reset()

m := th.LoadAndRunGenerator(`
m := th.LoadAndRunGeneratorWithBuildAnnotations(`
apiVersion: someteam.example.com/v1
kind: BashedConfigMap
metadata:
Expand All @@ -28,6 +28,9 @@ data:
username: alice
kind: ConfigMap
metadata:
annotations:
internal.config.kubernetes.io/generatorOptions: |
options: {}
name: example-configmap-test
`)
if m.Resources()[0].NeedHashSuffix() != true {
Expand Down

0 comments on commit a7f1ad0

Please sign in to comment.