Skip to content

Commit

Permalink
Run reset after k0s install has been run but host doesn't come up
Browse files Browse the repository at this point in the history
If "k0s install" has been run but the host fails to become ready, run
"k0s reset" in the phase's cleanup.

Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
  • Loading branch information
kke committed Oct 18, 2023
1 parent 7e7a67e commit 97e3e10
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
1 change: 1 addition & 0 deletions action/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func (a Apply) Run() error {

if result = a.Manager.Run(); result != nil {
analytics.Client.Publish("apply-failure", map[string]interface{}{"clusterID": a.Manager.Config.Spec.K0s.Metadata.ClusterID})
log.Info(phase.Colorize.Red("==> Apply failed").String())
return result
}

Expand Down
10 changes: 10 additions & 0 deletions phase/initialize_k0s.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster"
"github.com/k0sproject/k0sctl/pkg/node"
"github.com/k0sproject/k0sctl/pkg/retry"
"github.com/k0sproject/rig/exec"
log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -40,11 +41,18 @@ func (p *InitializeK0s) ShouldRun() bool {
// CleanUp cleans up the environment override file
func (p *InitializeK0s) CleanUp() {
h := p.leader

log.Infof("%s: cleaning up", h)
if len(h.Environment) > 0 {
if err := h.Configurer.CleanupServiceEnvironment(h, h.K0sServiceName()); err != nil {
log.Warnf("%s: failed to clean up service environment: %s", h, err.Error())
}
}
if h.Metadata.K0sInstalled {
if err := h.Exec(h.Configurer.K0sCmdf("reset --data-dir=%s", h.K0sDataDir()), exec.Sudo(h)); err != nil {
log.Warnf("%s: k0s reset failed", h)
}
}
}

// Run the phase
Expand Down Expand Up @@ -72,6 +80,8 @@ func (p *InitializeK0s) Run() error {
return err
}

h.Metadata.K0sInstalled = true

if len(h.Environment) > 0 {
log.Infof("%s: updating service environment", h)
if err := h.Configurer.UpdateServiceEnvironment(h, h.K0sServiceName(), h.Environment); err != nil {
Expand Down
20 changes: 15 additions & 5 deletions phase/install_controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ func (p *InstallControllers) Title() string {
// Prepare the phase
func (p *InstallControllers) Prepare(config *v1beta1.Cluster) error {
p.Config = config
var controllers cluster.Hosts = p.Config.Spec.Hosts.Controllers()
p.leader = p.Config.Spec.K0sLeader()
p.hosts = controllers.Filter(func(h *cluster.Host) bool {
p.hosts = p.Config.Spec.Hosts.Controllers().Filter(func(h *cluster.Host) bool {
return !h.Reset && !h.Metadata.NeedsUpgrade && (h != p.leader && h.Metadata.K0sRunningVersion == nil)
})

Expand All @@ -44,13 +43,22 @@ func (p *InstallControllers) ShouldRun() bool {

// CleanUp cleans up the environment override files on hosts
func (p *InstallControllers) CleanUp() {
for _, h := range p.hosts {
_ = p.hosts.Filter(func(h *cluster.Host) bool {
return !h.Metadata.Ready
}).ParallelEach(func(h *cluster.Host) error {
log.Infof("%s: cleaning up", h)
if len(h.Environment) > 0 {
if err := h.Configurer.CleanupServiceEnvironment(h, h.K0sServiceName()); err != nil {
log.Warnf("%s: failed to clean up service environment: %s", h, err.Error())
log.Warnf("%s: failed to clean up service environment: %v", h, err)
}
}
}
if h.Metadata.K0sInstalled {
if err := h.Exec(h.Configurer.K0sCmdf("reset --data-dir=%s", h.K0sDataDir()), exec.Sudo(h)); err != nil {
log.Warnf("%s: k0s reset failed", h)
}
}
return nil
})
}

// Run the phase
Expand Down Expand Up @@ -119,6 +127,7 @@ func (p *InstallControllers) Run() error {
if err = h.Exec(cmd); err != nil {
return err
}
h.Metadata.K0sInstalled = true

if len(h.Environment) > 0 {
log.Infof("%s: updating service environment", h)
Expand All @@ -140,6 +149,7 @@ func (p *InstallControllers) Run() error {
if err := p.waitJoined(h); err != nil {
return err
}
h.Metadata.Ready = true
}

return nil
Expand Down
22 changes: 16 additions & 6 deletions phase/install_workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ func (p *InstallWorkers) Title() string {
// Prepare the phase
func (p *InstallWorkers) Prepare(config *v1beta1.Cluster) error {
p.Config = config
var workers cluster.Hosts = p.Config.Spec.Hosts.Workers()
p.hosts = workers.Filter(func(h *cluster.Host) bool {
p.hosts = p.Config.Spec.Hosts.Workers().Filter(func(h *cluster.Host) bool {
return !h.Reset && !h.Metadata.NeedsUpgrade && (h.Metadata.K0sRunningVersion == nil || !h.Metadata.Ready)
})
p.leader = p.Config.Spec.K0sLeader()
Expand All @@ -42,15 +41,24 @@ func (p *InstallWorkers) ShouldRun() bool {
return len(p.hosts) > 0
}

// CleanUp cleans up the environment override files on hosts
// CleanUp attempts to clean up any changes after a failed install
func (p *InstallWorkers) CleanUp() {
for _, h := range p.hosts {
_ = p.hosts.Filter(func(h *cluster.Host) bool {
return !h.Metadata.Ready
}).ParallelEach(func(h *cluster.Host) error {
log.Infof("%s: cleaning up", h)
if len(h.Environment) > 0 {
if err := h.Configurer.CleanupServiceEnvironment(h, h.K0sServiceName()); err != nil {
log.Warnf("%s: failed to clean up service environment: %s", h, err.Error())
log.Warnf("%s: failed to clean up service environment: %v", h, err)
}
}
}
if h.Metadata.K0sInstalled {
if err := h.Exec(h.Configurer.K0sCmdf("reset --data-dir=%s", h.K0sDataDir()), exec.Sudo(h)); err != nil {
log.Warnf("%s: k0s reset failed", h)
}
}
return nil
})
}

// Run the phase
Expand Down Expand Up @@ -137,6 +145,8 @@ func (p *InstallWorkers) Run() error {
return err
}

h.Metadata.K0sInstalled = true

if len(h.Environment) > 0 {
log.Infof("%s: updating service environment", h)
if err := h.Configurer.UpdateServiceEnvironment(h, h.K0sServiceName(), h.Environment); err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ type HostMetadata struct {
K0sBinaryVersion *version.Version
K0sBinaryTempFile string
K0sRunningVersion *version.Version
K0sInstalled bool
Arch string
IsK0sLeader bool
Hostname string
Expand Down

0 comments on commit 97e3e10

Please sign in to comment.