Skip to content

Commit f6efd26

Browse files
authored
Validate k0s config before installing into k0s config path (#567)
* 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]>
1 parent 65abec4 commit f6efd26

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

phase/configure_k0s.go

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"bytes"
66
"context"
77
"fmt"
8+
"path/filepath"
89
"strings"
910
"time"
1011

@@ -65,13 +66,13 @@ func (p *ConfigureK0s) Run() error {
6566
return p.parallelDo(controllers, p.configureK0s)
6667
}
6768

68-
func (p *ConfigureK0s) validateConfig(h *cluster.Host) error {
69+
func (p *ConfigureK0s) validateConfig(h *cluster.Host, configPath string) error {
6970
log.Infof("%s: validating configuration", h)
7071
var cmd string
7172
if h.Exec(h.Configurer.K0sCmdf("config validate --help"), exec.Sudo(h)) == nil {
72-
cmd = h.Configurer.K0sCmdf(`config validate --config "%s"`, h.K0sConfigPath())
73+
cmd = h.Configurer.K0sCmdf(`config validate --config "%s"`, configPath)
7374
} else {
74-
cmd = h.Configurer.K0sCmdf(`validate config --config "%s"`, h.K0sConfigPath())
75+
cmd = h.Configurer.K0sCmdf(`validate config --config "%s"`, configPath)
7576
}
7677

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

110-
if err := h.Configurer.WriteFile(h, h.K0sConfigPath(), cfg, "0600"); err != nil {
111+
tempConfigPath, err := h.Configurer.TempFile(h)
112+
if err != nil {
113+
return fmt.Errorf("failed to create temporary file for config: %w", err)
114+
}
115+
116+
if err := h.Configurer.WriteFile(h, tempConfigPath, cfg, "0600"); err != nil {
111117
return err
112118
}
113119

114-
if err := p.validateConfig(h); err != nil {
120+
if err := p.validateConfig(h, tempConfigPath); err != nil {
115121
return err
116122
}
117123

118124
if equalConfig(oldcfg, cfg) {
119125
log.Debugf("%s: configuration did not change", h)
120126
} else {
121-
log.Infof("%s: configuration was changed", h)
127+
log.Infof("%s: configuration was changed, installing new configuration", h)
128+
configPath := h.K0sConfigPath()
129+
configDir := filepath.Dir(configPath)
130+
131+
if !h.Configurer.FileExist(h, configDir) {
132+
if err := h.Execf(`install -d 0750 -o root -g root "%s"`, configDir, exec.Sudo(h)); err != nil {
133+
return fmt.Errorf("failed to create k0s configuration directory: %w", err)
134+
}
135+
}
136+
137+
if err := h.Execf(`install -m 0600 -o root -g root "%s" "%s"`, tempConfigPath, configPath, exec.Sudo(h)); err != nil {
138+
return fmt.Errorf("failed to install k0s configuration: %w", err)
139+
}
140+
122141
if h.Metadata.K0sRunningVersion != nil && !h.Metadata.NeedsUpgrade {
123142
log.Infof("%s: restarting the k0s service", h)
124143
if err := h.Configurer.RestartService(h, h.K0sServiceName()); err != nil {

0 commit comments

Comments
 (0)