Skip to content

Commit d9db360

Browse files
author
Dmitriy Matrenichev
committed
fix: properly output multi-doc machine config in get mc
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 <dmitry.matrenichev@siderolabs.com>
1 parent 31af6b3 commit d9db360

4 files changed

Lines changed: 30 additions & 4 deletions

File tree

cmd/talosctl/cmd/talos/edit.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ func editFn(c *client.Client) func(context.Context, string, resource.Resource, e
5858
return errors.New("only the machineconfig resource can be edited")
5959
}
6060

61-
metadata := mc.Metadata()
62-
id := metadata.ID()
61+
id := mc.Metadata().ID()
6362

6463
body, err := yaml.Marshal(mc.Spec())
6564
if err != nil {

cmd/talosctl/cmd/talos/output/json.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"github.com/cosi-project/runtime/pkg/resource/meta"
1414
"github.com/cosi-project/runtime/pkg/state"
1515
yaml "gopkg.in/yaml.v3"
16+
17+
"github.com/siderolabs/talos/pkg/machinery/resources/config"
1618
)
1719

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

3840
// prepareEncodableData prepares the data of a resource to be encoded as JSON and populates it with some extra information.
3941
func (j *JSON) prepareEncodableData(node string, r resource.Resource, event state.EventType) (map[string]interface{}, error) {
42+
if r.Metadata().Type() == config.MachineConfigType {
43+
r = &mcYamlRepr{r}
44+
}
45+
4046
out, err := resource.MarshalYAML(r)
4147
if err != nil {
4248
return nil, err

cmd/talosctl/cmd/talos/output/table.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ func (table *Table) WriteResource(node string, r resource.Resource, event state.
9696
return nil
9797
}
9898

99-
values = append([]string{label}, values...)
99+
values = slices.Insert(values, 0, label)
100100
}
101101

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

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

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

cmd/talosctl/cmd/talos/output/yaml.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"github.com/cosi-project/runtime/pkg/resource/meta"
1414
"github.com/cosi-project/runtime/pkg/state"
1515
yaml "gopkg.in/yaml.v3"
16+
17+
"github.com/siderolabs/talos/pkg/machinery/resources/config"
1618
)
1719

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

3941
// WriteResource implements output.Writer interface.
4042
func (y *YAML) WriteResource(node string, r resource.Resource, event state.EventType) error {
43+
if r.Metadata().Type() == config.MachineConfigType {
44+
r = &mcYamlRepr{r}
45+
}
46+
4147
out, err := resource.MarshalYAML(r)
4248
if err != nil {
4349
return err
@@ -62,3 +68,18 @@ func (y *YAML) WriteResource(node string, r resource.Resource, event state.Event
6268
func (y *YAML) Flush() error {
6369
return nil
6470
}
71+
72+
type mcYamlRepr struct{ resource.Resource }
73+
74+
func (m *mcYamlRepr) Spec() any { return &mcYamlSpec{res: m.Resource} }
75+
76+
type mcYamlSpec struct{ res resource.Resource }
77+
78+
func (m *mcYamlSpec) MarshalYAML() (any, error) {
79+
out, err := yaml.Marshal(m.res.Spec())
80+
if err != nil {
81+
return nil, err
82+
}
83+
84+
return string(out), err
85+
}

0 commit comments

Comments
 (0)