Skip to content

Commit 3301ba3

Browse files
committed
Ensure NillableDuration can round-trip with unstructured
1 parent 75f26e6 commit 3301ba3

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

pkg/apis/v1/duration.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package v1
1818

1919
import (
20+
"encoding/base64"
2021
"encoding/json"
2122
"fmt"
2223
"slices"
@@ -52,6 +53,17 @@ func (d *NillableDuration) UnmarshalJSON(b []byte) error {
5253
if err != nil {
5354
return err
5455
}
56+
57+
// check for base64 encoded string, can happen from unstructuredConverter marshalling []byte
58+
base64Decoded, err := base64.StdEncoding.DecodeString(str)
59+
if err == nil && base64Decoded[0] == byte('"') {
60+
b = base64Decoded
61+
err = json.Unmarshal(base64Decoded, &str)
62+
if err != nil {
63+
return err
64+
}
65+
} // otherwise, it is not base64 encoded
66+
5567
if str == Never {
5668
return nil
5769
}

pkg/apis/v1/nodepool_validation_cel_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ var _ = Describe("CEL/Validation", func() {
7272
nodePool.Spec.Template.Spec.ExpireAfter = MustParseNillableDuration("Never")
7373
Expect(env.Client.Create(ctx, nodePool)).To(Succeed())
7474
})
75+
It("should support round trip to/from unstructured", func() {
76+
nodePool.Spec.Template.Spec.ExpireAfter = MustParseNillableDuration("30s")
77+
u := lo.Must(runtime.DefaultUnstructuredConverter.ToUnstructured(nodePool))
78+
nodePool2 := &NodePool{}
79+
lo.Must0(runtime.DefaultUnstructuredConverter.FromUnstructured(u, nodePool2))
80+
Expect(nodePool2).To(Equal(nodePool))
81+
})
7582
DescribeTable("should succeed on a valid expireAfter", func(value string) {
7683
u := lo.Must(runtime.DefaultUnstructuredConverter.ToUnstructured(nodePool))
7784
lo.Must0(unstructured.SetNestedField(u, value, "spec", "template", "spec", "expireAfter"))

0 commit comments

Comments
 (0)