github.com/banzaicloud/operator-tools@v0.28.10/pkg/utils/sort.go (about)

     1  // Copyright © 2020 Banzai Cloud
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package utils
    16  
    17  import (
    18  	"sort"
    19  
    20  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    21  	"k8s.io/apimachinery/pkg/runtime"
    22  )
    23  
    24  type RuntimeObjects []runtime.Object
    25  type ResourceOrder string
    26  type ResourceScoreFunc func(o runtime.Object) int
    27  
    28  const (
    29  	InstallResourceOrder   ResourceOrder = "Install"
    30  	UninstallResourceOrder ResourceOrder = "Uninstall"
    31  )
    32  
    33  func (os RuntimeObjects) Sort(order ResourceOrder) {
    34  	var score ResourceScoreFunc
    35  
    36  	switch order {
    37  	case InstallResourceOrder:
    38  		score = InstallObjectOrder()
    39  	case UninstallResourceOrder:
    40  		score = UninstallObjectOrder()
    41  	default:
    42  		score = func(o runtime.Object) int { return 0 }
    43  	}
    44  
    45  	sort.Slice(os, func(i, j int) bool {
    46  		iScore := score(os[i])
    47  		jScore := score(os[j])
    48  		iGVK := os[i].GetObjectKind().GroupVersionKind()
    49  		jGVK := os[j].GetObjectKind().GroupVersionKind()
    50  		var iName, jName string
    51  		if o, ok := os[i].(metav1.Object); ok {
    52  			iName = o.GetName()
    53  		}
    54  		if o, ok := os[j].(metav1.Object); ok {
    55  			jName = o.GetName()
    56  		}
    57  		return iScore < jScore ||
    58  			(iScore == jScore &&
    59  				iGVK.Group < jGVK.Group) ||
    60  			(iScore == jScore &&
    61  				iGVK.Group == jGVK.Group &&
    62  				iGVK.Kind < os[j].GetObjectKind().GroupVersionKind().Kind) ||
    63  			(iScore == jScore &&
    64  				iGVK.Group == jGVK.Group &&
    65  				iGVK.Kind == jGVK.Kind &&
    66  				iName < jName)
    67  	})
    68  }
    69  
    70  func InstallObjectOrder() func(o runtime.Object) int {
    71  	var Order = []string{
    72  		"CustomResourceDefinition",
    73  		"Namespace",
    74  		"ResourceQuota",
    75  		"LimitRange",
    76  		"PodSecurityPolicy",
    77  		"PodDisruptionBudget",
    78  		"Secret",
    79  		"ConfigMap",
    80  		"StorageClass",
    81  		"PersistentVolume",
    82  		"PersistentVolumeClaim",
    83  		"ServiceAccount",
    84  		"ClusterRole",
    85  		"ClusterRoleList",
    86  		"ClusterRoleBinding",
    87  		"ClusterRoleBindingList",
    88  		"Role",
    89  		"RoleList",
    90  		"RoleBinding",
    91  		"RoleBindingList",
    92  		"Service",
    93  		"DaemonSet",
    94  		"Pod",
    95  		"ReplicationController",
    96  		"ReplicaSet",
    97  		"Deployment",
    98  		"HorizontalPodAutoscaler",
    99  		"StatefulSet",
   100  		"Job",
   101  		"CronJob",
   102  		"Ingress",
   103  		"APIService",
   104  		"ValidatingWebhookConfiguration",
   105  		"MutatingWebhookConfiguration",
   106  	}
   107  
   108  	order := make(map[string]int, len(Order))
   109  	for i, kind := range Order {
   110  		order[kind] = i
   111  	}
   112  
   113  	return func(o runtime.Object) int {
   114  		if nr, ok := order[o.GetObjectKind().GroupVersionKind().Kind]; ok {
   115  			return nr
   116  		}
   117  		return 1000
   118  	}
   119  }
   120  
   121  func UninstallObjectOrder() func(o runtime.Object) int {
   122  	var Order = []string{
   123  		"MutatingWebhookConfiguration",
   124  		"ValidatingWebhookConfiguration",
   125  		"APIService",
   126  		"Ingress",
   127  		"Service",
   128  		"CronJob",
   129  		"Job",
   130  		"StatefulSet",
   131  		"HorizontalPodAutoscaler",
   132  		"Deployment",
   133  		"ReplicaSet",
   134  		"ReplicationController",
   135  		"Pod",
   136  		"DaemonSet",
   137  		"RoleBindingList",
   138  		"RoleBinding",
   139  		"RoleList",
   140  		"Role",
   141  		"ClusterRoleBindingList",
   142  		"ClusterRoleBinding",
   143  		"ClusterRoleList",
   144  		"ClusterRole",
   145  		"ServiceAccount",
   146  		"PersistentVolumeClaim",
   147  		"PersistentVolume",
   148  		"StorageClass",
   149  		"ConfigMap",
   150  		"Secret",
   151  		"PodDisruptionBudget",
   152  		"PodSecurityPolicy",
   153  		"LimitRange",
   154  		"ResourceQuota",
   155  		"Policy",
   156  		"Gateway",
   157  		"VirtualService",
   158  		"DestinationRule",
   159  		"Handler",
   160  		"Instance",
   161  		"Rule",
   162  		"Namespace",
   163  		"CustomResourceDefinition",
   164  	}
   165  
   166  	order := make(map[string]int, len(Order))
   167  	for i, kind := range Order {
   168  		order[kind] = i
   169  	}
   170  
   171  	return func(o runtime.Object) int {
   172  		if nr, ok := order[o.GetObjectKind().GroupVersionKind().Kind]; ok {
   173  			return nr
   174  		}
   175  		return 0
   176  	}
   177  }