Skip to content

Commit

Permalink
Validate k0s config before installing into k0s config path (#567)
Browse files Browse the repository at this point in the history
* Validate k0s config before installing into k0s config path

Signed-off-by: Kimmo Lehto <[email protected]>

* Ensure config dir exists

Signed-off-by: Kimmo Lehto <[email protected]>

---------

Signed-off-by: Kimmo Lehto <[email protected]>
  • Loading branch information
kke authored Oct 9, 2023
1 parent 65abec4 commit f6efd26
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions phase/configure_k0s.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"context"
"fmt"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -65,13 +66,13 @@ func (p *ConfigureK0s) Run() error {
return p.parallelDo(controllers, p.configureK0s)
}

func (p *ConfigureK0s) validateConfig(h *cluster.Host) error {
func (p *ConfigureK0s) validateConfig(h *cluster.Host, configPath string) error {
log.Infof("%s: validating configuration", h)
var cmd string
if h.Exec(h.Configurer.K0sCmdf("config validate --help"), exec.Sudo(h)) == nil {
cmd = h.Configurer.K0sCmdf(`config validate --config "%s"`, h.K0sConfigPath())
cmd = h.Configurer.K0sCmdf(`config validate --config "%s"`, configPath)
} else {
cmd = h.Configurer.K0sCmdf(`validate config --config "%s"`, h.K0sConfigPath())
cmd = h.Configurer.K0sCmdf(`validate config --config "%s"`, configPath)
}

output, err := h.ExecOutput(cmd, exec.Sudo(h))
Expand Down Expand Up @@ -107,18 +108,36 @@ func (p *ConfigureK0s) configureK0s(h *cluster.Host) error {
return err
}

if err := h.Configurer.WriteFile(h, h.K0sConfigPath(), cfg, "0600"); err != nil {
tempConfigPath, err := h.Configurer.TempFile(h)
if err != nil {
return fmt.Errorf("failed to create temporary file for config: %w", err)
}

if err := h.Configurer.WriteFile(h, tempConfigPath, cfg, "0600"); err != nil {
return err
}

if err := p.validateConfig(h); err != nil {
if err := p.validateConfig(h, tempConfigPath); err != nil {
return err
}

if equalConfig(oldcfg, cfg) {
log.Debugf("%s: configuration did not change", h)
} else {
log.Infof("%s: configuration was changed", h)
log.Infof("%s: configuration was changed, installing new configuration", h)
configPath := h.K0sConfigPath()
configDir := filepath.Dir(configPath)

if !h.Configurer.FileExist(h, configDir) {
if err := h.Execf(`install -d 0750 -o root -g root "%s"`, configDir, exec.Sudo(h)); err != nil {
return fmt.Errorf("failed to create k0s configuration directory: %w", err)
}
}

if err := h.Execf(`install -m 0600 -o root -g root "%s" "%s"`, tempConfigPath, configPath, exec.Sudo(h)); err != nil {
return fmt.Errorf("failed to install k0s configuration: %w", err)
}

if h.Metadata.K0sRunningVersion != nil && !h.Metadata.NeedsUpgrade {
log.Infof("%s: restarting the k0s service", h)
if err := h.Configurer.RestartService(h, h.K0sServiceName()); err != nil {
Expand Down

0 comments on commit f6efd26

Please sign in to comment.