From 5cf0d3c889ad8cc45965819a18393573e80a49b1 Mon Sep 17 00:00:00 2001 From: sbhat14 Date: Thu, 7 Nov 2024 14:19:31 -0800 Subject: [PATCH] fix: retry Signed-off-by: sbhat14 --- pkg/kube/structured/structured.go | 30 ++++++++++++++++---------- pkg/kube/structured/structured_test.go | 10 +++++++++ 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/pkg/kube/structured/structured.go b/pkg/kube/structured/structured.go index 765f384..a6e7069 100644 --- a/pkg/kube/structured/structured.go +++ b/pkg/kube/structured/structured.go @@ -20,6 +20,7 @@ import ( "net/http" "os" "strconv" + "strings" "time" log "github.com/sirupsen/logrus" @@ -236,17 +237,24 @@ func PersistentVolExists(kubeClientset kubernetes.Interface, name, expectedPhase } func PersistentVolClaimExists(kubeClientset kubernetes.Interface, name, expectedPhase string, namespace string) error { - vol, err := util.RetryOnError(&util.DefaultRetry, util.IsRetriable, func() (interface{}, error) { - return GetPersistentVolumeClaim(kubeClientset, name, namespace) - }) - if err != nil { - return err - } - phase := string(vol.(*corev1.PersistentVolumeClaim).Status.Phase) - if phase != expectedPhase { - return fmt.Errorf("persistentvolumeclaim had unexpected phase %v, expected phase %v", phase, expectedPhase) - } - return nil + _, err := util.RetryOnError( + &util.DefaultRetry, + func(err error) bool { + msg := fmt.Sprintf("persistentvolumeclaim had unexpected phase") + return util.IsRetriable(err) || strings.Contains(err.Error(), msg) + }, + func() (interface{}, error) { + vol, err := GetPersistentVolumeClaim(kubeClientset, name, namespace) + if err != nil { + return nil, err + } + phase := string(vol.Status.Phase) + if phase != expectedPhase { + return nil, fmt.Errorf("persistentvolumeclaim had unexpected phase %v, expected phase %v", phase, expectedPhase) + } + return nil, nil + }) + return err } func ValidatePrometheusVolumeClaimTemplatesName(kubeClientset kubernetes.Interface, statefulsetName, namespace, volumeClaimTemplatesName string) error { diff --git a/pkg/kube/structured/structured_test.go b/pkg/kube/structured/structured_test.go index 13137bf..98adfe6 100644 --- a/pkg/kube/structured/structured_test.go +++ b/pkg/kube/structured/structured_test.go @@ -505,6 +505,16 @@ func TestPersistentVolClaimExists(t *testing.T) { }, wantErr: false, }, + { + name: "pvc found with wrong phase", + args: args{ + kubeClientset: fake.NewSimpleClientset(getResource(t, persistentVolumeClaimType, persistentvolumeClaimName)), + name: persistentvolumeClaimName, + namespace: "", + expectedPhase: "Released", + }, + wantErr: true, + }, { name: "pvc not found Test", args: args{