Skip to content

Commit

Permalink
fix: properly output multi-doc machine config in get mc
Browse files Browse the repository at this point in the history
For `get mc -o json|yaml` we pretend that `spec` field is string and not an actual yaml map. That way you
can see the full spec in unformatted view using `talosctl -n <node> get mc -o yaml` or formatted using
`talosctl -n <node> get mc -o yaml | yq .spec`.

`edit mc` command is unaffected.

Fixes #8687

Signed-off-by: Dmitriy Matrenichev <[email protected]>
  • Loading branch information
DmitriyMV committed Jul 8, 2024
1 parent 31af6b3 commit d9db360
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
3 changes: 1 addition & 2 deletions cmd/talosctl/cmd/talos/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ func editFn(c *client.Client) func(context.Context, string, resource.Resource, e
return errors.New("only the machineconfig resource can be edited")
}

metadata := mc.Metadata()
id := metadata.ID()
id := mc.Metadata().ID()

body, err := yaml.Marshal(mc.Spec())
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions cmd/talosctl/cmd/talos/output/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/cosi-project/runtime/pkg/resource/meta"
"github.com/cosi-project/runtime/pkg/state"
yaml "gopkg.in/yaml.v3"

"github.com/siderolabs/talos/pkg/machinery/resources/config"
)

// JSON outputs resources in JSON format.
Expand All @@ -37,6 +39,10 @@ func (j *JSON) WriteHeader(definition *meta.ResourceDefinition, withEvents bool)

// prepareEncodableData prepares the data of a resource to be encoded as JSON and populates it with some extra information.
func (j *JSON) prepareEncodableData(node string, r resource.Resource, event state.EventType) (map[string]interface{}, error) {
if r.Metadata().Type() == config.MachineConfigType {
r = &mcYamlRepr{r}
}

out, err := resource.MarshalYAML(r)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions cmd/talosctl/cmd/talos/output/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (table *Table) WriteResource(node string, r resource.Resource, event state.
return nil
}

values = append([]string{label}, values...)
values = slices.Insert(values, 0, label)
}

yml, err := yaml.Marshal(r.Spec())
Expand All @@ -121,7 +121,7 @@ func (table *Table) WriteResource(node string, r resource.Resource, event state.
values = append(values, value)
}

values = append([]string{node}, values...)
values = slices.Insert(values, 0, node)

_, err = fmt.Fprintln(&table.w, strings.Join(values, "\t"))

Expand Down
21 changes: 21 additions & 0 deletions cmd/talosctl/cmd/talos/output/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/cosi-project/runtime/pkg/resource/meta"
"github.com/cosi-project/runtime/pkg/state"
yaml "gopkg.in/yaml.v3"

"github.com/siderolabs/talos/pkg/machinery/resources/config"
)

// YAML outputs resources in YAML format.
Expand All @@ -38,6 +40,10 @@ func (y *YAML) WriteHeader(definition *meta.ResourceDefinition, withEvents bool)

// WriteResource implements output.Writer interface.
func (y *YAML) WriteResource(node string, r resource.Resource, event state.EventType) error {
if r.Metadata().Type() == config.MachineConfigType {
r = &mcYamlRepr{r}
}

out, err := resource.MarshalYAML(r)
if err != nil {
return err
Expand All @@ -62,3 +68,18 @@ func (y *YAML) WriteResource(node string, r resource.Resource, event state.Event
func (y *YAML) Flush() error {
return nil
}

type mcYamlRepr struct{ resource.Resource }

func (m *mcYamlRepr) Spec() any { return &mcYamlSpec{res: m.Resource} }

type mcYamlSpec struct{ res resource.Resource }

func (m *mcYamlSpec) MarshalYAML() (any, error) {
out, err := yaml.Marshal(m.res.Spec())
if err != nil {
return nil, err
}

return string(out), err
}

0 comments on commit d9db360

Please sign in to comment.