From c10890dd670e86952cd0b27fe4417d20a25b43d0 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 | 45 ++++++++++++++++--- kustomize/commands/build/reorderoutput.go | 40 ++++++++--------- .../commands/build/reorderoutput_string.go | 25 ----------- 3 files changed, 58 insertions(+), 52 deletions(-) delete mode 100644 kustomize/commands/build/reorderoutput_string.go diff --git a/api/krusty/kustomizer.go b/api/krusty/kustomizer.go index 8de435360f9..660b5b028b2 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,11 +91,9 @@ func (b *Kustomizer) Run( if err != nil { return nil, err } - if b.options.DoLegacyResourceSort { - err = builtins.NewLegacyOrderTransformerPlugin().Transform(m) - if err != nil { - return nil, err - } + err = b.applySortOrder(m, kt) + if err != nil { + return nil, err } if b.options.AddManagedbyLabel || utils.StringSliceContains(kt.Kustomization().BuildMetadata, types.ManagedByLabelOption) { t := builtins.LabelTransformerPlugin{ @@ -126,3 +125,39 @@ func (b *Kustomizer) Run( } return m, nil } + +func (b *Kustomizer) applySortOrder(m resmap.ResMap, kt *target.KustTarget) error { + // 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 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, + }, + } + return pl.Transform(m) + } + return nil +} diff --git a/kustomize/commands/build/reorderoutput.go b/kustomize/commands/build/reorderoutput.go index c0495bedc0c..861618c923f 100644 --- a/kustomize/commands/build/reorderoutput.go +++ b/kustomize/commands/build/reorderoutput.go @@ -7,15 +7,8 @@ import ( "fmt" "github.com/spf13/pflag" -) - -//go:generate stringer -type=reorderOutput -type reorderOutput int - -const ( - unspecified reorderOutput = iota - none - legacy + flag "github.com/spf13/pflag" + "sigs.k8s.io/kustomize/api/krusty" ) const flagReorderOutputName = "reorder" @@ -23,32 +16,35 @@ 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.") + string(krusty.ReorderOptionLegacy), + "Reorder the resources just before output. Use '"+string(krusty.ReorderOptionLegacy)+"' to"+ + " apply a legacy reordering (Namespaces first, Webhooks last, etc)."+ + " Use '"+string(krusty.ReorderOptionNone)+"' to suppress a final reordering.") } func validateFlagReorderOutput() error { switch theFlags.reorderOutput { - case none.String(), legacy.String(): + case string(krusty.ReorderOptionNone), string(krusty.ReorderOptionLegacy): return nil default: return fmt.Errorf( "illegal flag value --%s %s; legal values: %v", flagReorderOutputName, theFlags.reorderOutput, - []string{legacy.String(), none.String()}) + []string{string(krusty.ReorderOptionLegacy), string(krusty.ReorderOptionNone)}) } } -func getFlagReorderOutput() reorderOutput { +func getFlagReorderOutput(flags *flag.FlagSet) krusty.ReorderOption { + isReorderSet := flags.Changed(flagReorderOutputName) + if !isReorderSet { + theFlags.reorderOutput = string(krusty.ReorderOptionUnspecified) + } switch theFlags.reorderOutput { - case none.String(): - return none - case legacy.String(): - return legacy + case string(krusty.ReorderOptionNone): + return krusty.ReorderOptionNone + case string(krusty.ReorderOptionLegacy): + return krusty.ReorderOptionLegacy default: - return unspecified + return krusty.ReorderOptionUnspecified } } diff --git a/kustomize/commands/build/reorderoutput_string.go b/kustomize/commands/build/reorderoutput_string.go deleted file mode 100644 index 1fa60051e9b..00000000000 --- a/kustomize/commands/build/reorderoutput_string.go +++ /dev/null @@ -1,25 +0,0 @@ -// Code generated by "stringer -type=reorderOutput"; DO NOT EDIT. - -package build - -import "strconv" - -func _() { - // An "invalid array index" compiler error signifies that the constant values have changed. - // Re-run the stringer command to generate them again. - var x [1]struct{} - _ = x[unspecified-0] - _ = x[none-1] - _ = x[legacy-2] -} - -const _reorderOutput_name = "unspecifiednonelegacy" - -var _reorderOutput_index = [...]uint8{0, 11, 15, 21} - -func (i reorderOutput) String() string { - if i < 0 || i >= reorderOutput(len(_reorderOutput_index)-1) { - return "reorderOutput(" + strconv.FormatInt(int64(i), 10) + ")" - } - return _reorderOutput_name[_reorderOutput_index[i]:_reorderOutput_index[i+1]] -}