Make ordering configurable (#4019)

* api: Add new types for customizeable resource ordering

Signed-off-by: Yannis Zarkadas <yanniszark@arrikto.com>

* plugins: Implement SortOrderTransformer plugin

Implement the SortOrderTransformer plugin. This plugin allows the user
to customize the order that kustomize will output resources in.

The API for the plugin is the following:

sortOptions:
  order: legacy | fifo
  legacySortOptions:
    orderFirst:
    - {GVK}
    orderLast:
    - {GVK}

Signed-off-by: Yannis Zarkadas <yanniszark@arrikto.com>

* plugins: Add boilerplate and generate code for new SortOrderTransformer

Signed-off-by: Yannis Zarkadas <yanniszark@arrikto.com>

* build: Add option to denote if the reorder flag was set by the user

We want to take different actions if the reorder flag was set by the
user or filled by the default value. Thus, we propagate this information
from build to the krusty options.

Signed-off-by: Yannis Zarkadas <yanniszark@gmail.com>

* 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 <yanniszark@gmail.com>

* krusty: Add e2e test for SortOrderTransformer

Signed-off-by: Yannis Zarkadas <yanniszark@arrikto.com>

* plugins: Purge LegacyOrderTransformer

Signed-off-by: Yannis Zarkadas <yanniszark@gmail.com>

* Update go.work.sum

Signed-off-by: Yannis Zarkadas <yanniszark@gmail.com>

* review: Make review changes

Signed-off-by: Yannis Zarkadas <yanniszark@gmail.com>

Signed-off-by: Yannis Zarkadas <yanniszark@arrikto.com>
Signed-off-by: Yannis Zarkadas <yanniszark@gmail.com>
This commit is contained in:
Yannis Zarkadas
2022-12-02 16:59:53 -05:00
committed by GitHub
parent 1e2e7bbc0b
commit a502717460
30 changed files with 1532 additions and 1092 deletions

View File

@@ -101,6 +101,9 @@ type Kustomization struct {
// value of the specified field has been determined.
Vars []Var `json:"vars,omitempty" yaml:"vars,omitempty"`
// SortOptions change the order that kustomize outputs resources.
SortOptions *SortOptions `json:"sortOptions,omitempty" yaml:"sortOptions,omitempty"`
//
// Operands - what kustomize operates on.
//

28
api/types/sortoptions.go Normal file
View File

@@ -0,0 +1,28 @@
// Copyright 2021 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package types
// SortOptions defines the order that kustomize outputs resources.
type SortOptions struct {
// Order selects the ordering strategy.
Order SortOrder `json:"order,omitempty" yaml:"order,omitempty"`
// LegacySortOptions tweaks the sorting for the "legacy" sort ordering
// strategy.
LegacySortOptions *LegacySortOptions `json:"legacySortOptions,omitempty" yaml:"legacySortOptions,omitempty"`
}
// SortOrder defines different ordering strategies.
type SortOrder string
const LegacySortOrder SortOrder = "legacy"
const FIFOSortOrder SortOrder = "fifo"
// LegacySortOptions define various options for tweaking the "legacy" ordering
// strategy.
type LegacySortOptions struct {
// OrderFirst selects the resource kinds to order first.
OrderFirst []string `json:"orderFirst" yaml:"orderFirst"`
// OrderLast selects the resource kinds to order last.
OrderLast []string `json:"orderLast" yaml:"orderLast"`
}