Skip to content

Commit

Permalink
Delay service readiness until after startuphooks have finished (k3s-i…
Browse files Browse the repository at this point in the history
…o#5649)

* Move startup hooks wg into a runtime pointer, check before notifying systemd
* Switch default systemd notification to server
* Add 1 sec delay to allow etcd to write to disk
Signed-off-by: Derek Nola <derek.nola@suse.com>
  • Loading branch information
dereknola committed Jun 15, 2022
1 parent 642402e commit 1caf32d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
10 changes: 8 additions & 2 deletions pkg/agent/run.go
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/rancher/k3s/pkg/nodeconfig"
"github.com/rancher/k3s/pkg/rootless"
"github.com/rancher/k3s/pkg/util"
"github.com/rancher/k3s/pkg/version"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
Expand Down Expand Up @@ -159,8 +160,13 @@ func run(ctx context.Context, cfg cmds.Agent, proxy proxy.Proxy) error {
}
}

os.Setenv("NOTIFY_SOCKET", notifySocket)
systemd.SdNotify(true, "READY=1\n")
// By default, the server is responsible for notifying systemd
// On agent-only nodes, the agent will notify systemd
if notifySocket != "" {
logrus.Info(version.Program + " agent is up and running")
os.Setenv("NOTIFY_SOCKET", notifySocket)
systemd.SdNotify(true, "READY=1\n")
}

<-ctx.Done()
return ctx.Err()
Expand Down
11 changes: 6 additions & 5 deletions pkg/cli/server/server.go
Expand Up @@ -419,6 +419,7 @@ func run(app *cli.Context, cfg *cmds.Server, leaderControllers server.CustomCont
logrus.Info("Starting " + version.Program + " " + app.App.Version)

notifySocket := os.Getenv("NOTIFY_SOCKET")
os.Unsetenv("NOTIFY_SOCKET")

ctx := signals.SetupSignalHandler(context.Background())

Expand All @@ -430,16 +431,16 @@ func run(app *cli.Context, cfg *cmds.Server, leaderControllers server.CustomCont
if !serverConfig.ControlConfig.DisableAPIServer {
<-serverConfig.ControlConfig.Runtime.APIServerReady
logrus.Info("Kube API server is now running")
} else {
serverConfig.ControlConfig.Runtime.StartupHooksWg.Wait()
}
if !serverConfig.ControlConfig.DisableETCD {
<-serverConfig.ControlConfig.Runtime.ETCDReady
logrus.Info("ETCD server is now running")
}

logrus.Info(version.Program + " is up and running")
if (cfg.DisableAgent || cfg.DisableAPIServer) && notifySocket != "" {
os.Setenv("NOTIFY_SOCKET", notifySocket)
systemd.SdNotify(true, "READY=1\n")
}
os.Setenv("NOTIFY_SOCKET", notifySocket)
systemd.SdNotify(true, "READY=1\n")
}()

ip := serverConfig.ControlConfig.BindAddress
Expand Down
2 changes: 2 additions & 0 deletions pkg/daemons/config/types.go
Expand Up @@ -8,6 +8,7 @@ import (
"net/http"
"sort"
"strings"
"sync"
"time"

"github.com/k3s-io/kine/pkg/endpoint"
Expand Down Expand Up @@ -217,6 +218,7 @@ type ControlRuntime struct {
APIServerReady <-chan struct{}
AgentReady <-chan struct{}
ETCDReady <-chan struct{}
StartupHooksWg *sync.WaitGroup
ClusterControllerStart func(ctx context.Context) error
LeaderElectedClusterControllerStart func(ctx context.Context) error

Expand Down
4 changes: 4 additions & 0 deletions pkg/server/secrets-encrypt.go
Expand Up @@ -187,6 +187,10 @@ func encryptionConfigHandler(ctx context.Context, server *config.Control) http.H
genErrorMessage(resp, http.StatusBadRequest, err)
return
}
// If a user kills the k3s server immediately after this call, we run into issues where the files
// have not yet been written. This sleep ensures that things have time to sync to disk before
// the request completes.
time.Sleep(1 * time.Second)
resp.WriteHeader(http.StatusOK)
})
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/server/server.go
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"
"time"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -70,6 +71,8 @@ func StartServer(ctx context.Context, config *Config, cfg *cmds.Server) error {
go startOnAPIServerReady(ctx, config)
}

config.ControlConfig.Runtime.StartupHooksWg = &sync.WaitGroup{}
config.ControlConfig.Runtime.StartupHooksWg.Add(len(config.StartupHooks))
for _, hook := range config.StartupHooks {
if err := hook(ctx, config.ControlConfig.Runtime.APIServerReady, config.ControlConfig.Runtime.KubeConfigAdmin); err != nil {
return errors.Wrap(err, "startup hook")
Expand Down

0 comments on commit 1caf32d

Please sign in to comment.