-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Pod limit range #2080
Merged
Merged
Pod limit range #2080
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 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 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 |
---|---|---|
|
@@ -217,11 +217,16 @@ func TestApplyVpa(t *testing.T) { | |
} | ||
|
||
type fakeLimitRangeCalculator struct { | ||
limitRange apiv1.LimitRangeItem | ||
containerLimitRange apiv1.LimitRangeItem | ||
podLimitRange apiv1.LimitRangeItem | ||
} | ||
|
||
func (nlrc *fakeLimitRangeCalculator) GetContainerLimitRangeItem(namespace string) (*apiv1.LimitRangeItem, error) { | ||
return &nlrc.limitRange, nil | ||
return &nlrc.containerLimitRange, nil | ||
} | ||
|
||
func (nlrc *fakeLimitRangeCalculator) GetPodLimitRangeItem(namespace string) (*apiv1.LimitRangeItem, error) { | ||
return &nlrc.podLimitRange, nil | ||
} | ||
|
||
func TestApplyCapsToLimitRange(t *testing.T) { | ||
|
@@ -276,11 +281,218 @@ func TestApplyCapsToLimitRange(t *testing.T) { | |
}, | ||
} | ||
|
||
calculator := fakeLimitRangeCalculator{limitRange} | ||
calculator := fakeLimitRangeCalculator{containerLimitRange: limitRange} | ||
processor := NewCappingRecommendationProcessor(&calculator) | ||
processedRecommendation, annotations, err := processor.Apply(&recommendation, nil, nil, &pod) | ||
assert.NoError(t, err) | ||
assert.Contains(t, annotations, "container") | ||
assert.ElementsMatch(t, []string{"cpu capped to fit Max in container LimitRange", "memory capped to fit Min in container LimitRange"}, annotations["container"]) | ||
assert.Equal(t, expectedRecommendation, *processedRecommendation) | ||
} | ||
|
||
func TestApplyPodLimitRange(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
resources []vpa_types.RecommendedContainerResources | ||
pod apiv1.Pod | ||
limitRange apiv1.LimitRangeItem | ||
resourceName apiv1.ResourceName | ||
expect []vpa_types.RecommendedContainerResources | ||
}{ | ||
{ | ||
name: "cap target cpu to max", | ||
resources: []vpa_types.RecommendedContainerResources{ | ||
{ | ||
ContainerName: "container1", | ||
Target: apiv1.ResourceList{ | ||
apiv1.ResourceCPU: resource.MustParse("1"), | ||
}, | ||
}, | ||
{ | ||
ContainerName: "container2", | ||
Target: apiv1.ResourceList{ | ||
apiv1.ResourceCPU: resource.MustParse("1"), | ||
}, | ||
}, | ||
}, | ||
pod: apiv1.Pod{ | ||
Spec: apiv1.PodSpec{ | ||
Containers: []apiv1.Container{ | ||
{ | ||
Resources: apiv1.ResourceRequirements{ | ||
Requests: apiv1.ResourceList{ | ||
apiv1.ResourceCPU: resource.MustParse("1"), | ||
}, | ||
Limits: apiv1.ResourceList{ | ||
apiv1.ResourceCPU: resource.MustParse("1"), | ||
}, | ||
}, | ||
}, | ||
{ | ||
Resources: apiv1.ResourceRequirements{ | ||
Requests: apiv1.ResourceList{ | ||
apiv1.ResourceCPU: resource.MustParse("1"), | ||
}, | ||
Limits: apiv1.ResourceList{ | ||
apiv1.ResourceCPU: resource.MustParse("1"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
limitRange: apiv1.LimitRangeItem{ | ||
Max: apiv1.ResourceList{ | ||
apiv1.ResourceCPU: resource.MustParse("1"), | ||
}, | ||
}, | ||
resourceName: apiv1.ResourceCPU, | ||
expect: []vpa_types.RecommendedContainerResources{ | ||
{ | ||
ContainerName: "container1", | ||
Target: apiv1.ResourceList{ | ||
apiv1.ResourceCPU: *resource.NewMilliQuantity(500, resource.DecimalSI), | ||
}, | ||
}, | ||
{ | ||
ContainerName: "container2", | ||
Target: apiv1.ResourceList{ | ||
apiv1.ResourceCPU: *resource.NewMilliQuantity(500, resource.DecimalSI), | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "cap cpu to max", | ||
resources: []vpa_types.RecommendedContainerResources{ | ||
{ | ||
ContainerName: "container1", | ||
Target: apiv1.ResourceList{ | ||
apiv1.ResourceCPU: resource.MustParse("1"), | ||
}, | ||
}, | ||
{ | ||
ContainerName: "container2", | ||
Target: apiv1.ResourceList{ | ||
apiv1.ResourceCPU: resource.MustParse("1"), | ||
}, | ||
}, | ||
}, | ||
pod: apiv1.Pod{ | ||
Spec: apiv1.PodSpec{ | ||
Containers: []apiv1.Container{ | ||
{ | ||
Resources: apiv1.ResourceRequirements{ | ||
Requests: apiv1.ResourceList{ | ||
apiv1.ResourceCPU: resource.MustParse("1"), | ||
}, | ||
Limits: apiv1.ResourceList{ | ||
apiv1.ResourceCPU: resource.MustParse("1"), | ||
}, | ||
}, | ||
}, | ||
{ | ||
Resources: apiv1.ResourceRequirements{ | ||
Requests: apiv1.ResourceList{ | ||
apiv1.ResourceCPU: resource.MustParse("1"), | ||
}, | ||
Limits: apiv1.ResourceList{ | ||
apiv1.ResourceCPU: resource.MustParse("1"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
limitRange: apiv1.LimitRangeItem{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is Pod type the default? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, it doesn't matter for this test but I see how this could be confusing. Done. |
||
Max: apiv1.ResourceList{ | ||
apiv1.ResourceCPU: resource.MustParse("1"), | ||
}, | ||
}, | ||
resourceName: apiv1.ResourceCPU, | ||
expect: []vpa_types.RecommendedContainerResources{ | ||
{ | ||
ContainerName: "container1", | ||
Target: apiv1.ResourceList{ | ||
apiv1.ResourceCPU: *resource.NewMilliQuantity(500, resource.DecimalSI), | ||
}, | ||
}, | ||
{ | ||
ContainerName: "container2", | ||
Target: apiv1.ResourceList{ | ||
apiv1.ResourceCPU: *resource.NewMilliQuantity(500, resource.DecimalSI), | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "cap mem to min", | ||
resources: []vpa_types.RecommendedContainerResources{ | ||
{ | ||
ContainerName: "container1", | ||
Target: apiv1.ResourceList{ | ||
apiv1.ResourceMemory: resource.MustParse("1G"), | ||
}, | ||
}, | ||
{ | ||
ContainerName: "container2", | ||
Target: apiv1.ResourceList{ | ||
apiv1.ResourceMemory: resource.MustParse("1G"), | ||
}, | ||
}, | ||
}, | ||
pod: apiv1.Pod{ | ||
Spec: apiv1.PodSpec{ | ||
Containers: []apiv1.Container{ | ||
{ | ||
Resources: apiv1.ResourceRequirements{ | ||
Requests: apiv1.ResourceList{ | ||
apiv1.ResourceMemory: resource.MustParse("1"), | ||
}, | ||
Limits: apiv1.ResourceList{ | ||
apiv1.ResourceMemory: resource.MustParse("1"), | ||
}, | ||
}, | ||
}, | ||
{ | ||
Resources: apiv1.ResourceRequirements{ | ||
Requests: apiv1.ResourceList{ | ||
apiv1.ResourceMemory: resource.MustParse("1"), | ||
}, | ||
Limits: apiv1.ResourceList{ | ||
apiv1.ResourceMemory: resource.MustParse("1"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
limitRange: apiv1.LimitRangeItem{ | ||
Min: apiv1.ResourceList{ | ||
apiv1.ResourceMemory: resource.MustParse("4G"), | ||
}, | ||
}, | ||
resourceName: apiv1.ResourceMemory, | ||
expect: []vpa_types.RecommendedContainerResources{ | ||
{ | ||
ContainerName: "container1", | ||
Target: apiv1.ResourceList{ | ||
apiv1.ResourceMemory: resource.MustParse("2000000000000m"), | ||
}, | ||
}, | ||
{ | ||
ContainerName: "container2", | ||
Target: apiv1.ResourceList{ | ||
apiv1.ResourceMemory: resource.MustParse("2000000000000m"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
got := applyPodLimitRange(tc.resources, &tc.pod, tc.limitRange, tc.resourceName) | ||
assert.Equal(t, tc.expect, got) | ||
}) | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe also both min and max
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Test is now redundant. I'll change limit ranges to have both min and max.