-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reinstall already installed k0s to reconfigure installFlags
Signed-off-by: Kimmo Lehto <[email protected]>
- Loading branch information
Showing
6 changed files
with
196 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package phase | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math" | ||
"strings" | ||
"time" | ||
|
||
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1" | ||
"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" | ||
) | ||
|
||
type Reinstall struct { | ||
GenericPhase | ||
hosts cluster.Hosts | ||
} | ||
|
||
// Title for the phase | ||
func (p *Reinstall) Title() string { | ||
return "Reinstall" | ||
} | ||
|
||
// Prepare the phase | ||
func (p *Reinstall) Prepare(config *v1beta1.Cluster) error { | ||
p.Config = config | ||
p.hosts = p.Config.Spec.Hosts.Filter(func(h *cluster.Host) bool { | ||
return !h.Metadata.K0sInstalled && h.Metadata.K0sRunningVersion != nil && !h.Reset && h.FlagsChanged() | ||
}) | ||
|
||
return nil | ||
} | ||
|
||
// ShouldRun is true when there are hosts that needs to be reinstalled | ||
func (p *Reinstall) ShouldRun() bool { | ||
return cluster.K0sForceFlagSince.Check(p.Config.Spec.K0s.Version) && len(p.hosts) > 0 | ||
} | ||
|
||
// Run the phase | ||
func (p *Reinstall) Run() error { | ||
if !cluster.K0sForceFlagSince.Check(p.Config.Spec.K0s.Version) { | ||
log.Warnf("k0s version %s does not support install --force flag, installFlags won't be reconfigured", p.Config.Spec.K0s.Version) | ||
return nil | ||
} | ||
controllers := p.hosts.Controllers() | ||
if len(controllers) > 0 { | ||
log.Infof("Reinstalling %d controllers sequentially", len(controllers)) | ||
err := controllers.Each(func(h *cluster.Host) error { | ||
return p.reinstall(h) | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
workers := p.hosts.Workers() | ||
if len(workers) == 0 { | ||
return nil | ||
} | ||
|
||
concurrentReinstalls := int(math.Floor(float64(len(p.hosts)) * 0.10)) | ||
if concurrentReinstalls == 0 { | ||
concurrentReinstalls = 1 | ||
} | ||
|
||
log.Infof("Reinstalling max %d workers in parallel", concurrentReinstalls) | ||
|
||
return p.hosts.BatchedParallelEach(concurrentReinstalls, p.reinstall) | ||
} | ||
|
||
func (p *Reinstall) reinstall(h *cluster.Host) error { | ||
if p.Config.Spec.K0s.DynamicConfig && h.Role != "worker" { | ||
h.InstallFlags.AddOrReplace("--enable-dynamic-config") | ||
} | ||
|
||
h.InstallFlags.AddOrReplace("--force") | ||
|
||
cmd, err := h.K0sInstallCommand() | ||
if err != nil { | ||
return err | ||
} | ||
log.Infof("%s: reinstalling k0s", h) | ||
err = p.Wet(h, fmt.Sprintf("reinstall k0s using `%s", strings.ReplaceAll(cmd, h.Configurer.K0sBinaryPath(), "k0s")), func() error { | ||
if err := h.Exec(cmd, exec.Sudo(h)); err != nil { | ||
return fmt.Errorf("failed to reinstall k0s: %w", err) | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = p.Wet(h, "restart k0s service", func() error { | ||
if err := h.Configurer.RestartService(h, h.K0sServiceName()); err != nil { | ||
return fmt.Errorf("failed to restart k0s: %w", err) | ||
} | ||
log.Infof("%s: waiting for the k0s service to start", h) | ||
if err := retry.Timeout(context.TODO(), retry.DefaultTimeout, node.ServiceRunningFunc(h, h.K0sServiceName())); err != nil { | ||
return fmt.Errorf("k0s did not restart: %w", err) | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("restart after reinstall: %w", err) | ||
} | ||
|
||
if h != p.Config.Spec.K0sLeader() { | ||
return nil | ||
} | ||
|
||
if NoWait || !p.IsWet() { | ||
log.Warnf("%s: skipping scheduler and system pod checks because --no-wait given", h) | ||
return nil | ||
} | ||
|
||
log.Infof("%s: waiting for the scheduler to become ready", h) | ||
if err := retry.Timeout(context.TODO(), retry.DefaultTimeout, node.ScheduledEventsAfterFunc(h, time.Now())); err != nil { | ||
if !Force { | ||
return fmt.Errorf("failed to observe scheduling events after api start-up, you can ignore this check by using --force: %w", err) | ||
} | ||
log.Warnf("%s: failed to observe scheduling events after api start-up: %s", h, err) | ||
} | ||
|
||
log.Infof("%s: waiting for system pods to become ready", h) | ||
if err := retry.Timeout(context.TODO(), retry.DefaultTimeout, node.SystemPodsRunningFunc(h)); err != nil { | ||
if !Force { | ||
return fmt.Errorf("all system pods not running after api start-up, you can ignore this check by using --force: %w", err) | ||
} | ||
log.Warnf("%s: failed to observe system pods running after api start-up: %s", h, err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters