From f5df47b1c855e02c66f57756af224bc9b1055b09 Mon Sep 17 00:00:00 2001 From: Alexander Matyushentsev Date: Fri, 1 Feb 2019 09:02:06 -0800 Subject: [PATCH] Fix: kind sorter incorrectly compares unknown and namespace (#5186) Signed-off-by: Alexander Matyushentsev --- pkg/tiller/kind_sorter.go | 17 ++++++++++++----- pkg/tiller/kind_sorter_test.go | 21 +++++++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/pkg/tiller/kind_sorter.go b/pkg/tiller/kind_sorter.go index 8aff4e6c1..ceeb0f928 100644 --- a/pkg/tiller/kind_sorter.go +++ b/pkg/tiller/kind_sorter.go @@ -124,14 +124,16 @@ func (k *kindSorter) Less(i, j int) bool { b := k.manifests[j] first, aok := k.ordering[a.Head.Kind] second, bok := k.ordering[b.Head.Kind] - // if same kind (including unknown) sub sort alphanumeric - if first == second { - // if both are unknown and of different kind sort by kind alphabetically - if !aok && !bok && a.Head.Kind != b.Head.Kind { + + if !aok && !bok { + // if both are unknown then sort alphabetically by kind and name + if a.Head.Kind != b.Head.Kind { return a.Head.Kind < b.Head.Kind + } else { + return a.Name < b.Name } - return a.Name < b.Name } + // unknown kind is last if !aok { return false @@ -139,6 +141,11 @@ func (k *kindSorter) Less(i, j int) bool { if !bok { return true } + + // if same kind sub sort alphanumeric + if first == second { + return a.Name < b.Name + } // sort different kinds return first < second } diff --git a/pkg/tiller/kind_sorter_test.go b/pkg/tiller/kind_sorter_test.go index 1c187e90d..56822f995 100644 --- a/pkg/tiller/kind_sorter_test.go +++ b/pkg/tiller/kind_sorter_test.go @@ -223,3 +223,24 @@ func TestKindSorterSubSort(t *testing.T) { }) } } + +func TestKindSorterNamespaceAgainstUnknown(t *testing.T) { + unknown := Manifest{ + Name: "a", + Head: &util.SimpleHead{Kind: "Unknown"}, + } + namespace := Manifest{ + Name: "b", + Head: &util.SimpleHead{Kind: "Namespace"}, + } + + manifests := []Manifest{unknown, namespace} + sortByKind(manifests, InstallOrder) + + expectedOrder := []Manifest{namespace, unknown} + for i, manifest := range manifests { + if expectedOrder[i].Name != manifest.Name { + t.Errorf("Expected %s, got %s", expectedOrder[i].Name, manifest.Name) + } + } +}