Repository: code-ready/crc
Pull request #2327 (2021-05-10 10:21:58)

Don't use kubeadmin-passwd file from the bundle
This patch will remove all the occurance where we are trying to use
the kubeadmin-passwd file for `kubeadmin` user because now every crc
start generate a new passwd and stored in
`~/.crc/machine/crc/kubeadmin-passwd` file.



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

This pull-request has been approved by: *<a href="https://github.com/code-ready/crc/pull/2327#" title="Author self-approved">praveenkumar</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)~~ [praveenkumar]

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":[]} -->
openshift-ci[bot]: @praveenkumar: The following test **failed**, say `/retest` to rerun all failed tests:

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

[Full PR test history](https://prow.ci.openshift.org/pr-history?org=code-ready&repo=crc&pr=2327). [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 -->
openshift-ci[bot]: @praveenkumar: The following tests **failed**, say `/retest` to rerun all failed tests:

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

[Full PR test history](https://prow.ci.openshift.org/pr-history?org=code-ready&repo=crc&pr=2327). [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 -->
openshift-ci[bot]: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/code-ready/crc/pull/2327#pullrequestreview-662926693" title="Approved">cfergeau</a>*, *<a href="https://github.com/code-ready/crc/pull/2327#" title="Author self-approved">praveenkumar</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)~~ [cfergeau,praveenkumar]

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":[]} -->
openshift-ci[bot]: New changes are detected. LGTM label has been removed.

Patch:
diff --git a/pkg/crc/cluster/kubeadmin_password.go b/pkg/crc/cluster/kubeadmin_password.go
index 1835fe351..49acda7a1 100644
--- a/pkg/crc/cluster/kubeadmin_password.go
+++ b/pkg/crc/cluster/kubeadmin_password.go
@@ -3,16 +3,13 @@ package cluster
 import (
 	"crypto/rand"
 	"encoding/base64"
-	"errors"
 	"fmt"
 	"io/ioutil"
 	"math/big"
-	"os"
 	"strings"
 
 	"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/oc"
 	crcos "github.com/code-ready/crc/pkg/os"
 	"golang.org/x/crypto/bcrypt"
@@ -57,13 +54,10 @@ func UpdateKubeAdminUserPassword(ocConfig oc.Config) error {
 	return ioutil.WriteFile(kubeAdminPasswordFile, []byte(kubeAdminPassword), 0600)
 }
 
-func GetKubeadminPassword(bundle *bundle.CrcBundleInfo) (string, error) {
+func GetKubeadminPassword() (string, error) {
 	kubeAdminPasswordFile := constants.GetKubeAdminPasswordPath()
 	rawData, err := ioutil.ReadFile(kubeAdminPasswordFile)
 	if err != nil {
-		if errors.Is(err, os.ErrNotExist) {
-			return bundle.GetKubeadminPassword()
-		}
 		return "", err
 	}
 	return strings.TrimSpace(string(rawData)), nil
diff --git a/pkg/crc/machine/bundle/copier.go b/pkg/crc/machine/bundle/copier.go
index 18562c577..0179ebc41 100644
--- a/pkg/crc/machine/bundle/copier.go
+++ b/pkg/crc/machine/bundle/copier.go
@@ -63,12 +63,6 @@ func (copier *Copier) CopyPrivateSSHKey(srcPath string) error {
 	return crcos.CopyFileContents(srcPath, destPath, 0400)
 }
 
-func (copier *Copier) SetKubeAdminPassword(kubePassword string) error {
-	kubeAdminPasswordFileName := copier.srcBundle.ClusterInfo.KubeadminPasswordFile
-	path := copier.resolvePath(kubeAdminPasswordFileName)
-	return ioutil.WriteFile(path, []byte(kubePassword), 0600)
-}
-
 func (copier *Copier) CopyKubeConfig() error {
 	kubeConfigFileName := filepath.Base(copier.srcBundle.GetKubeConfigPath())
 	srcPath := copier.srcBundle.GetKubeConfigPath()
diff --git a/pkg/crc/machine/bundle/metadata.go b/pkg/crc/machine/bundle/metadata.go
index 1567fecb0..5e9acac0a 100644
--- a/pkg/crc/machine/bundle/metadata.go
+++ b/pkg/crc/machine/bundle/metadata.go
@@ -2,7 +2,6 @@ package bundle
 
 import (
 	"fmt"
-	"io/ioutil"
 	"os"
 	"path/filepath"
 	"regexp"
@@ -32,14 +31,13 @@ type BuildInfo struct {
 }
 
 type ClusterInfo struct {
-	OpenShiftVersion      string `json:"openshiftVersion"`
-	ClusterName           string `json:"clusterName"`
-	BaseDomain            string `json:"baseDomain"`
-	AppsDomain            string `json:"appsDomain"`
-	SSHPrivateKeyFile     string `json:"sshPrivateKeyFile"`
-	KubeConfig            string `json:"kubeConfig"`
-	KubeadminPasswordFile string `json:"kubeadminPasswordFile"`
-	OpenshiftPullSecret   string `json:"openshiftPullSecret,omitempty"`
+	OpenShiftVersion    string `json:"openshiftVersion"`
+	ClusterName         string `json:"clusterName"`
+	BaseDomain          string `json:"baseDomain"`
+	AppsDomain          string `json:"appsDomain"`
+	SSHPrivateKeyFile   string `json:"sshPrivateKeyFile"`
+	KubeConfig          string `json:"kubeConfig"`
+	OpenshiftPullSecret string `json:"openshiftPullSecret,omitempty"`
 }
 
 type Node struct {
@@ -154,14 +152,6 @@ func (bundle *CrcBundleInfo) GetKernelCommandLine() string {
 	return bundle.Nodes[0].KernelCmdLine
 }
 
-func (bundle *CrcBundleInfo) GetKubeadminPassword() (string, error) {
-	rawData, err := ioutil.ReadFile(bundle.resolvePath(bundle.ClusterInfo.KubeadminPasswordFile))
-	if err != nil {
-		return "", err
-	}
-	return strings.TrimSpace(string(rawData)), nil
-}
-
 func (bundle *CrcBundleInfo) GetBundleBuildTime() (time.Time, error) {
 	return time.Parse(time.RFC3339, strings.TrimSpace(bundle.BuildInfo.BuildTime))
 }
@@ -177,7 +167,6 @@ func (bundle *CrcBundleInfo) GetBundleNameWithoutExtension() string {
 func (bundle *CrcBundleInfo) verify() error {
 	files := []string{
 		bundle.GetOcPath(),
-		bundle.resolvePath(bundle.ClusterInfo.KubeadminPasswordFile),
 		bundle.GetKubeConfigPath(),
 		bundle.GetSSHKeyPath(),
 		bundle.GetDiskImagePath(),
diff --git a/pkg/crc/machine/generate_bundle.go b/pkg/crc/machine/generate_bundle.go
index 5942a25f9..ecb554b21 100644
--- a/pkg/crc/machine/generate_bundle.go
+++ b/pkg/crc/machine/generate_bundle.go
@@ -58,14 +58,6 @@ func (client *client) GenerateBundle() error {
 		return err
 	}
 
-	kubePassword, err := cluster.GetKubeadminPassword(bundleMetadata)
-	if err != nil {
-		return err
-	}
-	if err := copier.SetKubeAdminPassword(kubePassword); err != nil {
-		return err
-	}
-
 	if err := copier.CopyPrivateSSHKey(constants.GetPrivateKeyPath()); err != nil {
 		return err
 	}
diff --git a/pkg/crc/machine/machine.go b/pkg/crc/machine/machine.go
index bc37c3596..bc3edbdc3 100644
--- a/pkg/crc/machine/machine.go
+++ b/pkg/crc/machine/machine.go
@@ -15,7 +15,7 @@ import (
 )
 
 func getClusterConfig(bundleInfo *bundle.CrcBundleInfo) (*types.ClusterConfig, error) {
-	kubeadminPassword, err := cluster.GetKubeadminPassword(bundleInfo)
+	kubeadminPassword, err := cluster.GetKubeadminPassword()
 	if err != nil {
 		return nil, fmt.Errorf("Error reading kubeadmin password from bundle %v", err)
 	}
diff --git a/pkg/crc/machine/start.go b/pkg/crc/machine/start.go
index f511a5213..f8e0dd092 100644
--- a/pkg/crc/machine/start.go
+++ b/pkg/crc/machine/start.go
@@ -249,11 +249,6 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
 		}
 	}
 
-	clusterConfig, err := getClusterConfig(crcBundleMetadata)
-	if err != nil {
-		return nil, errors.Wrap(err, "Cannot create cluster configuration")
-	}
-
 	// Post-VM start
 	vmState, err = host.Driver.GetState()
 	if err != nil {
@@ -396,10 +391,6 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
 	if err := cluster.UpdateKubeAdminUserPassword(ocConfig); err != nil {
 		return nil, errors.Wrap(err, "Failed to update kubeadmin user password")
 	}
-	clusterConfig.KubeAdminPass, err = cluster.GetKubeadminPassword(crcBundleMetadata)
-	if err != nil {
-		return nil, errors.Wrap(err, "Failed to get kubeadmin user password")
-	}
 
 	if err := cluster.EnsureClusterIDIsNotEmpty(ocConfig); err != nil {
 		return nil, errors.Wrap(err, "Failed to update cluster ID")
@@ -449,6 +440,11 @@ func (client *client) Start(ctx context.Context, startConfig types.StartConfig)
 
 	waitForProxyPropagation(ctx, ocConfig, proxyConfig)
 
+	clusterConfig, err := getClusterConfig(crcBundleMetadata)
+	if err != nil {
+		return nil, errors.Wrap(err, "Cannot get cluster configuration")
+	}
+
 	logging.Info("Adding crc-admin and crc-developer contexts to kubeconfig...")
 	if err := writeKubeconfig(instanceIP, clusterConfig); err != nil {
 		logging.Errorf("Cannot update kubeconfig: %v", err)


Test patch:
diff --git a/pkg/crc/machine/bundle/copier_test.go b/pkg/crc/machine/bundle/copier_test.go
index c21a3531e..ef4a32708 100644
--- a/pkg/crc/machine/bundle/copier_test.go
+++ b/pkg/crc/machine/bundle/copier_test.go
@@ -36,7 +36,6 @@ func TestGenerateBundle(t *testing.T) {
 
 	assert.NoError(t, copier.CopyKubeConfig())
 
-	assert.NoError(t, copier.SetKubeAdminPassword("****-****-****"))
 	assert.NoError(t, copier.CopyPrivateSSHKey(copier.srcBundle.GetSSHKeyPath()))
 	assert.NoError(t, copier.CopyFilesFromFileList())
 
@@ -53,7 +52,6 @@ func createDummyBundleFiles(t *testing.T, bundle *CrcBundleInfo) {
 
 	files := []string{
 		bundle.GetOcPath(),
-		bundle.resolvePath(bundle.ClusterInfo.KubeadminPasswordFile),
 		bundle.GetKubeConfigPath(),
 		bundle.GetSSHKeyPath(),
 		bundle.GetDiskImagePath(),
diff --git a/pkg/crc/machine/bundle/metadata_test.go b/pkg/crc/machine/bundle/metadata_test.go
index 6c4a6a80b..e5b49d5e9 100644
--- a/pkg/crc/machine/bundle/metadata_test.go
+++ b/pkg/crc/machine/bundle/metadata_test.go
@@ -27,8 +27,7 @@ const reference = `{
     "baseDomain": "testing",
     "appsDomain": "apps-crc.testing",
     "sshPrivateKeyFile": "id_ecdsa_crc",
-    "kubeConfig": "kubeconfig",
-    "kubeadminPasswordFile": "kubeadmin-password"
+    "kubeConfig": "kubeconfig"
   },
   "nodes": [
     {
@@ -73,13 +72,12 @@ var parsedReference = CrcBundleInfo{
 		SncVersion:                "git4.1.14-137-g14e7",
 	},
 	ClusterInfo: ClusterInfo{
-		OpenShiftVersion:      "4.6.1",
-		ClusterName:           "crc",
-		BaseDomain:            "testing",
-		AppsDomain:            "apps-crc.testing",
-		SSHPrivateKeyFile:     "id_ecdsa_crc",
-		KubeConfig:            "kubeconfig",
-		KubeadminPasswordFile: "kubeadmin-password",
+		OpenShiftVersion:  "4.6.1",
+		ClusterName:       "crc",
+		BaseDomain:        "testing",
+		AppsDomain:        "apps-crc.testing",
+		SSHPrivateKeyFile: "id_ecdsa_crc",
+		KubeConfig:        "kubeconfig",
 	}, Nodes: []Node{
 		{
 			Kind:       []string{"master", "worker"},