mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 17:30:00 +00:00
If ResourceRequirements changed, always mark a proposed resize
This commit is contained in:
parent
bf4f1832e2
commit
99dcf07e21
@ -1297,26 +1297,17 @@ func MarkPodProposedForResize(oldPod, newPod *api.Pod) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for i, c := range newPod.Spec.Containers {
|
for i, c := range newPod.Spec.Containers {
|
||||||
|
if c.Name != oldPod.Spec.Containers[i].Name {
|
||||||
|
return // Update is invalid (container mismatch): let validation handle it.
|
||||||
|
}
|
||||||
if c.Resources.Requests == nil {
|
if c.Resources.Requests == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if cmp.Equal(oldPod.Spec.Containers[i].Resources, c.Resources) {
|
if cmp.Equal(oldPod.Spec.Containers[i].Resources, c.Resources) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
findContainerStatus := func(css []api.ContainerStatus, cName string) (api.ContainerStatus, bool) {
|
|
||||||
for i := range css {
|
|
||||||
if css[i].Name == cName {
|
|
||||||
return css[i], true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return api.ContainerStatus{}, false
|
|
||||||
}
|
|
||||||
if cs, ok := findContainerStatus(newPod.Status.ContainerStatuses, c.Name); ok {
|
|
||||||
if !cmp.Equal(c.Resources.Requests, cs.AllocatedResources) {
|
|
||||||
newPod.Status.Resize = api.PodResizeStatusProposed
|
newPod.Status.Resize = api.PodResizeStatusProposed
|
||||||
break
|
return
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2807,14 +2807,13 @@ func TestDropSidecarContainers(t *testing.T) {
|
|||||||
func TestMarkPodProposedForResize(t *testing.T) {
|
func TestMarkPodProposedForResize(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
desc string
|
desc string
|
||||||
newPod *api.Pod
|
newPodSpec api.PodSpec
|
||||||
oldPod *api.Pod
|
oldPodSpec api.PodSpec
|
||||||
expectedPod *api.Pod
|
expectProposedResize bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
desc: "nil requests",
|
desc: "nil requests",
|
||||||
newPod: &api.Pod{
|
newPodSpec: api.PodSpec{
|
||||||
Spec: api.PodSpec{
|
|
||||||
Containers: []api.Container{
|
Containers: []api.Container{
|
||||||
{
|
{
|
||||||
Name: "c1",
|
Name: "c1",
|
||||||
@ -2822,17 +2821,7 @@ func TestMarkPodProposedForResize(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Status: api.PodStatus{
|
oldPodSpec: api.PodSpec{
|
||||||
ContainerStatuses: []api.ContainerStatus{
|
|
||||||
{
|
|
||||||
Name: "c1",
|
|
||||||
Image: "image",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
oldPod: &api.Pod{
|
|
||||||
Spec: api.PodSpec{
|
|
||||||
Containers: []api.Container{
|
Containers: []api.Container{
|
||||||
{
|
{
|
||||||
Name: "c1",
|
Name: "c1",
|
||||||
@ -2840,38 +2829,11 @@ func TestMarkPodProposedForResize(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Status: api.PodStatus{
|
expectProposedResize: false,
|
||||||
ContainerStatuses: []api.ContainerStatus{
|
|
||||||
{
|
|
||||||
Name: "c1",
|
|
||||||
Image: "image",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
expectedPod: &api.Pod{
|
|
||||||
Spec: api.PodSpec{
|
|
||||||
Containers: []api.Container{
|
|
||||||
{
|
|
||||||
Name: "c1",
|
|
||||||
Image: "image",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Status: api.PodStatus{
|
|
||||||
ContainerStatuses: []api.ContainerStatus{
|
|
||||||
{
|
|
||||||
Name: "c1",
|
|
||||||
Image: "image",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "resources unchanged",
|
desc: "resources unchanged",
|
||||||
newPod: &api.Pod{
|
newPodSpec: api.PodSpec{
|
||||||
Spec: api.PodSpec{
|
|
||||||
Containers: []api.Container{
|
Containers: []api.Container{
|
||||||
{
|
{
|
||||||
Name: "c1",
|
Name: "c1",
|
||||||
@ -2883,17 +2845,7 @@ func TestMarkPodProposedForResize(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Status: api.PodStatus{
|
oldPodSpec: api.PodSpec{
|
||||||
ContainerStatuses: []api.ContainerStatus{
|
|
||||||
{
|
|
||||||
Name: "c1",
|
|
||||||
Image: "image",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
oldPod: &api.Pod{
|
|
||||||
Spec: api.PodSpec{
|
|
||||||
Containers: []api.Container{
|
Containers: []api.Container{
|
||||||
{
|
{
|
||||||
Name: "c1",
|
Name: "c1",
|
||||||
@ -2905,42 +2857,11 @@ func TestMarkPodProposedForResize(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Status: api.PodStatus{
|
expectProposedResize: false,
|
||||||
ContainerStatuses: []api.ContainerStatus{
|
|
||||||
{
|
|
||||||
Name: "c1",
|
|
||||||
Image: "image",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
expectedPod: &api.Pod{
|
|
||||||
Spec: api.PodSpec{
|
|
||||||
Containers: []api.Container{
|
|
||||||
{
|
|
||||||
Name: "c1",
|
|
||||||
Image: "image",
|
|
||||||
Resources: api.ResourceRequirements{
|
|
||||||
Requests: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
|
||||||
Limits: api.ResourceList{api.ResourceCPU: resource.MustParse("200m")},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Status: api.PodStatus{
|
|
||||||
ContainerStatuses: []api.ContainerStatus{
|
|
||||||
{
|
|
||||||
Name: "c1",
|
|
||||||
Image: "image",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "resize desired",
|
desc: "requests resized",
|
||||||
newPod: &api.Pod{
|
newPodSpec: api.PodSpec{
|
||||||
Spec: api.PodSpec{
|
|
||||||
Containers: []api.Container{
|
Containers: []api.Container{
|
||||||
{
|
{
|
||||||
Name: "c1",
|
Name: "c1",
|
||||||
@ -2960,23 +2881,7 @@ func TestMarkPodProposedForResize(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Status: api.PodStatus{
|
oldPodSpec: api.PodSpec{
|
||||||
ContainerStatuses: []api.ContainerStatus{
|
|
||||||
{
|
|
||||||
Name: "c1",
|
|
||||||
Image: "image",
|
|
||||||
AllocatedResources: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "c2",
|
|
||||||
Image: "image",
|
|
||||||
AllocatedResources: api.ResourceList{api.ResourceCPU: resource.MustParse("200m")},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
oldPod: &api.Pod{
|
|
||||||
Spec: api.PodSpec{
|
|
||||||
Containers: []api.Container{
|
Containers: []api.Container{
|
||||||
{
|
{
|
||||||
Name: "c1",
|
Name: "c1",
|
||||||
@ -2991,28 +2896,16 @@ func TestMarkPodProposedForResize(t *testing.T) {
|
|||||||
Image: "image",
|
Image: "image",
|
||||||
Resources: api.ResourceRequirements{
|
Resources: api.ResourceRequirements{
|
||||||
Requests: api.ResourceList{api.ResourceCPU: resource.MustParse("200m")},
|
Requests: api.ResourceList{api.ResourceCPU: resource.MustParse("200m")},
|
||||||
Limits: api.ResourceList{api.ResourceCPU: resource.MustParse("300m")},
|
Limits: api.ResourceList{api.ResourceCPU: resource.MustParse("400m")},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Status: api.PodStatus{
|
expectProposedResize: true,
|
||||||
ContainerStatuses: []api.ContainerStatus{
|
|
||||||
{
|
|
||||||
Name: "c1",
|
|
||||||
Image: "image",
|
|
||||||
AllocatedResources: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "c2",
|
desc: "limits resized",
|
||||||
Image: "image",
|
newPodSpec: api.PodSpec{
|
||||||
AllocatedResources: api.ResourceList{api.ResourceCPU: resource.MustParse("200m")},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
expectedPod: &api.Pod{
|
|
||||||
Spec: api.PodSpec{
|
|
||||||
Containers: []api.Container{
|
Containers: []api.Container{
|
||||||
{
|
{
|
||||||
Name: "c1",
|
Name: "c1",
|
||||||
@ -3032,33 +2925,37 @@ func TestMarkPodProposedForResize(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Status: api.PodStatus{
|
oldPodSpec: api.PodSpec{
|
||||||
Resize: api.PodResizeStatusProposed,
|
Containers: []api.Container{
|
||||||
ContainerStatuses: []api.ContainerStatus{
|
|
||||||
{
|
{
|
||||||
Name: "c1",
|
Name: "c1",
|
||||||
Image: "image",
|
Image: "image",
|
||||||
AllocatedResources: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
Resources: api.ResourceRequirements{
|
||||||
|
Requests: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
||||||
|
Limits: api.ResourceList{api.ResourceCPU: resource.MustParse("200m")},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "c2",
|
Name: "c2",
|
||||||
Image: "image",
|
Image: "image",
|
||||||
AllocatedResources: api.ResourceList{api.ResourceCPU: resource.MustParse("200m")},
|
Resources: api.ResourceRequirements{
|
||||||
|
Requests: api.ResourceList{api.ResourceCPU: resource.MustParse("300m")},
|
||||||
|
Limits: api.ResourceList{api.ResourceCPU: resource.MustParse("500m")},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
expectProposedResize: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "the number of containers in the pod has increased; no action should be taken.",
|
desc: "the number of containers in the pod has increased; no action should be taken.",
|
||||||
newPod: &api.Pod{
|
newPodSpec: api.PodSpec{
|
||||||
Spec: api.PodSpec{
|
|
||||||
Containers: []api.Container{
|
Containers: []api.Container{
|
||||||
{
|
{
|
||||||
Name: "c1",
|
Name: "c1",
|
||||||
Image: "image",
|
Image: "image",
|
||||||
Resources: api.ResourceRequirements{
|
Resources: api.ResourceRequirements{
|
||||||
Requests: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
Requests: api.ResourceList{api.ResourceCPU: resource.MustParse("200m")},
|
||||||
Limits: api.ResourceList{api.ResourceCPU: resource.MustParse("200m")},
|
Limits: api.ResourceList{api.ResourceCPU: resource.MustParse("200m")},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@ -3072,23 +2969,7 @@ func TestMarkPodProposedForResize(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Status: api.PodStatus{
|
oldPodSpec: api.PodSpec{
|
||||||
ContainerStatuses: []api.ContainerStatus{
|
|
||||||
{
|
|
||||||
Name: "c1",
|
|
||||||
Image: "image",
|
|
||||||
AllocatedResources: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "c2",
|
|
||||||
Image: "image",
|
|
||||||
AllocatedResources: api.ResourceList{api.ResourceCPU: resource.MustParse("200m")},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
oldPod: &api.Pod{
|
|
||||||
Spec: api.PodSpec{
|
|
||||||
Containers: []api.Container{
|
Containers: []api.Container{
|
||||||
{
|
{
|
||||||
Name: "c1",
|
Name: "c1",
|
||||||
@ -3100,80 +2981,23 @@ func TestMarkPodProposedForResize(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Status: api.PodStatus{
|
expectProposedResize: false,
|
||||||
ContainerStatuses: []api.ContainerStatus{
|
|
||||||
{
|
|
||||||
Name: "c1",
|
|
||||||
Image: "image",
|
|
||||||
AllocatedResources: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
expectedPod: &api.Pod{
|
|
||||||
Spec: api.PodSpec{
|
|
||||||
Containers: []api.Container{
|
|
||||||
{
|
|
||||||
Name: "c1",
|
|
||||||
Image: "image",
|
|
||||||
Resources: api.ResourceRequirements{
|
|
||||||
Requests: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
|
||||||
Limits: api.ResourceList{api.ResourceCPU: resource.MustParse("200m")},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "c2",
|
|
||||||
Image: "image",
|
|
||||||
Resources: api.ResourceRequirements{
|
|
||||||
Requests: api.ResourceList{api.ResourceCPU: resource.MustParse("300m")},
|
|
||||||
Limits: api.ResourceList{api.ResourceCPU: resource.MustParse("400m")},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Status: api.PodStatus{
|
|
||||||
ContainerStatuses: []api.ContainerStatus{
|
|
||||||
{
|
|
||||||
Name: "c1",
|
|
||||||
Image: "image",
|
|
||||||
AllocatedResources: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Name: "c2",
|
|
||||||
Image: "image",
|
|
||||||
AllocatedResources: api.ResourceList{api.ResourceCPU: resource.MustParse("200m")},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "the number of containers in the pod has decreased; no action should be taken.",
|
desc: "the number of containers in the pod has decreased; no action should be taken.",
|
||||||
newPod: &api.Pod{
|
newPodSpec: api.PodSpec{
|
||||||
Spec: api.PodSpec{
|
|
||||||
Containers: []api.Container{
|
Containers: []api.Container{
|
||||||
{
|
{
|
||||||
Name: "c1",
|
Name: "c1",
|
||||||
Image: "image",
|
Image: "image",
|
||||||
Resources: api.ResourceRequirements{
|
Resources: api.ResourceRequirements{
|
||||||
Requests: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
Requests: api.ResourceList{api.ResourceCPU: resource.MustParse("200m")},
|
||||||
Limits: api.ResourceList{api.ResourceCPU: resource.MustParse("200m")},
|
Limits: api.ResourceList{api.ResourceCPU: resource.MustParse("200m")},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Status: api.PodStatus{
|
oldPodSpec: api.PodSpec{
|
||||||
ContainerStatuses: []api.ContainerStatus{
|
|
||||||
{
|
|
||||||
Name: "c1",
|
|
||||||
Image: "image",
|
|
||||||
AllocatedResources: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
oldPod: &api.Pod{
|
|
||||||
Spec: api.PodSpec{
|
|
||||||
Containers: []api.Container{
|
Containers: []api.Container{
|
||||||
{
|
{
|
||||||
Name: "c1",
|
Name: "c1",
|
||||||
@ -3193,23 +3017,11 @@ func TestMarkPodProposedForResize(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Status: api.PodStatus{
|
expectProposedResize: false,
|
||||||
ContainerStatuses: []api.ContainerStatus{
|
|
||||||
{
|
|
||||||
Name: "c1",
|
|
||||||
Image: "image",
|
|
||||||
AllocatedResources: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "c2",
|
desc: "containers reordered",
|
||||||
Image: "image",
|
newPodSpec: api.PodSpec{
|
||||||
AllocatedResources: api.ResourceList{api.ResourceCPU: resource.MustParse("200m")},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
expectedPod: &api.Pod{
|
|
||||||
Spec: api.PodSpec{
|
|
||||||
Containers: []api.Container{
|
Containers: []api.Container{
|
||||||
{
|
{
|
||||||
Name: "c1",
|
Name: "c1",
|
||||||
@ -3219,26 +3031,52 @@ func TestMarkPodProposedForResize(t *testing.T) {
|
|||||||
Limits: api.ResourceList{api.ResourceCPU: resource.MustParse("200m")},
|
Limits: api.ResourceList{api.ResourceCPU: resource.MustParse("200m")},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "c2",
|
||||||
|
Image: "image",
|
||||||
|
Resources: api.ResourceRequirements{
|
||||||
|
Requests: api.ResourceList{api.ResourceCPU: resource.MustParse("300m")},
|
||||||
|
Limits: api.ResourceList{api.ResourceCPU: resource.MustParse("400m")},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
oldPodSpec: api.PodSpec{
|
||||||
|
Containers: []api.Container{
|
||||||
|
{
|
||||||
|
Name: "c2",
|
||||||
|
Image: "image",
|
||||||
|
Resources: api.ResourceRequirements{
|
||||||
|
Requests: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
||||||
|
Limits: api.ResourceList{api.ResourceCPU: resource.MustParse("200m")},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Status: api.PodStatus{
|
|
||||||
ContainerStatuses: []api.ContainerStatus{
|
|
||||||
{
|
{
|
||||||
Name: "c1",
|
Name: "c1",
|
||||||
Image: "image",
|
Image: "image",
|
||||||
AllocatedResources: api.ResourceList{api.ResourceCPU: resource.MustParse("100m")},
|
Resources: api.ResourceRequirements{
|
||||||
|
Requests: api.ResourceList{api.ResourceCPU: resource.MustParse("300m")},
|
||||||
|
Limits: api.ResourceList{api.ResourceCPU: resource.MustParse("400m")},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
expectProposedResize: false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tc := range testCases {
|
for _, tc := range testCases {
|
||||||
t.Run(tc.desc, func(t *testing.T) {
|
t.Run(tc.desc, func(t *testing.T) {
|
||||||
MarkPodProposedForResize(tc.oldPod, tc.newPod)
|
newPod := &api.Pod{Spec: tc.newPodSpec}
|
||||||
if diff := cmp.Diff(tc.expectedPod, tc.newPod); diff != "" {
|
newPodUnchanged := newPod.DeepCopy()
|
||||||
t.Errorf("unexpected pod spec (-want, +got):\n%s", diff)
|
oldPod := &api.Pod{Spec: tc.oldPodSpec}
|
||||||
|
MarkPodProposedForResize(oldPod, newPod)
|
||||||
|
if tc.expectProposedResize {
|
||||||
|
assert.Equal(t, api.PodResizeStatusProposed, newPod.Status.Resize)
|
||||||
|
} else {
|
||||||
|
assert.Equal(t, api.PodResizeStatus(""), newPod.Status.Resize)
|
||||||
}
|
}
|
||||||
|
newPod.Status.Resize = newPodUnchanged.Status.Resize // Only field that might have changed.
|
||||||
|
assert.Equal(t, newPodUnchanged, newPod, "No fields other than .status.resize should be modified")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user