Skip to content

Commit

Permalink
fix build annotations getting lost after applying JSON 6902 patch (#4111
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Serializator committed Nov 7, 2021
1 parent 2c4b195 commit bb7f7ec
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
18 changes: 18 additions & 0 deletions api/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,24 @@ func (r *Resource) PrefixesSuffixesEquals(o ResCtx) bool {
utils.SameEndingSubSlice(r.GetNameSuffixes(), o.GetNameSuffixes())
}

// GetBuildAnnotations returns annotations from the resource
// which are build annotations
func (r Resource) GetBuildAnnotations() map[string]string {
annotations := r.GetAnnotations()
buildAnnotations := make(map[string]string)
if len(annotations) == 0 {
return buildAnnotations
}

for _, buildAnnotation := range BuildAnnotations {
if value, ok := annotations[buildAnnotation]; ok {
buildAnnotations[buildAnnotation] = value
}
}

return buildAnnotations
}

// 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.
Expand Down
18 changes: 18 additions & 0 deletions api/resource/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1427,3 +1427,21 @@ spec:
resid.FromString("gr2_ver2_knd2|ns2|name2"),
})
}

func TestGetBuildAnnotations(t *testing.T) {
r, err := factory.FromBytes([]byte(`
apiVersion: v1
kind: Pod
metadata:
name: clown
annotations:
app.kubernetes.io/name: clown
internal.config.kubernetes.io/prefixes: ",abc-"
internal.config.kubernetes.io/suffixes: ",-abc"
`))
assert.NoError(t, err)
assert.Equal(t, r.GetBuildAnnotations(), map[string]string{
"internal.config.kubernetes.io/prefixes": ",abc-",
"internal.config.kubernetes.io/suffixes": ",-abc",
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,23 @@ func (p *plugin) Transform(m resmap.ResMap) error {
return err
}
for _, res := range resources {
buildAnnotations := res.GetBuildAnnotations()

err = res.ApplyFilter(patchjson6902.Filter{
Patch: p.JsonOp,
})
if err != nil {
return err
}

annotations := res.GetAnnotations()
for key, value := range buildAnnotations {
annotations[key] = value
}
err = res.SetAnnotations(annotations)
if err != nil {
return err
}
}
return nil
}
7 changes: 7 additions & 0 deletions plugin/builtin/patchtransformer/PatchTransformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,19 @@ func (p *plugin) transformJson6902(m resmap.ResMap, patch jsonpatch.Patch) error
}
for _, res := range resources {
res.StorePreviousId()
buildAnnotations := res.GetBuildAnnotations()
err = res.ApplyFilter(patchjson6902.Filter{
Patch: p.Patch,
})
if err != nil {
return err
}

annotations := res.GetAnnotations()
for key, value := range buildAnnotations {
annotations[key] = value
}
err = res.SetAnnotations(annotations)
}
return nil
}
Expand Down

0 comments on commit bb7f7ec

Please sign in to comment.