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 Nov 21, 2022
1 parent 09bd592 commit 40ae45b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 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
9 changes: 6 additions & 3 deletions kustomize/commands/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"

"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
"sigs.k8s.io/kustomize/api/konfig"
"sigs.k8s.io/kustomize/api/krusty"
"sigs.k8s.io/kustomize/api/types"
Expand Down Expand Up @@ -75,7 +76,7 @@ func NewCmdBuild(
return err
}
k := krusty.MakeKustomizer(
HonorKustomizeFlags(krusty.MakeDefaultOptions()),
HonorKustomizeFlags(krusty.MakeDefaultOptions(), cmd.Flags()),
)
m, err := k.Run(fSys, theArgs.kustomizationPath)
if err != nil {
Expand Down Expand Up @@ -104,6 +105,8 @@ func NewCmdBuild(
AddFlagEnablePlugins(cmd.Flags())
AddFlagReorderOutput(cmd.Flags())
AddFlagEnableManagedbyLabel(cmd.Flags())
cmd.Flags().MarkDeprecated(flagReorderOutputName,
"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 All @@ -130,8 +133,8 @@ 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
func HonorKustomizeFlags(kOpts *krusty.Options, flags *flag.FlagSet) *krusty.Options {
kOpts.Reorder = getFlagReorderOutput(flags)
kOpts.LoadRestrictions = getFlagLoadRestrictorValue()
if theFlags.enable.plugins {
c := types.EnabledPluginConfig(types.BploUseStaticallyLinked)
Expand Down

0 comments on commit 40ae45b

Please sign in to comment.