Skip to content

Commit

Permalink
gvk: Make orderFirst and orderLast public
Browse files Browse the repository at this point in the history
Make orderFirst and orderLast public in preparation of changes to make
the resource ordering configurable.

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

kustomization: Extend with sortOrder and legacySortOptions fields

Extend the kustomization.yaml API with the 'sortOrder' and
'legacySortOptions' fields.

Refs #3913

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

plugins: Extend LegacyOrderTransformer with customizable ordering

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

plugins: Generate new LegacyOrderTransformer

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

Fix existing LegacyOrderTransformer invocations

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

Throw an error if legacySortOptions is set and sortOrder is not legacy

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

api: Updated dep files after running tests

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

cmd/config: Updated dep files after running tests

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

cmd/pluginator: Updated dep files after running tests

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

kustomize: Updated dep files after running tests

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

wip

Signed-off-by: Yannis Zarkadas <yanniszark@arrikto.com>
  • Loading branch information
yanniszark committed Jul 27, 2021
1 parent 91b862b commit bb9e19d
Show file tree
Hide file tree
Showing 14 changed files with 777 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ _builtinplugins = \
ImageTagTransformer.go \
LabelTransformer.go \
LegacyOrderTransformer.go \
SortOrderTransformer.go \
NamespaceTransformer.go \
PatchJson6902Transformer.go \
PatchStrategicMergeTransformer.go \
Expand Down Expand Up @@ -164,6 +165,7 @@ $(pGen)/HashTransformer.go: $(pSrc)/hashtransformer/HashTransformer.go
$(pGen)/ImageTagTransformer.go: $(pSrc)/imagetagtransformer/ImageTagTransformer.go
$(pGen)/LabelTransformer.go: $(pSrc)/labeltransformer/LabelTransformer.go
$(pGen)/LegacyOrderTransformer.go: $(pSrc)/legacyordertransformer/LegacyOrderTransformer.go
$(pGen)/SortOrderTransformer.go: $(pSrc)/sortordertransformer/SortOrderTransformer.go
$(pGen)/NamespaceTransformer.go: $(pSrc)/namespacetransformer/NamespaceTransformer.go
$(pGen)/PatchJson6902Transformer.go: $(pSrc)/patchjson6902transformer/PatchJson6902Transformer.go
$(pGen)/PatchStrategicMergeTransformer.go: $(pSrc)/patchstrategicmergetransformer/PatchStrategicMergeTransformer.go
Expand Down
118 changes: 118 additions & 0 deletions api/builtins/SortOrderTransformer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions api/internal/plugins/builtinhelpers/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const (
ValueAddTransformer
HelmChartInflationGenerator
ReplacementTransformer
SortOrderTransformer
)

var stringToBuiltinPluginTypeMap map[string]BuiltinPluginType
Expand Down Expand Up @@ -78,4 +79,5 @@ var TransformerFactories = map[BuiltinPluginType]func() resmap.TransformerPlugin
ReplacementTransformer: builtins.NewReplacementTransformerPlugin,
ReplicaCountTransformer: builtins.NewReplicaCountTransformerPlugin,
ValueAddTransformer: builtins.NewValueAddTransformerPlugin,
SortOrderTransformer: builtins.NewSortOrderTransformerPlugin,
}
17 changes: 15 additions & 2 deletions api/internal/target/kusttarget_configplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (kt *KustTarget) configureBuiltinGenerators() (
func (kt *KustTarget) configureBuiltinTransformers(
tc *builtinconfig.TransformerConfig) (
result []resmap.Transformer, err error) {
for _, bpt := range []builtinhelpers.BuiltinPluginType{
bpts := []builtinhelpers.BuiltinPluginType{
builtinhelpers.PatchStrategicMergeTransformer,
builtinhelpers.PatchTransformer,
builtinhelpers.NamespaceTransformer,
Expand All @@ -58,7 +58,9 @@ func (kt *KustTarget) configureBuiltinTransformers(
builtinhelpers.ReplicaCountTransformer,
builtinhelpers.ImageTagTransformer,
builtinhelpers.ReplacementTransformer,
} {
builtinhelpers.SortOrderTransformer,
}
for _, bpt := range bpts {
r, err := transformerConfigurators[bpt](
kt, bpt, builtinhelpers.TransformerFactories[bpt], tc)
if err != nil {
Expand Down Expand Up @@ -339,6 +341,17 @@ var transformerConfigurators = map[builtinhelpers.BuiltinPluginType]func(
result = append(result, p)
return result, nil
},
builtinhelpers.SortOrderTransformer: func(
kt *KustTarget, bpt builtinhelpers.BuiltinPluginType, f tFactory, _ *builtinconfig.TransformerConfig) (
result []resmap.Transformer, err error) {
p := f()
err = kt.configureBuiltinPlugin(p, kt.kustomization, bpt)
if err != nil {
return nil, err
}
result = append(result, p)
return result, nil
},
builtinhelpers.ReplicaCountTransformer: func(
kt *KustTarget, bpt builtinhelpers.BuiltinPluginType, f tFactory, tc *builtinconfig.TransformerConfig) (
result []resmap.Transformer, err error) {
Expand Down
15 changes: 13 additions & 2 deletions api/krusty/kustomizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"sigs.k8s.io/kustomize/api/types"
"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 @@ -89,8 +90,18 @@ func (b *Kustomizer) Run(
if err != nil {
return nil, err
}
if b.options.DoLegacyResourceSort {
err = builtins.NewLegacyOrderTransformerPlugin().Transform(m)
if b.options.DoLegacyResourceSort &&
kt.Kustomization().SortOptions == nil {
pl := &builtins.SortOrderTransformerPlugin{
SortOptions: &types.SortOptions{
Order: types.LegacySortOrder,
LegacySortOptions: &types.LegacySortOptions{
OrderFirst: resid.OrderFirst,
OrderLast: resid.OrderLast,
},
},
}
err = pl.Transform(m)
if err != nil {
return nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions api/types/kustomization.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,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.
//
Expand Down
19 changes: 19 additions & 0 deletions api/types/sortoptions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2021 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

package types

type SortOptions struct {
Order SortOrder `json:"order,omitempty" yaml:"order,omitempty"`
LegacySortOptions *LegacySortOptions `json:"legacySortOptions,omitempty" yaml:"legacySortOptions,omitempty"`
}

type SortOrder string

var LegacySortOrder SortOrder = "legacy"
var FIFOSortOrder SortOrder = "fifo"

type LegacySortOptions struct {
OrderFirst []string `json:"orderFirst" yaml:"orderFirst"`
OrderLast []string `json:"orderLast" yaml:"orderLast"`
}
10 changes: 5 additions & 5 deletions kyaml/resid/gvk.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (x Gvk) Equals(o Gvk) bool {
// a Service should come before things that refer to it.
// Namespace should be first.
// In some cases order just specified to provide determinism.
var orderFirst = []string{
var OrderFirst = []string{
"Namespace",
"ResourceQuota",
"StorageClass",
Expand All @@ -157,16 +157,16 @@ var orderFirst = []string{
"CronJob",
"PodDisruptionBudget",
}
var orderLast = []string{
var OrderLast = []string{
"MutatingWebhookConfiguration",
"ValidatingWebhookConfiguration",
}
var typeOrders = func() map[string]int {
m := map[string]int{}
for i, n := range orderFirst {
m[n] = -len(orderFirst) + i
for i, n := range OrderFirst {
m[n] = -len(OrderFirst) + i
}
for i, n := range orderLast {
for i, n := range OrderLast {
m[n] = 1 + i
}
return m
Expand Down
8 changes: 4 additions & 4 deletions kyaml/resid/gvk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ var lessThanTests = []struct {
Gvk{Group: "a", Version: "b", Kind: "ClusterRole"}},
{Gvk{Group: "a", Version: "d", Kind: "Namespace"},
Gvk{Group: "b", Version: "c", Kind: "Namespace"}},
{Gvk{Group: "a", Version: "b", Kind: orderFirst[len(orderFirst)-1]},
Gvk{Group: "a", Version: "b", Kind: orderLast[0]}},
{Gvk{Group: "a", Version: "b", Kind: orderFirst[len(orderFirst)-1]},
{Gvk{Group: "a", Version: "b", Kind: OrderFirst[len(OrderFirst)-1]},
Gvk{Group: "a", Version: "b", Kind: OrderLast[0]}},
{Gvk{Group: "a", Version: "b", Kind: OrderFirst[len(OrderFirst)-1]},
Gvk{Group: "a", Version: "b", Kind: "CustomKindX"}},
{Gvk{Group: "a", Version: "b", Kind: "CustomKindX"},
Gvk{Group: "a", Version: "b", Kind: orderLast[0]}},
Gvk{Group: "a", Version: "b", Kind: OrderLast[0]}},
{Gvk{Group: "a", Version: "b", Kind: "CustomKindA"},
Gvk{Group: "a", Version: "b", Kind: "CustomKindB"}},
{Gvk{Group: "a", Version: "b", Kind: "CustomKindX"},
Expand Down
Loading

0 comments on commit bb9e19d

Please sign in to comment.