From 0a4628488c14768e9fc00a03b6cb1c412f47525f Mon Sep 17 00:00:00 2001 From: praveenkumar Date: Wed, 6 Apr 2022 20:15:45 +0530 Subject: [PATCH] Daemon: Move daemon running check to daemon client package This move will help us to consume, daemon running check in the preflight to make decision around if we need to start the daemon as part of preflight or use the existing one. Also Break DaemonRunning func to GetVersion and CheckIfOlderVersion because DaemonRunning function is doing 2 things, first it check if daemon client is responding and then it check if running daemon is using same version as crc version which query to it. This pr breaks this in two different functions so on consumer side it should be easy to utilize those different functionality. --- cmd/crc/cmd/start.go | 13 +++---------- pkg/crc/daemonclient/client.go | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/cmd/crc/cmd/start.go b/cmd/crc/cmd/start.go index 7e544c8779..1e5fdc87af 100644 --- a/cmd/crc/cmd/start.go +++ b/cmd/crc/cmd/start.go @@ -25,7 +25,6 @@ import ( crcversion "github.com/code-ready/crc/pkg/crc/version" crcos "github.com/code-ready/crc/pkg/os" "github.com/code-ready/crc/pkg/os/shell" - pkgerrors "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" ) @@ -315,19 +314,13 @@ func commandLinePrefix(shell string) string { return "$" } -const genericDaemonNotRunningMessage = "Is 'crc daemon' running? Cannot reach daemon API" - func checkDaemonStarted() error { if crcConfig.GetNetworkMode(config) == network.SystemNetworkingMode { return nil } - daemonClient := daemonclient.New() - version, err := daemonClient.APIClient.Version() + v, err := daemonclient.GetVersionFromDaemonAPI() if err != nil { - return pkgerrors.Wrap(err, genericDaemonNotRunningMessage) - } - if version.CrcVersion != crcversion.GetCRCVersion() { - return fmt.Errorf("The executable version (%s) doesn't match the daemon version (%s)", crcversion.GetCRCVersion(), version.CrcVersion) + return err } - return nil + return daemonclient.CheckIfOlderVersion(v) } diff --git a/pkg/crc/daemonclient/client.go b/pkg/crc/daemonclient/client.go index c3e20d5798..4106b49618 100644 --- a/pkg/crc/daemonclient/client.go +++ b/pkg/crc/daemonclient/client.go @@ -1,12 +1,17 @@ package daemonclient import ( + "fmt" "net/http" "github.com/code-ready/crc/pkg/crc/api/client" + crcversion "github.com/code-ready/crc/pkg/crc/version" networkclient "github.com/containers/gvisor-tap-vsock/pkg/client" + pkgerrors "github.com/pkg/errors" ) +const genericDaemonNotRunningMessage = "Is 'crc daemon' running? Cannot reach daemon API" + type Client struct { NetworkClient *networkclient.Client APIClient *client.Client @@ -22,3 +27,19 @@ func New() *Client { }, "http://unix/api"), } } + +func GetVersionFromDaemonAPI() (*client.VersionResult, error) { + apiClient := client.New(&http.Client{Transport: transport()}, "http://unix/api") + version, err := apiClient.Version() + if err != nil { + return nil, pkgerrors.Wrap(err, genericDaemonNotRunningMessage) + } + return &version, nil +} + +func CheckIfOlderVersion(version *client.VersionResult) error { + if version.CrcVersion != crcversion.GetCRCVersion() { + return fmt.Errorf("The executable version (%s) doesn't match the daemon version (%s)", crcversion.GetCRCVersion(), version.CrcVersion) + } + return nil +}