|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "context" |
5 | 6 | "fmt" |
6 | 7 | "io" |
@@ -236,24 +237,27 @@ func runK0sctlApply(ctx context.Context) error { |
236 | 237 | // is overwritten, no questions asked. |
237 | 238 | func runK0sctlKubeconfig(ctx context.Context) error { |
238 | 239 | bin := defaults.PathToHelmVMBinary("k0sctl") |
239 | | - kpath := defaults.PathToConfig("kubeconfig") |
240 | | - fp, err := os.OpenFile(kpath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) |
241 | | - if err != nil { |
242 | | - return fmt.Errorf("unable to open kubeconfig: %w", err) |
243 | | - } |
244 | | - defer fp.Close() |
245 | 240 | cfgpath := defaults.PathToConfig("k0sctl.yaml") |
246 | 241 | if _, err := os.Stat(cfgpath); err != nil { |
247 | | - os.RemoveAll(kpath) |
248 | 242 | return fmt.Errorf("cluster configuration not found") |
249 | 243 | } |
250 | | - kctl := exec.Command(bin, "kubeconfig", "-c", cfgpath, "--disable-telemetry") |
251 | | - kctl.Stderr = fp |
252 | | - kctl.Stdout = fp |
| 244 | + buf := bytes.NewBuffer(nil) |
| 245 | + kctl := exec.Command(bin, "kubeconfig", "-c", cfgpath) |
| 246 | + kctl.Stderr, kctl.Stdout = buf, buf |
253 | 247 | if err := kctl.Run(); err != nil { |
254 | | - os.RemoveAll(kpath) |
| 248 | + logrus.Errorf("Failed to read kubeconfig:") |
| 249 | + logrus.Errorf(buf.String()) |
255 | 250 | return fmt.Errorf("unable to run kubeconfig: %w", err) |
256 | 251 | } |
| 252 | + kpath := defaults.PathToConfig("kubeconfig") |
| 253 | + fp, err := os.OpenFile(kpath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) |
| 254 | + if err != nil { |
| 255 | + return fmt.Errorf("unable to open kubeconfig: %w", err) |
| 256 | + } |
| 257 | + defer fp.Close() |
| 258 | + if _, err := io.Copy(fp, buf); err != nil { |
| 259 | + return fmt.Errorf("unable to write kubeconfig: %w", err) |
| 260 | + } |
257 | 261 | logrus.Infof("Kubeconfig saved to %s", kpath) |
258 | 262 | return nil |
259 | 263 | } |
|
0 commit comments