Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regression 4388 #4445

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions api/internal/builtins/PrefixTransformer.go

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

1 change: 1 addition & 0 deletions api/internal/builtins/SuffixTransformer.go

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

62 changes: 62 additions & 0 deletions api/krusty/legacy_order_test.go
@@ -0,0 +1,62 @@
package krusty_test

import (
"testing"

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

func TestIssue4388(t *testing.T) {
th := kusttest_test.MakeHarness(t)
th.WriteK(".", `
resources:
- resources.yaml
`)
th.WriteF("resources.yaml", `
apiVersion: v1
kind: ConfigMap
metadata:
name: testing
data:
key: value
---
apiVersion: v1
kind: ConfigMap
metadata:
name: testing-one
data:
key: value
---
apiVersion: v1
kind: ConfigMap
metadata:
name: testing-two
data:
key: value
`)
opts := th.MakeDefaultOptions()
opts.DoLegacyResourceSort = true
m := th.Run(".", opts)
th.AssertActualEqualsExpected(m, `
apiVersion: v1
data:
key: value
kind: ConfigMap
metadata:
name: testing
---
apiVersion: v1
data:
key: value
kind: ConfigMap
metadata:
name: testing-one
---
apiVersion: v1
data:
key: value
kind: ConfigMap
metadata:
name: testing-two
`)
}
2 changes: 1 addition & 1 deletion api/resmap/idslice.go
Expand Up @@ -33,5 +33,5 @@ func (a IdSlice) Less(i, j int) bool {
if !a[i].Gvk.Equals(a[j].Gvk) {
return a[i].Gvk.IsLessThan(a[j].Gvk)
}
return a[i].String() < a[j].String()
return a[i].LegacySortString() < a[j].LegacySortString()
}
25 changes: 24 additions & 1 deletion kyaml/resid/gvk.go
Expand Up @@ -97,6 +97,29 @@ func (x Gvk) String() string {
return strings.Join([]string{k, v, g}, fieldSep)
}

// legacySortString returns an older version of String() that LegacyOrderTransformer depends on
// to keep its ordering stable across Kustomize versions
func (x Gvk) legacySortString() string {
legacyNoGroup := "~G"
legacyNoVersion := "~V"
legacyNoKind := "~K"
legacyFieldSeparator := "_"

g := x.Group
if g == "" {
g = legacyNoGroup
}
v := x.Version
if v == "" {
v = legacyNoVersion
}
k := x.Kind
if k == "" {
k = legacyNoKind
}
return strings.Join([]string{g, v, k}, legacyFieldSeparator)
}

// ApiVersion returns the combination of Group and Version
func (x Gvk) ApiVersion() string {
var sb strings.Builder
Expand Down Expand Up @@ -180,7 +203,7 @@ func (x Gvk) IsLessThan(o Gvk) bool {
if indexI != indexJ {
return indexI < indexJ
}
return x.String() < o.String()
return x.legacySortString() < o.legacySortString()
}

// IsSelected returns true if `selector` selects `x`; otherwise, false.
Expand Down
4 changes: 2 additions & 2 deletions kyaml/resid/gvk_test.go
Expand Up @@ -47,8 +47,8 @@ var lessThanTests = []struct {
Gvk{Group: "a", Version: "c", Kind: "ClusterRole"}},
{Gvk{Group: "a", Version: "c", Kind: "Namespace"},
Gvk{Group: "a", Version: "b", Kind: "ClusterRole"}},
{Gvk{Group: "b", Version: "c", Kind: "Namespace"},
Gvk{Group: "a", Version: "d", Kind: "Namespace"}},
{Gvk{Group: "a", Version: "d", Kind: "Namespace"},
Gvk{Group: "b", Version: "c", Kind: "Namespace"}},
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{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]},
Expand Down
19 changes: 19 additions & 0 deletions kyaml/resid/resid.go
Expand Up @@ -60,6 +60,25 @@ func (id ResId) String() string {
[]string{id.Gvk.String(), strings.Join([]string{nm, ns}, fieldSep)}, separator)
}

// LegacySortString returns an older version of String() that LegacyOrderTransformer depends on
// to keep its ordering stable across Kustomize versions
func (id ResId) LegacySortString() string {
legacyNoNamespace := "~X"
legacyNoName := "~N"
legacySeparator := "|"

ns := id.Namespace
if ns == "" {
ns = legacyNoNamespace
}
nm := id.Name
if nm == "" {
nm = legacyNoName
}
return strings.Join(
[]string{id.Gvk.String(), ns, nm}, legacySeparator)
}

func FromString(s string) ResId {
values := strings.Split(s, separator)
gvk := GvkFromString(values[0])
Expand Down