Skip to content

Commit 137a7f3

Browse files
authored
base64 encode last-applied annotation (#28)
1 parent 233ed4f commit 137a7f3

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

integration_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,13 @@ func TestIntegration(t *testing.T) {
223223
pod := i.(*v1.Pod)
224224
pod.Spec.Affinity = nil
225225
}),
226+
NewTestMatch("secret matches with original",
227+
&v1.Secret{
228+
ObjectMeta: standardObjectMeta(),
229+
Data: map[string][]byte{
230+
"key": []byte("secretValue"),
231+
},
232+
}),
226233
NewTestMatch("serviceaccount matches with original",
227234
&v1.ServiceAccount{
228235
ObjectMeta: standardObjectMeta(),

main_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,17 @@ func testMatchOnObject(testItem *TestItem) error {
280280
log.Printf("Failed to remove object %s", existing.GetName())
281281
}
282282
}()
283+
case *v1.Secret:
284+
existing, err = testContext.Client.CoreV1().Secrets(newObject.GetNamespace()).Create(newObject.(*v1.Secret))
285+
if err != nil {
286+
return emperror.WrapWith(err, "failed to create object", "object", newObject)
287+
}
288+
defer func() {
289+
err = testContext.Client.CoreV1().Secrets(newObject.GetNamespace()).Delete(existing.GetName(), deleteOptions)
290+
if err != nil {
291+
log.Printf("Failed to remove object %s", existing.GetName())
292+
}
293+
}()
283294
case *v1beta1.CustomResourceDefinition:
284295
existing, err = testContext.ExtensionsClient.ApiextensionsV1beta1().CustomResourceDefinitions().Create(newObject.(*v1beta1.CustomResourceDefinition))
285296
if err != nil {

patch/annotation.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package patch
1616

1717
import (
18+
"encoding/base64"
19+
1820
json "github.com/json-iterator/go"
1921

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

59+
// Try to base64 decode, and fallback to non-base64 encoded content for backwards compatibility.
60+
if decoded, err := base64.StdEncoding.DecodeString(original); err == nil {
61+
return decoded, nil
62+
}
63+
5764
return []byte(original), nil
5865
}
5966

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

76-
annots[a.key] = string(original)
83+
annots[a.key] = base64.StdEncoding.EncodeToString(original)
7784
return a.metadataAccessor.SetAnnotations(obj, annots)
7885
}
7986

@@ -107,13 +114,14 @@ func (a *Annotator) GetModifiedConfiguration(obj runtime.Object, annotate bool)
107114
if len(annots) == 0 {
108115
a.metadataAccessor.SetAnnotations(obj, nil)
109116
}
117+
110118
modified, err = json.Marshal(obj)
111119
if err != nil {
112120
return nil, err
113121
}
114122

115123
if annotate {
116-
annots[a.key] = string(modified)
124+
annots[a.key] = base64.StdEncoding.EncodeToString(modified)
117125
if err := a.metadataAccessor.SetAnnotations(obj, annots); err != nil {
118126
return nil, err
119127
}

0 commit comments

Comments
 (0)