@@ -65,6 +65,8 @@ const (
6565 omittableFieldsMustNotBeSet = false
6666 zeroMustBeSet = true
6767 zeroMustNotBeSet = false
68+ zeroSecondsMustBeSet = true
69+ zeroSecondsMustNotBeSet = false
6870)
6971
7072// allObjs can be used to look at all the objects / how they change across generation while debugging.
@@ -127,9 +129,10 @@ func TestAPIAndWebhookChanges(t *testing.T) {
127129 g .Eventually (func () error {
128130 cluster1Refs , err = getClusterTopologyReferences (cluster1 , "v1beta1" ,
129131 checkOmittableFromPatchesField (omittableFieldsMustNotBeSet ), // The defaulting webhook drops the omittable field when using v1beta1.
130- checkPtrTypeToType (), // "" should never show up in the yaml ("" is not a valid value).
131- checkTypeToPtrType (), // zero or false should never show up due to omitempty (mutating webhook is dropping omitempty values).
132- checkTypeToOmitZeroType (zeroMustBeSet ), // zero value should show up (no omitzero).
132+ checkPtrTypeToType (), // "" should never show up in the yaml ("" is not a valid value).
133+ checkTypeToPtrType (), // zero or false should never show up due to omitempty (mutating webhook is dropping omitempty values).
134+ checkDurationToPtrInt32 (zeroSecondsMustBeSet ), // metav1.Duration is marshalled as 0s.
135+ checkTypeToOmitZeroType (zeroMustBeSet ), // zero value should show up (no omitzero).
133136 )
134137 if err != nil {
135138 return err
@@ -184,9 +187,10 @@ func TestAPIAndWebhookChanges(t *testing.T) {
184187 g .Eventually (func () error {
185188 cluster1RefsNew , err = getClusterTopologyReferences (cluster1 , "v1beta2" ,
186189 checkOmittableFromPatchesField (omittableFieldsMustBeSet ), // The defaulting webhook does not drop the omittable field anymore when using v1beta2.
187- checkPtrTypeToType (), // "" should never show up in the yaml due to omitempty (we set to "" in conversion, but omitempty drops it from yaml).
188- checkTypeToPtrType (), // zero or false should never show up (we drop zero or false on conversion, we assume implicitly set)
189- checkTypeToOmitZeroType (zeroMustNotBeSet ), // zero value must not show up (omitzero through conversion).
190+ checkPtrTypeToType (), // "" should never show up in the yaml due to omitempty (we set to "" in conversion, but omitempty drops it from yaml).
191+ checkTypeToPtrType (), // zero or false should never show up (we drop zero or false on conversion, we assume implicitly set)
192+ checkDurationToPtrInt32 (zeroSecondsMustNotBeSet ), // 0 should never show up (we drop 0 on conversion, we assume implicitly set)
193+ checkTypeToOmitZeroType (zeroMustNotBeSet ), // zero value must not show up (omitzero through conversion).
190194 )
191195 if err != nil {
192196 return err
@@ -205,9 +209,10 @@ func TestAPIAndWebhookChanges(t *testing.T) {
205209 g .Eventually (func () error {
206210 cluster2Refs , err = getClusterTopologyReferences (cluster2 , "v1beta2" ,
207211 checkOmittableFromPatchesField (omittableFieldsMustNotBeSet ), // The conversion webhook drops the omittable field when using v1beta1.
208- checkPtrTypeToType (), // "" should never show up in the yaml ("" is not a valid value).
209- checkTypeToPtrType (), // zero or false should never show up (we drop zero or false on conversion, we assume implicitly set)
210- checkTypeToOmitZeroType (zeroMustNotBeSet ), // zero value must not show up (omitzero through conversion, also not a valid value).
212+ checkPtrTypeToType (), // "" should never show up in the yaml ("" is not a valid value).
213+ checkTypeToPtrType (), // zero or false should never show up (we drop zero or false on conversion, we assume implicitly set)
214+ checkDurationToPtrInt32 (zeroSecondsMustNotBeSet ), // 0 should never show up (we drop 0 on conversion, we assume implicitly set)
215+ checkTypeToOmitZeroType (zeroMustNotBeSet ), // zero value must not show up (omitzero through conversion, also not a valid value).
211216 )
212217 if err != nil {
213218 return err
@@ -231,9 +236,10 @@ func TestAPIAndWebhookChanges(t *testing.T) {
231236 g .Eventually (func () error {
232237 cluster2RefsNew , err = getClusterTopologyReferences (cluster2 , "v1beta2" ,
233238 checkOmittableFromPatchesField (omittableFieldsMustBeSet ), // The defaulting webhook do not drop anymore the omittable field when using v1beta2.
234- checkPtrTypeToType (), // "" should never show up in the yaml due to omitempty (also not a valid value).
235- checkTypeToPtrType (), // zero or false should never show up (it will show up if someone explicitly set zero or false)
236- checkTypeToOmitZeroType (zeroMustNotBeSet ), // zero value must not show up (omitzero, also not a valid value).
239+ checkPtrTypeToType (), // "" should never show up in the yaml due to omitempty (also not a valid value).
240+ checkTypeToPtrType (), // zero or false should never show up (it will show up if someone explicitly set zero or false)
241+ checkDurationToPtrInt32 (zeroSecondsMustNotBeSet ), // 0 should never show up (it will show up if someone explicitly sets 0)
242+ checkTypeToOmitZeroType (zeroMustNotBeSet ), // zero value must not show up (omitzero, also not a valid value).
237243 )
238244 if err != nil {
239245 return err
@@ -284,9 +290,10 @@ func createT1ClusterClass(g *WithT, ns *corev1.Namespace, ct1 client.Client) *cl
284290 Spec : testt1v1beta1.TestResourceTemplateSpec {
285291 Template : testt1v1beta1.TestResourceTemplateResource {
286292 Spec : testt1v1beta1.TestResourceSpec {
287- BoolToPtrBool : true ,
288- PtrStringToString : ptr .To ("Something" ),
289- Int32ToPtrInt32 : int32 (4 ),
293+ BoolToPtrBool : true ,
294+ PtrStringToString : ptr .To ("Something" ),
295+ Int32ToPtrInt32 : int32 (4 ),
296+ DurationToPtrInt32 : metav1.Duration {Duration : 5 * time .Second },
290297 StructWithOnlyOptionalFields : testt1v1beta1.StructWithOnlyOptionalFields {
291298 A : "Something" ,
292299 },
@@ -307,6 +314,7 @@ func createT1ClusterClass(g *WithT, ns *corev1.Namespace, ct1 client.Client) *cl
307314 BoolToPtrBool : false ,
308315 PtrStringToString : nil ,
309316 Int32ToPtrInt32 : 0 ,
317+ DurationToPtrInt32 : metav1.Duration {},
310318 StructWithOnlyOptionalFields : testt1v1beta1.StructWithOnlyOptionalFields {},
311319 },
312320 },
@@ -481,9 +489,10 @@ func createT2ClusterClass(g *WithT, ns *corev1.Namespace, ct2 client.Client) *cl
481489 Spec : testt2v1beta2.TestResourceTemplateSpec {
482490 Template : testt2v1beta2.TestResourceTemplateResource {
483491 Spec : testt2v1beta2.TestResourceSpec {
484- BoolToPtrBool : ptr .To (true ),
485- PtrStringToString : "Something" ,
486- Int32ToPtrInt32 : ptr.To [int32 ](4 ),
492+ BoolToPtrBool : ptr .To (true ),
493+ PtrStringToString : "Something" ,
494+ Int32ToPtrInt32 : ptr.To [int32 ](4 ),
495+ DurationToPtrInt32 : ptr.To [int32 ](5 ),
487496 StructWithOnlyOptionalFields : testt2v1beta2.StructWithOnlyOptionalFields {
488497 A : "Something" ,
489498 },
@@ -504,6 +513,7 @@ func createT2ClusterClass(g *WithT, ns *corev1.Namespace, ct2 client.Client) *cl
504513 Spec : testt2v1beta2.TestResourceSpec {
505514 BoolToPtrBool : nil ,
506515 Int32ToPtrInt32 : nil ,
516+ DurationToPtrInt32 : nil ,
507517 StructWithOnlyOptionalFields : testt2v1beta2.StructWithOnlyOptionalFields {},
508518 },
509519 },
@@ -863,6 +873,36 @@ func checkTypeToPtrType() func(obj *unstructured.Unstructured) error {
863873 }
864874}
865875
876+ func checkDurationToPtrInt32 (mustBeSet bool ) func (obj * unstructured.Unstructured ) error {
877+ return func (obj * unstructured.Unstructured ) error {
878+ switch obj .GetKind () {
879+ case "TestResource" :
880+ value , exists , err := unstructured .NestedFieldCopy (obj .Object , "spec" , "durationToPtrInt32" )
881+ if err != nil {
882+ return err
883+ }
884+ if ! mustBeSet && exists && (reflect .DeepEqual (value , "0s" ) || reflect .DeepEqual (value , int64 (0 ))) {
885+ return errors .New ("expected to not contain a 0s durationToPtrInt32 field" )
886+ }
887+ if mustBeSet && ! exists {
888+ return errors .New ("expected to contain a durationToPtrInt32 field" )
889+ }
890+ case "TestResourceTemplate" :
891+ value , exists , err := unstructured .NestedFieldCopy (obj .Object , "spec" , "template" , "spec" , "durationToPtrInt32" )
892+ if err != nil {
893+ return err
894+ }
895+ if ! mustBeSet && exists && (reflect .DeepEqual (value , "0s" ) || reflect .DeepEqual (value , int64 (0 ))) {
896+ return errors .New ("expected to not contain a 0s durationToPtrInt32 field" )
897+ }
898+ if mustBeSet && ! exists {
899+ return errors .New ("expected to contain a durationToPtrInt32 field" )
900+ }
901+ }
902+ return nil
903+ }
904+ }
905+
866906func checkTypeToOmitZeroType (mustBeSet bool ) func (obj * unstructured.Unstructured ) error {
867907 return func (obj * unstructured.Unstructured ) error {
868908 switch obj .GetKind () {
0 commit comments