1
16
17 package releaseutil
18
19 import (
20 "sort"
21
22 "helm.sh/helm/v3/pkg/release"
23 )
24
25
26 type KindSortOrder []string
27
28
29
30
31 var InstallOrder KindSortOrder = []string{
32 "PriorityClass",
33 "Namespace",
34 "NetworkPolicy",
35 "ResourceQuota",
36 "LimitRange",
37 "PodSecurityPolicy",
38 "PodDisruptionBudget",
39 "ServiceAccount",
40 "Secret",
41 "SecretList",
42 "ConfigMap",
43 "StorageClass",
44 "PersistentVolume",
45 "PersistentVolumeClaim",
46 "CustomResourceDefinition",
47 "ClusterRole",
48 "ClusterRoleList",
49 "ClusterRoleBinding",
50 "ClusterRoleBindingList",
51 "Role",
52 "RoleList",
53 "RoleBinding",
54 "RoleBindingList",
55 "Service",
56 "DaemonSet",
57 "Pod",
58 "ReplicationController",
59 "ReplicaSet",
60 "Deployment",
61 "HorizontalPodAutoscaler",
62 "StatefulSet",
63 "Job",
64 "CronJob",
65 "IngressClass",
66 "Ingress",
67 "APIService",
68 }
69
70
71
72
73 var UninstallOrder KindSortOrder = []string{
74 "APIService",
75 "Ingress",
76 "IngressClass",
77 "Service",
78 "CronJob",
79 "Job",
80 "StatefulSet",
81 "HorizontalPodAutoscaler",
82 "Deployment",
83 "ReplicaSet",
84 "ReplicationController",
85 "Pod",
86 "DaemonSet",
87 "RoleBindingList",
88 "RoleBinding",
89 "RoleList",
90 "Role",
91 "ClusterRoleBindingList",
92 "ClusterRoleBinding",
93 "ClusterRoleList",
94 "ClusterRole",
95 "CustomResourceDefinition",
96 "PersistentVolumeClaim",
97 "PersistentVolume",
98 "StorageClass",
99 "ConfigMap",
100 "SecretList",
101 "Secret",
102 "ServiceAccount",
103 "PodDisruptionBudget",
104 "PodSecurityPolicy",
105 "LimitRange",
106 "ResourceQuota",
107 "NetworkPolicy",
108 "Namespace",
109 "PriorityClass",
110 }
111
112
113
114
115 func sortManifestsByKind(manifests []Manifest, ordering KindSortOrder) []Manifest {
116 sort.SliceStable(manifests, func(i, j int) bool {
117 return lessByKind(manifests[i], manifests[j], manifests[i].Head.Kind, manifests[j].Head.Kind, ordering)
118 })
119
120 return manifests
121 }
122
123
124
125
126 func sortHooksByKind(hooks []*release.Hook, ordering KindSortOrder) []*release.Hook {
127 h := hooks
128 sort.SliceStable(h, func(i, j int) bool {
129 return lessByKind(h[i], h[j], h[i].Kind, h[j].Kind, ordering)
130 })
131
132 return h
133 }
134
135 func lessByKind(_ interface{}, _ interface{}, kindA string, kindB string, o KindSortOrder) bool {
136 ordering := make(map[string]int, len(o))
137 for v, k := range o {
138 ordering[k] = v
139 }
140
141 first, aok := ordering[kindA]
142 second, bok := ordering[kindB]
143
144 if !aok && !bok {
145
146 if kindA != kindB {
147 return kindA < kindB
148 }
149 return first < second
150 }
151
152 if !aok {
153 return false
154 }
155 if !bok {
156 return true
157 }
158
159 return first < second
160 }
161
View as plain text