github.com/azure-devops-engineer/helm@v3.0.0-alpha.2+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  	"ClusterRoleBinding",
    40  	"Role",
    41  	"RoleBinding",
    42  	"Service",
    43  	"DaemonSet",
    44  	"Pod",
    45  	"ReplicationController",
    46  	"ReplicaSet",
    47  	"Deployment",
    48  	"HorizontalPodAutoscaler",
    49  	"StatefulSet",
    50  	"Job",
    51  	"CronJob",
    52  	"Ingress",
    53  	"APIService",
    54  }
    55  
    56  // UninstallOrder is the order in which manifests should be uninstalled (by Kind).
    57  //
    58  // Those occurring earlier in the list get uninstalled before those occurring later in the list.
    59  var UninstallOrder KindSortOrder = []string{
    60  	"APIService",
    61  	"Ingress",
    62  	"Service",
    63  	"CronJob",
    64  	"Job",
    65  	"StatefulSet",
    66  	"HorizontalPodAutoscaler",
    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 KindSortOrder) []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 KindSortOrder) *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  }