Skip to content

Commit

Permalink
fix: correctly rewrite deletionGracePeriodSeconds
Browse files Browse the repository at this point in the history
  • Loading branch information
mhrabovcin committed Mar 28, 2024
1 parent a03e206 commit 325a369
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
1 change: 1 addition & 0 deletions pkg/rewriter/generated.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ func GeneratedValues() ResourceRewriter {
RemoveField("metadata", "generateName"),
RemoveField("metadata", "creationTimestamp"),
RemoveField("metadata", "deletionTimestamp"),
RemoveField("metadata", "deletionGracePeriodSeconds"),
RemoveField("metadata", "uid"),
RemoveField("metadata", "resourceVersion"),
)
Expand Down
29 changes: 17 additions & 12 deletions pkg/rewriter/generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,52 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/ptr"
)

func TestGeneratedValues(t *testing.T) {
r := GeneratedValues()
timestamp := metav1.NewTime(time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC))
pod := &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "generated-",
CreationTimestamp: timestamp,
DeletionTimestamp: &timestamp,
UID: types.UID("1000"),
ResourceVersion: "2000",
GenerateName: "generated-",
CreationTimestamp: timestamp,
DeletionTimestamp: &timestamp,
DeletionGracePeriodSeconds: ptr.To(int64(30)),
UID: types.UID("1000"),
ResourceVersion: "2000",
},
}
pod = testRewriterBeforeImport(t, r, pod)
assert.Empty(t, pod.GetGenerateName())
assert.Empty(t, pod.GetCreationTimestamp())
assert.Empty(t, pod.GetDeletionTimestamp())
assert.Empty(t, pod.GetDeletionGracePeriodSeconds())
assert.Empty(t, pod.GetUID())
assert.Empty(t, pod.GetResourceVersion())

expectedFields := map[string]string{
"generateName": "generated-",
"creationTimestamp": "2023-01-01T00:00:00Z",
"deletionTimestamp": "2023-01-01T00:00:00Z",
"uid": "1000",
"resourceVersion": "2000",
expectedFields := map[string]any{
"generateName": "generated-",
"creationTimestamp": "2023-01-01T00:00:00Z",
"deletionTimestamp": "2023-01-01T00:00:00Z",
"deletionGracePeriodSeconds": 30,
"uid": "1000",
"resourceVersion": "2000",
}
for k, v := range expectedFields {
fieldName := fmt.Sprintf("metadata.%s", k)
annotation := annotationForOriginalValue(fieldName)
assert.Contains(t, pod.GetAnnotations(), annotation, "annotation %q missing", 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)
assert.EqualValues(t, v, unserialized, "wrong value stored in %q metadata %q", fieldName, pod.GetAnnotations()[annotation])
}

pod = testRewriterBeforeServing(t, r, pod)
assert.Equal(t, "generated-", pod.GetGenerateName())
assert.Equal(t, timestamp.Format(time.RFC3339), pod.GetCreationTimestamp().In(time.UTC).Format(time.RFC3339))
assert.Equal(t, timestamp.Format(time.RFC3339), pod.GetDeletionTimestamp().In(time.UTC).Format(time.RFC3339))
assert.Equal(t, int64(30), ptr.Deref(pod.GetDeletionGracePeriodSeconds(), 0))
assert.Equal(t, types.UID("1000"), pod.GetUID())
assert.Equal(t, "2000", pod.GetResourceVersion())
}

0 comments on commit 325a369

Please sign in to comment.