add support for order-weight for non-hook artifacts

Signed-off-by: Ashok Pon Kumar <ashokponkumar@gmail.com>
Signed-off-by: Ashok Pon Kumar <ashokponkumar@in.ibm.com>
pull/8441/head
Ashok Pon Kumar 5 years ago
parent be57f75163
commit dffba66dcf

@ -31,7 +31,7 @@ const resourcePolicyAnno = "helm.sh/resource-policy"
// during an uninstallRelease action.
const keepPolicy = "keep"
func filterManifestsToKeep(manifests []releaseutil.Manifest) (keep, remaining []releaseutil.Manifest) {
func filterManifestsToKeep(manifests []*releaseutil.Manifest) (keep, remaining []*releaseutil.Manifest) {
for _, m := range manifests {
if m.Head.Metadata == nil || m.Head.Metadata.Annotations == nil || len(m.Head.Metadata.Annotations) == 0 {
remaining = append(remaining, m)

@ -108,12 +108,13 @@ var UninstallOrder KindSortOrder = []string{
// sort manifests by kind.
//
// Results are sorted by 'ordering', keeping order of items with equal kind/priority
func sortManifestsByKind(manifests []Manifest, ordering KindSortOrder) []Manifest {
sort.SliceStable(manifests, func(i, j int) bool {
return lessByKind(manifests[i], manifests[j], manifests[i].Head.Kind, manifests[j].Head.Kind, ordering)
func sortManifestsByKind(manifests []*Manifest, ordering KindSortOrder) []*Manifest {
m := manifests
sort.SliceStable(m, func(i, j int) bool {
return lessByKind(m[i], m[j], m[i].Head.Kind, m[j].Head.Kind, ordering)
})
return manifests
return m
}
// sort hooks by kind, using an out-of-place sort to preserve the input parameters.

@ -24,7 +24,7 @@ import (
)
func TestKindSorter(t *testing.T) {
manifests := []Manifest{
manifests := []*Manifest{
{
Name: "E",
Head: &SimpleHead{Kind: "SecretList"},
@ -199,7 +199,7 @@ func TestKindSorter(t *testing.T) {
// TestKindSorterKeepOriginalOrder verifies manifests of same kind are kept in original order
func TestKindSorterKeepOriginalOrder(t *testing.T) {
manifests := []Manifest{
manifests := []*Manifest{
{
Name: "a",
Head: &SimpleHead{Kind: "ClusterRole"},
@ -259,19 +259,19 @@ func TestKindSorterKeepOriginalOrder(t *testing.T) {
}
func TestKindSorterNamespaceAgainstUnknown(t *testing.T) {
unknown := Manifest{
unknown := &Manifest{
Name: "a",
Head: &SimpleHead{Kind: "Unknown"},
}
namespace := Manifest{
namespace := &Manifest{
Name: "b",
Head: &SimpleHead{Kind: "Namespace"},
}
manifests := []Manifest{unknown, namespace}
manifests := []*Manifest{unknown, namespace}
manifests = sortManifestsByKind(manifests, InstallOrder)
expectedOrder := []Manifest{namespace, unknown}
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)

@ -23,6 +23,9 @@ import (
"strings"
)
// OrderWeightAnnotation is the label name for a weight of a manifest
const OrderWeightAnnotation = "helm.sh/order-weight"
// SimpleHead defines what the structure of the head of a manifest file
type SimpleHead struct {
Version string `json:"apiVersion"`

@ -34,6 +34,7 @@ import (
type Manifest struct {
Name string
Content string
Weight int
Head *SimpleHead
}
@ -47,7 +48,7 @@ type manifestFile struct {
// result is an intermediate structure used during sorting.
type result struct {
hooks []*release.Hook
generic []Manifest
generic []*Manifest
}
// TODO: Refactor this out. It's here because naming conventions were not followed through.
@ -75,7 +76,7 @@ var events = map[string]release.HookEvent{
//
// Files that do not parse into the expected format are simply placed into a map and
// returned.
func SortManifests(files map[string]string, apis chartutil.VersionSet, ordering KindSortOrder) ([]*release.Hook, []Manifest, error) {
func SortManifests(files map[string]string, apis chartutil.VersionSet, ordering KindSortOrder) ([]*release.Hook, []*Manifest, error) {
result := &result{}
var sortedFilePaths []string
@ -108,7 +109,11 @@ func SortManifests(files map[string]string, apis chartutil.VersionSet, ordering
}
}
return sortHooksByKind(result.hooks, ordering), sortManifestsByKind(result.generic, ordering), nil
sortedManifests := sortManifestsByKind(result.generic, ordering)
// manifests are ordered by kind, so keep order stable
sort.Stable(manifestByWeight(sortedManifests))
return sortHooksByKind(result.hooks, ordering), sortedManifests, nil
}
// sort takes a manifestFile object which may contain multiple resource definition
@ -147,7 +152,7 @@ func (file *manifestFile) sort(result *result) error {
}
if !hasAnyAnnotation(entry) {
result.generic = append(result.generic, Manifest{
result.generic = append(result.generic, &Manifest{
Name: file.path,
Content: m,
Head: &entry,
@ -157,9 +162,11 @@ func (file *manifestFile) sort(result *result) error {
hookTypes, ok := entry.Metadata.Annotations[release.HookAnnotation]
if !ok {
result.generic = append(result.generic, Manifest{
ow := calculateOrderWeight(entry)
result.generic = append(result.generic, &Manifest{
Name: file.path,
Content: m,
Weight: ow,
Head: &entry,
})
continue
@ -222,6 +229,18 @@ func calculateHookWeight(entry SimpleHead) int {
return hw
}
// calculateOrderWeight finds the weight in the hook weight annotation.
//
// If no weight is found, the assigned weight is 0
func calculateOrderWeight(entry SimpleHead) int {
ows := entry.Metadata.Annotations[OrderWeightAnnotation]
ow, err := strconv.Atoi(ows)
if err != nil {
ow = 0
}
return ow
}
// operateAnnotationValues finds the given annotation and runs the operate function with the value of that annotation
func operateAnnotationValues(entry SimpleHead, annotation string, operate func(p string)) {
if dps, ok := entry.Metadata.Annotations[annotation]; ok {
@ -231,3 +250,12 @@ func operateAnnotationValues(entry SimpleHead, annotation string, operate func(p
}
}
}
// manifestByWeight is a sorter for manifests
type manifestByWeight []*Manifest
func (x manifestByWeight) Len() int { return len(x) }
func (x manifestByWeight) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
func (x manifestByWeight) Less(i, j int) bool {
return x[i].Weight < x[j].Weight
}

@ -193,7 +193,7 @@ metadata:
}
// Verify the sort order
sorted := []Manifest{}
sorted := []*Manifest{}
for _, s := range data {
manifests := SplitManifests(s.manifest)
@ -209,7 +209,7 @@ metadata:
// only keep track of non-hook manifests
if s.hooks[name] == nil {
another := Manifest{
another := &Manifest{
Content: m,
Name: name,
Head: &sh,

Loading…
Cancel
Save