Skip to content

Commit 55bd1fa

Browse files
authored
feat: move join and reset commands to top level (#406)
* move join and reset commands to top level * test both 'reset' and 'uninstall' aliases * point to new reset command * add 'sudo ./' to join command reset suggestion * remove alias, improve descriptions * add a make command to build darwin binaries for testing
1 parent b490eb0 commit 55bd1fa

File tree

9 files changed

+36
-28
lines changed

9 files changed

+36
-28
lines changed

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ static: pkg/goods/bins/k0s \
8989
embedded-cluster-linux-amd64: static
9090
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "$(LD_FLAGS)" -o ./output/bin/$(APP_NAME) ./cmd/embedded-cluster
9191

92+
# for testing
93+
.PHONY: embedded-cluster-darwin-amd64
94+
embedded-cluster-darwin-amd64:
95+
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "$(LD_FLAGS)" -o ./output/bin/$(APP_NAME) ./cmd/embedded-cluster
96+
9297
.PHONY: unit-tests
9398
unit-tests:
9499
go test -v $(shell go list ./... | grep -v /e2e)

cmd/embedded-cluster/install.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func runOutro(c *cli.Context) error {
301301
// file is created. Resulting kubeconfig is stored in the configuration dir.
302302
var installCommand = &cli.Command{
303303
Name: "install",
304-
Usage: fmt.Sprintf("Install %s", defaults.BinaryName()),
304+
Usage: fmt.Sprintf("Install %s", binName),
305305
Before: func(c *cli.Context) error {
306306
if os.Getuid() != 0 {
307307
return fmt.Errorf("install command must be run as root")
@@ -326,14 +326,14 @@ var installCommand = &cli.Command{
326326
},
327327
},
328328
Action: func(c *cli.Context) error {
329-
logrus.Debugf("checking if %s is already installed", defaults.BinaryName())
329+
logrus.Debugf("checking if %s is already installed", binName)
330330
if installed, err := isAlreadyInstalled(); err != nil {
331331
return err
332332
} else if installed {
333333
logrus.Errorf("An installation has been detected on this machine.")
334334
logrus.Infof("If you want to reinstall you need to remove the existing installation")
335335
logrus.Infof("first. You can do this by running the following command:")
336-
logrus.Infof("\n sudo ./%s node reset\n", defaults.BinaryName())
336+
logrus.Infof("\n sudo ./%s uninstall\n", binName)
337337
return ErrNothingElseToAdd
338338
}
339339
metrics.ReportApplyStarted(c)

cmd/embedded-cluster/join.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func getJoinToken(ctx context.Context, baseURL, shortToken string) (*JoinCommand
9595

9696
var joinCommand = &cli.Command{
9797
Name: "join",
98-
Usage: "Join the current node to an existing cluster",
98+
Usage: fmt.Sprintf("Join the current node to a %s cluster", binName),
9999
ArgsUsage: "<url> <token>",
100100
Before: func(c *cli.Context) error {
101101
if os.Getuid() != 0 {
@@ -104,20 +104,19 @@ var joinCommand = &cli.Command{
104104
return nil
105105
},
106106
Action: func(c *cli.Context) error {
107-
logrus.Debugf("checking if %s is already installed", defaults.BinaryName())
107+
logrus.Debugf("checking if %s is already installed", binName)
108108
if installed, err := isAlreadyInstalled(); err != nil {
109109
return err
110110
} else if installed {
111111
logrus.Errorf("An installation has been detected on this machine.")
112112
logrus.Infof("If you want to reinstall you need to remove the existing installation")
113113
logrus.Infof("first. You can do this by running the following command:")
114-
logrus.Infof("\n %s node reset\n", defaults.BinaryName())
114+
logrus.Infof("\n sudo ./%s uninstall\n", binName)
115115
return ErrNothingElseToAdd
116116
}
117117

118-
binname := defaults.BinaryName()
119118
if c.Args().Len() != 2 {
120-
return fmt.Errorf("usage: %s node join <url> <token>", binname)
119+
return fmt.Errorf("usage: %s node join <url> <token>", binName)
121120
}
122121

123122
logrus.Infof("Fetching join token remotely")
@@ -127,7 +126,7 @@ var joinCommand = &cli.Command{
127126
}
128127

129128
metrics.ReportJoinStarted(c.Context, jcmd.MetricsBaseURL, jcmd.ClusterID)
130-
logrus.Infof("Materializing %s binaries", binname)
129+
logrus.Infof("Materializing %s binaries", binName)
131130
if err := goods.Materialize(); err != nil {
132131
err := fmt.Errorf("unable to materialize binaries: %w", err)
133132
metrics.ReportJoinFailed(c.Context, jcmd.MetricsBaseURL, jcmd.ClusterID, err)
@@ -147,7 +146,7 @@ var joinCommand = &cli.Command{
147146
return err
148147
}
149148

150-
logrus.Infof("Installing %s binaries", binname)
149+
logrus.Infof("Installing %s binaries", binName)
151150
if err := installK0sBinary(); err != nil {
152151
err := fmt.Errorf("unable to install k0s binary: %w", err)
153152
metrics.ReportJoinFailed(c.Context, jcmd.MetricsBaseURL, jcmd.ClusterID, err)
@@ -175,7 +174,7 @@ var joinCommand = &cli.Command{
175174
return err
176175
}
177176

178-
logrus.Infof("Starting %s service", binname)
177+
logrus.Infof("Starting %s service", binName)
179178
if err := startK0sService(); err != nil {
180179
err := fmt.Errorf("unable to start service: %w", err)
181180
metrics.ReportJoinFailed(c.Context, jcmd.MetricsBaseURL, jcmd.ClusterID, err)

cmd/embedded-cluster/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ func main() {
3131
shellCommand,
3232
nodeCommands,
3333
versionCommand,
34+
joinCommand,
35+
uninstallCommand,
3436
},
3537
}
3638
if err := app.RunContext(ctx, os.Args); err != nil {

cmd/embedded-cluster/node.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ import (
55
)
66

77
var nodeCommands = &cli.Command{
8-
Name: "node",
9-
Usage: "Manage cluster nodes",
8+
Name: "node",
9+
Usage: "Manage cluster nodes",
10+
Hidden: true, // this has been replaced by top-level commands
1011
Subcommands: []*cli.Command{
1112
joinCommand,
12-
resetCommand,
13+
uninstallCommand,
1314
},
1415
}

cmd/embedded-cluster/reset.go renamed to cmd/embedded-cluster/uninstall.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func (h *hostInfo) checkResetSafety(c *cli.Context) (bool, string, error) {
194194
}
195195
}
196196
if len(workers) > 0 && len(controllers) == 1 {
197-
message := fmt.Sprintf("Cannot reset the last %s node when there are other nodes in the cluster.", h.RoleName)
197+
message := fmt.Sprintf("Cannot uninstall the last %s node when there are other nodes in the cluster.", h.RoleName)
198198
return false, message, nil
199199
}
200200
return true, "", nil
@@ -284,19 +284,19 @@ func checkErrPrompt(c *cli.Context, err error) bool {
284284
if c.Bool("force") {
285285
return true
286286
}
287-
logrus.Info("An error occurred while trying to reset this node.")
287+
logrus.Info("An error occurred while trying to uninstall this node.")
288288
if c.Bool("no-prompt") {
289289
return false
290290
}
291291
logrus.Info("Continuing may leave the cluster in an unexpected state.")
292292
return prompts.New().Confirm("Do you want to continue anyway?", false)
293293
}
294294

295-
var resetCommand = &cli.Command{
296-
Name: "reset",
295+
var uninstallCommand = &cli.Command{
296+
Name: "uninstall",
297297
Before: func(c *cli.Context) error {
298298
if os.Getuid() != 0 {
299-
return fmt.Errorf("node reset command must be run as root")
299+
return fmt.Errorf("uninstall command must be run as root")
300300
}
301301
return nil
302302
},
@@ -309,14 +309,14 @@ var resetCommand = &cli.Command{
309309
},
310310
&cli.BoolFlag{
311311
Name: "force",
312-
Usage: "Ignore errors encountered when resetting the node (implies --no-prompt)",
312+
Usage: "Ignore errors encountered when uninstalling the node (implies --no-prompt)",
313313
Value: false,
314314
},
315315
},
316-
Usage: "Reset the current node",
316+
Usage: fmt.Sprintf("Uninstall %s from the current node", binName),
317317
Action: func(c *cli.Context) error {
318318
logrus.Info("This will remove this node from the cluster and completely reset it.")
319-
logrus.Info("Do not reset another node until this reset is complete.")
319+
logrus.Info("Do not uninstall another node until this is complete.")
320320
if !c.Bool("force") && !c.Bool("no-prompt") && !prompts.New().Confirm("Do you want to continue?", false) {
321321
return fmt.Errorf("Aborting")
322322
}
@@ -374,7 +374,7 @@ var resetCommand = &cli.Command{
374374
}
375375

376376
// reset
377-
logrus.Infof("Resetting %s...", binName)
377+
logrus.Infof("Uninstalling %s...", binName)
378378
err = stopAndResetK0s()
379379
if !checkErrPrompt(c, err) {
380380
return err

e2e/reset_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func TestMultiNodeReset(t *testing.T) {
104104
bin := strings.Split(command, " ")[0]
105105
// reset worker node
106106
t.Logf("%s: resetting worker node", time.Now().Format(time.RFC3339))
107-
stdout, stderr, err = RunCommandOnNode(t, tc, 3, []string{bin, "node", "reset", "--no-prompt"})
107+
stdout, stderr, err = RunCommandOnNode(t, tc, 3, []string{bin, "uninstall", "--no-prompt"})
108108
if err != nil {
109109
t.Logf("stdout: %s\nstderr: %s", stdout, stderr)
110110
t.Fatalf("fail to reset worker node")
@@ -114,7 +114,7 @@ func TestMultiNodeReset(t *testing.T) {
114114
// reset a controller node
115115
// this should fail with a prompt to override
116116
t.Logf("%s: resetting controller node", time.Now().Format(time.RFC3339))
117-
stdout, stderr, err = RunCommandOnNode(t, tc, 2, []string{bin, "node", "reset", "--no-prompt"})
117+
stdout, stderr, err = RunCommandOnNode(t, tc, 2, []string{bin, "uninstall", "--no-prompt"})
118118
if err != nil {
119119
t.Logf("stdout: %s\nstderr: %s", stdout, stderr)
120120
t.Fatalf("fail to remove controller node %s:", err)

e2e/scripts/reset-installation.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
set -euo pipefail
33

44
main() {
5-
if ! embedded-cluster node reset --no-prompt | tee /tmp/log ; then
6-
echo "Failed to reset embedded-cluster"
5+
if ! embedded-cluster uninstall --no-prompt | tee /tmp/log ; then
6+
echo "Failed to uninstall embedded-cluster"
77
exit 1
88
fi
99

e2e/sudo_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ func TestCommandsRequireSudo(t *testing.T) {
2525
}
2626
for _, cmd := range [][]string{
2727
{"embedded-cluster", "node", "join", "https://test", "token"},
28-
{"embedded-cluster", "node", "reset", "--force"},
28+
{"embedded-cluster", "join", "https://test", "token"},
29+
{"embedded-cluster", "uninstall", "--force"},
2930
{"embedded-cluster", "shell"},
3031
{"embedded-cluster", "install", "--no-prompt"},
3132
} {

0 commit comments

Comments
 (0)