Skip to content
Merged
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
44 changes: 38 additions & 6 deletions phase/gather_k0s_facts.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package phase

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net"
"net/url"
Expand Down Expand Up @@ -148,13 +150,43 @@ func (p *GatherK0sFacts) listEtcdMembers(h *cluster.Host) error {
// etcd member-list outputs json like:
// {"members":{"controller0":"https://172.17.0.2:2380","controller1":"https://172.17.0.3:2380"}}
// on versions like ~1.21.x etcd member-list outputs to stderr with extra fields (from logrus).
output, err := h.ExecOutput(h.Configurer.K0sCmdf("etcd member-list --data-dir=%s 2>&1", h.K0sDataDir()), exec.Sudo(h))
if err != nil {
return fmt.Errorf("failed to run list etcd members command: %w", err)
}

result := make(map[string]any)
if err := json.Unmarshal([]byte(output), &result); err != nil {
// Sometimes, random log statements may appear on stderr, which can break
// the parsing. Try to parse stdout first, then fallback to stderr. Error
// out if none of both was a JSON document.

var stdout, stderr bytes.Buffer
if cmd, err := h.ExecStreams(
h.Configurer.K0sCmdf("etcd member-list --data-dir=%s", h.K0sDataDir()),
nil /*stdin*/, &stdout, &stderr,
exec.Sudo(h),
); err != nil {
return fmt.Errorf("failed to create etcd member-list command: %w", err)
} else if err := cmd.Wait(); err != nil {
return fmt.Errorf("failed to run etcd member-list command: %w", err)
}

var (
result map[string]any
errs []error
)
for _, output := range [][]byte{stdout.Bytes(), stderr.Bytes()} {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a bit overkill to try parsing stderr for json, but sure, why not

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for the old k0s versions which write the JSON to stderr.

if len(output) < 1 {
continue
}
unmarshalled := make(map[string]any)
err := json.Unmarshal(output, &unmarshalled)
if err == nil {
result = unmarshalled
break
}
errs = append(errs, err)
}
if result == nil {
err := errors.Join(errs...)
if err == nil {
err = errors.New("no data")
}
return fmt.Errorf("failed to decode etcd member-list output: %w", err)
}

Expand Down
Loading