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