Skip to content

Commit

Permalink
Manage name changes (prefix/suffix) via YAML annotations rather than …
Browse files Browse the repository at this point in the history
…via in-memory-only fields.
  • Loading branch information
natasha41575 committed Jan 11, 2021
1 parent ea5d08b commit bd4580d
Show file tree
Hide file tree
Showing 34 changed files with 538 additions and 60 deletions.
1 change: 1 addition & 0 deletions api/builtins/HashTransformer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/builtins/NamespaceTransformer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/builtins/PatchTransformer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions api/builtins/PrefixSuffixTransformer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions api/internal/accumulator/namereferencetransformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,7 @@ func TestNameReferenceNamespace(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}

m.RemoveIdAnnotations()
if err = expected.ErrorIfNotEqualLists(m); err != nil {
t.Fatalf("actual doesn't match expected: %v", err)
}
Expand Down Expand Up @@ -882,6 +883,7 @@ func TestNameReferenceClusterWide(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}

m.RemoveIdAnnotations()
if err = expected.ErrorIfNotEqualLists(m); err != nil {
t.Fatalf("actual doesn't match expected: %v", err)
}
Expand Down Expand Up @@ -1008,6 +1010,7 @@ func TestNameReferenceNamespaceTransformation(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}

m.RemoveIdAnnotations()
if err = expected.ErrorIfNotEqualLists(m); err != nil {
t.Fatalf("actual doesn't match expected: %v", err)
}
Expand Down Expand Up @@ -1044,6 +1047,7 @@ func TestNameReferenceCandidateSelection(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}

m.RemoveIdAnnotations()
if err = expected.ErrorIfNotEqualLists(m); err != nil {
t.Fatalf("actual doesn't match expected: %v", err)
}
Expand Down
3 changes: 2 additions & 1 deletion api/internal/accumulator/resaccumulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@ func TestResolveVarsWithNoambiguation(t *testing.T) {
// went through a prefix transformer.
r := m.GetByIndex(1)
r.AddNamePrefix("sub-")
r.SetName("sub-backendOne") // original name remains "backendOne"
r.SetName("sub-backendOne")
r.SetOriginalName("backendOne", true)

err = ra2.AppendAll(m)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions api/internal/plugins/execplugin/execplugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ s/$BAR/bar baz/g
"argsFromFile": "sed-input.txt",
})

pluginConfig.RemoveIdAnnotations()
p := NewExecPlugin(
pLdr.AbsolutePluginPath(
konfig.DisabledPluginConfig(),
Expand Down
2 changes: 2 additions & 0 deletions api/internal/target/kusttarget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,15 @@ metadata:
t.Fatalf("unexpected error %v", err)
}
}
assert.NoError(t, expected.RemoveIdAnnotations())
expYaml, err := expected.AsYaml()
assert.NoError(t, err)

kt := makeKustTargetWithRf(t, th.GetFSys(), "/whatever", pvd)
assert.NoError(t, kt.Load())
actual, err := kt.MakeCustomizedResMap()
assert.NoError(t, err)
assert.NoError(t, actual.RemoveIdAnnotations())
actYaml, err := actual.AsYaml()
assert.NoError(t, err)
assert.Equal(t, expYaml, actYaml)
Expand Down
4 changes: 4 additions & 0 deletions api/krusty/kustomizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,9 @@ func (b *Kustomizer) Run(path string) (resmap.ResMap, error) {
}
t.Transform(m)
}
err = m.RemoveIdAnnotations()
if err != nil {
return nil, err
}
return m, nil
}
2 changes: 1 addition & 1 deletion api/krusty/transformerplugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ data:
`)

m := th.Run("/app", th.MakeOptionsPluginsEnabled())
th.AssertActualEqualsExpected(m, `
th.AssertActualEqualsExpectedNoIdAnnotations(m, `
apiVersion: v1
data:
foo: foo
Expand Down
1 change: 0 additions & 1 deletion api/resmap/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,6 @@ spec:

rm, err := rmF.ConflatePatches([]*resource.Resource{r1, r2})
assert.NoError(t, err)

yml, err = rm.AsYaml()
assert.NoError(t, err)

Expand Down
2 changes: 2 additions & 0 deletions api/resmap/resmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,6 @@ type ResMap interface {
// selected set of resources.
ApplySmPatch(
selectedSet *resource.IdSet, patch *resource.Resource) error

RemoveIdAnnotations() error
}
11 changes: 11 additions & 0 deletions api/resmap/reswrangler.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ func (m *resWrangler) ApplySmPatch(
patchCopy.SetName(res.GetName())
patchCopy.SetNamespace(res.GetNamespace())
patchCopy.SetGvk(res.GetGvk())
patchCopy.SetOriginalName(res.GetOriginalName(), true)
err := res.ApplySmPatch(patchCopy)
if err != nil {
// Check for an error string from UnmarshalJSON that's indicative
Expand Down Expand Up @@ -619,3 +620,13 @@ func (m *resWrangler) ApplySmPatch(
m.AppendAll(newRm)
return nil
}

func (m *resWrangler) RemoveIdAnnotations() error {
for _, r := range m.Resources() {
err := r.RemoveIdAnnotations()
if err != nil {
return err
}
}
return nil
}
3 changes: 2 additions & 1 deletion api/resmap/reswrangler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,6 @@ rules:
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

rnodes, err := rm.ToRNodeSlice()
if err != nil {
t.Fatalf("unexpected error: %v", err)
Expand Down Expand Up @@ -999,6 +998,7 @@ spec:
return
}
assert.False(t, tc.errorExpected)
assert.NoError(t, m.RemoveIdAnnotations())
yml, err := m.AsYaml()
assert.NoError(t, err)
assert.Equal(t, strings.Join(tc.expected, "---\n"), string(yml))
Expand Down Expand Up @@ -1111,6 +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)
assert.NoError(t, m.RemoveIdAnnotations())
yml, err := m.AsYaml()
assert.NoError(t, err, name)
assert.Equal(t, tc.expected, string(yml), name)
Expand Down
10 changes: 5 additions & 5 deletions api/resource/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ func (rf *Factory) FromMap(m map[string]interface{}) *Resource {

// FromMapWithName returns a new instance with the given "original" name.
func (rf *Factory) FromMapWithName(n string, m map[string]interface{}) *Resource {
return rf.makeOne(rf.kf.FromMap(m), nil).setOriginalName(n)
return rf.makeOne(rf.kf.FromMap(m), nil).SetOriginalName(n, true)
}

// FromMapWithNamespace returns a new instance with the given "original" namespace.
func (rf *Factory) FromMapWithNamespace(n string, m map[string]interface{}) *Resource {
return rf.makeOne(rf.kf.FromMap(m), nil).setOriginalNs(n)
return rf.makeOne(rf.kf.FromMap(m), nil).SetOriginalNs(n, true)
}

// FromMapWithNamespaceAndName returns a new instance with the given "original" namespace.
func (rf *Factory) FromMapWithNamespaceAndName(ns string, n string, m map[string]interface{}) *Resource {
return rf.makeOne(rf.kf.FromMap(m), nil).setOriginalNs(ns).setOriginalName(n)
return rf.makeOne(rf.kf.FromMap(m), nil).SetOriginalNs(ns, true).SetOriginalName(n, true)
}

// FromMapAndOption returns a new instance of Resource with given options.
Expand All @@ -72,7 +72,7 @@ func (rf *Factory) makeOne(
kunStr: u,
options: o,
}
return r.setOriginalName(r.kunStr.GetName()).setOriginalNs(r.GetNamespace())
return r
}

// SliceFromPatches returns a slice of resources given a patch path
Expand Down Expand Up @@ -157,7 +157,7 @@ func (rf *Factory) SliceFromBytesWithNames(names []string, in []byte) ([]*Resour
return nil, fmt.Errorf("number of names doesn't match number of resources")
}
for i, res := range result {
res.originalName = names[i]
res.SetOriginalName(names[i], true)
}
return result, nil
}
Expand Down
Loading

0 comments on commit bd4580d

Please sign in to comment.