Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit 7c865cb

Browse files
authored
tests/framework: optionally retry on pod creation failure (#4091)
Optionally retries on app pod creation failure when the webhook configuration is not processed by the API server on time. Potential fix for #3973 Signed-off-by: Shashank Ram <[email protected]>
1 parent 5c491ac commit 7c865cb

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

tests/framework/common.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ func registerFlags(td *OsmTestData) {
150150
flag.BoolVar(&td.EnableNsMetricTag, "EnableMetricsTag", true, "Enable tagging Namespaces for metrics collection")
151151
flag.BoolVar(&td.DeployOnOpenShift, "deployOnOpenShift", false, "Configure tests to run on OpenShift")
152152
flag.BoolVar(&td.DeployOnWindowsWorkers, "deployOnWindowsWorkers", false, "Configure tests to run on Windows workers")
153+
flag.BoolVar(&td.RetryAppPodCreation, "retryAppPodCreation", true, "Retry app pod creation on error")
153154
}
154155

155156
// ValidateStringParams validates input string parameters are valid

tests/framework/common_apps.go

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ const (
5858

5959
// CPUPanel is the ID of the CPU panel on OSM's MeshDetails dashboard
6060
CPUPanel int = 14
61+
62+
// maxPodCreationRetries determines the max number of retries for creating
63+
// a Pod (including via a Deployment) upon failure
64+
maxPodCreationRetries = 2
65+
66+
// delayIntervalForPodCreationRetries
67+
delayIntervalForPodCreationRetries = 5 * time.Second
6168
)
6269

6370
var (
@@ -105,24 +112,49 @@ func (td *OsmTestData) createRoleBinding(ns string, roleBinding *rbacv1.RoleBind
105112
return rb, nil
106113
}
107114

115+
func (td *OsmTestData) getMaxPodCreationRetries() int {
116+
if td.RetryAppPodCreation {
117+
return maxPodCreationRetries
118+
}
119+
return 1
120+
}
121+
108122
// CreatePod is a wrapper to create a pod
109123
func (td *OsmTestData) CreatePod(ns string, pod corev1.Pod) (*corev1.Pod, error) {
110-
podRet, err := td.Client.CoreV1().Pods(ns).Create(context.Background(), &pod, metav1.CreateOptions{})
111-
if err != nil {
112-
err := fmt.Errorf("Could not create Pod: %v", err)
113-
return nil, err
124+
maxRetries := td.getMaxPodCreationRetries()
125+
126+
for i := 1; i <= maxRetries; i++ {
127+
if i > 1 {
128+
// Sleep before next retry
129+
time.Sleep(delayIntervalForPodCreationRetries)
130+
}
131+
podRet, err := td.Client.CoreV1().Pods(ns).Create(context.Background(), &pod, metav1.CreateOptions{})
132+
if err != nil {
133+
td.T.Logf("Could not create Pod in attempt %d due to error: %v", i, err)
134+
continue
135+
}
136+
return podRet, nil
114137
}
115-
return podRet, nil
138+
return nil, errors.Errorf("Error creating pod in namespace %s after %d attempts", ns, maxRetries)
116139
}
117140

118141
// CreateDeployment is a wrapper to create a deployment
119142
func (td *OsmTestData) CreateDeployment(ns string, deployment appsv1.Deployment) (*appsv1.Deployment, error) {
120-
deploymentRet, err := td.Client.AppsV1().Deployments(ns).Create(context.Background(), &deployment, metav1.CreateOptions{})
121-
if err != nil {
122-
err := fmt.Errorf("Could not create Deployment: %v", err)
123-
return nil, err
143+
maxRetries := td.getMaxPodCreationRetries()
144+
145+
for i := 1; i <= maxRetries; i++ {
146+
if i > 1 {
147+
// Sleep before next retry
148+
time.Sleep(delayIntervalForPodCreationRetries)
149+
}
150+
deploymentRet, err := td.Client.AppsV1().Deployments(ns).Create(context.Background(), &deployment, metav1.CreateOptions{})
151+
if err != nil {
152+
td.T.Logf("Could not create Deployment in attempt %d due to error: %v", i, err)
153+
continue
154+
}
155+
return deploymentRet, nil
124156
}
125-
return deploymentRet, nil
157+
return nil, errors.Errorf("Error creating Deployment in namespace %s after %d attempts", ns, maxRetries)
126158
}
127159

128160
// CreateService is a wrapper to create a service

tests/framework/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ type OsmTestData struct {
7474

7575
DeployOnOpenShift bool // Determines whether to configure tests for OpenShift
7676
DeployOnWindowsWorkers bool // Determines whether to configure tests to run on Windows workers
77+
78+
RetryAppPodCreation bool // Whether to retry app pod creation due to issue #3973
7779
}
7880

7981
// InstallOSMOpts describes install options for OSM

0 commit comments

Comments
 (0)