Skip to content

Commit

Permalink
fix: support non-string values
Browse files Browse the repository at this point in the history
  • Loading branch information
mhrabovcin committed Apr 11, 2023
1 parent 23051f0 commit 32e82f8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
5 changes: 4 additions & 1 deletion pkg/rewriter/generated_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rewriter

import (
"encoding/json"
"fmt"
"testing"
"time"
Expand Down Expand Up @@ -41,7 +42,9 @@ func TestGeneratedValues(t *testing.T) {
fieldName := fmt.Sprintf("metadata.%s", k)
annotation := annotationForOriginalValue(fieldName)
assert.Contains(t, pod.GetAnnotations(), annotation, "annotation %q missing", fieldName)
assert.Equal(t, v, pod.GetAnnotations()[annotation], "wrong value stored in %q", fieldName)
var unserialized any
assert.NoError(t, json.Unmarshal([]byte(pod.GetAnnotations()[annotation]), &unserialized))
assert.Equal(t, v, unserialized, "wrong value stored in %q", fieldName)
}

pod = testRewriterBeforeServing(t, r, pod)
Expand Down
17 changes: 14 additions & 3 deletions pkg/rewriter/rewriter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rewriter

import (
"encoding/json"
"fmt"
"strings"

Expand Down Expand Up @@ -41,7 +42,7 @@ func (r *removeField) annotationName() string {
}

func (r *removeField) BeforeImport(u *unstructured.Unstructured) error {
s, ok, err := unstructured.NestedString(u.Object, r.fieldPath...)
value, ok, err := unstructured.NestedFieldCopy(u.Object, r.fieldPath...)
if err != nil {
return err
}
Expand All @@ -51,7 +52,12 @@ func (r *removeField) BeforeImport(u *unstructured.Unstructured) error {
}

unstructured.RemoveNestedField(u.Object, r.fieldPath...)
return addAnnotation(u, r.annotationName(), s)
serialized, err := json.Marshal(value)
if err != nil {
return err
}

return addAnnotation(u, r.annotationName(), string(serialized))
}

func (r *removeField) BeforeServing(u *unstructured.Unstructured) error {
Expand All @@ -63,7 +69,12 @@ func (r *removeField) BeforeServing(u *unstructured.Unstructured) error {
return nil
}

if err := unstructured.SetNestedField(u.Object, value, r.fieldPath...); err != nil {
var unserialized any
if err := json.Unmarshal([]byte(value), &unserialized); err != nil {
return err
}

if err := unstructured.SetNestedField(u.Object, unserialized, r.fieldPath...); err != nil {
return err
}
unstructured.RemoveNestedField(u.Object, "metadata", "annotations", r.annotationName())
Expand Down
14 changes: 14 additions & 0 deletions pkg/rewriter/rewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,17 @@ func TestRemoveField_SkipWithoutAnnotation(t *testing.T) {
pod = testRewriterBeforeServing(t, removeResourceVersion, pod)
assert.Equal(t, "1000", pod.GetResourceVersion())
}

func TestRemoveField_NilValue(t *testing.T) {
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{},
}
assert.Empty(t, pod.GetCreationTimestamp())
removeCreationTimetstamp := RemoveField("metadata", "creationTimestamp")
pod = testRewriterBeforeImport(t, removeCreationTimetstamp, pod)
assert.Contains(t, pod.GetAnnotations(), annotationForOriginalValue("metadata.creationTimestamp"))
assert.Equal(t, pod.GetAnnotations()[annotationForOriginalValue("metadata.creationTimestamp")], "null")
pod = testRewriterBeforeServing(t, removeCreationTimetstamp, pod)
assert.Empty(t, pod.GetCreationTimestamp())
assert.NotContains(t, pod.GetAnnotations(), annotationForOriginalValue("metadata.creationTimestamp"))
}

0 comments on commit 32e82f8

Please sign in to comment.