Skip to content

Commit

Permalink
api/krusty: Ensure sort ordering works with CLI flag and kustomization
Browse files Browse the repository at this point in the history
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:
kubernetes-sigs#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 <yanniszark@gmail.com>
  • Loading branch information
yanniszark committed Oct 17, 2022
1 parent 24e0475 commit a8734de
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
41 changes: 39 additions & 2 deletions api/krusty/kustomizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -19,6 +20,7 @@ import (
"sigs.k8s.io/kustomize/kyaml/errors"
"sigs.k8s.io/kustomize/kyaml/filesys"
"sigs.k8s.io/kustomize/kyaml/openapi"
"sigs.k8s.io/kustomize/kyaml/resid"
)

// Kustomizer performs kustomizations.
Expand Down Expand Up @@ -90,8 +92,43 @@ 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.SortModeSetExplicitly {
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,
IsTopLevel: true,
}
err = pl.Transform(m)
if err != nil {
return nil, err
}
} else if b.options.DoLegacyResourceSort && kt.Kustomization().SortOptions == nil {
// Case 2: Sort order set in CLI flag only or not at all.
pl := &builtins.SortOrderTransformerPlugin{
SortOptions: &types.SortOptions{
Order: types.LegacySortOrder,
LegacySortOptions: &types.LegacySortOptions{
OrderFirst: resid.OrderFirst,
OrderLast: resid.OrderLast,
},
},
IsTopLevel: true,
}
err = pl.Transform(m)
if err != nil {
return nil, err
}
Expand Down
3 changes: 2 additions & 1 deletion kustomize/commands/build/reorderoutput.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func AddFlagReorderOutput(set *pflag.FlagSet) {
set.StringVar(
&theFlags.reorderOutput, flagReorderOutputName,
legacy.String(),
"Reorder the resources just before output. "+
"Please use the new 'sortOptions' field in kustomization.yaml instead"+
"of this flag. 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.")
Expand Down

0 comments on commit a8734de

Please sign in to comment.