|
5 | 5 | "bytes" |
6 | 6 | "context" |
7 | 7 | "fmt" |
| 8 | + "path/filepath" |
8 | 9 | "strings" |
9 | 10 | "time" |
10 | 11 |
|
@@ -65,13 +66,13 @@ func (p *ConfigureK0s) Run() error { |
65 | 66 | return p.parallelDo(controllers, p.configureK0s) |
66 | 67 | } |
67 | 68 |
|
68 | | -func (p *ConfigureK0s) validateConfig(h *cluster.Host) error { |
| 69 | +func (p *ConfigureK0s) validateConfig(h *cluster.Host, configPath string) error { |
69 | 70 | log.Infof("%s: validating configuration", h) |
70 | 71 | var cmd string |
71 | 72 | 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) |
73 | 74 | } else { |
74 | | - cmd = h.Configurer.K0sCmdf(`validate config --config "%s"`, h.K0sConfigPath()) |
| 75 | + cmd = h.Configurer.K0sCmdf(`validate config --config "%s"`, configPath) |
75 | 76 | } |
76 | 77 |
|
77 | 78 | output, err := h.ExecOutput(cmd, exec.Sudo(h)) |
@@ -107,18 +108,36 @@ func (p *ConfigureK0s) configureK0s(h *cluster.Host) error { |
107 | 108 | return err |
108 | 109 | } |
109 | 110 |
|
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 { |
111 | 117 | return err |
112 | 118 | } |
113 | 119 |
|
114 | | - if err := p.validateConfig(h); err != nil { |
| 120 | + if err := p.validateConfig(h, tempConfigPath); err != nil { |
115 | 121 | return err |
116 | 122 | } |
117 | 123 |
|
118 | 124 | if equalConfig(oldcfg, cfg) { |
119 | 125 | log.Debugf("%s: configuration did not change", h) |
120 | 126 | } 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 | + |
122 | 141 | if h.Metadata.K0sRunningVersion != nil && !h.Metadata.NeedsUpgrade { |
123 | 142 | log.Infof("%s: restarting the k0s service", h) |
124 | 143 | if err := h.Configurer.RestartService(h, h.K0sServiceName()); err != nil { |
|
0 commit comments