This repository was archived by the owner on Jan 13, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +16
-1
lines changed
Expand file tree Collapse file tree 1 file changed +16
-1
lines changed Original file line number Diff line number Diff 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
278281func (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
You can’t perform that action at this time.
0 commit comments