Skip to content

Commit

Permalink
Fix regression 4388
Browse files Browse the repository at this point in the history
  • Loading branch information
KnVerey committed Feb 2, 2022
1 parent 01d7fae commit a86723c
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 4 deletions.
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"}},
{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

0 comments on commit a86723c

Please sign in to comment.