add support for weighted manifests

pull/12540/head
Chris Randles 2 years ago
parent f3099cdb67
commit 4d4022f66e

@ -18,6 +18,7 @@ package releaseutil
import ( import (
"sort" "sort"
"strconv"
"helm.sh/helm/v3/pkg/release" "helm.sh/helm/v3/pkg/release"
) )
@ -109,11 +110,29 @@ var UninstallOrder KindSortOrder = []string{
"PriorityClass", "PriorityClass",
} }
// OrderWeightAnnotation is the label name for configuring the sorted weight of a manifest
const OrderWeightAnnotation = "helm.sh/order-weight"
// 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
// if a manifest defines a `helm.sh/order-weight` annotation, i
func sortManifestsByKind(manifests []Manifest, ordering KindSortOrder) []Manifest { func sortManifestsByKind(manifests []Manifest, ordering KindSortOrder) []Manifest {
sort.SliceStable(manifests, func(i, j int) bool { sort.SliceStable(manifests, func(i, j int) bool {
iPriority, err := strconv.Atoi(manifests[i].Head.Metadata.Annotations[OrderWeightAnnotation])
if err != nil {
iPriority = 0
}
jPriority, err := strconv.Atoi(manifests[j].Head.Metadata.Annotations[OrderWeightAnnotation])
if err != nil {
jPriority = 0
}
if iPriority < jPriority {
return true
} else if iPriority > jPriority {
return false
}
// if the resolved priorities match, default to the legacy lessByKind logic for final sorting
return lessByKind(manifests[i], manifests[j], manifests[i].Head.Kind, manifests[j].Head.Kind, ordering) return lessByKind(manifests[i], manifests[j], manifests[i].Head.Kind, manifests[j].Head.Kind, ordering)
}) })

Loading…
Cancel
Save