From f51c803b56d5d92a92a12eaed7d61a2f05aaa0e2 Mon Sep 17 00:00:00 2001 From: Yannis Zarkadas Date: Sat, 2 Oct 2021 18:05:32 -0400 Subject: [PATCH] api/krusty: Ensure sort ordering works with CLI flag and kustomization Sort order can be defined in two places: - (new) kustomization file - (old) CLI flag We want the kustomization file to take precedence over the CLI flag. Eventually, we may want to move away from having a CLI flag altogether: https://github.com/kubernetes-sigs/kustomize/issues/3947 Case 1: Sort order set in kustomization file AND in CLI flag. Print a warning and let the kustomization file take precedence. Case 2: Sort order set in CLI flag only or not at all. Follow the CLI flag (defaults to legacy) and reorder at the end. Case 3: Sort order set in kustomization file only. Simply build the kustomization. Signed-off-by: Yannis Zarkadas --- api/krusty/kustomizer.go | 34 +++++++++++++++++++++-- kustomize/commands/build/reorderoutput.go | 22 +++++++-------- 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/api/krusty/kustomizer.go b/api/krusty/kustomizer.go index 8de435360f9..94962abd207 100644 --- a/api/krusty/kustomizer.go +++ b/api/krusty/kustomizer.go @@ -5,6 +5,7 @@ package krusty import ( "fmt" + "log" "sigs.k8s.io/kustomize/api/internal/builtins" pLdr "sigs.k8s.io/kustomize/api/internal/plugins/loader" @@ -90,8 +91,37 @@ func (b *Kustomizer) Run( if err != nil { return nil, err } - if b.options.DoLegacyResourceSort { - err = builtins.NewLegacyOrderTransformerPlugin().Transform(m) + // Sort order can be defined in two places: + // - (new) kustomization file + // - (old) CLI flag + // + // We want the kustomization file to take precedence over the CLI flag. + // Eventually, we may want to move away from having a CLI flag altogether: + // https://github.com/kubernetes-sigs/kustomize/issues/3947 + + // Case 1: Sort order set in kustomization file. + if kt.Kustomization().SortOptions != nil { + // If set in CLI flag too, warn the user. + if b.options.Reorder != ReorderOptionUnspecified { + log.Println("Warning: Sorting order is set both in 'kustomization.yaml'" + + " ('sortOptions') and in a CLI flag ('--reorder'). Using the" + + " kustomization file over the CLI flag.") + } + pl := &builtins.SortOrderTransformerPlugin{ + SortOptions: kt.Kustomization().SortOptions, + } + err = pl.Transform(m) + if err != nil { + return nil, err + } + } else if b.options.Reorder == ReorderOptionLegacy || b.options.Reorder == ReorderOptionUnspecified { + // Case 2: Sort order set in CLI flag only or not at all. + pl := &builtins.SortOrderTransformerPlugin{ + SortOptions: &types.SortOptions{ + Order: types.LegacySortOrder, + }, + } + err = pl.Transform(m) if err != nil { return nil, err } diff --git a/kustomize/commands/build/reorderoutput.go b/kustomize/commands/build/reorderoutput.go index c0495bedc0c..3ff3dadff0e 100644 --- a/kustomize/commands/build/reorderoutput.go +++ b/kustomize/commands/build/reorderoutput.go @@ -7,6 +7,7 @@ import ( "fmt" "github.com/spf13/pflag" + "sigs.k8s.io/kustomize/api/krusty" ) //go:generate stringer -type=reorderOutput @@ -23,32 +24,31 @@ const flagReorderOutputName = "reorder" func AddFlagReorderOutput(set *pflag.FlagSet) { set.StringVar( &theFlags.reorderOutput, flagReorderOutputName, - legacy.String(), - "Reorder the resources just before output. "+ - "Use '"+legacy.String()+"' to apply a legacy reordering "+ - "(Namespaces first, Webhooks last, etc). "+ - "Use '"+none.String()+"' to suppress a final reordering.") + unspecified.String(), + "Reorder the resources just before output. Use '"+legacy.String()+"' to"+ + " apply a legacy reordering (Namespaces first, Webhooks last, etc)."+ + " Use '"+none.String()+"' to suppress a final reordering.") } func validateFlagReorderOutput() error { switch theFlags.reorderOutput { - case none.String(), legacy.String(): + case none.String(), legacy.String(), unspecified.String(): return nil default: return fmt.Errorf( "illegal flag value --%s %s; legal values: %v", flagReorderOutputName, theFlags.reorderOutput, - []string{legacy.String(), none.String()}) + []string{legacy.String(), none.String(), unspecified.String()}) } } -func getFlagReorderOutput() reorderOutput { +func getFlagReorderOutput() krusty.ReorderOption { switch theFlags.reorderOutput { case none.String(): - return none + return krusty.ReorderOptionNone case legacy.String(): - return legacy + return krusty.ReorderOptionLegacy default: - return unspecified + return krusty.ReorderOptionUnspecified } }