release: clarify comparison for unknown kinds

Signed-off-by: Raushan Kumar <raus7n@gmail.com>
pull/32386/head
Raushan Kumar 3 months ago
parent ca5ca56275
commit 9ec011e9f1

@ -114,24 +114,24 @@ var UninstallOrder KindSortOrder = []string{
"PriorityClass", "PriorityClass",
} }
func buildKindOrderMap(o KindSortOrder) map[string]int { type KindOrderMap map[string]int
ordering := make(map[string]int, len(o))
for i, kind := range o { func NewKindOrderMap(ordering KindSortOrder) KindOrderMap {
ordering[kind] = i orderMap := make(KindOrderMap, len(ordering))
for i, kind := range ordering {
orderMap[kind] = i
} }
return ordering return orderMap
} }
// sort manifests by kind. // sort manifests by kind.
// Results are sorted by 'ordering', keeping order of items with equal kind/priority // Results are sorted by 'ordering', keeping order of items with equal kind/priority
func sortManifestsByKind(manifests []Manifest, ordering KindSortOrder) []Manifest { func sortManifestsByKind(manifests []Manifest, ordering KindSortOrder) []Manifest {
orderMap := buildKindOrderMap(ordering) orderMap := NewKindOrderMap(ordering)
sort.SliceStable(manifests, func(i, j int) bool { sort.SliceStable(manifests, func(i, j int) bool {
return lessByKind( return orderMap.Less(
manifests[i].Head.Kind, manifests[i].Head.Kind,
manifests[j].Head.Kind, manifests[j].Head.Kind,
orderMap,
) )
}) })
@ -142,34 +142,29 @@ func sortManifestsByKind(manifests []Manifest, ordering KindSortOrder) []Manifes
// //
// Results are sorted by 'ordering', keeping order of items with equal kind/priority // Results are sorted by 'ordering', keeping order of items with equal kind/priority
func sortHooksByKind(hooks []*release.Hook, ordering KindSortOrder) []*release.Hook { func sortHooksByKind(hooks []*release.Hook, ordering KindSortOrder) []*release.Hook {
orderMap := buildKindOrderMap(ordering) orderMap := NewKindOrderMap(ordering)
sort.SliceStable(hooks, func(i, j int) bool { sort.SliceStable(hooks, func(i, j int) bool {
return lessByKind( return orderMap.Less(
hooks[i].Kind, hooks[i].Kind,
hooks[j].Kind, hooks[j].Kind,
orderMap,
) )
}) })
return hooks return hooks
} }
func lessByKind( func (k KindOrderMap) Less(kindA, kindB string) bool {
kindA string, first, aok := k[kindA]
kindB string, second, bok := k[kindB]
orderMap map[string]int,
) bool {
first, aok := orderMap[kindA]
second, bok := orderMap[kindB]
if !aok && !bok { if !aok && !bok {
// if both are unknown then sort alphabetically by kind, keep original order if same kind // if both are unknown then sort alphabetically by kind, keep original order if same kind
if kindA != kindB { if kindA != kindB {
return kindA < kindB return kindA < kindB
} }
return first < second return false
} }
// unknown kind is last // unknown kind is last
if !aok { if !aok {
return false return false
@ -177,6 +172,7 @@ func lessByKind(
if !bok { if !bok {
return true return true
} }
// sort different kinds, keep original order if same priority // sort different kinds, keep original order if same priority
return first < second return first < second
} }

@ -114,25 +114,26 @@ var UninstallOrder KindSortOrder = []string{
"PriorityClass", "PriorityClass",
} }
func buildKindOrderMap(o KindSortOrder) map[string]int { type KindOrderMap map[string]int
ordering := make(map[string]int, len(o))
for i, kind := range o { func NewKindOrderMap(ordering KindSortOrder) KindOrderMap {
ordering[kind] = i orderMap := make(KindOrderMap, len(ordering))
for i, kind := range ordering {
orderMap[kind] = i
} }
return ordering return orderMap
} }
// sort manifests by kind. // sort manifests by kind.
// //
// Results are sorted by 'ordering', keeping order of items with equal kind/priority // Results are sorted by 'ordering', keeping order of items with equal kind/priority
func sortManifestsByKind(manifests []Manifest, ordering KindSortOrder) []Manifest { func sortManifestsByKind(manifests []Manifest, ordering KindSortOrder) []Manifest {
orderMap := buildKindOrderMap(ordering) orderMap := NewKindOrderMap(ordering)
sort.SliceStable(manifests, func(i, j int) bool { sort.SliceStable(manifests, func(i, j int) bool {
return lessByKind( return orderMap.Less(
manifests[i].Head.Kind, manifests[i].Head.Kind,
manifests[j].Head.Kind, manifests[j].Head.Kind,
orderMap,
) )
}) })
@ -143,34 +144,30 @@ func sortManifestsByKind(manifests []Manifest, ordering KindSortOrder) []Manifes
// //
// Results are sorted by 'ordering', keeping order of items with equal kind/priority // Results are sorted by 'ordering', keeping order of items with equal kind/priority
func sortHooksByKind(hooks []*release.Hook, ordering KindSortOrder) []*release.Hook { func sortHooksByKind(hooks []*release.Hook, ordering KindSortOrder) []*release.Hook {
orderMap := buildKindOrderMap(ordering) orderMap := NewKindOrderMap(ordering)
sort.SliceStable(hooks, func(i, j int) bool { sort.SliceStable(hooks, func(i, j int) bool {
return lessByKind( return orderMap.Less(
hooks[i].Kind, hooks[i].Kind,
hooks[j].Kind, hooks[j].Kind,
orderMap,
) )
}) })
return hooks return hooks
} }
func lessByKind( func (k KindOrderMap) Less(kindA, kindB string) bool {
kindA string, first, aok := k[kindA]
kindB string, second, bok := k[kindB]
orderMap map[string]int,
) bool {
first, aok := orderMap[kindA]
second, bok := orderMap[kindB]
if !aok && !bok { if !aok && !bok {
// if both are unknown then sort alphabetically by kind, keep original order if same kind // if both are unknown then sort alphabetically by kind, keep original order if same kind
if kindA != kindB { if kindA != kindB {
return kindA < kindB return kindA < kindB
} }
return first < second return false
} }
// unknown kind is last // unknown kind is last
if !aok { if !aok {
return false return false
@ -178,6 +175,7 @@ func lessByKind(
if !bok { if !bok {
return true return true
} }
// sort different kinds, keep original order if same priority // sort different kinds, keep original order if same priority
return first < second return first < second
} }

Loading…
Cancel
Save