Skip to content

Commit 672f41c

Browse files
committed
reuse polling function
1 parent e5c0a59 commit 672f41c

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

pkg/oc/admin/diagnostics/diagnostics/cluster/app_create/setup_cleanup.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ import (
66
"time"
77

88
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
"k8s.io/apimachinery/pkg/util/wait"
910

1011
appscmd "github.com/openshift/origin/pkg/apps/cmd"
1112
"github.com/openshift/origin/pkg/cmd/server/bootstrappolicy"
1213
newproject "github.com/openshift/origin/pkg/oc/admin/project"
1314
)
1415

16+
const podGoneTimeout = 30 // seconds to wait for previous app pods to disappear
17+
1518
func (d *AppCreate) prepareForApp() bool {
1619
defer func() {
1720
d.result.PrepDuration = jsonDuration(time.Since(time.Time(d.result.BeginTime)))
@@ -101,17 +104,20 @@ func (d *AppCreate) cleanupApp() {
101104

102105
func (d *AppCreate) waitForPodGone() bool {
103106
d.out.Debug("DCluAC045", fmt.Sprintf("%s: Waiting to ensure any previous pod for '%s' is gone.", now(), d.appName))
104-
for timeout := 30; timeout > 0; timeout-- {
107+
err := wait.PollImmediate(time.Second, time.Duration(podGoneTimeout)*time.Second, func() (bool, error) {
105108
pods, err := d.KubeClient.Core().Pods(d.project).List(metav1.ListOptions{LabelSelector: d.labelSelector})
106-
if err != nil {
107-
d.out.Error("DCluAC046", err, fmt.Sprintf("%s: Error on checking for previous app pod:\n%v", now(), err))
108-
return false
109-
}
110-
if len(pods.Items) == 0 {
111-
return true
109+
if err == nil && len(pods.Items) == 0 {
110+
return true, nil
112111
}
113-
time.Sleep(time.Second)
112+
return false, err
113+
})
114+
switch err {
115+
case nil:
116+
return true
117+
case wait.ErrWaitTimeout:
118+
d.out.Error("DCluAC046", err, fmt.Sprintf("%s: Previous app pod still present after %ds", now(), podGoneTimeout))
119+
default:
120+
d.out.Error("DCluAC047", err, fmt.Sprintf("%s: Error while checking for previous app pod:\n%v", now(), err))
114121
}
115-
d.out.Error("DCluAC047", nil, fmt.Sprintf("%s: Previous app pod still present after 30s", now()))
116122
return false
117123
}

0 commit comments

Comments
 (0)