From f2ee785cef7f71ffbae26cbffc2d436bcb09bbea Mon Sep 17 00:00:00 2001 From: dislbenn Date: Thu, 9 Jan 2025 08:47:17 -0500 Subject: [PATCH] added logic to check if namespace exist before creating template resource Signed-off-by: dislbenn --- controllers/backplaneconfig_controller.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/controllers/backplaneconfig_controller.go b/controllers/backplaneconfig_controller.go index b585165e..043485c3 100644 --- a/controllers/backplaneconfig_controller.go +++ b/controllers/backplaneconfig_controller.go @@ -1356,6 +1356,21 @@ func (r *MultiClusterEngineReconciler) applyTemplate(ctx context.Context, return result, err } } else { + // Check if the namespace exists if the template specifies a namespace. + if template.GetNamespace() != backplaneConfig.Spec.TargetNamespace && template.GetNamespace() != "" { + ns := &corev1.Namespace{} + if err := r.Client.Get(ctx, types.NamespacedName{Name: template.GetNamespace()}, ns); err != nil { + if apierrors.IsNotFound(err) { + r.Log.Info("Namespace does not exist; skipping resource creation", + "Name", template.GetName(), "Kind", template.GetKind(), "Namespace", template.GetNamespace()) + + // Skip further processing if the namespace does not exist. + return ctrl.Result{}, nil + } + return ctrl.Result{}, err + } + } + // Apply the object data. force := true err := r.Client.Patch(ctx, template, client.Apply,