Skip to content

Commit 9b2772a

Browse files
authoredJan 20, 2025··
feat: don't use time.Sleep and allow context cancellation (#1269)
1 parent 0ae8dee commit 9b2772a

File tree

6 files changed

+64
-13
lines changed

6 files changed

+64
-13
lines changed
 

‎internal/utils/cluster_init.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ const (
2828
// regardless of which cluster type (e.g. kind, gke). This includes the creation
2929
// of some special administrative namespaces and service accounts.
3030
func ClusterInitHooks(ctx context.Context, cluster clusters.Cluster) error {
31+
const (
32+
serviceAccountWaitTime = time.Second
33+
)
3134
// create the admin namespace if it doesn't already exist
3235
namespace, err := cluster.Client().CoreV1().Namespaces().Create(ctx, &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: AdminNamespace}}, metav1.CreateOptions{})
3336
if err != nil {
@@ -39,6 +42,7 @@ func ClusterInitHooks(ctx context.Context, cluster clusters.Cluster) error {
3942
// wait for the default service account to be available
4043
var defaultSAFound bool
4144
var defaultSA *corev1.ServiceAccount
45+
4246
for !defaultSAFound {
4347
select {
4448
case <-ctx.Done():
@@ -47,8 +51,12 @@ func ClusterInitHooks(ctx context.Context, cluster clusters.Cluster) error {
4751
defaultSA, err = cluster.Client().CoreV1().ServiceAccounts(namespace.Name).Get(ctx, "default", metav1.GetOptions{})
4852
if err != nil {
4953
if errors.IsNotFound(err) {
50-
time.Sleep(time.Second)
51-
continue // try again if its not there yet
54+
select {
55+
case <-time.After(serviceAccountWaitTime):
56+
continue // try again if its not there yet
57+
case <-ctx.Done():
58+
return fmt.Errorf("context completed before cluster init hooks could finish: %w", ctx.Err())
59+
}
5260
}
5361
return err // don't tolerate any errors except 404
5462
}

‎pkg/clusters/addons/istio/addon.go

+26-7
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ func (a *Addon) Version() semver.Version {
7474
// EnableMeshForNamespace will add the "istio-injection=enabled" label to the provided namespace
7575
// by name which will indicate to Istio to inject sidecard pods to add it to the mesh network.
7676
func (a *Addon) EnableMeshForNamespace(ctx context.Context, cluster clusters.Cluster, name string) error {
77+
const (
78+
namspaceWaitTime = time.Second
79+
)
80+
7781
for {
7882
select {
7983
case <-ctx.Done():
@@ -89,8 +93,12 @@ func (a *Addon) EnableMeshForNamespace(ctx context.Context, cluster clusters.Clu
8993
if errors.IsConflict(err) {
9094
// if there's a conflict then an update happened since we pulled the namespace,
9195
// simply pull and try again.
92-
time.Sleep(time.Second)
93-
continue
96+
select {
97+
case <-time.After(namspaceWaitTime):
98+
continue // try again if its not there yet
99+
case <-ctx.Done():
100+
continue // this will return an error in the next iteration
101+
}
94102
}
95103
return fmt.Errorf("could not enable mesh for namespace %s: %w", name, err)
96104
}
@@ -141,6 +149,8 @@ func (a *Addon) Deploy(ctx context.Context, cluster clusters.Cluster) error {
141149
return err
142150
}
143151

152+
const jobWaitTime = time.Second
153+
144154
// wait for the job to complete
145155
var deployJobCompleted bool
146156
for !deployJobCompleted {
@@ -155,7 +165,12 @@ func (a *Addon) Deploy(ctx context.Context, cluster clusters.Cluster) error {
155165
if a.istioDeployJob.Status.Succeeded > 0 {
156166
deployJobCompleted = true
157167
} else {
158-
time.Sleep(time.Second)
168+
select {
169+
case <-time.After(jobWaitTime):
170+
continue
171+
case <-ctx.Done():
172+
continue // this will return an error in the next iteration
173+
}
159174
}
160175
}
161176
}
@@ -302,8 +317,8 @@ func (a *Addon) deployExtras(ctx context.Context, cluster clusters.Cluster) erro
302317
}
303318

304319
const (
305-
defaultRetries = 3
306-
defaultRetryWaitSeconds = 3
320+
defaultRetries = 3
321+
defaultRetryWaitTime = 3 * time.Second
307322
)
308323

309324
// retryKubectlApply retries a command multiple times with a limit, and is particularly
@@ -319,8 +334,12 @@ func retryKubectlApply(ctx context.Context, args ...string) (err error) {
319334
if err = cmd.Run(); err == nil {
320335
break
321336
}
322-
time.Sleep(time.Second * defaultRetryWaitSeconds)
323-
count--
337+
select {
338+
case <-ctx.Done():
339+
continue
340+
case <-time.After(defaultRetryWaitTime):
341+
count--
342+
}
324343
}
325344

326345
if err != nil {

‎pkg/clusters/addons/knative/knative.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ func deleteKnative(ctx context.Context, cluster clusters.Cluster, version string
214214
}
215215
}
216216

217+
const namespaceWaitTime = time.Second
218+
217219
// wait for the namespace to tear down
218220
for {
219221
select {
@@ -229,7 +231,12 @@ func deleteKnative(ctx context.Context, cluster clusters.Cluster, version string
229231
}
230232
return err
231233
}
232-
time.Sleep(time.Second)
234+
235+
select {
236+
case <-ctx.Done():
237+
continue // this will return an error in the next iteration
238+
case <-time.After(namespaceWaitTime):
239+
}
233240
}
234241
}
235242
}

‎pkg/clusters/addons/kuma/addon.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ func (a *Addon) Version() (v semver.Version, ok bool) {
7979
// EnableMeshForNamespace will add the "kuma.io/sidecar-injection: enabled" label to the provided namespace,
8080
// enabling sidecar injections fo all Pods in the namespace
8181
func EnableMeshForNamespace(ctx context.Context, cluster clusters.Cluster, name string) error {
82+
const namespaceWaitTime = time.Second
83+
8284
for {
8385
select {
8486
case <-ctx.Done():
@@ -94,7 +96,11 @@ func EnableMeshForNamespace(ctx context.Context, cluster clusters.Cluster, name
9496
if errors.IsConflict(err) {
9597
// if there's a conflict then an update happened since we pulled the namespace,
9698
// simply pull and try again.
97-
time.Sleep(time.Second)
99+
select {
100+
case <-ctx.Done():
101+
continue // this will return an error in the next iteration
102+
case <-time.After(namespaceWaitTime):
103+
}
98104
continue
99105
}
100106
return fmt.Errorf("could not enable mesh for namespace %s: %w", name, err)

‎pkg/clusters/types/gke/builder.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,12 @@ func (b *Builder) Build(ctx context.Context) (clusters.Cluster, error) {
232232
clusterReady = true
233233
break
234234
}
235-
time.Sleep(waitForClusterTick)
235+
236+
select {
237+
case <-ctx.Done():
238+
continue // this will return an error in the next iteration
239+
case <-time.After(waitForClusterTick):
240+
}
236241
}
237242
}
238243

‎pkg/environments/implementation.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,13 @@ func (env *environment) WaitForReady(ctx context.Context) chan error {
123123
hung.Stop()
124124
return
125125
}
126-
time.Sleep(objectWaitSleepTime)
126+
// Wait before retry to prevent spamming env with readiness check.
127+
select {
128+
case <-time.After(objectWaitSleepTime):
129+
case <-ctx.Done():
130+
hung.Stop()
131+
return
132+
}
127133
}
128134
}
129135
}()

0 commit comments

Comments
 (0)