Repository: code-ready/crc
Pull request #2562 (2021-07-12 09:15:40)

Introduce a new type for crc status. Distinguish it from machine status.
crc pkgs should not leak libmachine state enums. This commit converts it
to the crc status type when needed.

Starting/Stopping are proper to crc. It is not used in drivers/machine.
libmachine None, Paused and Saved states are not used anymore and will
be deleted.

---

It will help refactor statuses while not touching too much libmachine.

openshift-ci[bot]: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/code-ready/crc/pull/2562#" title="Author self-approved">guillaumerose</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=code-ready%2Fcrc).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/code-ready/crc/blob/master/OWNERS)~~ [guillaumerose]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
guillaumerose: /retest
retest this please

Patch:
diff --git a/cmd/crc/cmd/console.go b/cmd/crc/cmd/console.go
index 83e6f5c31..961a8251b 100644
--- a/cmd/crc/cmd/console.go
+++ b/cmd/crc/cmd/console.go
@@ -8,8 +8,8 @@ import (
 
 	crcErrors "github.com/code-ready/crc/pkg/crc/errors"
 	"github.com/code-ready/crc/pkg/crc/machine"
+	"github.com/code-ready/crc/pkg/crc/machine/state"
 	"github.com/code-ready/crc/pkg/crc/machine/types"
-	"github.com/code-ready/machine/libmachine/state"
 	"github.com/pkg/browser"
 	"github.com/spf13/cobra"
 )
diff --git a/cmd/crc/cmd/status.go b/cmd/crc/cmd/status.go
index a87c362c7..ab4678da3 100644
--- a/cmd/crc/cmd/status.go
+++ b/cmd/crc/cmd/status.go
@@ -68,7 +68,7 @@ func getStatus(client machine.Client, cacheDir string) *status {
 
 	return &status{
 		Success:          true,
-		CrcStatus:        clusterStatus.CrcStatus.String(),
+		CrcStatus:        string(clusterStatus.CrcStatus),
 		OpenShiftStatus:  clusterStatus.OpenshiftStatus,
 		OpenShiftVersion: clusterStatus.OpenshiftVersion,
 		DiskUsage:        clusterStatus.DiskUse,
diff --git a/cmd/crc/cmd/stop.go b/cmd/crc/cmd/stop.go
index e853ffd68..38786936c 100644
--- a/cmd/crc/cmd/stop.go
+++ b/cmd/crc/cmd/stop.go
@@ -8,7 +8,7 @@ import (
 	crcErrors "github.com/code-ready/crc/pkg/crc/errors"
 	"github.com/code-ready/crc/pkg/crc/input"
 	"github.com/code-ready/crc/pkg/crc/machine"
-	"github.com/code-ready/machine/libmachine/state"
+	"github.com/code-ready/crc/pkg/crc/machine/state"
 	"github.com/spf13/cobra"
 )
 
diff --git a/pkg/crc/api/adapter.go b/pkg/crc/api/adapter.go
index 5d3ed231f..75584fd57 100644
--- a/pkg/crc/api/adapter.go
+++ b/pkg/crc/api/adapter.go
@@ -62,7 +62,7 @@ func (a *Adapter) Start(ctx context.Context, startConfig types.StartConfig) clie
 	}
 	return client.StartResult{
 		Success:        true,
-		Status:         res.Status.String(),
+		Status:         string(res.Status),
 		ClusterConfig:  res.ClusterConfig,
 		KubeletStarted: res.KubeletStarted,
 	}
@@ -78,7 +78,7 @@ func (a *Adapter) Status() client.ClusterStatusResult {
 		}
 	}
 	return client.ClusterStatusResult{
-		CrcStatus:        res.CrcStatus.String(),
+		CrcStatus:        string(res.CrcStatus),
 		OpenshiftStatus:  string(res.OpenshiftStatus),
 		OpenshiftVersion: res.OpenshiftVersion,
 		DiskUse:          res.DiskUse,
diff --git a/pkg/crc/machine/client.go b/pkg/crc/machine/client.go
index 8a3b2fdc5..77231a52a 100644
--- a/pkg/crc/machine/client.go
+++ b/pkg/crc/machine/client.go
@@ -5,9 +5,9 @@ import (
 	"time"
 
 	crcConfig "github.com/code-ready/crc/pkg/crc/config"
+	"github.com/code-ready/crc/pkg/crc/machine/state"
 	"github.com/code-ready/crc/pkg/crc/machine/types"
 	"github.com/code-ready/crc/pkg/crc/network"
-	"github.com/code-ready/machine/libmachine/state"
 	"github.com/kofalt/go-memoize"
 )
 
diff --git a/pkg/crc/machine/console.go b/pkg/crc/machine/console.go
index 1de905335..84ff3ed86 100644
--- a/pkg/crc/machine/console.go
+++ b/pkg/crc/machine/console.go
@@ -1,6 +1,7 @@
 package machine
 
 import (
+	"github.com/code-ready/crc/pkg/crc/machine/state"
 	"github.com/code-ready/crc/pkg/crc/machine/types"
 	"github.com/pkg/errors"
 )
@@ -34,6 +35,6 @@ func (client *client) GetConsoleURL() (*types.ConsoleResult, error) {
 
 	return &types.ConsoleResult{
 		ClusterConfig: *clusterConfig,
-		State:         vmState,
+		State:         state.FromMachine(vmState),
 	}, nil
 }
diff --git a/pkg/crc/machine/fakemachine/client.go b/pkg/crc/machine/fakemachine/client.go
index ac0fc2a67..f75bc24fb 100644
--- a/pkg/crc/machine/fakemachine/client.go
+++ b/pkg/crc/machine/fakemachine/client.go
@@ -4,9 +4,9 @@ import (
 	"context"
 	"errors"
 
+	"github.com/code-ready/crc/pkg/crc/machine/state"
 	"github.com/code-ready/crc/pkg/crc/machine/types"
 	"github.com/code-ready/crc/pkg/crc/network"
-	"github.com/code-ready/machine/libmachine/state"
 )
 
 func NewClient() *Client {
diff --git a/pkg/crc/machine/start.go b/pkg/crc/machine/start.go
index 7b453453b..aba0a3a82 100644
--- a/pkg/crc/machine/start.go
+++ b/pkg/crc/machine/start.go
@@ -20,6 +20,7 @@ import (
 	"github.com/code-ready/crc/pkg/crc/logging"
 	"github.com/code-ready/crc/pkg/crc/machine/bundle"
 	"github.com/code-ready/crc/pkg/crc/machine/config"
+	"github.com/code-ready/crc/pkg/crc/machine/state"
 	"github.com/code-ready/crc/pkg/crc/machine/types"
 	"github.com/code-ready/crc/pkg/crc/network"
 	"github.com/code-ready/crc/pkg/crc/oc"
@@ -32,10 +33,9 @@ import (
 	"github.com/code-ready/crc/pkg/libmachine"
 	"github.com/code-ready/crc/pkg/libmachine/host"
 	"github.com/code-ready/machine/libmachine/drivers"
-	"github.com/code-ready/machine/libmachine/state"
+	libmachinestate "github.com/code-ready/machine/libmachine/state"
 	"github.com/docker/go-units"
 	"github.com/pkg/errors"
-
 	"golang.org/x/crypto/ssh"
 	v1 "k8s.io/api/core/v1"
 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -212,7 +212,7 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
 	if err != nil {
 		return nil, errors.Wrap(err, "Error getting the machine state")
 	}
-	if vmState == state.Running {
+	if vmState == libmachinestate.Running {
 		logging.Infof("A CodeReady Containers VM for OpenShift %s is already running", crcBundleMetadata.GetOpenshiftVersion())
 		clusterConfig, err := getClusterConfig(crcBundleMetadata)
 		if err != nil {
@@ -221,7 +221,7 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
 
 		telemetry.SetStartType(ctx, telemetry.AlreadyRunningStartType)
 		return &types.StartResult{
-			Status:         vmState,
+			Status:         state.FromMachine(vmState),
 			ClusterConfig:  *clusterConfig,
 			KubeletStarted: true,
 		}, nil
@@ -258,7 +258,7 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
 	if err != nil {
 		return nil, errors.Wrap(err, "Error getting the state")
 	}
-	if vmState != state.Running {
+	if vmState != libmachinestate.Running {
 		return nil, errors.Wrap(err, "CodeReady Containers VM is not running")
 	}
 
@@ -473,7 +473,7 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
 	return &types.StartResult{
 		KubeletStarted: true,
 		ClusterConfig:  *clusterConfig,
-		Status:         vmState,
+		Status:         state.FromMachine(vmState),
 	}, nil
 }
 
@@ -492,7 +492,7 @@ func (client *client) IsRunning() (bool, error) {
 		// but reports not started on error
 		return false, errors.Wrap(err, "Error getting the state")
 	}
-	if vmState != state.Running {
+	if vmState != libmachinestate.Running {
 		return false, nil
 	}
 	return true, nil
@@ -571,7 +571,7 @@ func startHost(ctx context.Context, api libmachine.API, vm *host.Host) error {
 	}
 
 	logging.Debug("Waiting for machine to be running, this may take a few minutes...")
-	if err := crcerrors.RetryAfterWithContext(ctx, 3*time.Minute, host.MachineInState(vm.Driver, state.Running), 3*time.Second); err != nil {
+	if err := crcerrors.RetryAfterWithContext(ctx, 3*time.Minute, host.MachineInState(vm.Driver, libmachinestate.Running), 3*time.Second); err != nil {
 		return fmt.Errorf("Error waiting for machine to be running: %s", err)
 	}
 
diff --git a/pkg/crc/machine/state/state.go b/pkg/crc/machine/state/state.go
new file mode 100644
index 000000000..b5cbc4b31
--- /dev/null
+++ b/pkg/crc/machine/state/state.go
@@ -0,0 +1,24 @@
+package state
+
+import libmachinestate "github.com/code-ready/machine/libmachine/state"
+
+// State represents the state of crc (both VM and components)
+type State string
+
+const (
+	Running  State = "Running"
+	Stopped  State = "Stopped"
+	Stopping State = "Stopping"
+	Starting State = "Starting"
+	Error    State = "Error"
+)
+
+func FromMachine(input libmachinestate.State) State {
+	switch input {
+	case libmachinestate.Running:
+		return Running
+	case libmachinestate.Stopped:
+		return Stopped
+	}
+	return Error
+}
diff --git a/pkg/crc/machine/status.go b/pkg/crc/machine/status.go
index 3600810a4..903059392 100644
--- a/pkg/crc/machine/status.go
+++ b/pkg/crc/machine/status.go
@@ -7,9 +7,10 @@ import (
 	"github.com/code-ready/crc/pkg/crc/constants"
 	"github.com/code-ready/crc/pkg/crc/logging"
 	"github.com/code-ready/crc/pkg/crc/machine/bundle"
+	"github.com/code-ready/crc/pkg/crc/machine/state"
 	"github.com/code-ready/crc/pkg/crc/machine/types"
 	crcssh "github.com/code-ready/crc/pkg/crc/ssh"
-	"github.com/code-ready/machine/libmachine/state"
+	libmachinestate "github.com/code-ready/machine/libmachine/state"
 	"github.com/pkg/errors"
 )
 
@@ -39,9 +40,9 @@ func (client *client) Status() (*types.ClusterStatusResult, error) {
 		return nil, errors.Wrap(err, "Error loading bundle metadata")
 	}
 
-	if vmStatus != state.Running {
+	if vmStatus != libmachinestate.Running {
 		return &types.ClusterStatusResult{
-			CrcStatus:        vmStatus,
+			CrcStatus:        state.FromMachine(vmStatus),
 			OpenshiftStatus:  types.OpenshiftStopped,
 			OpenshiftVersion: crcBundleMetadata.GetOpenshiftVersion(),
 		}, nil
diff --git a/pkg/crc/machine/stop.go b/pkg/crc/machine/stop.go
index d00e424e8..268198137 100644
--- a/pkg/crc/machine/stop.go
+++ b/pkg/crc/machine/stop.go
@@ -3,10 +3,10 @@ package machine
 import (
 	"github.com/code-ready/crc/pkg/crc/constants"
 	"github.com/code-ready/crc/pkg/crc/logging"
+	"github.com/code-ready/crc/pkg/crc/machine/state"
 	"github.com/code-ready/crc/pkg/crc/oc"
 	crcssh "github.com/code-ready/crc/pkg/crc/ssh"
 	"github.com/code-ready/crc/pkg/libmachine/host"
-	"github.com/code-ready/machine/libmachine/state"
 	"github.com/pkg/errors"
 )
 
@@ -27,9 +27,13 @@ func (client *client) Stop() (state.State, error) {
 		if stateErr != nil {
 			logging.Debugf("Cannot get VM status after stopping it: %v", stateErr)
 		}
-		return status, errors.Wrap(err, "Cannot stop machine")
+		return state.FromMachine(status), errors.Wrap(err, "Cannot stop machine")
 	}
-	return host.Driver.GetState()
+	status, err := host.Driver.GetState()
+	if err != nil {
+		return state.Error, errors.Wrap(err, "Cannot get VM status")
+	}
+	return state.FromMachine(status), nil
 }
 
 // This should be removed after https://bugzilla.redhat.com/show_bug.cgi?id=1965992
diff --git a/pkg/crc/machine/sync.go b/pkg/crc/machine/sync.go
index ddbf15994..e053f6034 100644
--- a/pkg/crc/machine/sync.go
+++ b/pkg/crc/machine/sync.go
@@ -7,8 +7,8 @@ import (
 	"time"
 
 	"github.com/code-ready/crc/pkg/crc/logging"
+	"github.com/code-ready/crc/pkg/crc/machine/state"
 	"github.com/code-ready/crc/pkg/crc/machine/types"
-	"github.com/code-ready/machine/libmachine/state"
 )
 
 const startCancelTimeout = 15 * time.Second
diff --git a/pkg/crc/machine/types/types.go b/pkg/crc/machine/types/types.go
index bb9cd8bc1..9b3cf9e38 100644
--- a/pkg/crc/machine/types/types.go
+++ b/pkg/crc/machine/types/types.go
@@ -2,8 +2,8 @@ package types
 
 import (
 	"github.com/code-ready/crc/pkg/crc/cluster"
+	"github.com/code-ready/crc/pkg/crc/machine/state"
 	"github.com/code-ready/crc/pkg/crc/network"
-	"github.com/code-ready/machine/libmachine/state"
 )
 
 type StartConfig struct {


Test patch:
diff --git a/pkg/crc/machine/sync_test.go b/pkg/crc/machine/sync_test.go
index b973f50bd..7a05d16f3 100644
--- a/pkg/crc/machine/sync_test.go
+++ b/pkg/crc/machine/sync_test.go
@@ -6,8 +6,8 @@ import (
 	"sync"
 	"testing"
 
+	"github.com/code-ready/crc/pkg/crc/machine/state"
 	"github.com/code-ready/crc/pkg/crc/machine/types"
-	"github.com/code-ready/machine/libmachine/state"
 	"github.com/stretchr/testify/assert"
 )
 
diff --git a/test/integration/resize_test.go b/test/integration/resize_test.go
index b89c61bc1..1e0c80052 100644
--- a/test/integration/resize_test.go
+++ b/test/integration/resize_test.go
@@ -52,12 +52,7 @@ var _ = Describe("vary VM parameters: memory cpus, disk", func() {
 	Describe("use custom values", func() {
 
 		It("start CRC", func() {
-			if runtime.GOOS == "darwin" {
-				Expect(RunCRCExpectFail("start", "--memory", "12000", "--cpus", "6", "--disk-size", "40")).To(ContainSubstring("Disk resizing is not supported on macOS"))
-				Expect(RunCRCExpectSuccess("start", "--memory", "12000", "--cpus", "6")).To(ContainSubstring("Started the OpenShift cluster"))
-			} else {
-				Expect(RunCRCExpectSuccess("start", "--memory", "12000", "--cpus", "6", "--disk-size", "40")).To(ContainSubstring("Started the OpenShift cluster"))
-			}
+			Expect(RunCRCExpectSuccess("start", "--memory", "12000", "--cpus", "6", "--disk-size", "40")).To(ContainSubstring("Started the OpenShift cluster"))
 		})
 
 		It("check VM's memory size", func() {
@@ -76,11 +71,7 @@ var _ = Describe("vary VM parameters: memory cpus, disk", func() {
 		It("check VM's disk size", func() {
 			out, err := SendCommandToVM("df -h | grep sysroot")
 			Expect(err).NotTo(HaveOccurred())
-			if runtime.GOOS == "darwin" { // darwin does not support resize
-				Expect(out).Should(MatchRegexp(`.*31G[\s].*[\s]/sysroot`))
-			} else {
-				Expect(out).Should(MatchRegexp(`.*40G[\s].*[\s]/sysroot`))
-			}
+			Expect(out).Should(MatchRegexp(`.*40G[\s].*[\s]/sysroot`))
 		})
 
 		It("stop CRC", func() {
@@ -97,13 +88,10 @@ var _ = Describe("vary VM parameters: memory cpus, disk", func() {
 			Expect(RunCRCExpectFail("start", "--cpus", "3")).To(ContainSubstring("requires CPUs >= 4"))
 		})
 		It("start CRC with smaller disk", func() { // bigger than default && smaller than current
-			switch runtime.GOOS {
-			case "darwin":
-				Expect(RunCRCExpectFail("start", "--disk-size", "35")).To(ContainSubstring("Disk resizing is not supported on macOS"))
-			case "linux":
-				Expect(RunCRCExpectFail("start", "--disk-size", "35")).To(ContainSubstring("current disk image capacity is bigger than the requested size"))
-			case "windows":
+			if runtime.GOOS == "windows" {
 				Expect(RunCRCExpectFail("start", "--disk-size", "35")).To(ContainSubstring("Failed to set disk size to"))
+			} else {
+				Expect(RunCRCExpectFail("start", "--disk-size", "35")).To(ContainSubstring("current disk image capacity is bigger than the requested size"))
 			}
 		})
 		It("start CRC with sub-minimum disk", func() { // smaller than min = default = 31GiB