Skip to content

Commit

Permalink
fix build annotations getting lost after applying JSON 6902 patch
Browse files Browse the repository at this point in the history
  • Loading branch information
Serializator committed Nov 10, 2021
1 parent b16e4ec commit b6cb6c8
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 3 deletions.
12 changes: 12 additions & 0 deletions api/builtins/PatchJson6902Transformer.go

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

8 changes: 8 additions & 0 deletions api/builtins/PatchTransformer.go

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

6 changes: 3 additions & 3 deletions api/krusty/multiplepatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1647,7 +1647,7 @@ spec:
`)
}

// test for #4111, currently demonstrates incorrect behaviour
// test for #4111
func TestPatchPreservesInternalAnnotations(t *testing.T) {
th := kusttest_test.MakeHarness(t)
th.WriteK(".", `
Expand Down Expand Up @@ -1697,7 +1697,7 @@ spec:
containers:
- image: fluentd:latest
name: fluentd
serviceAccountName: fluentd-sa
serviceAccountName: fluentd-sa-abc
---
apiVersion: v1
kind: ServiceAccount
Expand All @@ -1706,4 +1706,4 @@ metadata:
note: this is a test annotation
name: fluentd-sa-abc
`)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"sigs.k8s.io/kustomize/api/ifc"
"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -82,12 +83,23 @@ func (p *plugin) Transform(m resmap.ResMap) error {
return err
}
for _, res := range resources {
internalAnnotations := kioutil.GetInternalAnnotations(&res.RNode)

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

annotations := res.GetAnnotations()
for key, value := range internalAnnotations {
annotations[key] = value
}
err = res.SetAnnotations(annotations)
if err != nil {
return err
}
}
return nil
}
1 change: 1 addition & 0 deletions plugin/builtin/patchjson6902transformer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/evanphx/json-patch v4.11.0+incompatible
github.com/pkg/errors v0.9.1
sigs.k8s.io/kustomize/api v0.8.9
sigs.k8s.io/kustomize/kyaml v0.12.0
sigs.k8s.io/yaml v1.2.0
)

Expand Down
8 changes: 8 additions & 0 deletions plugin/builtin/patchtransformer/PatchTransformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"sigs.k8s.io/kustomize/api/resmap"
"sigs.k8s.io/kustomize/api/resource"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -115,12 +116,19 @@ func (p *plugin) transformJson6902(m resmap.ResMap, patch jsonpatch.Patch) error
}
for _, res := range resources {
res.StorePreviousId()
internalAnnotations := kioutil.GetInternalAnnotations(&res.RNode)
err = res.ApplyFilter(patchjson6902.Filter{
Patch: p.Patch,
})
if err != nil {
return err
}

annotations := res.GetAnnotations()
for key, value := range internalAnnotations {
annotations[key] = value
}
err = res.SetAnnotations(annotations)
}
return nil
}
Expand Down
1 change: 1 addition & 0 deletions plugin/builtin/patchtransformer/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.16
require (
github.com/evanphx/json-patch v4.11.0+incompatible
sigs.k8s.io/kustomize/api v0.8.9
sigs.k8s.io/kustomize/kyaml v0.12.0
sigs.k8s.io/yaml v1.2.0
)

Expand Down

0 comments on commit b6cb6c8

Please sign in to comment.