resolve merge conflicts

This commit is contained in:
vivzbansal
2024-11-06 21:25:05 +00:00
parent e07aeb7c8b
commit d1fac494f4
24 changed files with 1652 additions and 216 deletions

View File

@@ -968,6 +968,131 @@ func doPodResizeTests() {
},
},
},
{
name: "Guaranteed QoS pod, one restartable init container - increase CPU & memory",
containers: []e2epod.ResizableContainerInfo{
{
Name: "c1",
Resources: &e2epod.ContainerResources{CPUReq: "100m", CPULim: "100m", MemReq: "200Mi", MemLim: "200Mi"},
CPUPolicy: &noRestart,
MemPolicy: &noRestart,
},
{
Name: "c1-init",
Resources: &e2epod.ContainerResources{CPUReq: "100m", CPULim: "100m", MemReq: "200Mi", MemLim: "200Mi"},
CPUPolicy: &noRestart,
MemPolicy: &noRestart,
IsRestartableInitCtr: true,
},
},
patchString: `{"spec":{"initcontainers":[
{"name":"c1-init", "resources":{"requests":{"cpu":"200m","memory":"400Mi"},"limits":{"cpu":"200m","memory":"400Mi"}}}
]}}`,
expected: []e2epod.ResizableContainerInfo{
{
Name: "c1",
Resources: &e2epod.ContainerResources{CPUReq: "100m", CPULim: "100m", MemReq: "200Mi", MemLim: "200Mi"},
CPUPolicy: &noRestart,
MemPolicy: &noRestart,
},
{
Name: "c1-init",
Resources: &e2epod.ContainerResources{CPUReq: "200m", CPULim: "200m", MemReq: "400Mi", MemLim: "400Mi"},
CPUPolicy: &noRestart,
MemPolicy: &noRestart,
IsRestartableInitCtr: true,
},
},
},
{
name: "Guaranteed QoS pod, one restartable init container - decrease CPU & memory",
containers: []e2epod.ResizableContainerInfo{
{
Name: "c1",
Resources: &e2epod.ContainerResources{CPUReq: "300m", CPULim: "300m", MemReq: "500Mi", MemLim: "500Mi"},
CPUPolicy: &noRestart,
MemPolicy: &noRestart,
},
{
Name: "c1-init",
Resources: &e2epod.ContainerResources{CPUReq: "300m", CPULim: "300m", MemReq: "500Mi", MemLim: "500Mi"},
CPUPolicy: &noRestart,
MemPolicy: &noRestart,
IsRestartableInitCtr: true,
},
},
patchString: `{"spec":{"initcontainers":[
{"name":"c1-init", "resources":{"requests":{"cpu":"100m","memory":"250Mi"},"limits":{"cpu":"100m","memory":"250Mi"}}}
]}}`,
expected: []e2epod.ResizableContainerInfo{
{
Name: "c1",
Resources: &e2epod.ContainerResources{CPUReq: "300m", CPULim: "300m", MemReq: "500Mi", MemLim: "500Mi"},
CPUPolicy: &noRestart,
MemPolicy: &noRestart,
},
{
Name: "c1-init",
Resources: &e2epod.ContainerResources{CPUReq: "100m", CPULim: "100m", MemReq: "250Mi", MemLim: "250Mi"},
CPUPolicy: &noRestart,
MemPolicy: &noRestart,
},
},
},
{
name: "Guaranteed QoS pod, one restartable init container - increase CPU & decrease memory",
containers: []e2epod.ResizableContainerInfo{
{
Name: "c1",
Resources: &e2epod.ContainerResources{CPUReq: "100m", CPULim: "100m", MemReq: "200Mi", MemLim: "200Mi"},
},
{
Name: "c1-init",
Resources: &e2epod.ContainerResources{CPUReq: "100m", CPULim: "100m", MemReq: "200Mi", MemLim: "200Mi"},
IsRestartableInitCtr: true,
},
},
patchString: `{"spec":{"initcontainers":[
{"name":"c1-init", "resources":{"requests":{"cpu":"200m","memory":"100Mi"},"limits":{"cpu":"200m","memory":"100Mi"}}}
]}}`,
expected: []e2epod.ResizableContainerInfo{
{
Name: "c1",
Resources: &e2epod.ContainerResources{CPUReq: "100m", CPULim: "100m", MemReq: "200Mi", MemLim: "200Mi"},
},
{
Name: "c1-init",
Resources: &e2epod.ContainerResources{CPUReq: "200m", CPULim: "200m", MemReq: "100Mi", MemLim: "100Mi"},
},
},
},
{
name: "Guaranteed QoS pod, one restartable init container - decrease CPU & increase memory",
containers: []e2epod.ResizableContainerInfo{
{
Name: "c1",
Resources: &e2epod.ContainerResources{CPUReq: "100m", CPULim: "100m", MemReq: "200Mi", MemLim: "200Mi"},
},
{
Name: "c1-init",
Resources: &e2epod.ContainerResources{CPUReq: "100m", CPULim: "100m", MemReq: "200Mi", MemLim: "200Mi"},
IsRestartableInitCtr: true,
},
},
patchString: `{"spec":{"initcontainers":[
{"name":"c1-init", "resources":{"requests":{"cpu":"50m","memory":"300Mi"},"limits":{"cpu":"50m","memory":"300Mi"}}}
]}}`,
expected: []e2epod.ResizableContainerInfo{
{
Name: "c1",
Resources: &e2epod.ContainerResources{CPUReq: "100m", CPULim: "100m", MemReq: "200Mi", MemLim: "200Mi"},
},
{
Name: "c1-init",
Resources: &e2epod.ContainerResources{CPUReq: "50m", CPULim: "50m", MemReq: "300Mi", MemLim: "300Mi"},
},
},
},
}
for idx := range tests {
@@ -1003,6 +1128,7 @@ func doPodResizeTests() {
ginkgo.By("verifying initial pod resources are as expected")
e2epod.VerifyPodResources(newPod, tc.containers)
ginkgo.By("verifying initial pod resize policy is as expected")
e2epod.VerifyPodResizePolicy(newPod, tc.containers)

View File

@@ -271,6 +271,21 @@ func (c *PodClient) mungeSpec(pod *v1.Pod) {
// been prepulled.
c.ImagePullPolicy = v1.PullNever
}
for i := range pod.Spec.InitContainers {
c := &pod.Spec.InitContainers[i]
if c.ImagePullPolicy == v1.PullAlways {
// If the image pull policy is PullAlways, the image doesn't need to be in
// the allow list or pre-pulled, because the image is expected to be pulled
// in the test anyway.
continue
}
// If the image policy is not PullAlways, the image must be in the pre-pull list and
// pre-pulled.
gomega.Expect(ImagePrePullList.Has(c.Image)).To(gomega.BeTrueBecause("Image %q is not in the pre-pull list, consider adding it to PrePulledImages in test/e2e/common/util.go or NodePrePullImageList in test/e2e_node/image_list.go", c.Image))
// Do not pull images during the tests because the images in pre-pull list should have
// been prepulled.
c.ImagePullPolicy = v1.PullNever
}
}
// WaitForSuccess waits for pod to succeed.

View File

@@ -98,11 +98,12 @@ func (cr *ContainerResources) ResourceRequirements() *v1.ResourceRequirements {
}
type ResizableContainerInfo struct {
Name string
Resources *ContainerResources
CPUPolicy *v1.ResourceResizeRestartPolicy
MemPolicy *v1.ResourceResizeRestartPolicy
RestartCount int32
Name string
Resources *ContainerResources
CPUPolicy *v1.ResourceResizeRestartPolicy
MemPolicy *v1.ResourceResizeRestartPolicy
RestartCount int32
IsRestartableInitCtr bool
}
type containerPatch struct {
@@ -123,7 +124,8 @@ type containerPatch struct {
type patchSpec struct {
Spec struct {
Containers []containerPatch `json:"containers"`
Containers []containerPatch `json:"containers"`
InitContainers []containerPatch `json:"initcontainers"`
} `json:"spec"`
}
@@ -160,10 +162,15 @@ func makeResizableContainer(tcInfo ResizableContainerInfo) v1.Container {
func MakePodWithResizableContainers(ns, name, timeStamp string, tcInfo []ResizableContainerInfo) *v1.Pod {
var testContainers []v1.Container
var testInitContainers []v1.Container
for _, ci := range tcInfo {
tc := makeResizableContainer(ci)
testContainers = append(testContainers, tc)
tc, _ := makeResizableContainer(ci)
if ci.IsRestartableInitCtr {
testInitContainers = append(testInitContainers, tc)
} else {
testContainers = append(testContainers, tc)
}
}
pod := &v1.Pod{
ObjectMeta: metav1.ObjectMeta{
@@ -179,15 +186,36 @@ func MakePodWithResizableContainers(ns, name, timeStamp string, tcInfo []Resizab
RestartPolicy: v1.RestartPolicyOnFailure,
},
}
if len(testInitContainers) > 0 {
pod.Spec.RestartPolicy = v1.RestartPolicyAlways
pod.Spec.InitContainers = testInitContainers
}
return pod
}
func VerifyPodResizePolicy(gotPod *v1.Pod, wantCtrs []ResizableContainerInfo) {
ginkgo.GinkgoHelper()
gomega.Expect(gotPod.Spec.Containers).To(gomega.HaveLen(len(wantCtrs)), "number of containers in pod spec should match")
for i, wantCtr := range wantCtrs {
gotCtr := &gotPod.Spec.Containers[i]
ctr := makeResizableContainer(wantCtr)
containers := gotPod.Spec.Containers
for _, c := range gotPod.Spec.InitContainers {
containers = append(containers, c)
}
gomega.Expect(containers).To(gomega.HaveLen(len(wantCtrs)), "number of containers in pod spec should match")
idx, initIdx := 0, 0
var gotCtr *v1.Container
for _, wantCtr := range wantCtrs {
if wantCtr.IsRestartableInitCtr {
gotCtr = &gotPod.Spec.InitContainers[initIdx]
initIdx += 1
} else {
gotCtr = &gotPod.Spec.Containers[idx]
idx += 1
}
ctr, _ := makeResizableContainer(wantCtr)
gomega.Expect(gotCtr.Name).To(gomega.Equal(ctr.Name))
gomega.Expect(gotCtr.ResizePolicy).To(gomega.Equal(ctr.ResizePolicy))
}
@@ -195,10 +223,24 @@ func VerifyPodResizePolicy(gotPod *v1.Pod, wantCtrs []ResizableContainerInfo) {
func VerifyPodResources(gotPod *v1.Pod, wantCtrs []ResizableContainerInfo) {
ginkgo.GinkgoHelper()
gomega.Expect(gotPod.Spec.Containers).To(gomega.HaveLen(len(wantCtrs)), "number of containers in pod spec should match")
for i, wantCtr := range wantCtrs {
gotCtr := &gotPod.Spec.Containers[i]
ctr := makeResizableContainer(wantCtr)
containers := gotPod.Spec.Containers
for _, c := range gotPod.Spec.InitContainers {
containers = append(containers, c)
}
gomega.Expect(containers).To(gomega.HaveLen(len(wantCtrs)), "number of containers in pod spec should match")
idx, initIdx := 0, 0
var gotCtr *v1.Container
for _, wantCtr := range wantCtrs {
if wantCtr.IsRestartableInitCtr {
gotCtr = &gotPod.Spec.InitContainers[initIdx]
initIdx += 1
} else {
gotCtr = &gotPod.Spec.Containers[idx]
idx += 1
}
ctr, _ := makeResizableContainer(wantCtr)
gomega.Expect(gotCtr.Name).To(gomega.Equal(ctr.Name))
gomega.Expect(gotCtr.Resources).To(gomega.Equal(ctr.Resources))
}
@@ -208,13 +250,25 @@ func VerifyPodStatusResources(gotPod *v1.Pod, wantCtrs []ResizableContainerInfo)
ginkgo.GinkgoHelper()
var errs []error
if len(gotPod.Status.ContainerStatuses) != len(wantCtrs) {
return fmt.Errorf("expectation length mismatch: got %d statuses, want %d",
len(gotPod.Status.ContainerStatuses), len(wantCtrs))
containerStatuses := gotPod.Status.ContainerStatuses
for _, cs := range gotPod.Status.InitContainerStatuses {
containerStatuses = append(containerStatuses, cs)
}
if len(containerStatuses) != len(wantCtrs) {
return fmt.Errorf("expectation length mismatch: got %d statuses, want %d",
len(containerStatuses), len(wantCtrs))
}
idx, initIdx := 0, 0
var gotCtrStatus *v1.ContainerStatus
for i, wantCtr := range wantCtrs {
gotCtrStatus := &gotPod.Status.ContainerStatuses[i]
if wantCtr.IsRestartableInitCtr {
gotCtrStatus = &gotPod.Status.InitContainerStatuses[initIdx]
initIdx += 1
} else {
gotCtrStatus = &gotPod.Status.ContainerStatuses[idx]
idx += 1
}
ctr := makeResizableContainer(wantCtr)
if gotCtrStatus.Name != ctr.Name {
errs = append(errs, fmt.Errorf("container status %d name %q != expected name %q", i, gotCtrStatus.Name, ctr.Name))
@@ -297,7 +351,11 @@ func verifyContainerRestarts(pod *v1.Pod, expectedContainers []ResizableContaine
}
errs := []error{}
for _, cs := range pod.Status.ContainerStatuses {
containerStatuses := pod.Status.ContainerStatuses
for _, cs := range pod.Status.InitContainerStatuses {
containerStatuses = append(containerStatuses, cs)
}
for _, cs := range containerStatuses {
expectedRestarts := expectContainerRestarts[cs.Name]
if cs.RestartCount != expectedRestarts {
errs = append(errs, fmt.Errorf("unexpected number of restarts for container %s: got %d, want %d", cs.Name, cs.RestartCount, expectedRestarts))
@@ -369,8 +427,11 @@ func ResizeContainerPatch(containers []ResizableContainerInfo) (string, error) {
cPatch.Resources.Requests.Memory = container.Resources.MemReq
cPatch.Resources.Limits.CPU = container.Resources.CPULim
cPatch.Resources.Limits.Memory = container.Resources.MemLim
patch.Spec.Containers = append(patch.Spec.Containers, cPatch)
if container.IsRestartableInitCtr {
patch.Spec.InitContainers = append(patch.Spec.InitContainers, cPatch)
} else {
patch.Spec.Containers = append(patch.Spec.Containers, cPatch)
}
}
patchBytes, err := json.Marshal(patch)