From d28f6bd66770067345bc2be2dc5a96e6dce6af07 Mon Sep 17 00:00:00 2001 From: Ben Oukhanov Date: Wed, 7 Jun 2023 11:39:37 +0300 Subject: [PATCH] fix(CNV-29761): ensure that tekton crd exists Checks if Tekton CRD is installed during Tekton operands reconciliation and during cleanup process when listing deprecated ClusterTasks. Signed-off-by: Ben Oukhanov --- internal/common/labels.go | 7 ++-- .../tekton-pipelines/tekton-pipelines.go | 16 ++++----- .../operands/tekton-tasks/tekton-tasks.go | 35 ++++++++++--------- 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/internal/common/labels.go b/internal/common/labels.go index b4e268105..75b91fa0d 100644 --- a/internal/common/labels.go +++ b/internal/common/labels.go @@ -14,9 +14,10 @@ const ( AppKubernetesManagedByLabel = "app.kubernetes.io/managed-by" AppKubernetesComponentLabel = "app.kubernetes.io/component" - AppComponentTektonPipelines AppComponent = "tektonPipelines" - AppComponentTektonTasks AppComponent = "tektonTasks" - AppKubernetesManagedByValue string = "ssp-operator" + AppComponentTektonPipelines AppComponent = "tektonPipelines" + AppComponentTektonTasks AppComponent = "tektonTasks" + AppKubernetesManagedByValue string = "ssp-operator" + TektonAppKubernetesManagedByValue string = "tekton-tasks-operator" ) type AppComponent string diff --git a/internal/operands/tekton-pipelines/tekton-pipelines.go b/internal/operands/tekton-pipelines/tekton-pipelines.go index 28f20f3d6..bc7a3f8bd 100644 --- a/internal/operands/tekton-pipelines/tekton-pipelines.go +++ b/internal/operands/tekton-pipelines/tekton-pipelines.go @@ -85,11 +85,9 @@ func (t *tektonPipelines) Reconcile(request *common.Request) ([]common.Reconcile request.Logger.V(1).Info("Tekton Pipelines resources were not deployed, because spec.featureGates.deployTektonTaskResources is set to false") return nil, nil } - // Solution to optional Tekton CRD is not implemented yet. - // Until then, do not check if Tekton CRD exists. - // if !request.CrdList.CrdExists(tektonCrd) { - // return nil, fmt.Errorf("Tekton CRD %s does not exist", tektonCrd) - // } + if !request.CrdList.CrdExists(tektonCrd) { + return nil, fmt.Errorf("Tekton CRD %s does not exist", tektonCrd) + } var reconcileFunc []common.ReconcileFunc reconcileFunc = append(reconcileFunc, reconcileClusterRolesFuncs(t.clusterRoles)...) @@ -114,9 +112,11 @@ func (t *tektonPipelines) Reconcile(request *common.Request) ([]common.Reconcile func (t *tektonPipelines) Cleanup(request *common.Request) ([]common.CleanupResult, error) { var objects []client.Object - for _, p := range t.pipelines { - o := p.DeepCopy() - objects = append(objects, o) + if request.CrdList.CrdExists(tektonCrd) { + for _, p := range t.pipelines { + o := p.DeepCopy() + objects = append(objects, o) + } } for _, cm := range t.configMaps { o := cm.DeepCopy() diff --git a/internal/operands/tekton-tasks/tekton-tasks.go b/internal/operands/tekton-tasks/tekton-tasks.go index 4ffeacfda..d08c471d0 100644 --- a/internal/operands/tekton-tasks/tekton-tasks.go +++ b/internal/operands/tekton-tasks/tekton-tasks.go @@ -149,11 +149,9 @@ func (t *tektonTasks) Reconcile(request *common.Request) ([]common.ReconcileResu request.Logger.V(1).Info("Tekton Tasks resources were not deployed, because spec.featureGates.deployTektonTaskResources is set to false") return nil, nil } - // Solution to optional Tekton CRD is not implemented yet. - // Until then, do not check if Tekton CRD exists. - // if !request.CrdList.CrdExists(tektonCrd) { - // return nil, fmt.Errorf("Tekton CRD %s does not exist", tektonCrd) - // } + if !request.CrdList.CrdExists(tektonCrd) { + return nil, fmt.Errorf("Tekton CRD %s does not exist", tektonCrd) + } var reconcileFunc []common.ReconcileFunc reconcileFunc = append(reconcileFunc, reconcileTektonTasksFuncs(t.tasks)...) @@ -177,9 +175,11 @@ func (t *tektonTasks) Reconcile(request *common.Request) ([]common.ReconcileResu func (t *tektonTasks) Cleanup(request *common.Request) ([]common.CleanupResult, error) { var objects []client.Object - for _, t := range t.tasks { - o := t.DeepCopy() - objects = append(objects, o) + if request.CrdList.CrdExists(tektonCrd) { + for _, t := range t.tasks { + o := t.DeepCopy() + objects = append(objects, o) + } } for _, rb := range t.roleBindings { o := rb.DeepCopy() @@ -199,14 +199,17 @@ func (t *tektonTasks) Cleanup(request *common.Request) ([]common.CleanupResult, objects = append(objects, o) } - clusterTasks, err := listDeprecatedClusterTasks(request) - if err != nil { - return nil, err - } - for _, ct := range clusterTasks { - o := ct.DeepCopy() - objects = append(objects, o) + if request.CrdList.CrdExists(tektonCrd) { + clusterTasks, err := listDeprecatedClusterTasks(request) + if err != nil { + return nil, err + } + for _, ct := range clusterTasks { + o := ct.DeepCopy() + objects = append(objects, o) + } } + return common.DeleteAll(request, objects...) } @@ -215,7 +218,7 @@ func (t *tektonTasks) Cleanup(request *common.Request) ([]common.CleanupResult, func listDeprecatedClusterTasks(request *common.Request) ([]pipeline.ClusterTask, error) { deprecatedClusterTasks := &pipeline.ClusterTaskList{} err := request.Client.List(request.Context, deprecatedClusterTasks, &client.MatchingLabels{ - common.AppKubernetesManagedByLabel: common.AppKubernetesManagedByValue, + common.AppKubernetesManagedByLabel: common.TektonAppKubernetesManagedByValue, }) if err != nil { return nil, err