Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate k0s config before installing into k0s config path #567

Merged
merged 2 commits into from
Oct 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading