Skip to content
This repository was archived by the owner on Jan 13, 2023. It is now read-only.

Commit 12ad157

Browse files
authored
Merge pull request #493 from thestormforge/field-ordering
Fix field ordering when printing YAML from the CLI
2 parents 23cfa02 + 85d7f01 commit 12ad157

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

cli/internal/commander/printer.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ import (
3333
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3434
"k8s.io/apimachinery/pkg/runtime"
3535
"k8s.io/apimachinery/pkg/util/duration"
36+
"sigs.k8s.io/kustomize/kyaml/kio"
37+
"sigs.k8s.io/kustomize/kyaml/kio/filters"
38+
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
3639
"sigs.k8s.io/yaml"
3740
)
3841

@@ -276,8 +279,20 @@ type marshalPrinter struct {
276279

277280
// PrintObj will marshal the supplied object
278281
func (p *marshalPrinter) PrintObj(obj interface{}, w io.Writer) error {
279-
// TODO It would be really nice if we could fix the field ordering for Unstructured objects
280282
if strings.ToLower(p.outputFormat) == "yaml" {
283+
// Hack to perform field ordering on maps that look like resources
284+
if m, ok := obj.(map[string]interface{}); ok && m["kind"] != nil && m["apiVersion"] != nil {
285+
node, err := kyaml.FromMap(m)
286+
if err != nil {
287+
return err
288+
}
289+
return kio.Pipeline{
290+
Inputs: []kio.Reader{kio.ResourceNodeSlice{node}},
291+
Filters: []kio.Filter{filters.FormatFilter{}},
292+
Outputs: []kio.Writer{&kio.ByteWriter{Writer: &prefixWriter{w: w}}},
293+
}.Execute()
294+
}
295+
281296
output, err := yaml.Marshal(obj)
282297
if err != nil {
283298
return err

0 commit comments

Comments
 (0)