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

Conversation

adammw
Copy link

@adammw adammw commented May 5, 2025

Description
Ensure NillableDuration can round-trip with default unstructured converter.

Previously this failed with "invalid duration" and a base64-encoded duration.

How was this change tested?

Test added to round-trip through the default unstructured converter.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label May 5, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: adammw
Once this PR has been reviewed and has the lgtm label, please assign jonathan-innis for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot requested review from engedaam and tallaxes May 5, 2025 03:28
@k8s-ci-robot
Copy link
Contributor

Welcome @adammw!

It looks like this is your first PR to kubernetes-sigs/karpenter 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/karpenter has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label May 5, 2025
@k8s-ci-robot
Copy link
Contributor

Hi @adammw. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label May 5, 2025
@adammw adammw force-pushed the adammw/nillableduration-roundtrip branch from 3301ba3 to 5531f34 Compare May 5, 2025 05:57
@coveralls
Copy link

Pull Request Test Coverage Report for Build 14830014839

Details

  • 5 of 7 (71.43%) changed or added relevant lines in 1 file are covered.
  • 2 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.05%) to 82.044%

Changes Missing Coverage Covered Lines Changed/Added Lines %
pkg/apis/v1/duration.go 5 7 71.43%
Files with Coverage Reduction New Missed Lines %
pkg/controllers/disruption/drift.go 2 89.66%
Totals Coverage Status
Change from base Build 14806030316: 0.05%
Covered Lines: 10084
Relevant Lines: 12291

💛 - Coveralls

@rschalo
Copy link
Contributor

rschalo commented May 8, 2025

Maybe I'm missing something but when does this actually happen?

@adammw
Copy link
Author

adammw commented May 12, 2025

We encounter this problem using any of the Karpenter API types that include the NIllableDuration in our internal project that manages the nodepool resources for us. Since the API type claims to support the FromUnstructured/ToUnstructured interface, the suggestion was that the fix should live upstream where those methods are defined.

@engedaam
Copy link
Contributor

/assign @rschalo

@rschalo
Copy link
Contributor

rschalo commented May 12, 2025

Can you please give an example? Is it something that is being set by the client managing the NodePool? What does the duration and NodePool look like when it fails?

@adammw
Copy link
Author

adammw commented May 22, 2025

An example from the test case which will fail with the following without the implementation changes:

runtime.DefaultUnstructuredConverter.FromUnstructured(u, nodePool)

returns error

time: invalid duration "IjMwcyI="

with a valid duration of 30s set.

Unstructured conversions are used often to write generic helper methods, we use them to detect and save changes back to Kubernetes in our controller. However without custom handling of UnmarshalJSON, the above error occurs from the byte array being marshalled to base64 and therefore cannot be unmashalled as a time duration.

@@ -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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants