Skip to content

Commit 2008750

Browse files
committed
remove empty creationTimestamp field
1 parent 40212c1 commit 2008750

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

pkg/api/testing/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ go_test(
9595
"//vendor/k8s.io/apimachinery/pkg/api/testing/fuzzer:go_default_library",
9696
"//vendor/k8s.io/apimachinery/pkg/api/testing/roundtrip:go_default_library",
9797
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
98+
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",
9899
"//vendor/k8s.io/apimachinery/pkg/conversion:go_default_library",
99100
"//vendor/k8s.io/apimachinery/pkg/conversion/unstructured:go_default_library",
100101
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",

pkg/api/testing/unstructured_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ import (
2525

2626
"k8s.io/api/core/v1"
2727
apiequality "k8s.io/apimachinery/pkg/api/equality"
28+
"k8s.io/apimachinery/pkg/api/meta"
2829
"k8s.io/apimachinery/pkg/api/testing/fuzzer"
30+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
31+
metaunstruct "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2932
"k8s.io/apimachinery/pkg/conversion/unstructured"
3033
"k8s.io/apimachinery/pkg/runtime"
3134
"k8s.io/apimachinery/pkg/util/diff"
@@ -131,6 +134,43 @@ func TestRoundTrip(t *testing.T) {
131134
}
132135
}
133136

137+
func TestRoundTripWithEmptyCreationTimestamp(t *testing.T) {
138+
for groupKey, group := range testapi.Groups {
139+
for kind := range group.ExternalTypes() {
140+
if nonRoundTrippableTypes.Has(kind) {
141+
continue
142+
}
143+
item, err := legacyscheme.Scheme.New(group.GroupVersion().WithKind(kind))
144+
if err != nil {
145+
t.Fatalf("Couldn't create external object %v: %v", kind, err)
146+
}
147+
t.Logf("Testing: %v in %v", kind, groupKey)
148+
149+
unstrBody, err := unstructured.DefaultConverter.ToUnstructured(item)
150+
if err != nil {
151+
t.Fatalf("ToUnstructured failed: %v", err)
152+
}
153+
154+
unstructObj := &metaunstruct.Unstructured{}
155+
unstructObj.Object = unstrBody
156+
157+
if meta, err := meta.Accessor(unstructObj); err == nil {
158+
meta.SetCreationTimestamp(metav1.Time{})
159+
} else {
160+
t.Fatalf("Unable to set creation timestamp: %v", err)
161+
}
162+
163+
// attempt to re-convert unstructured object - conversion should not fail
164+
// based on empty metadata fields, such as creationTimestamp
165+
newObj := reflect.New(reflect.TypeOf(item).Elem()).Interface().(runtime.Object)
166+
err = unstructured.DefaultConverter.FromUnstructured(unstructObj.Object, newObj)
167+
if err != nil {
168+
t.Fatalf("FromUnstructured failed: %v", err)
169+
}
170+
}
171+
}
172+
}
173+
134174
func BenchmarkToFromUnstructured(b *testing.B) {
135175
items := benchmarkItems(b)
136176
size := len(items)

staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/unstructured.go

+4
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,10 @@ func (u *Unstructured) GetCreationTimestamp() metav1.Time {
258258

259259
func (u *Unstructured) SetCreationTimestamp(timestamp metav1.Time) {
260260
ts, _ := timestamp.MarshalQueryParameter()
261+
if len(ts) == 0 || timestamp.Time.IsZero() {
262+
RemoveNestedField(u.Object, "metadata", "creationTimestamp")
263+
return
264+
}
261265
u.setNestedField(ts, "metadata", "creationTimestamp")
262266
}
263267

staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/unstructured_list_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,24 @@ func TestNilDeletionTimestamp(t *testing.T) {
6262
_, ok = metadata["deletionTimestamp"]
6363
assert.False(t, ok)
6464
}
65+
66+
func TestEmptyCreationTimestampIsOmitted(t *testing.T) {
67+
var u Unstructured
68+
now := metav1.Now()
69+
70+
// set an initial creationTimestamp and ensure the field exists
71+
u.SetCreationTimestamp(now)
72+
metadata := u.Object["metadata"].(map[string]interface{})
73+
creationTimestamp, exists := metadata["creationTimestamp"]
74+
if !exists {
75+
t.Fatalf("unexpected missing creationTimestamp")
76+
}
77+
78+
// set an empty timestamp and ensure the field no longer exists
79+
u.SetCreationTimestamp(metav1.Time{})
80+
metadata = u.Object["metadata"].(map[string]interface{})
81+
creationTimestamp, exists = metadata["creationTimestamp"]
82+
if exists {
83+
t.Errorf("unexpected creation timestamp field: %q", creationTimestamp)
84+
}
85+
}

0 commit comments

Comments
 (0)