diff --git a/pkg/server/api/api.go b/pkg/server/api/api.go index b9228f0dc..995676c38 100644 --- a/pkg/server/api/api.go +++ b/pkg/server/api/api.go @@ -104,3 +104,12 @@ func WaitUntilAPIReady(socketPath string) error { return err == nil, nil }) } + +// CheckAPIReadyNow checks API readiness once +func CheckAPIReadyNow(socketPath string) error { + _, err := DoCNI(GetAPIEndpoint(MultusHealthAPIEndpoint), nil, SocketPath(socketPath)) + if err != nil { + return fmt.Errorf("CheckAPIReadyNow: Daemon not reachable over socketfile: %v", err) + } + return nil +} diff --git a/pkg/server/api/shim.go b/pkg/server/api/shim.go index 168404ea4..08be996e7 100644 --- a/pkg/server/api/shim.go +++ b/pkg/server/api/shim.go @@ -42,7 +42,7 @@ type ShimNetConf struct { // CmdAdd implements the CNI spec ADD command handler func CmdAdd(args *skel.CmdArgs) error { - response, cniVersion, err := postRequest(args) + response, cniVersion, err := postRequest(args, true) if err != nil { return logging.Errorf("CmdAdd (shim): %v", err) } @@ -53,7 +53,7 @@ func CmdAdd(args *skel.CmdArgs) error { // CmdCheck implements the CNI spec CHECK command handler func CmdCheck(args *skel.CmdArgs) error { - _, _, err := postRequest(args) + _, _, err := postRequest(args, true) if err != nil { return logging.Errorf("CmdCheck (shim): %v", err) } @@ -63,7 +63,7 @@ func CmdCheck(args *skel.CmdArgs) error { // CmdDel implements the CNI spec DEL command handler func CmdDel(args *skel.CmdArgs) error { - _, _, err := postRequest(args) + _, _, err := postRequest(args, false) if err != nil { // No error in DEL (as of CNI spec) logging.Errorf("CmdDel (shim): %v", err) @@ -71,15 +71,21 @@ func CmdDel(args *skel.CmdArgs) error { return nil } -func postRequest(args *skel.CmdArgs) (*Response, string, error) { +func postRequest(args *skel.CmdArgs, waitForApi bool) (*Response, string, error) { multusShimConfig, err := shimConfig(args.StdinData) if err != nil { return nil, "", fmt.Errorf("invalid CNI configuration passed to multus-shim: %w", err) } // check API readiness - if err := WaitUntilAPIReady(multusShimConfig.MultusSocketDir); err != nil { - return nil, multusShimConfig.CNIVersion, err + if waitForApi { + if err := WaitUntilAPIReady(multusShimConfig.MultusSocketDir); err != nil { + return nil, multusShimConfig.CNIVersion, err + } + } else { + if err := CheckAPIReadyNow(multusShimConfig.MultusSocketDir); err != nil { + return nil, multusShimConfig.CNIVersion, err + } } cniRequest, err := newCNIRequest(args)