Skip to content

Commit

Permalink
Daemon: Move daemon running check to daemon client package
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
praveenkumar committed Apr 22, 2022
1 parent 0a7df3b commit 0a46284
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
13 changes: 3 additions & 10 deletions cmd/crc/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}
21 changes: 21 additions & 0 deletions pkg/crc/daemonclient/client.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
}

0 comments on commit 0a46284

Please sign in to comment.