Skip to content

Commit 56b6453

Browse files
committed
Change the cluster started summary
* Directly add the link to the webconsole. No more indirection with crc console. * Better display credentials * Include a small how-to for the oc command line
1 parent d9e144e commit 56b6453

6 files changed

Lines changed: 154 additions & 36 deletions

File tree

cmd/crc/cmd/oc_env.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func runOcEnv(args []string) error {
3939
fmt.Println(shell.GetEnvString(userShell, "HTTPS_PROXY", proxyConfig.HTTPSProxy))
4040
fmt.Println(shell.GetEnvString(userShell, "NO_PROXY", proxyConfig.GetNoProxyString()))
4141
}
42-
fmt.Println(shell.GenerateUsageHint(userShell, "crc oc-env"))
42+
fmt.Println(shell.GenerateUsageHintWithComment(userShell, "crc oc-env"))
4343
return nil
4444
}
4545

cmd/crc/cmd/podman_env.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func RunPodmanEnv(args []string) error {
4040
fmt.Println(shell.GetEnvString(userShell, "PODMAN_HOST", ip))
4141
fmt.Println(shell.GetEnvString(userShell, "PODMAN_IDENTITY_FILE", constants.GetPrivateKeyPath()))
4242
fmt.Println(shell.GetEnvString(userShell, "PODMAN_IGNORE_HOSTS", "1"))
43-
fmt.Println(shell.GenerateUsageHint(userShell, "crc podman-env"))
43+
fmt.Println(shell.GenerateUsageHintWithComment(userShell, "crc podman-env"))
4444
return nil
4545
}
4646

cmd/crc/cmd/start.go

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"fmt"
77
"io"
88
"os"
9+
"runtime"
910
"strings"
11+
"text/template"
1012

1113
cmdConfig "github.com/code-ready/crc/cmd/crc/cmd/config"
1214
"github.com/code-ready/crc/pkg/crc/cluster"
@@ -18,6 +20,7 @@ import (
1820
"github.com/code-ready/crc/pkg/crc/telemetry"
1921
"github.com/code-ready/crc/pkg/crc/validation"
2022
crcversion "github.com/code-ready/crc/pkg/crc/version"
23+
"github.com/code-ready/crc/pkg/os/shell"
2124
"github.com/spf13/cobra"
2225
"github.com/spf13/pflag"
2326
)
@@ -143,24 +146,18 @@ func (s *startResult) prettyPrintTo(writer io.Writer) error {
143146
return errors.New("either Error or ClusterConfig are needed")
144147
}
145148

146-
_, err := fmt.Fprintln(writer, strings.Join([]string{
147-
"Started the OpenShift cluster",
148-
"",
149-
"To access the cluster, first set up your environment by following the instructions returned by executing 'crc oc-env'.",
150-
fmt.Sprintf("Then you can access your cluster by running 'oc login -u %s -p %s %s'.", s.ClusterConfig.DeveloperCredentials.Username, s.ClusterConfig.DeveloperCredentials.Password, s.ClusterConfig.URL),
151-
fmt.Sprintf("To login as a cluster admin, run 'oc login -u %s -p %s %s'.", s.ClusterConfig.AdminCredentials.Username, s.ClusterConfig.AdminCredentials.Password, s.ClusterConfig.URL),
152-
"",
153-
"You can also run 'crc console' and use the above credentials to access the OpenShift web console.",
154-
"The console will open in your default browser.",
155-
}, "\n"))
149+
if err := writeTemplatedMessage(writer, s); err != nil {
150+
return err
151+
}
156152
if crcversion.IsOkdBuild() {
157-
fmt.Fprintln(writer, strings.Join([]string{
158-
"\n",
153+
_, err := fmt.Fprintln(writer, strings.Join([]string{
154+
"",
159155
"NOTE:",
160156
"This cluster was built from OKD - The Community Distribution of Kubernetes that powers Red Hat OpenShift.",
161157
"If you find an issue, please report it at https://github.com/openshift/okd"}, "\n"))
158+
return err
162159
}
163-
return err
160+
return nil
164161
}
165162

166163
func isDebugLog() bool {
@@ -203,3 +200,54 @@ func checkIfNewVersionAvailable(noUpdateCheck bool) {
203200
}
204201
logging.Debugf("No new version available. The latest version is %s", newVersion)
205202
}
203+
204+
const startTemplate = `Started the OpenShift cluster.
205+
206+
The server is accessible via web console at:
207+
{{ .ClusterConfig.WebConsoleURL }}
208+
209+
Log in as administrator:
210+
Username: {{ .ClusterConfig.AdminCredentials.Username }}
211+
Password: {{ .ClusterConfig.AdminCredentials.Password }}
212+
213+
Log in as user:
214+
Username: {{ .ClusterConfig.DeveloperCredentials.Username }}
215+
Password: {{ .ClusterConfig.DeveloperCredentials.Password }}
216+
217+
Use the 'oc' command line interface:
218+
{{ .CommandLinePrefix }} {{ .EvalCommandLine }}
219+
{{ .CommandLinePrefix }} oc login {{ .ClusterConfig.URL }}
220+
`
221+
222+
type templateVariables struct {
223+
ClusterConfig *clusterConfig
224+
EvalCommandLine string
225+
CommandLinePrefix string
226+
}
227+
228+
func writeTemplatedMessage(writer io.Writer, s *startResult) error {
229+
parsed, err := template.New("template").Parse(startTemplate)
230+
if err != nil {
231+
return err
232+
}
233+
234+
userShell, err := shell.GetShell("")
235+
if err != nil {
236+
userShell = ""
237+
}
238+
return parsed.Execute(writer, &templateVariables{
239+
ClusterConfig: s.ClusterConfig,
240+
EvalCommandLine: shell.GenerateUsageHint(userShell, "crc oc-env"),
241+
CommandLinePrefix: commandLinePrefix(userShell),
242+
})
243+
}
244+
245+
func commandLinePrefix(shell string) string {
246+
if runtime.GOOS == "windows" {
247+
if shell == "powershell" {
248+
return "PS>"
249+
}
250+
return ">"
251+
}
252+
return "$"
253+
}

cmd/crc/cmd/start_test.go

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package cmd
33
import (
44
"bytes"
55
"errors"
6+
"runtime"
67
"testing"
78

89
"github.com/code-ready/crc/pkg/crc/constants"
910
crcErrors "github.com/code-ready/crc/pkg/crc/errors"
11+
"github.com/code-ready/crc/pkg/os/shell"
1012
"github.com/stretchr/testify/assert"
1113
)
1214

@@ -15,7 +17,8 @@ func TestRenderActionPlainSuccess(t *testing.T) {
1517
assert.NoError(t, render(&startResult{
1618
Success: true,
1719
ClusterConfig: &clusterConfig{
18-
URL: constants.DefaultAPIURL,
20+
URL: constants.DefaultAPIURL,
21+
WebConsoleURL: constants.DefaultWebConsoleURL,
1922
AdminCredentials: credentials{
2023
Username: "kubeadmin",
2124
Password: "secret",
@@ -26,15 +29,10 @@ func TestRenderActionPlainSuccess(t *testing.T) {
2629
},
2730
},
2831
}, out, ""))
29-
assert.Equal(t, `Started the OpenShift cluster
30-
31-
To access the cluster, first set up your environment by following the instructions returned by executing 'crc oc-env'.
32-
Then you can access your cluster by running 'oc login -u developer -p developer https://api.crc.testing:6443'.
33-
To login as a cluster admin, run 'oc login -u kubeadmin -p secret https://api.crc.testing:6443'.
3432

35-
You can also run 'crc console' and use the above credentials to access the OpenShift web console.
36-
The console will open in your default browser.
37-
`, out.String())
33+
userShell, err := shell.GetShell("")
34+
assert.NoError(t, err)
35+
assert.Equal(t, expectedTemplate(userShell), out.String())
3836
}
3937

4038
func TestRenderActionPlainFailure(t *testing.T) {
@@ -91,3 +89,67 @@ func TestRenderActionJSONFailure(t *testing.T) {
9189
}, out, jsonFormat))
9290
assert.JSONEq(t, `{"success": false, "error": "broken"}`, out.String())
9391
}
92+
93+
const unixTemplate = `Started the OpenShift cluster.
94+
95+
The server is accessible via web console at:
96+
https://console-openshift-console.apps-crc.testing
97+
98+
Log in as administrator:
99+
Username: kubeadmin
100+
Password: secret
101+
102+
Log in as user:
103+
Username: developer
104+
Password: developer
105+
106+
Use the 'oc' command line interface:
107+
$ eval $(crc oc-env)
108+
$ oc login https://api.crc.testing:6443
109+
`
110+
111+
const powershellTemplate = `Started the OpenShift cluster.
112+
113+
The server is accessible via web console at:
114+
https://console-openshift-console.apps-crc.testing
115+
116+
Log in as administrator:
117+
Username: kubeadmin
118+
Password: secret
119+
120+
Log in as user:
121+
Username: developer
122+
Password: developer
123+
124+
Use the 'oc' command line interface:
125+
PS> & crc oc-env | Invoke-Expression
126+
PS> oc login https://api.crc.testing:6443
127+
`
128+
129+
const cmdTemplate = `Started the OpenShift cluster.
130+
131+
The server is accessible via web console at:
132+
https://console-openshift-console.apps-crc.testing
133+
134+
Log in as administrator:
135+
Username: kubeadmin
136+
Password: secret
137+
138+
Log in as user:
139+
Username: developer
140+
Password: developer
141+
142+
Use the 'oc' command line interface:
143+
> @FOR /f "tokens=*" %i IN ('crc oc-env') DO @call %i
144+
> oc login https://api.crc.testing:6443
145+
`
146+
147+
func expectedTemplate(shell string) string {
148+
if runtime.GOOS == "windows" {
149+
if shell == "powershell" {
150+
return powershellTemplate
151+
}
152+
return cmdTemplate
153+
}
154+
return unixTemplate
155+
}

pkg/os/shell/shell.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,31 @@ func isSupportedShell(userShell string) bool {
3131
return false
3232
}
3333

34-
func GenerateUsageHint(userShell, cmdLine string) string {
35-
cmd := ""
36-
comment := "#"
34+
func GenerateUsageHintWithComment(userShell, cmdLine string) string {
35+
return fmt.Sprintf("%s Run this command to configure your shell:\n%s %s",
36+
comment(userShell),
37+
comment(userShell),
38+
GenerateUsageHint(userShell, cmdLine))
39+
}
3740

41+
func comment(userShell string) string {
42+
if userShell == "cmd" {
43+
return "REM"
44+
}
45+
return "#"
46+
}
47+
48+
func GenerateUsageHint(userShell, cmdLine string) string {
3849
switch userShell {
3950
case "fish":
40-
cmd = fmt.Sprintf("eval (%s)", cmdLine)
51+
return fmt.Sprintf("eval (%s)", cmdLine)
4152
case "powershell":
42-
cmd = fmt.Sprintf("& %s | Invoke-Expression", cmdLine)
53+
return fmt.Sprintf("& %s | Invoke-Expression", cmdLine)
4354
case "cmd":
44-
cmd = fmt.Sprintf("\t@FOR /f \"tokens=*\" %%i IN ('%s') DO @call %%i", cmdLine)
45-
comment = "REM"
55+
return fmt.Sprintf("@FOR /f \"tokens=*\" %%i IN ('%s') DO @call %%i", cmdLine)
4656
default:
47-
cmd = fmt.Sprintf("eval $(%s)", cmdLine)
57+
return fmt.Sprintf("eval $(%s)", cmdLine)
4858
}
49-
50-
return fmt.Sprintf("%s Run this command to configure your shell:\n%s %s", comment, comment, cmd)
5159
}
5260

5361
func GetEnvString(userShell string, envName string, envValue string) string {

test/e2e/features/basic.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ Feature: Basic test
9797
When starting CRC with default bundle succeeds
9898
Then stdout should contain "Started the OpenShift cluster"
9999
# Check if user can copy-paste login details for developer and kubeadmin users
100-
And stdout should match "(?s)(.*)oc login -u developer -p developer https:\/\/api\.crc\.testing:6443(.*)$"
101-
And stdout should match "(?s)(.*)oc login -u kubeadmin -p ([a-zA-Z0-9]{5}-){3}[a-zA-Z0-9]{5} https:\/\/api\.crc\.testing:6443(.*)$"
100+
And stdout should match "(?s)(.*)oc login https:\/\/api\.crc\.testing:6443(.*)$"
101+
And stdout should match "(?s)(.*)https:\/\/console-openshift-console\.apps-crc\.testing(.*)$"
102102

103103
@darwin @linux @windows
104104
Scenario: CRC status and disk space check

0 commit comments

Comments
 (0)