Consistently handle component exit on shutdown

Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
This commit is contained in:
Brad Davidson 2024-01-25 00:44:12 +00:00 committed by Brad Davidson
parent 9e076db724
commit 753c00f30c
4 changed files with 52 additions and 10 deletions

View File

@ -99,10 +99,12 @@ func Run(ctx context.Context, cfg *config.Node) error {
cmd.Env = append(env, cenv...)
addDeathSig(cmd)
if err := cmd.Run(); err != nil {
err := cmd.Run()
if err != nil && !errors.Is(err, context.Canceled) {
logrus.Errorf("containerd exited: %s", err)
os.Exit(1)
}
os.Exit(1)
os.Exit(0)
}()
if err := cri.WaitForService(ctx, cfg.Containerd.Address, "containerd"); err != nil {

View File

@ -5,6 +5,7 @@ package cridockerd
import (
"context"
"errors"
"os"
"runtime/debug"
"strings"
@ -37,7 +38,12 @@ func Run(ctx context.Context, cfg *config.Node) error {
logrus.WithField("stack", string(debug.Stack())).Fatalf("cri-dockerd panic: %v", err)
}
}()
logrus.Fatalf("cri-dockerd exited: %v", command.ExecuteContext(ctx))
err := command.ExecuteContext(ctx)
if err != nil && !errors.Is(err, context.Canceled) {
logrus.Errorf("cri-dockerd exited: %v", err)
os.Exit(1)
}
os.Exit(0)
}()
return cri.WaitForService(ctx, cfg.CRIDockerd.Address, "cri-dockerd")

View File

@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net"
"os"
"path/filepath"
goruntime "runtime"
"strings"
@ -76,8 +77,10 @@ func Run(ctx context.Context, nodeConfig *config.Node, nodes typedcorev1.NodeInt
go func() {
err := flannel(ctx, nodeConfig.FlannelIface, nodeConfig.FlannelConfFile, nodeConfig.AgentConfig.KubeConfigKubelet, nodeConfig.FlannelIPv6Masq, netMode)
if err != nil && !errors.Is(err, context.Canceled) {
logrus.Fatalf("flannel exited: %v", err)
logrus.Errorf("flannel exited: %v", err)
os.Exit(1)
}
os.Exit(0)
}()
return nil

View File

@ -7,6 +7,7 @@ import (
"context"
"flag"
"net/http"
"os"
"runtime"
"runtime/debug"
"strconv"
@ -90,7 +91,12 @@ func (e *Embedded) Kubelet(ctx context.Context, args []string) error {
if err := util.WaitForAPIServerReady(ctx, e.nodeConfig.AgentConfig.KubeConfigKubelet, util.DefaultAPIServerReadyTimeout); err != nil {
logrus.Fatalf("Kubelet failed to wait for apiserver ready: %v", err)
}
logrus.Fatalf("kubelet exited: %v", command.ExecuteContext(ctx))
err := command.ExecuteContext(ctx)
if err != nil && !errors.Is(err, context.Canceled) {
logrus.Errorf("kubelet exited: %v", err)
os.Exit(1)
}
os.Exit(0)
}()
return nil
@ -106,7 +112,12 @@ func (e *Embedded) KubeProxy(ctx context.Context, args []string) error {
logrus.WithField("stack", string(debug.Stack())).Fatalf("kube-proxy panic: %v", err)
}
}()
logrus.Fatalf("kube-proxy exited: %v", command.ExecuteContext(ctx))
err := command.ExecuteContext(ctx)
if err != nil && !errors.Is(err, context.Canceled) {
logrus.Errorf("kube-proxy exited: %v", err)
os.Exit(1)
}
os.Exit(0)
}()
return nil
@ -128,7 +139,12 @@ func (*Embedded) APIServer(ctx context.Context, etcdReady <-chan struct{}, args
logrus.WithField("stack", string(debug.Stack())).Fatalf("apiserver panic: %v", err)
}
}()
logrus.Fatalf("apiserver exited: %v", command.ExecuteContext(ctx))
err := command.ExecuteContext(ctx)
if err != nil && !errors.Is(err, context.Canceled) {
logrus.Errorf("apiserver exited: %v", err)
os.Exit(1)
}
os.Exit(0)
}()
return nil
@ -157,7 +173,12 @@ func (e *Embedded) Scheduler(ctx context.Context, apiReady <-chan struct{}, args
logrus.WithField("stack", string(debug.Stack())).Fatalf("scheduler panic: %v", err)
}
}()
logrus.Fatalf("scheduler exited: %v", command.ExecuteContext(ctx))
err := command.ExecuteContext(ctx)
if err != nil && !errors.Is(err, context.Canceled) {
logrus.Errorf("scheduler exited: %v", err)
os.Exit(1)
}
os.Exit(0)
}()
return nil
@ -174,7 +195,12 @@ func (*Embedded) ControllerManager(ctx context.Context, apiReady <-chan struct{}
logrus.WithField("stack", string(debug.Stack())).Fatalf("controller-manager panic: %v", err)
}
}()
logrus.Fatalf("controller-manager exited: %v", command.ExecuteContext(ctx))
err := command.ExecuteContext(ctx)
if err != nil && !errors.Is(err, context.Canceled) {
logrus.Errorf("controller-manager exited: %v", err)
os.Exit(1)
}
os.Exit(0)
}()
return nil
@ -215,7 +241,12 @@ func (*Embedded) CloudControllerManager(ctx context.Context, ccmRBACReady <-chan
logrus.WithField("stack", string(debug.Stack())).Fatalf("cloud-controller-manager panic: %v", err)
}
}()
logrus.Errorf("cloud-controller-manager exited: %v", command.ExecuteContext(ctx))
err := command.ExecuteContext(ctx)
if err != nil && !errors.Is(err, context.Canceled) {
logrus.Errorf("cloud-controller-manager exited: %v", err)
os.Exit(1)
}
os.Exit(0)
}()
return nil