-
Notifications
You must be signed in to change notification settings - Fork 92
OADP-8548: PodResources unset fields should be output as "0" to fix parsing errors results in ignored resource settings #2385
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
msfrucht
wants to merge
4
commits into
openshift:oadp-dev
Choose a base branch
from
msfrucht:OADP-8548-dev
base: oadp-dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
85a8dc6
PodResources must be complete for Velero parser
msfrucht f4108cf
Revise to prevent DPA Spec mutation
msfrucht d29e2c5
Add test of defaults due to more complex logic returning new object
msfrucht a43e8d6
Merge remote-tracking branch 'origin/oadp-dev' into OADP-8548-dev
msfrucht File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package controller | ||
|
|
||
| import ( | ||
| "github.com/vmware-tanzu/velero/pkg/util/kube" | ||
| ) | ||
|
|
||
| const unbounded = "0" | ||
|
|
||
| // This module is for setting structure defaults where the default golang values for the type are not valid. | ||
| // int -> 0 | ||
| // float -> 0.0 | ||
| // string -> "" | ||
|
|
||
| // PodResources with emptystring will trigger parsing errors in Velero. | ||
| // Replace empty string with unbounded so partial resource setting is accepted. | ||
| // | ||
| // Returns a new PodResources objects with any default values set to "0". | ||
| // If nil, returns the existing nil pointer. | ||
| func newPodResourcesWithUnboundedDefaults(pr *kube.PodResources) *kube.PodResources { | ||
| if pr == nil { | ||
| return pr | ||
| } | ||
|
|
||
| prWithUnboundedDefaults := &kube.PodResources{} | ||
| if pr.CPURequest == "" { | ||
| prWithUnboundedDefaults.CPURequest = unbounded | ||
| } else { | ||
| prWithUnboundedDefaults.CPURequest = pr.CPURequest | ||
| } | ||
| if pr.CPULimit == "" { | ||
| prWithUnboundedDefaults.CPULimit = unbounded | ||
| } else { | ||
| prWithUnboundedDefaults.CPULimit = pr.CPULimit | ||
| } | ||
| if pr.MemoryRequest == "" { | ||
| prWithUnboundedDefaults.MemoryRequest = unbounded | ||
| } else { | ||
| prWithUnboundedDefaults.MemoryRequest = pr.MemoryRequest | ||
| } | ||
| if pr.MemoryLimit == "" { | ||
| prWithUnboundedDefaults.MemoryLimit = unbounded | ||
| } else { | ||
| prWithUnboundedDefaults.MemoryLimit = pr.MemoryLimit | ||
| } | ||
| if pr.EphemeralStorageRequest == "" { | ||
| prWithUnboundedDefaults.EphemeralStorageRequest = unbounded | ||
| } else { | ||
| prWithUnboundedDefaults.EphemeralStorageRequest = pr.EphemeralStorageRequest | ||
| } | ||
| if pr.EphemeralStorageLimit == "" { | ||
| prWithUnboundedDefaults.EphemeralStorageLimit = unbounded | ||
| } else { | ||
| prWithUnboundedDefaults.EphemeralStorageLimit = pr.EphemeralStorageLimit | ||
| } | ||
|
|
||
| return prWithUnboundedDefaults | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package controller | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| "github.com/vmware-tanzu/velero/pkg/util/kube" | ||
| ) | ||
|
|
||
| func Test_newPodResourcesWithUnboundedDefaults(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| input *kube.PodResources | ||
| want *kube.PodResources | ||
| }{ | ||
| { | ||
| name: "nil returns nil", | ||
| input: nil, | ||
| want: nil, | ||
| }, | ||
| { | ||
| name: "partially set fields get unset values replaced with unbounded", | ||
| input: &kube.PodResources{ | ||
| CPURequest: "100m", | ||
| MemoryRequest: "128Mi", | ||
| }, | ||
| want: &kube.PodResources{ | ||
| CPURequest: "100m", | ||
| CPULimit: "0", | ||
| MemoryRequest: "128Mi", | ||
| MemoryLimit: "0", | ||
| EphemeralStorageRequest: "0", | ||
| EphemeralStorageLimit: "0", | ||
| }, | ||
| }, | ||
| { | ||
| name: "all fields set returns unchanged", | ||
| input: &kube.PodResources{ | ||
| CPURequest: "100m", | ||
| CPULimit: "200m", | ||
| MemoryRequest: "128Mi", | ||
| MemoryLimit: "256Mi", | ||
| EphemeralStorageRequest: "1Gi", | ||
| EphemeralStorageLimit: "2Gi", | ||
| }, | ||
| want: &kube.PodResources{ | ||
| CPURequest: "100m", | ||
| CPULimit: "200m", | ||
| MemoryRequest: "128Mi", | ||
| MemoryLimit: "256Mi", | ||
| EphemeralStorageRequest: "1Gi", | ||
| EphemeralStorageLimit: "2Gi", | ||
| }, | ||
| }, | ||
| { | ||
| name: "zero-value struct gets all fields set to unbounded", | ||
| input: &kube.PodResources{}, | ||
| want: &kube.PodResources{ | ||
| CPURequest: "0", | ||
| CPULimit: "0", | ||
| MemoryRequest: "0", | ||
| MemoryLimit: "0", | ||
| EphemeralStorageRequest: "0", | ||
| EphemeralStorageLimit: "0", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| got := newPodResourcesWithUnboundedDefaults(tt.input) | ||
| require.Equal(t, tt.want, got) | ||
|
|
||
| // require the output object is not a mutation of the existing object | ||
| if tt.input != nil { | ||
| require.NotSame(t, tt.input, got) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.