@@ -25,7 +25,10 @@ import (
25
25
26
26
"k8s.io/api/core/v1"
27
27
apiequality "k8s.io/apimachinery/pkg/api/equality"
28
+ "k8s.io/apimachinery/pkg/api/meta"
28
29
"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"
29
32
"k8s.io/apimachinery/pkg/conversion/unstructured"
30
33
"k8s.io/apimachinery/pkg/runtime"
31
34
"k8s.io/apimachinery/pkg/util/diff"
@@ -131,6 +134,43 @@ func TestRoundTrip(t *testing.T) {
131
134
}
132
135
}
133
136
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
+
134
174
func BenchmarkToFromUnstructured (b * testing.B ) {
135
175
items := benchmarkItems (b )
136
176
size := len (items )
0 commit comments