From 14c7d8166106709fc3159943c8642972129cc8b6 Mon Sep 17 00:00:00 2001 From: pratap0007 Date: Wed, 7 Feb 2024 01:57:58 +0530 Subject: [PATCH] Fix Hub targetnamespace deletion This patch removes reconcilation of targetnamespace from Hub and creates namespace incase targetnamespace is not openshift-pipelines if targetnamespace of hub is not openshift-pipelines it was deleting openshift-pipelines namespace and hub targetnamespace during targetnamespace reconcilation Signed-off-by: Shiv Verma --- .../kubernetes/tektonhub/tektonhub.go | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/pkg/reconciler/kubernetes/tektonhub/tektonhub.go b/pkg/reconciler/kubernetes/tektonhub/tektonhub.go index ebb4c8b0f4..afc2a1f8b9 100644 --- a/pkg/reconciler/kubernetes/tektonhub/tektonhub.go +++ b/pkg/reconciler/kubernetes/tektonhub/tektonhub.go @@ -175,12 +175,8 @@ func (r *Reconciler) ReconcileKind(ctx context.Context, th *v1alpha1.TektonHub) th.SetDefaults(ctx) - // reconcile target namespace - if err := common.ReconcileTargetNamespace(ctx, nil, th, r.kubeClientSet); err != nil { - logger.Errorw("error on reconciling targetNamespace", - "targetNamespace", th.Spec.GetTargetNamespace(), - err, - ) + tc, _ := r.operatorClientSet.OperatorV1alpha1().TektonConfigs().Get(ctx, v1alpha1.ConfigResourceName, metav1.GetOptions{}) + if err := r.checkAndCreateNameSpace(ctx, th, tc.Spec.TargetNamespace); err != nil { return err } @@ -1035,3 +1031,24 @@ func getConfigDataFromHubURL(th *v1alpha1.TektonHub) (*Data, error) { return data, nil } + +// checks target namespace, creates namespace if not found +func (r *Reconciler) checkAndCreateNameSpace(ctx context.Context, th *v1alpha1.TektonHub, tektonConfigNamespace string) error { + if th.Spec.TargetNamespace != tektonConfigNamespace { + _, err := r.kubeClientSet.CoreV1().Namespaces().Get(ctx, th.Spec.GetTargetNamespace(), metav1.GetOptions{}) + if err != nil { + if apierrors.IsNotFound(err) { + ns := &corev1.Namespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: th.Spec.TargetNamespace, + }, + } + _, err := r.kubeClientSet.CoreV1().Namespaces().Create(ctx, ns, metav1.CreateOptions{}) + if err != nil { + return err + } + } + } + } + return nil +}