Skip to content

Commit

Permalink
NW-3975 | Added retry for watch pods (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexxed authored Nov 1, 2021
1 parent 4afd898 commit 079b09e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.14.1-alpine3.14 AS builder
FROM golang:1.17.2-alpine3.14 AS builder
RUN mkdir /build
WORKDIR /build
COPY . /build/
Expand Down
21 changes: 18 additions & 3 deletions cmd/kubexit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/client-go/util/retry"
)

func main() {
Expand Down Expand Up @@ -176,11 +177,21 @@ func waitForBirthDeps(birthDeps []string, namespace, podName string, timeout tim
defer stopPodWatcher()

log.Info("Watching pod updates...")
err := kubernetes.WatchPod(ctx, namespace, podName,
onReadyOfAll(birthDeps, stopPodWatcher),
err := retry.OnError(
retry.DefaultBackoff,
isRetryableError,
func() error {
watchErr := kubernetes.WatchPod(ctx, namespace, podName,
onReadyOfAll(birthDeps, stopPodWatcher),
)
if watchErr != nil {
return fmt.Errorf("failed to watch pod: %v", watchErr)
}
return nil
},
)
if err != nil {
return fmt.Errorf("failed to watch pod: %v", err)
return fmt.Errorf("retry watching pods failed: %v", err)
}

// Block until all birth deps are ready
Expand All @@ -197,6 +208,10 @@ func waitForBirthDeps(birthDeps []string, namespace, podName string, timeout tim
return nil
}

func isRetryableError(err error) bool {
return errors.Is(err, context.DeadlineExceeded)
}

// withCancelOnSignal calls cancel when one of the specified signals is recieved.
func withCancelOnSignal(ctx context.Context, signals ...os.Signal) context.Context {
ctx, cancel := context.WithCancel(ctx)
Expand Down
2 changes: 1 addition & 1 deletion pkg/supervisor/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func New(name string, args ...string) *Supervisor {
cmd.Stderr = os.Stderr
cmd.Env = os.Environ()
return &Supervisor{
cmd: cmd,
cmd: cmd,
shutdown: false,
}
}
Expand Down

0 comments on commit 079b09e

Please sign in to comment.