...

Source file src/helm.sh/helm/v3/pkg/releaseutil/kind_sorter.go

Documentation: helm.sh/helm/v3/pkg/releaseutil

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package releaseutil
    18  
    19  import (
    20  	"sort"
    21  
    22  	"helm.sh/helm/v3/pkg/release"
    23  )
    24  
    25  // KindSortOrder is an ordering of Kinds.
    26  type KindSortOrder []string
    27  
    28  // InstallOrder is the order in which manifests should be installed (by Kind).
    29  //
    30  // Those occurring earlier in the list get installed before those occurring later in the list.
    31  var InstallOrder KindSortOrder = []string{
    32  	"PriorityClass",
    33  	"Namespace",
    34  	"NetworkPolicy",
    35  	"ResourceQuota",
    36  	"LimitRange",
    37  	"PodSecurityPolicy",
    38  	"PodDisruptionBudget",
    39  	"ServiceAccount",
    40  	"Secret",
    41  	"SecretList",
    42  	"ConfigMap",
    43  	"StorageClass",
    44  	"PersistentVolume",
    45  	"PersistentVolumeClaim",
    46  	"CustomResourceDefinition",
    47  	"ClusterRole",
    48  	"ClusterRoleList",
    49  	"ClusterRoleBinding",
    50  	"ClusterRoleBindingList",
    51  	"Role",
    52  	"RoleList",
    53  	"RoleBinding",
    54  	"RoleBindingList",
    55  	"Service",
    56  	"DaemonSet",
    57  	"Pod",
    58  	"ReplicationController",
    59  	"ReplicaSet",
    60  	"Deployment",
    61  	"HorizontalPodAutoscaler",
    62  	"StatefulSet",
    63  	"Job",
    64  	"CronJob",
    65  	"IngressClass",
    66  	"Ingress",
    67  	"APIService",
    68  }
    69  
    70  // UninstallOrder is the order in which manifests should be uninstalled (by Kind).
    71  //
    72  // Those occurring earlier in the list get uninstalled before those occurring later in the list.
    73  var UninstallOrder KindSortOrder = []string{
    74  	"APIService",
    75  	"Ingress",
    76  	"IngressClass",
    77  	"Service",
    78  	"CronJob",
    79  	"Job",
    80  	"StatefulSet",
    81  	"HorizontalPodAutoscaler",
    82  	"Deployment",
    83  	"ReplicaSet",
    84  	"ReplicationController",
    85  	"Pod",
    86  	"DaemonSet",
    87  	"RoleBindingList",
    88  	"RoleBinding",
    89  	"RoleList",
    90  	"Role",
    91  	"ClusterRoleBindingList",
    92  	"ClusterRoleBinding",
    93  	"ClusterRoleList",
    94  	"ClusterRole",
    95  	"CustomResourceDefinition",
    96  	"PersistentVolumeClaim",
    97  	"PersistentVolume",
    98  	"StorageClass",
    99  	"ConfigMap",
   100  	"SecretList",
   101  	"Secret",
   102  	"ServiceAccount",
   103  	"PodDisruptionBudget",
   104  	"PodSecurityPolicy",
   105  	"LimitRange",
   106  	"ResourceQuota",
   107  	"NetworkPolicy",
   108  	"Namespace",
   109  	"PriorityClass",
   110  }
   111  
   112  // sort manifests by kind.
   113  //
   114  // Results are sorted by 'ordering', keeping order of items with equal kind/priority
   115  func sortManifestsByKind(manifests []Manifest, ordering KindSortOrder) []Manifest {
   116  	sort.SliceStable(manifests, func(i, j int) bool {
   117  		return lessByKind(manifests[i], manifests[j], manifests[i].Head.Kind, manifests[j].Head.Kind, ordering)
   118  	})
   119  
   120  	return manifests
   121  }
   122  
   123  // sort hooks by kind, using an out-of-place sort to preserve the input parameters.
   124  //
   125  // Results are sorted by 'ordering', keeping order of items with equal kind/priority
   126  func sortHooksByKind(hooks []*release.Hook, ordering KindSortOrder) []*release.Hook {
   127  	h := hooks
   128  	sort.SliceStable(h, func(i, j int) bool {
   129  		return lessByKind(h[i], h[j], h[i].Kind, h[j].Kind, ordering)
   130  	})
   131  
   132  	return h
   133  }
   134  
   135  func lessByKind(_ interface{}, _ interface{}, kindA string, kindB string, o KindSortOrder) bool {
   136  	ordering := make(map[string]int, len(o))
   137  	for v, k := range o {
   138  		ordering[k] = v
   139  	}
   140  
   141  	first, aok := ordering[kindA]
   142  	second, bok := ordering[kindB]
   143  
   144  	if !aok && !bok {
   145  		// if both are unknown then sort alphabetically by kind, keep original order if same kind
   146  		if kindA != kindB {
   147  			return kindA < kindB
   148  		}
   149  		return first < second
   150  	}
   151  	// unknown kind is last
   152  	if !aok {
   153  		return false
   154  	}
   155  	if !bok {
   156  		return true
   157  	}
   158  	// sort different kinds, keep original order if same priority
   159  	return first < second
   160  }
   161  

View as plain text