From 82324a7795d2802793c62600b36cced0d10d20fd Mon Sep 17 00:00:00 2001 From: Tomofumi Hayashi Date: Mon, 24 Jul 2023 20:20:16 +0900 Subject: [PATCH] This change introduces wait to generate config until API is ready --- cmd/multus-daemon/main.go | 17 +++++++++++++++++ pkg/server/server.go | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/cmd/multus-daemon/main.go b/cmd/multus-daemon/main.go index b65e6189a..be2c02988 100644 --- a/cmd/multus-daemon/main.go +++ b/cmd/multus-daemon/main.go @@ -25,6 +25,7 @@ import ( "os" "os/user" "path/filepath" + "time" utilruntime "k8s.io/apimachinery/pkg/util/runtime" utilwait "k8s.io/apimachinery/pkg/util/wait" @@ -74,6 +75,12 @@ func main() { os.Exit(1) } + // Wait until daemon ready + if waitUntilAPIReady(daemonConf.SocketDir) != nil { + logging.Panicf("failed to ready multus-daemon socket: %v", err) + os.Exit(1) + } + // Generate multus CNI config from current CNI config if multusConf.MultusConfigFile == "auto" { if multusConf.CNIVersion == "" { @@ -140,6 +147,16 @@ func main() { // never reached } +func waitUntilAPIReady(socketPath string) error { + apiReadyPollDuration := 100 * time.Millisecond + apiReadyPollTimeout := 1000 * time.Millisecond + + return utilwait.PollImmediate(apiReadyPollDuration, apiReadyPollTimeout, func() (bool, error) { + _, err := api.DoCNI(api.GetAPIEndpoint(api.MultusHealthAPIEndpoint), nil, api.SocketPath(socketPath)) + return err == nil, nil + }) +} + func startMultusDaemon(daemonConfig *srv.ControllerNetConf, stopCh chan struct{}, done chan struct{}) error { if user, err := user.Current(); err != nil || user.Uid != "0" { return fmt.Errorf("failed to run multus-daemon with root: %v, now running in uid: %s", err, user.Uid) diff --git a/pkg/server/server.go b/pkg/server/server.go index 23da971b3..4eb4e0770 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -225,7 +225,7 @@ func newCNIServer(rundir string, kubeClient *k8s.ClientInfo, exec invoke.Exec, s // handle for '/healthz' router.HandleFunc(api.MultusHealthAPIEndpoint, promhttp.InstrumentHandlerCounter(s.metrics.requestCounter.MustCurryWith(prometheus.Labels{"handler": api.MultusHealthAPIEndpoint}), http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.Method != http.MethodGet { + if r.Method != http.MethodGet && r.Method != http.MethodPost { http.Error(w, fmt.Sprintf("Method not allowed"), http.StatusMethodNotAllowed) return }