Skip to content

Commit

Permalink
Capture error output of k0s reset (#809)
Browse files Browse the repository at this point in the history
Signed-off-by: Kimmo Lehto <[email protected]>
  • Loading branch information
kke authored Dec 13, 2024
1 parent 91f766e commit ebf4911
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
11 changes: 8 additions & 3 deletions phase/reset_controllers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package phase

import (
"bytes"
"context"
"fmt"

"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1"
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster"
Expand Down Expand Up @@ -97,10 +99,13 @@ func (p *ResetControllers) Run() error {
}

log.Debugf("%s: resetting k0s...", h)
out, err := h.ExecOutput(h.Configurer.K0sCmdf("reset --data-dir=%s", h.K0sDataDir()), exec.Sudo(h))
var stdoutbuf, stderrbuf bytes.Buffer
cmd, err := h.ExecStreams(h.Configurer.K0sCmdf("reset --data-dir=%s", h.K0sDataDir()), nil, &stdoutbuf, &stderrbuf, exec.Sudo(h))
if err != nil {
log.Debugf("%s: k0s reset failed: %s", h, out)
log.Warnf("%s: k0s reported failure: %v", h, err)
return fmt.Errorf("failed to run k0s reset: %w", err)
}
if err := cmd.Wait(); err != nil {
log.Warnf("%s: k0s reset reported failure: %s %s", h, stderrbuf.String(), stdoutbuf.String())
}
log.Debugf("%s: resetting k0s completed", h)

Expand Down
15 changes: 10 additions & 5 deletions phase/reset_workers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package phase

import (
"bytes"
"context"
"fmt"

"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1"
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster"
Expand Down Expand Up @@ -87,10 +89,13 @@ func (p *ResetWorkers) Run() error {
}

log.Debugf("%s: resetting k0s...", h)
out, err := h.ExecOutput(h.Configurer.K0sCmdf("reset --data-dir=%s", h.K0sDataDir()), exec.Sudo(h))
var stdoutbuf, stderrbuf bytes.Buffer
cmd, err := h.ExecStreams(h.Configurer.K0sCmdf("reset --data-dir=%s", h.K0sDataDir()), nil, &stdoutbuf, &stderrbuf, exec.Sudo(h))
if err != nil {
log.Debugf("%s: k0s reset failed: %s", h, out)
log.Warnf("%s: k0s reported failure: %v", h, err)
return fmt.Errorf("failed to run k0s reset: %w", err)
}
if err := cmd.Wait(); err != nil {
log.Warnf("%s: k0s reset reported failure: %s %s", h, stderrbuf.String(), stdoutbuf.String())
}
log.Debugf("%s: resetting k0s completed", h)

Expand All @@ -99,14 +104,14 @@ func (p *ResetWorkers) Run() error {
log.Warnf("%s: failed to remove existing configuration %s: %s", h, h.Configurer.K0sConfigPath(), dErr)
}
log.Debugf("%s: removing config completed", h)

if len(h.Environment) > 0 {
if err := h.Configurer.CleanupServiceEnvironment(h, h.K0sServiceName()); err != nil {
log.Warnf("%s: failed to clean up service environment: %s", h, err.Error())
}
}

log.Infof("%s: reset", h)
return err
return nil
})
}

0 comments on commit ebf4911

Please sign in to comment.