Skip to content

Commit 0fb943d

Browse files
sgalsalehlaverya
andauthored
Clean up existing embedded cluster systemd service before creating symlink (#675)
* Clean up existing embedded cluster systemd service before creating symlink * use lstat instead of stat * lstat on reset too * both 'worker' and 'controller' nodes will have 'worker' in the command, use 'enable-worker' instead * add comment --------- Co-authored-by: Andrew Lavery <laverya@umich.edu>
1 parent bf3f84a commit 0fb943d

File tree

4 files changed

+15
-27
lines changed

4 files changed

+15
-27
lines changed

cmd/embedded-cluster/install.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,6 @@ func configureNetworkManager(c *cli.Context) error {
7979
return nil
8080
}
8181

82-
// runPostInstall is a helper function that run things just after the k0s install
83-
// command ran.
84-
func runPostInstall() error {
85-
src := "/etc/systemd/system/k0scontroller.service"
86-
dst := fmt.Sprintf("/etc/systemd/system/%s.service", defaults.BinaryName())
87-
if err := os.Symlink(src, dst); err != nil {
88-
return fmt.Errorf("failed to create symlink: %w", err)
89-
}
90-
if _, err := helpers.RunCommand("systemctl", "daemon-reload"); err != nil {
91-
return fmt.Errorf("unable to get reload systemctl daemon: %w", err)
92-
}
93-
return installAndEnableLocalArtifactMirror()
94-
}
95-
9682
// RunHostPreflights runs the host preflights we found embedded in the binary
9783
// on all configured hosts. We attempt to read HostPreflights from all the
9884
// embedded Helm Charts and from the Kots Application Release files.
@@ -505,9 +491,9 @@ var installCommand = &cli.Command{
505491
metrics.ReportApplyFinished(c, err)
506492
return err
507493
}
508-
logrus.Debugf("running post install")
509-
if err := runPostInstall(); err != nil {
510-
err := fmt.Errorf("unable to run post install: %w", err)
494+
logrus.Debugf("creating systemd unit files")
495+
if err := createSystemdUnitFiles(false); err != nil {
496+
err := fmt.Errorf("unable to create systemd unit files: %w", err)
511497
metrics.ReportApplyFinished(c, err)
512498
return err
513499
}

cmd/embedded-cluster/join.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ var joinCommand = &cli.Command{
212212
}
213213

214214
logrus.Debugf("creating systemd unit files")
215-
if err := createSystemdUnitFiles(jcmd.K0sJoinCommand); err != nil {
215+
// both controller and worker nodes will have 'worker' in the join command, but only controllers will have 'enable-worker'
216+
// https://github.com/replicatedhq/kots/blob/6a0602f4054d5d5f2d97e649b3303a059f0064d9/pkg/embeddedcluster/node_join.go#L183
217+
if err := createSystemdUnitFiles(!strings.Contains(jcmd.K0sJoinCommand, "enable-worker")); err != nil {
216218
err := fmt.Errorf("unable to create systemd unit files: %w", err)
217219
metrics.ReportJoinFailed(c.Context, jcmd.MetricsBaseURL, jcmd.ClusterID, err)
218220
return err
@@ -386,22 +388,22 @@ func systemdUnitFileName() string {
386388

387389
// createSystemdUnitFiles links the k0s systemd unit file. this also creates a new
388390
// systemd unit file for the local artifact mirror service.
389-
func createSystemdUnitFiles(fullcmd string) error {
391+
func createSystemdUnitFiles(isWorker bool) error {
390392
dst := systemdUnitFileName()
391-
if _, err := os.Stat(dst); err == nil {
393+
if _, err := os.Lstat(dst); err == nil {
392394
if err := os.Remove(dst); err != nil {
393395
return err
394396
}
395397
}
396398
src := "/etc/systemd/system/k0scontroller.service"
397-
if strings.Contains(fullcmd, "worker") {
399+
if isWorker {
398400
src = "/etc/systemd/system/k0sworker.service"
399401
}
400402
if err := os.Symlink(src, dst); err != nil {
401-
return err
403+
return fmt.Errorf("failed to create symlink: %w", err)
402404
}
403405
if _, err := helpers.RunCommand("systemctl", "daemon-reload"); err != nil {
404-
return err
406+
return fmt.Errorf("unable to get reload systemctl daemon: %w", err)
405407
}
406408
return installAndEnableLocalArtifactMirror()
407409
}

cmd/embedded-cluster/restore.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -762,9 +762,9 @@ var restoreCommand = &cli.Command{
762762
if err := installK0s(); err != nil {
763763
return fmt.Errorf("unable update cluster: %w", err)
764764
}
765-
logrus.Debugf("running post install")
766-
if err := runPostInstall(); err != nil {
767-
return fmt.Errorf("unable to run post install: %w", err)
765+
logrus.Debugf("creating systemd unit files")
766+
if err := createSystemdUnitFiles(false); err != nil {
767+
return fmt.Errorf("unable to create systemd unit files: %w", err)
768768
}
769769
logrus.Debugf("waiting for k0s to be ready")
770770
if err := waitForK0s(); err != nil {

cmd/embedded-cluster/uninstall.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ var resetCommand = &cli.Command{
412412
}
413413
}
414414

415-
if _, err := os.Stat(systemdUnitFileName()); err == nil {
415+
if _, err := os.Lstat(systemdUnitFileName()); err == nil {
416416
if err := os.Remove(systemdUnitFileName()); err != nil {
417417
return fmt.Errorf("failed to remove systemd unit file: %w", err)
418418
}

0 commit comments

Comments
 (0)