Skip to content

Commit

Permalink
fix resources ordering
Browse files Browse the repository at this point in the history
Signed-off-by: Thibault Mange <[email protected]>
  • Loading branch information
thibaultmg committed Jun 3, 2024
1 parent 75fbf57 commit ec27fc7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
19 changes: 0 additions & 19 deletions operators/endpointmetrics/pkg/rendering/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 0 additions & 8 deletions operators/endpointmetrics/pkg/rendering/renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit ec27fc7

Please sign in to comment.