| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | Copyright The Helm Authors. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  | you may not use this file except in compliance with the License. | 
					
						
							|  |  |  | You may obtain a copy of the License at | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  | distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  | See the License for the specific language governing permissions and | 
					
						
							|  |  |  | limitations under the License. | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-25 03:58:51 +08:00
										 |  |  | package util | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-17 04:43:17 +08:00
										 |  |  | import ( | 
					
						
							|  |  |  | 	"sort" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-26 22:04:32 +08:00
										 |  |  | 	release "helm.sh/helm/v4/pkg/release/v1" | 
					
						
							| 
									
										
										
										
											2020-01-17 04:43:17 +08:00
										 |  |  | ) | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | // KindSortOrder is an ordering of Kinds.
 | 
					
						
							|  |  |  | type KindSortOrder []string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // InstallOrder is the order in which manifests should be installed (by Kind).
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Those occurring earlier in the list get installed before those occurring later in the list.
 | 
					
						
							|  |  |  | var InstallOrder KindSortOrder = []string{ | 
					
						
							| 
									
										
										
										
											2022-04-05 16:29:43 +08:00
										 |  |  | 	"PriorityClass", | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | 	"Namespace", | 
					
						
							| 
									
										
										
										
											2019-10-10 19:41:55 +08:00
										 |  |  | 	"NetworkPolicy", | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | 	"ResourceQuota", | 
					
						
							|  |  |  | 	"LimitRange", | 
					
						
							| 
									
										
										
										
											2019-10-10 19:41:55 +08:00
										 |  |  | 	"PodSecurityPolicy", | 
					
						
							|  |  |  | 	"PodDisruptionBudget", | 
					
						
							| 
									
										
										
										
											2020-01-09 01:54:08 +08:00
										 |  |  | 	"ServiceAccount", | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | 	"Secret", | 
					
						
							| 
									
										
										
										
											2020-05-21 18:00:16 +08:00
										 |  |  | 	"SecretList", | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | 	"ConfigMap", | 
					
						
							|  |  |  | 	"StorageClass", | 
					
						
							|  |  |  | 	"PersistentVolume", | 
					
						
							|  |  |  | 	"PersistentVolumeClaim", | 
					
						
							|  |  |  | 	"CustomResourceDefinition", | 
					
						
							|  |  |  | 	"ClusterRole", | 
					
						
							| 
									
										
										
										
											2019-07-27 22:19:16 +08:00
										 |  |  | 	"ClusterRoleList", | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | 	"ClusterRoleBinding", | 
					
						
							| 
									
										
										
										
											2019-07-27 22:19:16 +08:00
										 |  |  | 	"ClusterRoleBindingList", | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | 	"Role", | 
					
						
							| 
									
										
										
										
											2019-07-27 22:19:16 +08:00
										 |  |  | 	"RoleList", | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | 	"RoleBinding", | 
					
						
							| 
									
										
										
										
											2019-07-27 22:19:16 +08:00
										 |  |  | 	"RoleBindingList", | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | 	"Service", | 
					
						
							|  |  |  | 	"DaemonSet", | 
					
						
							|  |  |  | 	"Pod", | 
					
						
							|  |  |  | 	"ReplicationController", | 
					
						
							|  |  |  | 	"ReplicaSet", | 
					
						
							|  |  |  | 	"Deployment", | 
					
						
							| 
									
										
										
										
											2019-07-19 23:41:45 +08:00
										 |  |  | 	"HorizontalPodAutoscaler", | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | 	"StatefulSet", | 
					
						
							|  |  |  | 	"Job", | 
					
						
							|  |  |  | 	"CronJob", | 
					
						
							| 
									
										
										
										
											2022-02-04 19:33:26 +08:00
										 |  |  | 	"IngressClass", | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | 	"Ingress", | 
					
						
							|  |  |  | 	"APIService", | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // UninstallOrder is the order in which manifests should be uninstalled (by Kind).
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Those occurring earlier in the list get uninstalled before those occurring later in the list.
 | 
					
						
							|  |  |  | var UninstallOrder KindSortOrder = []string{ | 
					
						
							|  |  |  | 	"APIService", | 
					
						
							|  |  |  | 	"Ingress", | 
					
						
							| 
									
										
										
										
											2022-02-04 19:33:26 +08:00
										 |  |  | 	"IngressClass", | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | 	"Service", | 
					
						
							|  |  |  | 	"CronJob", | 
					
						
							|  |  |  | 	"Job", | 
					
						
							|  |  |  | 	"StatefulSet", | 
					
						
							| 
									
										
										
										
											2019-07-19 23:41:45 +08:00
										 |  |  | 	"HorizontalPodAutoscaler", | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | 	"Deployment", | 
					
						
							|  |  |  | 	"ReplicaSet", | 
					
						
							|  |  |  | 	"ReplicationController", | 
					
						
							|  |  |  | 	"Pod", | 
					
						
							|  |  |  | 	"DaemonSet", | 
					
						
							| 
									
										
										
										
											2019-07-27 22:19:16 +08:00
										 |  |  | 	"RoleBindingList", | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | 	"RoleBinding", | 
					
						
							| 
									
										
										
										
											2019-07-27 22:19:16 +08:00
										 |  |  | 	"RoleList", | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | 	"Role", | 
					
						
							| 
									
										
										
										
											2019-07-27 22:19:16 +08:00
										 |  |  | 	"ClusterRoleBindingList", | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | 	"ClusterRoleBinding", | 
					
						
							| 
									
										
										
										
											2019-07-27 22:19:16 +08:00
										 |  |  | 	"ClusterRoleList", | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | 	"ClusterRole", | 
					
						
							|  |  |  | 	"CustomResourceDefinition", | 
					
						
							|  |  |  | 	"PersistentVolumeClaim", | 
					
						
							|  |  |  | 	"PersistentVolume", | 
					
						
							|  |  |  | 	"StorageClass", | 
					
						
							|  |  |  | 	"ConfigMap", | 
					
						
							| 
									
										
										
										
											2020-05-21 18:00:16 +08:00
										 |  |  | 	"SecretList", | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | 	"Secret", | 
					
						
							| 
									
										
										
										
											2020-01-09 01:54:08 +08:00
										 |  |  | 	"ServiceAccount", | 
					
						
							| 
									
										
										
										
											2019-10-10 19:41:55 +08:00
										 |  |  | 	"PodDisruptionBudget", | 
					
						
							|  |  |  | 	"PodSecurityPolicy", | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | 	"LimitRange", | 
					
						
							|  |  |  | 	"ResourceQuota", | 
					
						
							| 
									
										
										
										
											2019-10-10 19:41:55 +08:00
										 |  |  | 	"NetworkPolicy", | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | 	"Namespace", | 
					
						
							| 
									
										
										
										
											2022-04-05 16:29:43 +08:00
										 |  |  | 	"PriorityClass", | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-31 07:12:16 +08:00
										 |  |  | // sort manifests by kind.
 | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2019-10-31 05:22:42 +08:00
										 |  |  | // Results are sorted by 'ordering', keeping order of items with equal kind/priority
 | 
					
						
							| 
									
										
										
										
											2020-01-31 07:12:16 +08:00
										 |  |  | func sortManifestsByKind(manifests []Manifest, ordering KindSortOrder) []Manifest { | 
					
						
							|  |  |  | 	sort.SliceStable(manifests, func(i, j int) bool { | 
					
						
							|  |  |  | 		return lessByKind(manifests[i], manifests[j], manifests[i].Head.Kind, manifests[j].Head.Kind, ordering) | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2020-01-17 04:43:17 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-31 07:12:16 +08:00
										 |  |  | 	return manifests | 
					
						
							| 
									
										
										
										
											2020-01-17 04:43:17 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-31 07:12:16 +08:00
										 |  |  | // sort hooks by kind, using an out-of-place sort to preserve the input parameters.
 | 
					
						
							| 
									
										
										
										
											2020-01-17 04:43:17 +08:00
										 |  |  | //
 | 
					
						
							|  |  |  | // Results are sorted by 'ordering', keeping order of items with equal kind/priority
 | 
					
						
							| 
									
										
										
										
											2020-01-31 07:12:16 +08:00
										 |  |  | func sortHooksByKind(hooks []*release.Hook, ordering KindSortOrder) []*release.Hook { | 
					
						
							|  |  |  | 	h := hooks | 
					
						
							|  |  |  | 	sort.SliceStable(h, func(i, j int) bool { | 
					
						
							|  |  |  | 		return lessByKind(h[i], h[j], h[i].Kind, h[j].Kind, ordering) | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-31 07:12:16 +08:00
										 |  |  | 	return h | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-06 05:35:24 +08:00
										 |  |  | func lessByKind(_ interface{}, _ interface{}, kindA string, kindB string, o KindSortOrder) bool { | 
					
						
							| 
									
										
										
										
											2020-01-31 07:12:16 +08:00
										 |  |  | 	ordering := make(map[string]int, len(o)) | 
					
						
							|  |  |  | 	for v, k := range o { | 
					
						
							|  |  |  | 		ordering[k] = v | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-31 07:12:16 +08:00
										 |  |  | 	first, aok := ordering[kindA] | 
					
						
							|  |  |  | 	second, bok := ordering[kindB] | 
					
						
							| 
									
										
										
										
											2019-12-19 16:21:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if !aok && !bok { | 
					
						
							|  |  |  | 		// if both are unknown then sort alphabetically by kind, keep original order if same kind
 | 
					
						
							| 
									
										
										
										
											2020-01-31 07:12:16 +08:00
										 |  |  | 		if kindA != kindB { | 
					
						
							|  |  |  | 			return kindA < kindB | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-12-19 16:21:24 +08:00
										 |  |  | 		return first < second | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	// unknown kind is last
 | 
					
						
							|  |  |  | 	if !aok { | 
					
						
							|  |  |  | 		return false | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if !bok { | 
					
						
							|  |  |  | 		return true | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-10-31 05:22:42 +08:00
										 |  |  | 	// sort different kinds, keep original order if same priority
 | 
					
						
							| 
									
										
										
										
											2019-01-08 08:45:14 +08:00
										 |  |  | 	return first < second | 
					
						
							|  |  |  | } |