From 4dffb2f1720e6e509abb5e38a284155819c57fa5 Mon Sep 17 00:00:00 2001 From: Johan Majoor Date: Thu, 28 Mar 2024 14:51:51 -0700 Subject: [PATCH] Support List based resources in the KindSortOrder. Each List variant is automatically placed at the same order as their non-list equivalent. Signed-off-by: Johan Majoor --- pkg/releaseutil/kind_sorter.go | 72 +++++++++++------------------ pkg/releaseutil/kind_sorter_test.go | 2 +- 2 files changed, 28 insertions(+), 46 deletions(-) diff --git a/pkg/releaseutil/kind_sorter.go b/pkg/releaseutil/kind_sorter.go index bb8e84dda..13ce52c83 100644 --- a/pkg/releaseutil/kind_sorter.go +++ b/pkg/releaseutil/kind_sorter.go @@ -25,10 +25,10 @@ import ( // KindSortOrder is an ordering of Kinds. type KindSortOrder []string -// InstallOrder is the order in which manifests should be installed (by Kind). +// ResourceDependencyOrder 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{ +var ResourceDependencyOrder KindSortOrder = []string{ "PriorityClass", "Namespace", "NetworkPolicy", @@ -38,20 +38,15 @@ var InstallOrder KindSortOrder = []string{ "PodDisruptionBudget", "ServiceAccount", "Secret", - "SecretList", "ConfigMap", "StorageClass", "PersistentVolume", "PersistentVolumeClaim", "CustomResourceDefinition", "ClusterRole", - "ClusterRoleList", "ClusterRoleBinding", - "ClusterRoleBindingList", "Role", - "RoleList", "RoleBinding", - "RoleBindingList", "Service", "DaemonSet", "Pod", @@ -67,47 +62,34 @@ var InstallOrder KindSortOrder = []string{ "APIService", } +// addListKind adds the List equivalent for each resource kind in the list. +func addListKind(resources KindSortOrder) KindSortOrder { + output := make(KindSortOrder, 2*len(resources)) + for _, resource := range resources { + output = append(output, resource) + output = append(output, resource+"List") + } + return output +} + +// reverseKindOrder reverses the order of the KindSortOrder. +func reverseKindOrder(resources KindSortOrder) KindSortOrder { + for i := 0; i < len(resources)/2; i++ { + j := len(resources) - i - 1 + resources[i], resources[j] = resources[j], resources[i] + } + return resources +} + +// 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 = addListKind(ResourceDependencyOrder) + // 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", - "IngressClass", - "Service", - "CronJob", - "Job", - "StatefulSet", - "HorizontalPodAutoscaler", - "Deployment", - "ReplicaSet", - "ReplicationController", - "Pod", - "DaemonSet", - "RoleBindingList", - "RoleBinding", - "RoleList", - "Role", - "ClusterRoleBindingList", - "ClusterRoleBinding", - "ClusterRoleList", - "ClusterRole", - "CustomResourceDefinition", - "PersistentVolumeClaim", - "PersistentVolume", - "StorageClass", - "ConfigMap", - "SecretList", - "Secret", - "ServiceAccount", - "PodDisruptionBudget", - "PodSecurityPolicy", - "LimitRange", - "ResourceQuota", - "NetworkPolicy", - "Namespace", - "PriorityClass", -} +var UninstallOrder = reverseKindOrder(addListKind(ResourceDependencyOrder)) // sort manifests by kind. // diff --git a/pkg/releaseutil/kind_sorter_test.go b/pkg/releaseutil/kind_sorter_test.go index 9e24c4399..f08f351c2 100644 --- a/pkg/releaseutil/kind_sorter_test.go +++ b/pkg/releaseutil/kind_sorter_test.go @@ -181,7 +181,7 @@ func TestKindSorter(t *testing.T) { expected string }{ {"install", InstallOrder, "FaAbcC3deEf1gh2iIjJkKlLmnopqrxstuUvw!"}, - {"uninstall", UninstallOrder, "wvUmutsxrqponLlKkJjIi2hg1fEed3CcbAaF!"}, + {"uninstall", UninstallOrder, "wvUutsxrqponmLlKkJjIi2hg1fEed3CcbAaF!"}, } { var buf bytes.Buffer t.Run(test.description, func(t *testing.T) {