From ec27fc7a2407266a4ccfdc8a72f90a4bbe8fe5e5 Mon Sep 17 00:00:00 2001 From: Thibault Mange <22740367+thibaultmg@users.noreply.github.com> Date: Mon, 3 Jun 2024 17:11:41 +0200 Subject: [PATCH] fix resources ordering Signed-off-by: Thibault Mange <22740367+thibaultmg@users.noreply.github.com> --- .../observabilityaddon_controller.go | 21 +++++++++++++++++++ .../endpointmetrics/pkg/rendering/renderer.go | 19 ----------------- .../pkg/rendering/renderer_test.go | 8 ------- 3 files changed, 21 insertions(+), 27 deletions(-) diff --git a/operators/endpointmetrics/controllers/observabilityendpoint/observabilityaddon_controller.go b/operators/endpointmetrics/controllers/observabilityendpoint/observabilityaddon_controller.go index 159eec4c84..4e986fd202 100644 --- a/operators/endpointmetrics/controllers/observabilityendpoint/observabilityaddon_controller.go +++ b/operators/endpointmetrics/controllers/observabilityendpoint/observabilityaddon_controller.go @@ -264,6 +264,12 @@ func (r *ObservabilityAddonReconciler) Reconcile(ctx context.Context, req ctrl.R } deployer := deploying.NewDeployer(r.Client) + + // Ordering resources to ensure they are applied in the correct order + slices.SortFunc(toDeploy, func(a, b *unstructured.Unstructured) bool { + return (resourcePriority(a) - resourcePriority(b)) < 0 + }) + for _, res := range toDeploy { if res.GetNamespace() != namespace { globalRes = append(globalRes, res) @@ -622,3 +628,18 @@ func remove(list []string, s string) []string { } return result } + +// resourcePriority returns the priority of the resource. +// This is used to order the resources to be created in the correct order. +func resourcePriority(resource *unstructured.Unstructured) int { + switch resource.GetKind() { + case "Role", "ClusterRole": + return 1 + case "RoleBinding", "ClusterRoleBinding": + return 2 + case "CustomResourceDefinition": + return 3 + default: + return 4 + } +} diff --git a/operators/endpointmetrics/pkg/rendering/renderer.go b/operators/endpointmetrics/pkg/rendering/renderer.go index 254361968b..0d23a91888 100644 --- a/operators/endpointmetrics/pkg/rendering/renderer.go +++ b/operators/endpointmetrics/pkg/rendering/renderer.go @@ -12,7 +12,6 @@ import ( "strings" prometheusv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" - "golang.org/x/exp/slices" v1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -251,27 +250,9 @@ func Render( } } - // Ordering resources to ensure they are applied in the correct order - slices.SortFunc(resources, func(a, b *unstructured.Unstructured) bool { - return (resourcePriority(a) - resourcePriority(b)) < 0 - }) - return resources, nil } -func resourcePriority(resource *unstructured.Unstructured) int { - switch resource.GetKind() { - case "Role", "ClusterRole": - return 1 - case "RoleBinding", "ClusterRoleBinding": - return 2 - case "CustomResourceDefinition": - return 3 - default: - return 4 - } -} - func getDisabledMetrics(c runtimeclient.Client) (string, error) { cm := &corev1.ConfigMap{} err := c.Get(context.TODO(), types.NamespacedName{Name: operatorconfig.AllowlistConfigMapName, diff --git a/operators/endpointmetrics/pkg/rendering/renderer_test.go b/operators/endpointmetrics/pkg/rendering/renderer_test.go index e922ef0b75..e0126ba41d 100644 --- a/operators/endpointmetrics/pkg/rendering/renderer_test.go +++ b/operators/endpointmetrics/pkg/rendering/renderer_test.go @@ -58,14 +58,6 @@ func TestRender(t *testing.T) { } printObjs(t, objs) - - // ensure that objects are sorted - for i := 0; i < len(objs)-1; i++ { - if resourcePriority(objs[i]) > resourcePriority(objs[i+1]) { - t.Errorf("objects are not sorted") - } - } - } func printObjs(t *testing.T, objs []*unstructured.Unstructured) {