add fallback for GVK comparison

only return comparison of 'Kind' indices if they do not match
otherwise fall back to GVK string comparison
this reduces output instability
This commit is contained in:
Andrew Lavery
2018-11-01 12:22:21 -07:00
parent d718fe3ee1
commit 9d82d54c5b
2 changed files with 16 additions and 1 deletions

View File

@@ -97,7 +97,9 @@ func (x Gvk) IsLessThan(o Gvk) bool {
indexI, foundI := typeOrders[x.Kind] indexI, foundI := typeOrders[x.Kind]
indexJ, foundJ := typeOrders[o.Kind] indexJ, foundJ := typeOrders[o.Kind]
if foundI && foundJ { if foundI && foundJ {
return indexI < indexJ if indexI != indexJ {
return indexI < indexJ
}
} }
if foundI && !foundJ { if foundI && !foundJ {
return true return true

View File

@@ -48,6 +48,16 @@ var lessThanTests = []struct {
Gvk{Group: "a", Version: "b", Kind: "ClusterRole"}}, Gvk{Group: "a", Version: "b", Kind: "ClusterRole"}},
{Gvk{Group: "a", Version: "b", Kind: "a"}, {Gvk{Group: "a", Version: "b", Kind: "a"},
Gvk{Group: "a", Version: "b", Kind: "b"}}, Gvk{Group: "a", Version: "b", Kind: "b"}},
{Gvk{Group: "a", Version: "b", Kind: "Namespace"},
Gvk{Group: "a", Version: "c", Kind: "Namespace"}},
{Gvk{Group: "a", Version: "c", Kind: "Namespace"},
Gvk{Group: "b", Version: "c", Kind: "Namespace"}},
{Gvk{Group: "b", Version: "c", Kind: "Namespace"},
Gvk{Group: "a", Version: "c", Kind: "ClusterRole"}},
{Gvk{Group: "a", Version: "c", Kind: "Namespace"},
Gvk{Group: "a", Version: "b", Kind: "ClusterRole"}},
{Gvk{Group: "a", Version: "d", Kind: "Namespace"},
Gvk{Group: "b", Version: "c", Kind: "Namespace"}},
} }
func TestIsLessThan1(t *testing.T) { func TestIsLessThan1(t *testing.T) {
@@ -55,6 +65,9 @@ func TestIsLessThan1(t *testing.T) {
if !hey.x1.IsLessThan(hey.x2) { if !hey.x1.IsLessThan(hey.x2) {
t.Fatalf("%v should be less than %v", hey.x1, hey.x2) t.Fatalf("%v should be less than %v", hey.x1, hey.x2)
} }
if hey.x2.IsLessThan(hey.x1) {
t.Fatalf("%v should not be less than %v", hey.x2, hey.x1)
}
} }
} }