Repository: code-ready/crc
Pull request #2333 (2021-05-11 12:04:41)

Update the cluster name as per oc norms
This patch fixes following issue
```
$ crc start
$ crc whoami
kubeadmin
$ oc config get-contexts
$ oc config get-contexts
CURRENT   NAME
CLUSTER                AUTHINFO                         NAMESPACE
*         crc-admin
	  api.crc.testing:6443   kubeadmin
default
          crc-developer
api.crc.testing:6443   developer                        default
$ oc config get-clusters
NAME
api.crc.testing:6443

$ oc project openshift-machine-config-operator
$ oc config get-contexts
CURRENT   NAME
CLUSTER                AUTHINFO                         NAMESPACE
          crc-admin
api.crc.testing:6443   kubeadmin                        default
          crc-developer
api.crc.testing:6443   developer                        default
*
openshift-machine-config-operator/api-crc-testing:6443/kubeadmin
api-crc-testing:6443   kubeadmin/api-crc-testing:6443
openshift-machine-config-operator
$ oc config get-clusters
NAME
api-crc-testing:6443
api.crc.testing:6443

$ crc delete && crc start
$ oc whoami
Unable to connect to the server: x509: certificate signed by unknown
authority (possibly because of "crypto/rsa: verification error" while
trying to verify candidate authority certificate
"kube-apiserver-lb-signer")
```

Which is happen 2 different cluster having same
certificate-authority but after delete and start the `api.crc.testing`
server is only updated and not the `api-crc-testing` one which cause this issue.

fixes: #2332

openshift-ci[bot]: @praveenkumar: The following test **failed**, say `/retest` to rerun all failed tests:

Test name | Commit | Details | Rerun command
--- | --- | --- | ---
ci/prow/integration-crc | bc55ecaf70125839f7fdd60c9be7078c178c1507 | [link](https://prow.ci.openshift.org/view/gs/origin-ci-test/pr-logs/pull/code-ready_crc/2333/pull-ci-code-ready-crc-master-integration-crc/1392088275098800128) | `/test integration-crc`

[Full PR test history](https://prow.ci.openshift.org/pr-history?org=code-ready&repo=crc&pr=2333). [Your PR dashboard](https://prow.ci.openshift.org/pr?query=is:pr+state:open+author:praveenkumar).

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->

Patch:
diff --git a/pkg/crc/cluster/clusteroperator.go b/pkg/crc/cluster/clusteroperator.go
index 0d56d8835..fcf234747 100644
--- a/pkg/crc/cluster/clusteroperator.go
+++ b/pkg/crc/cluster/clusteroperator.go
@@ -71,10 +71,6 @@ func (status *Status) IsReady() bool {
 	return status.Available && !status.Progressing && !status.Degraded && !status.Disabled
 }
 
-func GetClusterOperatorStatus(ocConfig oc.Config, operator string) (*Status, error) {
-	return getStatus(ocConfig, []string{operator})
-}
-
 func GetClusterOperatorsStatus(ocConfig oc.Config) (*Status, error) {
 	return getStatus(ocConfig, []string{})
 }
@@ -102,31 +98,33 @@ func getStatus(ocConfig oc.Config, selector []string) (*Status, error) {
 		found = true
 		for _, con := range c.Status.Conditions {
 			switch con.Type {
-			case "Available":
-				if con.Status != "True" {
+			case openshiftapi.OperatorAvailable:
+				if con.Status != openshiftapi.ConditionTrue {
 					logging.Debug(c.ObjectMeta.Name, " operator not available, Reason: ", con.Reason)
 					cs.unavailable = append(cs.unavailable, c.ObjectMeta.Name)
 					cs.Available = false
 				}
-			case "Degraded":
-				if con.Status == "True" {
+			case openshiftapi.OperatorDegraded:
+				if con.Status == openshiftapi.ConditionTrue {
 					logging.Debug(c.ObjectMeta.Name, " operator is degraded, Reason: ", con.Reason)
 					cs.degraded = append(cs.degraded, c.ObjectMeta.Name)
 					cs.Degraded = true
 				}
-			case "Progressing":
-				if con.Status == "True" {
+			case openshiftapi.OperatorProgressing:
+				if con.Status == openshiftapi.ConditionTrue {
 					logging.Debug(c.ObjectMeta.Name, " operator is still progressing, Reason: ", con.Reason)
 					cs.progressing = append(cs.progressing, c.ObjectMeta.Name)
 					cs.Progressing = true
 				}
-			case "Upgradeable":
+			case openshiftapi.OperatorUpgradeable:
 				continue
-			case "Disabled":
-				if con.Status == "True" {
+			case "Disabled": // non official status, used by insights and cluster baremetal operators
+				if con.Status == openshiftapi.ConditionTrue {
 					logging.Debug(c.ObjectMeta.Name, " operator is disabled, Reason: ", con.Reason)
 					cs.Disabled = true
 				}
+			case "ManagementStateDegraded": // only for the network operator
+				continue
 			default:
 				logging.Debugf("Unexpected operator status for %s: %s", c.ObjectMeta.Name, con.Type)
 			}
diff --git a/pkg/crc/cluster/status.go b/pkg/crc/cluster/status.go
index 39f918b12..671ef44ec 100644
--- a/pkg/crc/cluster/status.go
+++ b/pkg/crc/cluster/status.go
@@ -10,7 +10,7 @@ import (
 )
 
 // WaitForClusterStable checks that the cluster is running a number of consecutive times
-func WaitForClusterStable(ctx context.Context, ocConfig oc.Config, monitoringEnabled bool) error {
+func WaitForClusterStable(ctx context.Context, ocConfig oc.Config) error {
 	if ctx.Err() != nil {
 		return ctx.Err()
 	}
diff --git a/pkg/crc/config/settings.go b/pkg/crc/config/settings.go
index f65a52e31..6a21855f2 100644
--- a/pkg/crc/config/settings.go
+++ b/pkg/crc/config/settings.go
@@ -79,7 +79,7 @@ func RegisterSettings(cfg *Config) {
 	cfg.AddSetting(ExperimentalFeatures, false, ValidateBool, SuccessfullyApplied,
 		"Enable experimental features (true/false, default: false)")
 	cfg.AddSetting(NetworkMode, string(defaultNetworkMode()), network.ValidateMode, network.SuccessfullyAppliedMode,
-		"Network mode (default or vsock)")
+		fmt.Sprintf("Network mode (%s or %s)", network.UserNetworkingMode, network.SystemNetworkingMode))
 	cfg.AddSetting(HostNetworkAccess, false, validateHostNetworkAccess, SuccessfullyApplied,
 		"Allow TCP/IP connections from the CodeReady Containers VM to services running on the host (true/false, default: false)")
 	// System tray auto-start config
diff --git a/pkg/crc/machine/kubeconfig.go b/pkg/crc/machine/kubeconfig.go
index fd6b950bd..20371c213 100644
--- a/pkg/crc/machine/kubeconfig.go
+++ b/pkg/crc/machine/kubeconfig.go
@@ -16,6 +16,7 @@ import (
 	"github.com/code-ready/crc/pkg/crc/constants"
 	"github.com/code-ready/crc/pkg/crc/machine/types"
 	"github.com/openshift/oc/pkg/helpers/tokencmd"
+	"k8s.io/apimachinery/third_party/forked/golang/netutil"
 	restclient "k8s.io/client-go/rest"
 	"k8s.io/client-go/tools/clientcmd"
 	"k8s.io/client-go/tools/clientcmd/api"
@@ -79,12 +80,14 @@ func certificateAuthority(kubeconfigFile string) ([]byte, error) {
 	return cluster.CertificateAuthorityData, nil
 }
 
+// https://github.com/openshift/oc/blob/f94afb52dc8a3185b3b9eacaf92ec34d80f8708d/pkg/helpers/kubeconfig/smart_merge.go#L21
 func hostname(clusterAPI string) (string, error) {
 	p, err := url.Parse(clusterAPI)
 	if err != nil {
 		return "", err
 	}
-	return p.Host, nil
+	h := netutil.CanonicalAddr(p)
+	return strings.ReplaceAll(h, ".", "-"), nil
 }
 
 func addContext(cfg *api.Config, ip string, clusterConfig *types.ClusterConfig, ca []byte, context, username, password string) error {
diff --git a/pkg/crc/machine/start.go b/pkg/crc/machine/start.go
index 8c1cfa571..f511a5213 100644
--- a/pkg/crc/machine/start.go
+++ b/pkg/crc/machine/start.go
@@ -443,7 +443,7 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
 
 	logging.Info("Starting OpenShift cluster... [waiting for the cluster to stabilize]")
 
-	if err := cluster.WaitForClusterStable(ctx, ocConfig, client.monitoringEnabled()); err != nil {
+	if err := cluster.WaitForClusterStable(ctx, ocConfig); err != nil {
 		logging.Errorf("Cluster is not ready: %v", err)
 	}
 
diff --git a/pkg/crc/preflight/preflight_linux.go b/pkg/crc/preflight/preflight_linux.go
index b47e34f97..265d60e49 100644
--- a/pkg/crc/preflight/preflight_linux.go
+++ b/pkg/crc/preflight/preflight_linux.go
@@ -228,16 +228,13 @@ func removeVsockCrcSettings() error {
 func getAllPreflightChecks() []Check {
 	usingSystemdResolved := checkSystemdResolvedIsRunning()
 	checks := getPreflightChecksForDistro(distro(), network.SystemNetworkingMode, usingSystemdResolved == nil)
-	checks = append(checks, wsl2PreflightChecks)
 	checks = append(checks, vsockPreflightChecks)
 	return checks
 }
 
 func getPreflightChecks(_ bool, _ bool, networkMode network.Mode) []Check {
 	usingSystemdResolved := checkSystemdResolvedIsRunning()
-	checks := getPreflightChecksForDistro(distro(), networkMode, usingSystemdResolved == nil)
-	checks = append(checks, wsl2PreflightChecks)
-	return checks
+	return getPreflightChecksForDistro(distro(), networkMode, usingSystemdResolved == nil)
 }
 
 func getNetworkChecks(networkMode network.Mode, systemdResolved bool) []Check {
@@ -260,6 +257,7 @@ func getNetworkChecks(networkMode network.Mode, systemdResolved bool) []Check {
 func getPreflightChecksForDistro(distro *linux.OsRelease, networkMode network.Mode, systemdResolved bool) []Check {
 	var checks []Check
 	checks = append(checks, nonWinPreflightChecks[:]...)
+	checks = append(checks, wsl2PreflightChecks)
 	checks = append(checks, genericPreflightChecks[:]...)
 	checks = append(checks, libvirtPreflightChecks(distro)...)
 	networkChecks := getNetworkChecks(networkMode, systemdResolved)


Test patch:
diff --git a/pkg/crc/cluster/clusteroperator_test.go b/pkg/crc/cluster/clusteroperator_test.go
index 3740c65fb..fea4afa03 100644
--- a/pkg/crc/cluster/clusteroperator_test.go
+++ b/pkg/crc/cluster/clusteroperator_test.go
@@ -32,25 +32,6 @@ func TestGetClusterOperatorsStatusProgressing(t *testing.T) {
 	assert.Equal(t, progressing, status)
 }
 
-func TestGetClusterOperatorStatus(t *testing.T) {
-	status, err := GetClusterOperatorStatus(ocConfig("co.json"), "authentication")
-	assert.NoError(t, err)
-	assert.Equal(t, available, status)
-
-	status, err = GetClusterOperatorStatus(ocConfig("co-progressing.json"), "authentication")
-	assert.NoError(t, err)
-	assert.Equal(t, progressing, status)
-
-	status, err = GetClusterOperatorStatus(ocConfig("co-progressing.json"), "cloud-credential")
-	assert.NoError(t, err)
-	assert.Equal(t, available, status)
-}
-
-func TestGetClusterOperatorStatusNotFound(t *testing.T) {
-	_, err := GetClusterOperatorStatus(ocConfig("co-progressing.json"), "not-found")
-	assert.EqualError(t, err, "no cluster operator found")
-}
-
 func ocConfig(s string) oc.Config {
 	return oc.Config{
 		Runner: &mockRunner{file: filepath.Join("testdata", s)},
diff --git a/pkg/crc/preflight/preflight_linux_test.go b/pkg/crc/preflight/preflight_linux_test.go
index ff2127ebb..526cea9f5 100644
--- a/pkg/crc/preflight/preflight_linux_test.go
+++ b/pkg/crc/preflight/preflight_linux_test.go
@@ -63,6 +63,7 @@ var checkListForDistros = []checkListForDistro{
 		systemdResolved: true,
 		checks: []Check{
 			{check: checkIfRunningAsNormalUser},
+			{check: checkRunningInsideWSL2},
 			{check: checkAdminHelperExecutableCached},
 			{check: checkSupportedCPUArch},
 			{configKeySuffix: "check-ram"},
@@ -96,6 +97,7 @@ var checkListForDistros = []checkListForDistro{
 		systemdResolved: false,
 		checks: []Check{
 			{check: checkIfRunningAsNormalUser},
+			{check: checkRunningInsideWSL2},
 			{check: checkAdminHelperExecutableCached},
 			{check: checkSupportedCPUArch},
 			{configKeySuffix: "check-ram"},
@@ -128,6 +130,7 @@ var checkListForDistros = []checkListForDistro{
 		systemdResolved: false,
 		checks: []Check{
 			{check: checkIfRunningAsNormalUser},
+			{check: checkRunningInsideWSL2},
 			{check: checkAdminHelperExecutableCached},
 			{check: checkSupportedCPUArch},
 			{configKeySuffix: "check-ram"},
@@ -154,6 +157,7 @@ var checkListForDistros = []checkListForDistro{
 		systemdResolved: true,
 		checks: []Check{
 			{check: checkIfRunningAsNormalUser},
+			{check: checkRunningInsideWSL2},
 			{check: checkAdminHelperExecutableCached},
 			{check: checkSupportedCPUArch},
 			{configKeySuffix: "check-ram"},
@@ -187,6 +191,7 @@ var checkListForDistros = []checkListForDistro{
 		systemdResolved: false,
 		checks: []Check{
 			{check: checkIfRunningAsNormalUser},
+			{check: checkRunningInsideWSL2},
 			{check: checkAdminHelperExecutableCached},
 			{check: checkSupportedCPUArch},
 			{configKeySuffix: "check-ram"},
@@ -219,6 +224,7 @@ var checkListForDistros = []checkListForDistro{
 		systemdResolved: false,
 		checks: []Check{
 			{check: checkIfRunningAsNormalUser},
+			{check: checkRunningInsideWSL2},
 			{check: checkAdminHelperExecutableCached},
 			{check: checkSupportedCPUArch},
 			{configKeySuffix: "check-ram"},
@@ -245,6 +251,7 @@ var checkListForDistros = []checkListForDistro{
 		systemdResolved: true,
 		checks: []Check{
 			{check: checkIfRunningAsNormalUser},
+			{check: checkRunningInsideWSL2},
 			{check: checkAdminHelperExecutableCached},
 			{check: checkSupportedCPUArch},
 			{configKeySuffix: "check-ram"},
@@ -278,6 +285,7 @@ var checkListForDistros = []checkListForDistro{
 		systemdResolved: false,
 		checks: []Check{
 			{check: checkIfRunningAsNormalUser},
+			{check: checkRunningInsideWSL2},
 			{check: checkAdminHelperExecutableCached},
 			{check: checkSupportedCPUArch},
 			{configKeySuffix: "check-ram"},
@@ -310,6 +318,7 @@ var checkListForDistros = []checkListForDistro{
 		systemdResolved: false,
 		checks: []Check{
 			{check: checkIfRunningAsNormalUser},
+			{check: checkRunningInsideWSL2},
 			{check: checkAdminHelperExecutableCached},
 			{check: checkSupportedCPUArch},
 			{configKeySuffix: "check-ram"},
@@ -336,6 +345,7 @@ var checkListForDistros = []checkListForDistro{
 		systemdResolved: true,
 		checks: []Check{
 			{check: checkIfRunningAsNormalUser},
+			{check: checkRunningInsideWSL2},
 			{check: checkAdminHelperExecutableCached},
 			{check: checkSupportedCPUArch},
 			{configKeySuffix: "check-ram"},
@@ -370,6 +380,7 @@ var checkListForDistros = []checkListForDistro{
 		systemdResolved: false,
 		checks: []Check{
 			{check: checkIfRunningAsNormalUser},
+			{check: checkRunningInsideWSL2},
 			{check: checkAdminHelperExecutableCached},
 			{check: checkSupportedCPUArch},
 			{configKeySuffix: "check-ram"},
@@ -403,6 +414,7 @@ var checkListForDistros = []checkListForDistro{
 		systemdResolved: false,
 		checks: []Check{
 			{check: checkIfRunningAsNormalUser},
+			{check: checkRunningInsideWSL2},
 			{check: checkAdminHelperExecutableCached},
 			{check: checkSupportedCPUArch},
 			{configKeySuffix: "check-ram"},