Merge pull request #528 from laverya/fallback-to-string-sort-in-gvk-comparison-if-indices-equal

add fallback for GVK comparison
This commit is contained in:
k8s-ci-robot
2018-11-02 13:07:56 -07:00
committed by GitHub
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]
indexJ, foundJ := typeOrders[o.Kind]
if foundI && foundJ {
return indexI < indexJ
if indexI != indexJ {
return indexI < indexJ
}
}
if foundI && !foundJ {
return true

View File

@@ -48,6 +48,16 @@ var lessThanTests = []struct {
Gvk{Group: "a", Version: "b", Kind: "ClusterRole"}},
{Gvk{Group: "a", Version: "b", Kind: "a"},
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) {
@@ -55,6 +65,9 @@ func TestIsLessThan1(t *testing.T) {
if !hey.x1.IsLessThan(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)
}
}
}