github.com/felipejfc/helm@v2.1.2+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  var InstallOrder SortOrder = []string{"Namespace", "Secret", "ConfigMap", "PersistentVolume", "ServiceAccount", "Service", "Pod", "ReplicationController", "Deployment", "DaemonSet", "Ingress", "Job"}
    28  
    29  // UninstallOrder is the order in which manifests should be uninstalled (by Kind)
    30  var UninstallOrder SortOrder = []string{"Service", "Pod", "ReplicationController", "Deployment", "DaemonSet", "ConfigMap", "Secret", "PersistentVolume", "ServiceAccount", "Ingress", "Job", "Namespace"}
    31  
    32  // sortByKind does an in-place sort of manifests by Kind.
    33  //
    34  // Results are sorted by 'ordering'
    35  func sortByKind(manifests []manifest, ordering SortOrder) []manifest {
    36  	ks := newKindSorter(manifests, ordering)
    37  	sort.Sort(ks)
    38  	return ks.manifests
    39  }
    40  
    41  type kindSorter struct {
    42  	ordering  map[string]int
    43  	manifests []manifest
    44  }
    45  
    46  func newKindSorter(m []manifest, s SortOrder) *kindSorter {
    47  	o := make(map[string]int, len(s))
    48  	for v, k := range s {
    49  		o[k] = v
    50  	}
    51  
    52  	return &kindSorter{
    53  		manifests: m,
    54  		ordering:  o,
    55  	}
    56  }
    57  
    58  func (k *kindSorter) Len() int { return len(k.manifests) }
    59  
    60  func (k *kindSorter) Swap(i, j int) { k.manifests[i], k.manifests[j] = k.manifests[j], k.manifests[i] }
    61  
    62  func (k *kindSorter) Less(i, j int) bool {
    63  	a := k.manifests[i]
    64  	b := k.manifests[j]
    65  	first, ok := k.ordering[a.head.Kind]
    66  	if !ok {
    67  		// Unknown is always last
    68  		return false
    69  	}
    70  	second, ok := k.ordering[b.head.Kind]
    71  	if !ok {
    72  		return true
    73  	}
    74  	return first < second
    75  }