From 9d82d54c5b06a79a5faaf71742504d0390e61ed1 Mon Sep 17 00:00:00 2001 From: Andrew Lavery Date: Thu, 1 Nov 2018 12:22:21 -0700 Subject: [PATCH] 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 --- pkg/gvk/gvk.go | 4 +++- pkg/gvk/gvk_test.go | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pkg/gvk/gvk.go b/pkg/gvk/gvk.go index 8eaf7fafd..d9e3d4424 100644 --- a/pkg/gvk/gvk.go +++ b/pkg/gvk/gvk.go @@ -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 diff --git a/pkg/gvk/gvk_test.go b/pkg/gvk/gvk_test.go index 469e603aa..6e235ed3e 100644 --- a/pkg/gvk/gvk_test.go +++ b/pkg/gvk/gvk_test.go @@ -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) + } } }