Skip to content
Draft
Show file tree
Hide file tree
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
11 changes: 7 additions & 4 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,14 @@
- Use `Wet(host, dryMsg, funcs...)` and `DryMsg(host, msg)` (both on `GenericPhase`) so dry-run output is an accurate per-host plan.
- If a phase needs alternate dry-run behavior, implement the dry-run interface instead of partially running mutating logic.

## Logging
- Use `log "github.com/sirupsen/logrus"` — it is the only logger in this project.
- Per-host messages must prefix the host: `log.Infof("%s: doing thing", h)`.
## Logging and display
- Use `log "github.com/k0sproject/k0sctl/internal/log"` — a thin printf-style facade over `log/slog`; it is the only logger in this project.
- Host-scoped messages go through the host's logger: `h.Log().Infof("doing thing")` — this attaches the `host` attribute so records can be routed per host. Do not prefix messages with `%s: ` manually.
- In code that receives a `context.Context` from a per-host operation (e.g. retry helpers), use `log.FromContext(ctx)` to inherit the host scope.
- Use `log.Debug`/`log.Debugf` for internal state, `log.Info`/`log.Infof` for user-visible progress, `log.Warn`/`log.Warnf` for recoverable problems.
- Do not use `fmt.Print*` for diagnostic output.
- Structured attributes use rig v2's attribute keys (`log.KeyHost`, `log.KeyError`, `log.KeyDuration`) so k0sctl and rig records stay uniform. k0sctl adds `log.KeyPhase` (phase lifecycle records emitted by phase/manager.go) and `log.KeyAttempt` (retry counters from pkg/retry).
- The screen is rendered by `internal/display` (a slog handler): a live TTY renderer with per-host status rows and log tails, or a plain line renderer for non-TTY/CI/--debug/--dry-run. Displays route records by attributes — never parse log message text to detect state, and never embed ANSI colors in log messages.
- Do not use `fmt.Print*` for diagnostic output. Direct writes to `Manager.Writer` are only for final reports (dry-run summary, kubeconfig) that happen while no live display is running.

## Error Wrapping
- Wrap errors with context using `fmt.Errorf("doing X: %w", err)`.
Expand Down
10 changes: 4 additions & 6 deletions action/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package action

import (
"context"
"fmt"
"io"
"os"
"os/exec"
Expand All @@ -12,7 +11,7 @@ import (

"github.com/k0sproject/k0sctl/phase"

log "github.com/sirupsen/logrus"
log "github.com/k0sproject/k0sctl/internal/log"
)

type ApplyOptions struct {
Expand Down Expand Up @@ -111,7 +110,7 @@ func (a Apply) Run(ctx context.Context) error {
var result error

if result = a.Manager.Run(ctx); result != nil {
log.Info(phase.Colorize.Red("==> Apply failed").String())
log.Error("==> Apply failed")
return result
}

Expand All @@ -122,8 +121,7 @@ func (a Apply) Run(ctx context.Context) error {
}

duration := time.Since(start).Truncate(time.Second)
text := fmt.Sprintf("==> Finished in %s", duration)
log.Info(phase.Colorize.Green(text).String())
log.Infof("==> Finished in %s", duration)

for _, host := range a.Manager.Config.Spec.Hosts {
if host.Reset {
Expand Down Expand Up @@ -159,7 +157,7 @@ func (a Apply) Run(ctx context.Context) error {
}

log.Info("Tip: To access the cluster you can now fetch the admin kubeconfig using:")
log.Info(" " + phase.Colorize.Cyan(cmd.String()).String())
log.Info(" " + cmd.String())
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions action/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"io"
"time"

log "github.com/k0sproject/k0sctl/internal/log"
"github.com/k0sproject/k0sctl/phase"
log "github.com/sirupsen/logrus"
)

type Backup struct {
Expand Down Expand Up @@ -40,6 +40,6 @@ func (b Backup) Run(ctx context.Context) error {

duration := time.Since(start).Truncate(time.Second)
text := fmt.Sprintf("==> Finished in %s", duration)
log.Info(phase.Colorize.Green(text).String())
log.Info(text)
return nil
}
12 changes: 6 additions & 6 deletions action/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ func (k *Kubeconfig) Run(ctx context.Context) error {
// do not need to connect to all nodes
k.Manager.Config.Spec.Hosts = cluster.Hosts{k.Manager.Config.Spec.K0sLeader()}

k.Manager.AddPhase(
&phase.Connect{},
&phase.DetectOS{},
&phase.GetKubeconfig{APIAddress: k.KubeconfigAPIAddress, User: k.KubeconfigUser, Cluster: k.KubeconfigCluster},
&phase.Disconnect{},
)
k.Manager.AddPhase(
&phase.Connect{},
&phase.DetectOS{},
&phase.GetKubeconfig{APIAddress: k.KubeconfigAPIAddress, User: k.KubeconfigUser, Cluster: k.KubeconfigCluster},
&phase.Disconnect{},
)

return k.Manager.Run(ctx)
}
4 changes: 2 additions & 2 deletions action/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"os"
"time"

log "github.com/k0sproject/k0sctl/internal/log"
"github.com/k0sproject/k0sctl/phase"
log "github.com/sirupsen/logrus"

"github.com/AlecAivazis/survey/v2"
"github.com/mattn/go-isatty"
Expand Down Expand Up @@ -71,7 +71,7 @@ func (r Reset) Run(ctx context.Context) error {

duration := time.Since(start).Truncate(time.Second)
text := fmt.Sprintf("==> Finished in %s", duration)
log.Info(phase.Colorize.Green(text).String())
log.Info(text)

return nil
}
2 changes: 1 addition & 1 deletion cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"strings"

"github.com/k0sproject/k0sctl/action"
log "github.com/k0sproject/k0sctl/internal/log"
"github.com/k0sproject/k0sctl/phase"
"github.com/k0sproject/k0sctl/pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster"
log "github.com/sirupsen/logrus"

"github.com/urfave/cli/v2"
)
Expand Down
2 changes: 1 addition & 1 deletion cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"time"

"github.com/k0sproject/k0sctl/action"
log "github.com/k0sproject/k0sctl/internal/log"
"github.com/k0sproject/k0sctl/phase"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)

Expand Down
Loading
Loading