Skip to content

Commit

Permalink
Implement graceful shutdown. (#26)
Browse files Browse the repository at this point in the history
* Disable timeout to guarantee deletion flow succeeds before stop.

* Fix.
  • Loading branch information
Gerrit91 authored Sep 23, 2021
1 parent 65a6e65 commit 8671594
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
8 changes: 6 additions & 2 deletions controllers/duros_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,17 @@ func (r *DurosReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
}
} else {
// object is being deleted
// we don't pass the cancelled context here because then our deletion
// procedure will stop prematurely
deletionCtx := context.Background()

if containsString(duros.GetFinalizers(), DurosFinalizerName) {
if err := r.cleanupResources(ctx); err != nil {
if err := r.cleanupResources(deletionCtx); err != nil {
return requeue, err
}

controllerutil.RemoveFinalizer(duros, DurosFinalizerName)
if err := r.Update(ctx, duros); err != nil {
if err := r.Update(deletionCtx, duros); err != nil {
return requeue, err
}
}
Expand Down
15 changes: 9 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"flag"
"os"
"time"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand Down Expand Up @@ -111,13 +112,15 @@ func main() {

restConfig := ctrl.GetConfigOrDie()

disabledTimeout := time.Duration(-1) // wait for all runnables to finish before dying
mgr, err := ctrl.NewManager(restConfig, ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
LeaderElection: enableLeaderElection,
LeaderElectionID: "duros-controller-leader-election",
Namespace: namespace,
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
LeaderElection: enableLeaderElection,
LeaderElectionID: "duros-controller-leader-election",
Namespace: namespace,
GracefulShutdownTimeout: &disabledTimeout,
})
if err != nil {
setupLog.Error(err, "unable to start duros-controller")
Expand Down

0 comments on commit 8671594

Please sign in to comment.