add RetryOnConflict to pod status updates

This commit is contained in:
Matthias Bertschy 2021-07-02 09:20:28 +02:00
parent 8ac5d4d6a9
commit b1f34ea205

View File

@ -22,6 +22,7 @@ import (
"encoding/json"
"fmt"
"io"
"k8s.io/client-go/util/retry"
"runtime/debug"
"strconv"
"strings"
@ -948,6 +949,10 @@ var _ = SIGDescribe("Pods", func() {
framework.Logf("Observed event: %+v", event.Object)
return false, nil
})
if err != nil {
p, _ := f.ClientSet.CoreV1().Pods(testNamespaceName).Get(context.TODO(), testPodName, metav1.GetOptions{})
framework.Logf("Pod: %+v", p)
}
framework.ExpectNoError(err, "failed to see Pod %v in namespace %v running", testPod.ObjectMeta.Name, testNamespaceName)
ginkgo.By("patching the Pod with a new Label and updated data")
@ -989,32 +994,35 @@ var _ = SIGDescribe("Pods", func() {
framework.ExpectEqual(pod.ObjectMeta.Labels["test-pod"], "patched", "failed to patch Pod - missing label")
framework.ExpectEqual(pod.Spec.Containers[0].Image, testPodImage2, "failed to patch Pod - wrong image")
ginkgo.By("getting the PodStatus")
podStatusUnstructured, err := dc.Resource(podResource).Namespace(testNamespaceName).Get(context.TODO(), testPodName, metav1.GetOptions{}, "status")
framework.ExpectNoError(err, "failed to fetch PodStatus of Pod %s in namespace %s", testPodName, testNamespaceName)
podStatusBytes, err := json.Marshal(podStatusUnstructured)
framework.ExpectNoError(err, "failed to marshal unstructured response")
var podStatus v1.Pod
err = json.Unmarshal(podStatusBytes, &podStatus)
framework.ExpectNoError(err, "failed to unmarshal JSON bytes to a Pod object type")
ginkgo.By("replacing the Pod's status Ready condition to False")
podStatusUpdated := podStatus
podStatusFieldPatchCount := 0
podStatusFieldPatchCountTotal := 2
for pos, cond := range podStatusUpdated.Status.Conditions {
if (cond.Type == v1.PodReady && cond.Status == v1.ConditionTrue) || (cond.Type == v1.ContainersReady && cond.Status == v1.ConditionTrue) {
podStatusUpdated.Status.Conditions[pos].Status = v1.ConditionFalse
podStatusFieldPatchCount++
var podStatusUpdate *v1.Pod
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
podStatusUnstructured, err := dc.Resource(podResource).Namespace(testNamespaceName).Get(context.TODO(), testPodName, metav1.GetOptions{}, "status")
framework.ExpectNoError(err, "failed to fetch PodStatus of Pod %s in namespace %s", testPodName, testNamespaceName)
podStatusBytes, err := json.Marshal(podStatusUnstructured)
framework.ExpectNoError(err, "failed to marshal unstructured response")
var podStatus v1.Pod
err = json.Unmarshal(podStatusBytes, &podStatus)
framework.ExpectNoError(err, "failed to unmarshal JSON bytes to a Pod object type")
podStatusUpdated := podStatus
podStatusFieldPatchCount := 0
podStatusFieldPatchCountTotal := 2
for pos, cond := range podStatusUpdated.Status.Conditions {
if (cond.Type == v1.PodReady && cond.Status == v1.ConditionTrue) || (cond.Type == v1.ContainersReady && cond.Status == v1.ConditionTrue) {
podStatusUpdated.Status.Conditions[pos].Status = v1.ConditionFalse
podStatusFieldPatchCount++
}
}
}
framework.ExpectEqual(podStatusFieldPatchCount, podStatusFieldPatchCountTotal, "failed to patch all relevant Pod conditions")
podStatusUpdate, err := f.ClientSet.CoreV1().Pods(testNamespaceName).UpdateStatus(context.TODO(), &podStatusUpdated, metav1.UpdateOptions{})
framework.ExpectEqual(podStatusFieldPatchCount, podStatusFieldPatchCountTotal, "failed to patch all relevant Pod conditions")
podStatusUpdate, err = f.ClientSet.CoreV1().Pods(testNamespaceName).UpdateStatus(context.TODO(), &podStatusUpdated, metav1.UpdateOptions{})
return err
})
framework.ExpectNoError(err, "failed to update PodStatus of Pod %s in namespace %s", testPodName, testNamespaceName)
ginkgo.By("check the Pod again to ensure its Ready conditions are False")
podStatusFieldPatchCount = 0
podStatusFieldPatchCountTotal = 2
podStatusFieldPatchCount := 0
podStatusFieldPatchCountTotal := 2
for _, cond := range podStatusUpdate.Status.Conditions {
if (cond.Type == v1.PodReady && cond.Status == v1.ConditionFalse) || (cond.Type == v1.ContainersReady && cond.Status == v1.ConditionFalse) {
podStatusFieldPatchCount++