github.com/koderover/helm@v2.17.0+incompatible/pkg/tiller/kind_sorter.go (about) 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 tiller 18 19 import ( 20 "sort" 21 ) 22 23 // SortOrder is an ordering of Kinds. 24 type SortOrder []string 25 26 // InstallOrder is the order in which manifests should be installed (by Kind). 27 // 28 // Those occurring earlier in the list get installed before those occurring later in the list. 29 var InstallOrder SortOrder = []string{ 30 "Namespace", 31 "NetworkPolicy", 32 "ResourceQuota", 33 "LimitRange", 34 "PodSecurityPolicy", 35 "PodDisruptionBudget", 36 "Secret", 37 "ConfigMap", 38 "StorageClass", 39 "PersistentVolume", 40 "PersistentVolumeClaim", 41 "ServiceAccount", 42 "CustomResourceDefinition", 43 "ClusterRole", 44 "ClusterRoleList", 45 "ClusterRoleBinding", 46 "ClusterRoleBindingList", 47 "Role", 48 "RoleList", 49 "RoleBinding", 50 "RoleBindingList", 51 "Service", 52 "DaemonSet", 53 "Pod", 54 "ReplicationController", 55 "ReplicaSet", 56 "Deployment", 57 "HorizontalPodAutoscaler", 58 "StatefulSet", 59 "Job", 60 "CronJob", 61 "Ingress", 62 "APIService", 63 } 64 65 // UninstallOrder is the order in which manifests should be uninstalled (by Kind). 66 // 67 // Those occurring earlier in the list get uninstalled before those occurring later in the list. 68 var UninstallOrder SortOrder = []string{ 69 "APIService", 70 "Ingress", 71 "Service", 72 "CronJob", 73 "Job", 74 "StatefulSet", 75 "HorizontalPodAutoscaler", 76 "Deployment", 77 "ReplicaSet", 78 "ReplicationController", 79 "Pod", 80 "DaemonSet", 81 "RoleBindingList", 82 "RoleBinding", 83 "RoleList", 84 "Role", 85 "ClusterRoleBindingList", 86 "ClusterRoleBinding", 87 "ClusterRoleList", 88 "ClusterRole", 89 "CustomResourceDefinition", 90 "ServiceAccount", 91 "PersistentVolumeClaim", 92 "PersistentVolume", 93 "StorageClass", 94 "ConfigMap", 95 "Secret", 96 "PodDisruptionBudget", 97 "PodSecurityPolicy", 98 "LimitRange", 99 "ResourceQuota", 100 "NetworkPolicy", 101 "Namespace", 102 } 103 104 // sortByKind does an in-place sort of manifests by Kind. 105 // 106 // Results are sorted by 'ordering' 107 func sortByKind(manifests []Manifest, ordering SortOrder) []Manifest { 108 ks := newKindSorter(manifests, ordering) 109 sort.Sort(ks) 110 return ks.manifests 111 } 112 113 type kindSorter struct { 114 ordering map[string]int 115 manifests []Manifest 116 } 117 118 func newKindSorter(m []Manifest, s SortOrder) *kindSorter { 119 o := make(map[string]int, len(s)) 120 for v, k := range s { 121 o[k] = v 122 } 123 124 return &kindSorter{ 125 manifests: m, 126 ordering: o, 127 } 128 } 129 130 func (k *kindSorter) Len() int { return len(k.manifests) } 131 132 func (k *kindSorter) Swap(i, j int) { k.manifests[i], k.manifests[j] = k.manifests[j], k.manifests[i] } 133 134 func (k *kindSorter) Less(i, j int) bool { 135 a := k.manifests[i] 136 b := k.manifests[j] 137 first, aok := k.ordering[a.Head.Kind] 138 second, bok := k.ordering[b.Head.Kind] 139 140 if !aok && !bok { 141 // if both are unknown then sort alphabetically by kind and name 142 if a.Head.Kind != b.Head.Kind { 143 return a.Head.Kind < b.Head.Kind 144 } 145 return a.Name < b.Name 146 } 147 148 // unknown kind is last 149 if !aok { 150 return false 151 } 152 if !bok { 153 return true 154 } 155 156 // if same kind sub sort alphanumeric 157 if first == second { 158 return a.Name < b.Name 159 } 160 // sort different kinds 161 return first < second 162 } 163 164 // SortByKind sorts manifests in InstallOrder 165 func SortByKind(manifests []Manifest) []Manifest { 166 ordering := InstallOrder 167 ks := newKindSorter(manifests, ordering) 168 sort.Sort(ks) 169 return ks.manifests 170 }