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