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

Commit

Permalink
Merge pull request #493 from thestormforge/field-ordering
Browse files Browse the repository at this point in the history
Fix field ordering when printing YAML from the CLI
  • Loading branch information
jgustie authored Jul 26, 2022
2 parents 23cfa02 + 85d7f01 commit 12ad157
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion cli/internal/commander/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/duration"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/kio/filters"
kyaml "sigs.k8s.io/kustomize/kyaml/yaml"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -276,8 +279,20 @@ type marshalPrinter struct {

// PrintObj will marshal the supplied object
func (p *marshalPrinter) PrintObj(obj interface{}, w io.Writer) error {
// TODO It would be really nice if we could fix the field ordering for Unstructured objects
if strings.ToLower(p.outputFormat) == "yaml" {
// Hack to perform field ordering on maps that look like resources
if m, ok := obj.(map[string]interface{}); ok && m["kind"] != nil && m["apiVersion"] != nil {
node, err := kyaml.FromMap(m)
if err != nil {
return err
}
return kio.Pipeline{
Inputs: []kio.Reader{kio.ResourceNodeSlice{node}},
Filters: []kio.Filter{filters.FormatFilter{}},
Outputs: []kio.Writer{&kio.ByteWriter{Writer: &prefixWriter{w: w}}},
}.Execute()
}

output, err := yaml.Marshal(obj)
if err != nil {
return err
Expand Down

0 comments on commit 12ad157

Please # to comment.