Skip to content

Commit

Permalink
Fix versioned clustertask getting deleted during upgrade
Browse files Browse the repository at this point in the history
This will fix the issue of versioned clustertask getting
deleted during upgrade
Now two installersets will be created for versioned and
non version clustertask and versioned clustertask installerset
will only be deleted when there will be two of same version
otherwise installerset will remain there of different version ending
in versioned clustertask to stay on cluster
  • Loading branch information
piyush-garg committed Jan 28, 2022
1 parent b0c6763 commit 03e41b0
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
1 change: 1 addition & 0 deletions pkg/reconciler/openshift/tektonaddon/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package tektonaddon

const (
ClusterTaskInstallerSet = "ClusterTask"
VersionedClusterTaskInstallerSet = "VersionedClusterTask"
PipelinesTemplateInstallerSet = "PipelinesTemplate"
TriggersResourcesInstallerSet = "TriggersResources"
ConsoleCLIInstallerSet = "ConsoleCLI"
Expand Down
72 changes: 71 additions & 1 deletion pkg/reconciler/openshift/tektonaddon/tektonaddon.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"knative.dev/pkg/apis"
"knative.dev/pkg/logging"
pkgreconciler "knative.dev/pkg/reconciler"
Expand Down Expand Up @@ -196,6 +197,33 @@ func (r *Reconciler) ReconcileKind(ctx context.Context, ta *v1alpha1.TektonAddon
return nil
}

// If clusterTasks are enabled then create an InstallerSet
// with the versioned clustertask manifest
if ctVal == "true" {

exist, err := checkIfInstallerSetExist(ctx, r.operatorClientSet, r.version,
fmt.Sprintf("%s=%s,%s=%s", tektoninstallerset.InstallerSetType, VersionedClusterTaskInstallerSet, tektoninstallerset.ReleaseVersionKey, r.version),
)
if err != nil {
return err
}

if !exist {
return r.ensureVersionedClusterTasks(ctx, ta)
}
} else {
// if disabled then delete the installer Set if exist
if err := r.deleteInstallerSet(ctx,
fmt.Sprintf("%s=%s", tektoninstallerset.InstallerSetType, VersionedClusterTaskInstallerSet)); err != nil {
return err
}
}

if err := r.checkComponentStatus(ctx, fmt.Sprintf("%s=%s,%s=%s", tektoninstallerset.InstallerSetType, VersionedClusterTaskInstallerSet, tektoninstallerset.ReleaseVersionKey, r.version)); err != nil {
ta.Status.MarkInstallerSetNotReady(err.Error())
return nil
}

// If pipeline templates are enabled then create an InstallerSet
// with their manifest
if ptVal == "true" {
Expand Down Expand Up @@ -336,8 +364,11 @@ func (r *Reconciler) ensureClusterTasks(ctx context.Context, ta *v1alpha1.Tekton
return err
}

communityClusterTaskManifest := r.manifest
clusterTaskManifest = clusterTaskManifest.Filter(
mf.Not(byContains(getFormattedVersion(r.version))),
)

communityClusterTaskManifest := r.manifest
if err := r.appendCommunityTarget(ctx, &communityClusterTaskManifest, ta); err != nil {
// Continue if failed to resolve community task URL.
// (Ex: on disconnected cluster community tasks won't be reachable because of proxy).
Expand All @@ -358,6 +389,29 @@ func (r *Reconciler) ensureClusterTasks(ctx context.Context, ta *v1alpha1.Tekton
return nil
}

func (r *Reconciler) ensureVersionedClusterTasks(ctx context.Context, ta *v1alpha1.TektonAddon) error {
clusterTaskManifest := r.manifest
// Read clusterTasks from ko data
if err := applyAddons(&clusterTaskManifest, "02-clustertasks"); err != nil {
return err
}
// Run transformers
if err := r.addonTransform(ctx, &clusterTaskManifest, ta); err != nil {
return err
}

clusterTaskManifest = clusterTaskManifest.Filter(
byContains(getFormattedVersion(r.version)),
)

if err := createInstallerSet(ctx, r.operatorClientSet, ta, clusterTaskManifest,
r.version, VersionedClusterTaskInstallerSet, "addon-versioned-clustertasks"); err != nil {
return err
}

return nil
}

// checkIfInstallerSetExist checks if installer set exists for a component and return true/false based on it
// and if installer set which already exist is of older version then it deletes and return false to create a new
// installer set
Expand Down Expand Up @@ -506,3 +560,19 @@ func findValue(params []v1alpha1.Param, name string) (string, bool) {
}
return "", false
}

// byContains returns resources with specific string in name
func byContains(name string) mf.Predicate {
return func(u *unstructured.Unstructured) bool {
return strings.Contains(u.GetName(), name)
}
}

func getFormattedVersion(version string) string {
endIndex := strings.LastIndex(version, ".")
if endIndex != -1 {
version = version[:endIndex]
}
version = strings.TrimPrefix(version, "v")
return strings.Replace(version, ".", "-", -1)
}

0 comments on commit 03e41b0

Please sign in to comment.