From b7cdd9168aa3ffa6c7738361e4cf74a0b929dadb Mon Sep 17 00:00:00 2001 From: Daniel Wilson <102226203+danielwilsonkainos@users.noreply.github.com> Date: Tue, 3 Sep 2024 21:57:17 +0100 Subject: [PATCH] Sort built-in Namespace kind before CRDs with the same name (#5458) * add core api sort priority for NAmespace kind in gvkLessThan * fix logic * fix syntax * fix syntax * fix logic * regenerate builtin plugins --- api/internal/builtins/SortOrderTransformer.go | 3 +++ api/types/kustomization.go | 1 + .../SortOrderTransformer.go | 3 +++ .../SortOrderTransformer_test.go | 24 +++++++++++++++++++ 4 files changed, 31 insertions(+) diff --git a/api/internal/builtins/SortOrderTransformer.go b/api/internal/builtins/SortOrderTransformer.go index c50f627fee..90e290719e 100644 --- a/api/internal/builtins/SortOrderTransformer.go +++ b/api/internal/builtins/SortOrderTransformer.go @@ -151,6 +151,9 @@ func gvkLessThan(gvk1, gvk2 resid.Gvk, typeOrders map[string]int) bool { if index1 != index2 { return index1 < index2 } + if (gvk1.Kind == types.NamespaceKind && gvk2.Kind == types.NamespaceKind) && (gvk1.Group == "" || gvk2.Group == "") { + return legacyGVKSortString(gvk1) > legacyGVKSortString(gvk2) + } return legacyGVKSortString(gvk1) < legacyGVKSortString(gvk2) } diff --git a/api/types/kustomization.go b/api/types/kustomization.go index 5fe32ebcf7..6caa296644 100644 --- a/api/types/kustomization.go +++ b/api/types/kustomization.go @@ -22,6 +22,7 @@ const ( MetadataNamespacePath = "metadata/namespace" MetadataNamespaceApiVersion = "v1" MetadataNamePath = "metadata/name" + NamespaceKind = "Namespace" OriginAnnotations = "originAnnotations" TransformerAnnotations = "transformerAnnotations" diff --git a/plugin/builtin/sortordertransformer/SortOrderTransformer.go b/plugin/builtin/sortordertransformer/SortOrderTransformer.go index 03b3e0b243..9f037eafaa 100644 --- a/plugin/builtin/sortordertransformer/SortOrderTransformer.go +++ b/plugin/builtin/sortordertransformer/SortOrderTransformer.go @@ -154,6 +154,9 @@ func gvkLessThan(gvk1, gvk2 resid.Gvk, typeOrders map[string]int) bool { if index1 != index2 { return index1 < index2 } + if (gvk1.Kind == types.NamespaceKind && gvk2.Kind == types.NamespaceKind) && (gvk1.Group == "" || gvk2.Group == "") { + return legacyGVKSortString(gvk1) > legacyGVKSortString(gvk2) + } return legacyGVKSortString(gvk1) < legacyGVKSortString(gvk2) } diff --git a/plugin/builtin/sortordertransformer/SortOrderTransformer_test.go b/plugin/builtin/sortordertransformer/SortOrderTransformer_test.go index 77dffafa4d..64bd3b4b1c 100644 --- a/plugin/builtin/sortordertransformer/SortOrderTransformer_test.go +++ b/plugin/builtin/sortordertransformer/SortOrderTransformer_test.go @@ -69,6 +69,12 @@ kind: Namespace metadata: name: apple --- +apiVersion: servicebus.azure.com/v1api20210101preview +kind: Namespace +metadata: + name: aso-namespace + namespace: default +--- apiVersion: v1 kind: Secret metadata: @@ -108,6 +114,12 @@ kind: Namespace metadata: name: apple --- +apiVersion: servicebus.azure.com/v1api20210101preview +kind: Namespace +metadata: + name: aso-namespace + namespace: default +--- apiVersion: v1 kind: Role metadata: @@ -228,6 +240,12 @@ kind: Namespace metadata: name: apple --- +apiVersion: servicebus.azure.com/v1api20210101preview +kind: Namespace +metadata: + name: aso-namespace + namespace: default +--- apiVersion: v1 kind: Deployment metadata: @@ -306,6 +324,12 @@ kind: Namespace metadata: name: apple --- +apiVersion: servicebus.azure.com/v1api20210101preview +kind: Namespace +metadata: + name: aso-namespace + namespace: default +--- apiVersion: v1 kind: Deployment metadata: