github.com/zoumo/helm@v2.5.0+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  }
    53  
    54  // UninstallOrder is the order in which manifests should be uninstalled (by Kind).
    55  //
    56  // Those occurring earlier in the list get uninstalled before those occurring later in the list.
    57  var UninstallOrder SortOrder = []string{
    58  	"Ingress",
    59  	"Service",
    60  	"CronJob",
    61  	"Job",
    62  	"StatefulSet",
    63  	"Deployment",
    64  	"ReplicaSet",
    65  	"ReplicationController",
    66  	"Pod",
    67  	"DaemonSet",
    68  	"RoleBinding",
    69  	"Role",
    70  	"ClusterRoleBinding",
    71  	"ClusterRole",
    72  	"ServiceAccount",
    73  	"PersistentVolumeClaim",
    74  	"PersistentVolume",
    75  	"ConfigMap",
    76  	"Secret",
    77  	"LimitRange",
    78  	"ResourceQuota",
    79  	"Namespace",
    80  }
    81  
    82  // sortByKind does an in-place sort of manifests by Kind.
    83  //
    84  // Results are sorted by 'ordering'
    85  func sortByKind(manifests []manifest, ordering SortOrder) []manifest {
    86  	ks := newKindSorter(manifests, ordering)
    87  	sort.Sort(ks)
    88  	return ks.manifests
    89  }
    90  
    91  type kindSorter struct {
    92  	ordering  map[string]int
    93  	manifests []manifest
    94  }
    95  
    96  func newKindSorter(m []manifest, s SortOrder) *kindSorter {
    97  	o := make(map[string]int, len(s))
    98  	for v, k := range s {
    99  		o[k] = v
   100  	}
   101  
   102  	return &kindSorter{
   103  		manifests: m,
   104  		ordering:  o,
   105  	}
   106  }
   107  
   108  func (k *kindSorter) Len() int { return len(k.manifests) }
   109  
   110  func (k *kindSorter) Swap(i, j int) { k.manifests[i], k.manifests[j] = k.manifests[j], k.manifests[i] }
   111  
   112  func (k *kindSorter) Less(i, j int) bool {
   113  	a := k.manifests[i]
   114  	b := k.manifests[j]
   115  	first, ok := k.ordering[a.head.Kind]
   116  	if !ok {
   117  		// Unknown is always last
   118  		return false
   119  	}
   120  	second, ok := k.ordering[b.head.Kind]
   121  	if !ok {
   122  		return true
   123  	}
   124  	return first < second
   125  }