Skip to content

Commit

Permalink
base64 encode last-applied annotation (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
bonifaido authored May 13, 2020
1 parent 233ed4f commit 137a7f3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
7 changes: 7 additions & 0 deletions integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,13 @@ func TestIntegration(t *testing.T) {
pod := i.(*v1.Pod)
pod.Spec.Affinity = nil
}),
NewTestMatch("secret matches with original",
&v1.Secret{
ObjectMeta: standardObjectMeta(),
Data: map[string][]byte{
"key": []byte("secretValue"),
},
}),
NewTestMatch("serviceaccount matches with original",
&v1.ServiceAccount{
ObjectMeta: standardObjectMeta(),
Expand Down
11 changes: 11 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,17 @@ func testMatchOnObject(testItem *TestItem) error {
log.Printf("Failed to remove object %s", existing.GetName())
}
}()
case *v1.Secret:
existing, err = testContext.Client.CoreV1().Secrets(newObject.GetNamespace()).Create(newObject.(*v1.Secret))
if err != nil {
return emperror.WrapWith(err, "failed to create object", "object", newObject)
}
defer func() {
err = testContext.Client.CoreV1().Secrets(newObject.GetNamespace()).Delete(existing.GetName(), deleteOptions)
if err != nil {
log.Printf("Failed to remove object %s", existing.GetName())
}
}()
case *v1beta1.CustomResourceDefinition:
existing, err = testContext.ExtensionsClient.ApiextensionsV1beta1().CustomResourceDefinitions().Create(newObject.(*v1beta1.CustomResourceDefinition))
if err != nil {
Expand Down
12 changes: 10 additions & 2 deletions patch/annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package patch

import (
"encoding/base64"

json "github.com/json-iterator/go"

"k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -54,6 +56,11 @@ func (a *Annotator) GetOriginalConfiguration(obj runtime.Object) ([]byte, error)
return nil, nil
}

// Try to base64 decode, and fallback to non-base64 encoded content for backwards compatibility.
if decoded, err := base64.StdEncoding.DecodeString(original); err == nil {
return decoded, nil
}

return []byte(original), nil
}

Expand All @@ -73,7 +80,7 @@ func (a *Annotator) SetOriginalConfiguration(obj runtime.Object, original []byte
annots = map[string]string{}
}

annots[a.key] = string(original)
annots[a.key] = base64.StdEncoding.EncodeToString(original)
return a.metadataAccessor.SetAnnotations(obj, annots)
}

Expand Down Expand Up @@ -107,13 +114,14 @@ func (a *Annotator) GetModifiedConfiguration(obj runtime.Object, annotate bool)
if len(annots) == 0 {
a.metadataAccessor.SetAnnotations(obj, nil)
}

modified, err = json.Marshal(obj)
if err != nil {
return nil, err
}

if annotate {
annots[a.key] = string(modified)
annots[a.key] = base64.StdEncoding.EncodeToString(modified)
if err := a.metadataAccessor.SetAnnotations(obj, annots); err != nil {
return nil, err
}
Expand Down

0 comments on commit 137a7f3

Please sign in to comment.