From 233082493dcbcd1cea2b05cc1b07d1f2fd209678 Mon Sep 17 00:00:00 2001 From: savitaashture Date: Fri, 4 Feb 2022 13:06:02 +0530 Subject: [PATCH] Update labels of tekton dashboard installersets --- .../tektondashboard/tektondashboard.go | 43 +++++++++++-------- .../tektondashboarddeployment_test.go | 5 +++ test/resources/tektonaddons.go | 18 ++++---- test/resources/tektondashboards.go | 5 +++ 4 files changed, 45 insertions(+), 26 deletions(-) diff --git a/pkg/reconciler/kubernetes/tektondashboard/tektondashboard.go b/pkg/reconciler/kubernetes/tektondashboard/tektondashboard.go index 8ddc8e023e..2e22736025 100644 --- a/pkg/reconciler/kubernetes/tektondashboard/tektondashboard.go +++ b/pkg/reconciler/kubernetes/tektondashboard/tektondashboard.go @@ -27,6 +27,7 @@ import ( pipelineinformer "github.com/tektoncd/operator/pkg/client/informers/externalversions/operator/v1alpha1" tektondashboardreconciler "github.com/tektoncd/operator/pkg/client/injection/reconciler/operator/v1alpha1/tektondashboard" "github.com/tektoncd/operator/pkg/reconciler/common" + "github.com/tektoncd/operator/pkg/reconciler/kubernetes/tektoninstallerset" "github.com/tektoncd/operator/pkg/reconciler/shared/hash" corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -62,7 +63,14 @@ type Reconciler struct { var _ tektondashboardreconciler.Interface = (*Reconciler)(nil) var _ tektondashboardreconciler.Finalizer = (*Reconciler)(nil) -var watchedResourceName = "dashboard" +var ( + ls = metav1.LabelSelector{ + MatchLabels: map[string]string{ + v1alpha1.CreatedByKey: createdByValue, + v1alpha1.InstallerSetType: v1alpha1.DashboardResourceName, + }, + } +) const createdByValue = "TektonDashboard" @@ -86,11 +94,6 @@ func (r *Reconciler) FinalizeKind(ctx context.Context, original *v1alpha1.Tekton return err } - ls := metav1.LabelSelector{ - MatchLabels: map[string]string{ - v1alpha1.CreatedByKey: createdByValue, - }, - } labelSelector, err := common.LabelSelector(ls) if err != nil { return err @@ -119,9 +122,9 @@ func (r *Reconciler) ReconcileKind(ctx context.Context, td *v1alpha1.TektonDashb logger.Infow("Reconciling TektonDashboards", "status", td.Status) - if td.GetName() != watchedResourceName { + if td.GetName() != v1alpha1.DashboardResourceName { msg := fmt.Sprintf("Resource ignored, Expected Name: %s, Got Name: %s", - watchedResourceName, + v1alpha1.DashboardResourceName, td.GetName(), ) logger.Error(msg) @@ -152,11 +155,16 @@ func (r *Reconciler) ReconcileKind(ctx context.Context, td *v1alpha1.TektonDashb // Mark PreReconcile Complete td.Status.MarkPreReconcilerComplete() - // Check if an tekton installer set already exists, if not then create - existingInstallerSet := td.Status.GetTektonInstallerSet() + // Check if an tektoninstallerset already exists, if not then create + labelSelector, err := common.LabelSelector(ls) + if err != nil { + return err + } + existingInstallerSet, err := tektoninstallerset.CurrentInstallerSetName(ctx, r.operatorClientSet, labelSelector) + if err != nil { + return err + } if existingInstallerSet == "" { - td.Status.MarkInstallerSetNotAvailable("Dashboard Installer Set Not Available") - createdIs, err := r.createInstallerSet(ctx, td) if err != nil { return err @@ -181,7 +189,7 @@ func (r *Reconciler) ReconcileKind(ctx context.Context, td *v1alpha1.TektonDashb } installerSetTargetNamespace := installedTIS.Annotations[v1alpha1.TargetNamespaceKey] - installerSetReleaseVersion := installedTIS.Annotations[v1alpha1.ReleaseVersionKey] + installerSetReleaseVersion := installedTIS.Labels[v1alpha1.ReleaseVersionKey] // Check if TargetNamespace of existing TektonInstallerSet is same as expected // Check if Release Version in TektonInstallerSet is same as expected @@ -344,7 +352,7 @@ func (r *Reconciler) createInstallerSet(ctx context.Context, td *v1alpha1.Tekton } // create installer set - tis := makeInstallerSet(td, manifest, specHash, r.operatorVersion) + tis := r.makeInstallerSet(td, manifest, specHash) createdIs, err := r.operatorClientSet.OperatorV1alpha1().TektonInstallerSets(). Create(ctx, tis, metav1.CreateOptions{}) if err != nil { @@ -353,16 +361,17 @@ func (r *Reconciler) createInstallerSet(ctx context.Context, td *v1alpha1.Tekton return createdIs, nil } -func makeInstallerSet(td *v1alpha1.TektonDashboard, manifest mf.Manifest, tdSpecHash, releaseVersion string) *v1alpha1.TektonInstallerSet { +func (r *Reconciler) makeInstallerSet(td *v1alpha1.TektonDashboard, manifest mf.Manifest, tdSpecHash string) *v1alpha1.TektonInstallerSet { ownerRef := *metav1.NewControllerRef(td, td.GetGroupVersionKind()) return &v1alpha1.TektonInstallerSet{ ObjectMeta: metav1.ObjectMeta{ GenerateName: fmt.Sprintf("%s-", v1alpha1.DashboardResourceName), Labels: map[string]string{ - v1alpha1.CreatedByKey: createdByValue, + v1alpha1.CreatedByKey: createdByValue, + v1alpha1.InstallerSetType: v1alpha1.DashboardResourceName, + v1alpha1.ReleaseVersionKey: r.operatorVersion, }, Annotations: map[string]string{ - v1alpha1.ReleaseVersionKey: releaseVersion, v1alpha1.TargetNamespaceKey: td.Spec.TargetNamespace, v1alpha1.LastAppliedHashKey: tdSpecHash, }, diff --git a/test/e2e/kubernetes/tektondashboarddeployment_test.go b/test/e2e/kubernetes/tektondashboarddeployment_test.go index f6bd0aee55..ad26ec092e 100644 --- a/test/e2e/kubernetes/tektondashboarddeployment_test.go +++ b/test/e2e/kubernetes/tektondashboarddeployment_test.go @@ -74,6 +74,11 @@ func TestTektonDashboardsDeployment(t *testing.T) { resources.AssertTektonDashboardCRReadyStatus(t, clients, crNames) }) + // Test if TektonInstallerSets are created. + t.Run("verify-dashboard-installersets", func(t *testing.T) { + resources.AssertDashboardInstallerSets(t, clients) + }) + // Delete the TektonDashboard CR instance to see if all resources will be removed t.Run("delete-dashboard", func(t *testing.T) { resources.AssertTektonDashboardCRReadyStatus(t, clients, crNames) diff --git a/test/resources/tektonaddons.go b/test/resources/tektonaddons.go index a522e82861..1854d3a735 100644 --- a/test/resources/tektonaddons.go +++ b/test/resources/tektonaddons.go @@ -96,15 +96,15 @@ func AssertTektonAddonCRReadyStatus(t *testing.T, clients *utils.Clients, names // AssertTektonInstallerSets verifies if the TektonInstallerSets are created. func AssertTektonInstallerSets(t *testing.T, clients *utils.Clients) { - assertInstallerSetsForAddon(t, clients, tektonaddon.ClusterTaskInstallerSet) - assertInstallerSetsForAddon(t, clients, tektonaddon.VersionedClusterTaskInstallerSet) - assertInstallerSetsForAddon(t, clients, tektonaddon.PipelinesTemplateInstallerSet) - assertInstallerSetsForAddon(t, clients, tektonaddon.TriggersResourcesInstallerSet) - assertInstallerSetsForAddon(t, clients, tektonaddon.ConsoleCLIInstallerSet) - assertInstallerSetsForAddon(t, clients, tektonaddon.MiscellaneousResourcesInstallerSet) + assertInstallerSets(t, clients, tektonaddon.ClusterTaskInstallerSet) + assertInstallerSets(t, clients, tektonaddon.VersionedClusterTaskInstallerSet) + assertInstallerSets(t, clients, tektonaddon.PipelinesTemplateInstallerSet) + assertInstallerSets(t, clients, tektonaddon.TriggersResourcesInstallerSet) + assertInstallerSets(t, clients, tektonaddon.ConsoleCLIInstallerSet) + assertInstallerSets(t, clients, tektonaddon.MiscellaneousResourcesInstallerSet) } -func assertInstallerSetsForAddon(t *testing.T, clients *utils.Clients, component string) { +func assertInstallerSets(t *testing.T, clients *utils.Clients, component string) { ls := metav1.LabelSelector{ MatchLabels: map[string]string{ v1alpha1.InstallerSetType: component, @@ -114,13 +114,13 @@ func assertInstallerSetsForAddon(t *testing.T, clients *utils.Clients, component if err != nil { t.Fatal(err) } - clusterTaskIS, err := clients.TektonInstallerSet().List(context.TODO(), metav1.ListOptions{ + installerSets, err := clients.TektonInstallerSet().List(context.TODO(), metav1.ListOptions{ LabelSelector: labelSelector, }) if err != nil { t.Fatalf("failed to get TektonInstallerSet for %s : %v", component, err) } - if len(clusterTaskIS.Items) > 1 { + if len(installerSets.Items) > 1 { t.Fatalf("multiple installer sets for %s TektonInstallerSet", component) } } diff --git a/test/resources/tektondashboards.go b/test/resources/tektondashboards.go index 85bf4bd917..50c6507e8b 100644 --- a/test/resources/tektondashboards.go +++ b/test/resources/tektondashboards.go @@ -83,6 +83,11 @@ func IsTektonDashboardReady(s *v1alpha1.TektonDashboard, err error) (bool, error return s.Status.IsReady(), err } +// AssertTektonInstallerSets verifies if the TektonInstallerSets are created. +func AssertDashboardInstallerSets(t *testing.T, clients *utils.Clients) { + assertInstallerSets(t, clients, v1alpha1.DashboardResourceName) +} + // AssertTektonDashboardCRReadyStatus verifies if the TektonDashboard reaches the READY status. func AssertTektonDashboardCRReadyStatus(t *testing.T, clients *utils.Clients, names utils.ResourceNames) { if _, err := WaitForTektonDashboardState(clients.TektonDashboard(), names.TektonDashboard,