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