github.com/pensu/helm@v2.6.1+incompatible/pkg/tiller/kind_sorter.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 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 "Secret", 34 "ConfigMap", 35 "PersistentVolume", 36 "PersistentVolumeClaim", 37 "ServiceAccount", 38 "ClusterRole", 39 "ClusterRoleBinding", 40 "Role", 41 "RoleBinding", 42 "Service", 43 "DaemonSet", 44 "Pod", 45 "ReplicationController", 46 "ReplicaSet", 47 "Deployment", 48 "StatefulSet", 49 "Job", 50 "CronJob", 51 "Ingress", 52 "APIService", 53 } 54 55 // UninstallOrder is the order in which manifests should be uninstalled (by Kind). 56 // 57 // Those occurring earlier in the list get uninstalled before those occurring later in the list. 58 var UninstallOrder SortOrder = []string{ 59 "APIService", 60 "Ingress", 61 "Service", 62 "CronJob", 63 "Job", 64 "StatefulSet", 65 "Deployment", 66 "ReplicaSet", 67 "ReplicationController", 68 "Pod", 69 "DaemonSet", 70 "RoleBinding", 71 "Role", 72 "ClusterRoleBinding", 73 "ClusterRole", 74 "ServiceAccount", 75 "PersistentVolumeClaim", 76 "PersistentVolume", 77 "ConfigMap", 78 "Secret", 79 "LimitRange", 80 "ResourceQuota", 81 "Namespace", 82 } 83 84 // sortByKind does an in-place sort of manifests by Kind. 85 // 86 // Results are sorted by 'ordering' 87 func sortByKind(manifests []manifest, ordering SortOrder) []manifest { 88 ks := newKindSorter(manifests, ordering) 89 sort.Sort(ks) 90 return ks.manifests 91 } 92 93 type kindSorter struct { 94 ordering map[string]int 95 manifests []manifest 96 } 97 98 func newKindSorter(m []manifest, s SortOrder) *kindSorter { 99 o := make(map[string]int, len(s)) 100 for v, k := range s { 101 o[k] = v 102 } 103 104 return &kindSorter{ 105 manifests: m, 106 ordering: o, 107 } 108 } 109 110 func (k *kindSorter) Len() int { return len(k.manifests) } 111 112 func (k *kindSorter) Swap(i, j int) { k.manifests[i], k.manifests[j] = k.manifests[j], k.manifests[i] } 113 114 func (k *kindSorter) Less(i, j int) bool { 115 a := k.manifests[i] 116 b := k.manifests[j] 117 first, aok := k.ordering[a.head.Kind] 118 second, bok := k.ordering[b.head.Kind] 119 if first == second { 120 // same kind (including unknown) so sub sort alphanumeric 121 return a.name < b.name 122 } 123 // unknown kind is last 124 if !aok { 125 return false 126 } 127 if !bok { 128 return true 129 } 130 // sort different kinds 131 return first < second 132 }