Skip to content

fix: Ensure NillableDuration can round-trip with unstructured #2192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pkg/apis/v1/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1

import (
"encoding/base64"
"encoding/json"
"fmt"
"slices"
Expand Down Expand Up @@ -52,6 +53,17 @@ func (d *NillableDuration) UnmarshalJSON(b []byte) error {
if err != nil {
return err
}

// check for base64 encoded string, can happen from unstructuredConverter marshaling []byte
base64Decoded, err := base64.StdEncoding.DecodeString(str)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the issue that we need to do a base64 decode here or that we just need to return the string representation of the byte array on L82 -- or maybe better yet, just store the string representation in Raw?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to try alternative implementations - my understanding was that FromUnstructured is hard coded to call MarshalJSON on the []byte (rather than it being on string) which causes the base64 encoding.

if err == nil && base64Decoded[0] == byte('"') {
b = base64Decoded
err = json.Unmarshal(base64Decoded, &str)
if err != nil {
return err
}
} // otherwise, it is not base64 encoded

if str == Never {
return nil
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/v1/nodepool_validation_cel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ var _ = Describe("CEL/Validation", func() {
nodePool.Spec.Template.Spec.ExpireAfter = MustParseNillableDuration("Never")
Expect(env.Client.Create(ctx, nodePool)).To(Succeed())
})
It("should support round trip to/from unstructured", func() {
nodePool.Spec.Template.Spec.ExpireAfter = MustParseNillableDuration("30s")
u := lo.Must(runtime.DefaultUnstructuredConverter.ToUnstructured(nodePool))
nodePool2 := &NodePool{}
lo.Must0(runtime.DefaultUnstructuredConverter.FromUnstructured(u, nodePool2))
Expect(nodePool2).To(Equal(nodePool))
})
DescribeTable("should succeed on a valid expireAfter", func(value string) {
u := lo.Must(runtime.DefaultUnstructuredConverter.ToUnstructured(nodePool))
lo.Must0(unstructured.SetNestedField(u, value, "spec", "template", "spec", "expireAfter"))
Expand Down
Loading