Skip to content

Commit

Permalink
build: Add option to denote if the reorder flag was set by the user
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
yanniszark committed Oct 17, 2022
1 parent c2a26f1 commit ed74549
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
3 changes: 2 additions & 1 deletion api/krusty/legacy_order_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package krusty_test
import (
"testing"

"sigs.k8s.io/kustomize/api/krusty"
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
)

Expand Down Expand Up @@ -38,7 +39,7 @@ data:
key: value
`)
opts := th.MakeDefaultOptions()
opts.DoLegacyResourceSort = true
opts.Reorder = krusty.ReorderOptionLegacy
m := th.Run(".", opts)
th.AssertActualEqualsExpected(m, `
apiVersion: v1
Expand Down
28 changes: 22 additions & 6 deletions api/krusty/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import (
"sigs.k8s.io/kustomize/api/types"
)

type ReorderOption string

const (
ReorderOptionLegacy ReorderOption = "legacy"
ReorderOptionNone ReorderOption = "none"
ReorderOptionUnspecified ReorderOption = "unspecified"
)

// Options holds high-level kustomize configuration options,
// e.g. are plugins enabled, should the loader be restricted
// to the kustomization root, etc.
Expand All @@ -16,7 +24,15 @@ type Options struct {
// per a particular sort order. When false, don't do the
// sort, and instead respect the depth-first resource input
// order as specified by the kustomization file(s).
DoLegacyResourceSort bool

// Sort the resources before emitting them. Possible values:
// - "legacy": Use a fixed order that kustomize provides for backwards
// compatibility.
// - "none": Respect the depth-first resource input order as specified by the
// kustomization file.
// - "unspecified": The user didn't specify any preference. Kustomize will
// select the appropriate default.
Reorder ReorderOption

// When true, a label
// app.kubernetes.io/managed-by: kustomize-<version>
Expand All @@ -37,11 +53,11 @@ type Options struct {
// MakeDefaultOptions returns a default instance of Options.
func MakeDefaultOptions() *Options {
return &Options{
DoLegacyResourceSort: false,
AddManagedbyLabel: false,
LoadRestrictions: types.LoadRestrictionsRootOnly,
DoPrune: false,
PluginConfig: types.DisabledPluginConfig(),
Reorder: ReorderOptionNone,
AddManagedbyLabel: false,
LoadRestrictions: types.LoadRestrictionsRootOnly,
DoPrune: false,
PluginConfig: types.DisabledPluginConfig(),
}
}

Expand Down
4 changes: 3 additions & 1 deletion kustomize/commands/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ func NewCmdBuild(
AddFlagEnablePlugins(cmd.Flags())
AddFlagReorderOutput(cmd.Flags())
AddFlagEnableManagedbyLabel(cmd.Flags())
cmd.Flags().MarkDeprecated(flagReorderOutputName,
"The flag `reorder` has been deprecated. Use the new 'sortOptions' field in kustomization.yaml instead.")
cmd.Flags().MarkDeprecated(managedByFlag,
"The flag `enable-managedby-label` has been deprecated. Use the `managedByLabel` option in the `buildMetadata` field instead.")
AddFlagEnableHelm(cmd.Flags())
Expand Down Expand Up @@ -131,7 +133,7 @@ func Validate(args []string) error {
// HonorKustomizeFlags feeds command line data to the krusty options.
// Flags and such are held in private package variables.
func HonorKustomizeFlags(kOpts *krusty.Options) *krusty.Options {
kOpts.DoLegacyResourceSort = getFlagReorderOutput() == legacy
kOpts.Reorder = getFlagReorderOutput()
kOpts.LoadRestrictions = getFlagLoadRestrictorValue()
if theFlags.enable.plugins {
c := types.EnabledPluginConfig(types.BploUseStaticallyLinked)
Expand Down

0 comments on commit ed74549

Please sign in to comment.