diff --git a/config/crs/openshift/config/all/operator_v1alpha1_config_cr.yaml b/config/crs/openshift/config/all/operator_v1alpha1_config_cr.yaml index 9f21a16e35..7b9b88175b 100644 --- a/config/crs/openshift/config/all/operator_v1alpha1_config_cr.yaml +++ b/config/crs/openshift/config/all/operator_v1alpha1_config_cr.yaml @@ -27,6 +27,8 @@ spec: value: "true" - name: resolverStepActions value: "true" + - name: communityClusterTasks + value: "true" params: - name: createRbacResource value: "true" diff --git a/pkg/reconciler/openshift/tektonaddon/community_tasks.go b/pkg/reconciler/openshift/tektonaddon/community_tasks.go new file mode 100644 index 0000000000..659cde59ae --- /dev/null +++ b/pkg/reconciler/openshift/tektonaddon/community_tasks.go @@ -0,0 +1,87 @@ +/* +Copyright 2022 The Tekton Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package tektonaddon + +import ( + "context" + "strings" + + mf "github.com/manifestival/manifestival" + "github.com/tektoncd/operator/pkg/apis/operator/v1alpha1" + "github.com/tektoncd/operator/pkg/reconciler/common" + "github.com/tektoncd/operator/pkg/reconciler/kubernetes/tektoninstallerset/client" +) + +var communityResourceURLs = []string{ + "https://raw.githubusercontent.com/tektoncd/catalog/master/task/jib-maven/0.5/jib-maven.yaml", + "https://raw.githubusercontent.com/tektoncd/catalog/master/task/helm-upgrade-from-source/0.3/helm-upgrade-from-source.yaml", + "https://raw.githubusercontent.com/tektoncd/catalog/master/task/helm-upgrade-from-repo/0.2/helm-upgrade-from-repo.yaml", + "https://raw.githubusercontent.com/tektoncd/catalog/master/task/trigger-jenkins-job/0.1/trigger-jenkins-job.yaml", + "https://raw.githubusercontent.com/tektoncd/catalog/master/task/pull-request/0.1/pull-request.yaml", + "https://raw.githubusercontent.com/tektoncd/catalog/master/task/kubeconfig-creator/0.1/kubeconfig-creator.yaml", + "https://raw.githubusercontent.com/tektoncd/catalog/main/task/argocd-task-sync-and-wait/0.2/argocd-task-sync-and-wait.yaml", +} + +func (r *Reconciler) EnsureCommunityTask(ctx context.Context, enable string, ta *v1alpha1.TektonAddon) error { + if len(r.communityTaskManifest.Resources()) == 0 { + return nil + } + manifest := *r.communityTaskManifest + if enable == "true" { + if err := r.installerSetClient.CustomSet(ctx, ta, CommunityTaskInstallerSet, &manifest, filterAndTransformCommunityTask(), nil); err != nil { + return err + } + } else { + if err := r.installerSetClient.CleanupCustomSet(ctx, CommunityTaskInstallerSet); err != nil { + return err + } + } + return nil +} + +func filterAndTransformCommunityTask() client.FilterAndTransform { + return func(ctx context.Context, manifest *mf.Manifest, comp v1alpha1.TektonComponent) (*mf.Manifest, error) { + instance := comp.(*v1alpha1.TektonAddon) + addonImages := common.ToLowerCaseKeys(common.ImagesFromEnv(common.AddonsImagePrefix)) + + extra := []mf.Transformer{ + injectLabel(labelProviderType, providerTypeCommunity, overwrite, "Task"), + common.TaskImages(ctx, addonImages), + } + if err := common.Transform(ctx, manifest, instance, extra...); err != nil { + return nil, err + } + return manifest, nil + } +} + +func appendCommunityTasks(manifest *mf.Manifest) error { + urls := strings.Join(communityResourceURLs, ",") + m, err := mf.ManifestFrom(mf.Path(urls)) + if err != nil { + return err + } + *manifest = manifest.Append(m) + return nil +} + +func fetchCommunityTasks(manifest *mf.Manifest) error { + if err := appendCommunityTasks(manifest); err != nil { + return err + } + return nil +} diff --git a/pkg/reconciler/openshift/tektonaddon/const.go b/pkg/reconciler/openshift/tektonaddon/const.go index 89e56a911b..807d12b774 100644 --- a/pkg/reconciler/openshift/tektonaddon/const.go +++ b/pkg/reconciler/openshift/tektonaddon/const.go @@ -22,6 +22,7 @@ const ( OpenShiftConsoleInstallerSet = "OpenShiftConsole" VersionedResolverTaskInstallerSet = "VersionedResolverTask" VersionedResolverStepActionInstallerSet = "VersionedResolverStepAction" + CommunityTaskInstallerSet = "CommunityTask" versionedClusterTaskPatchChar = "0" PipelinesTemplateInstallerSet = "PipelinesTemplate" TriggersResourcesInstallerSet = "TriggersResources" diff --git a/pkg/reconciler/openshift/tektonaddon/controller.go b/pkg/reconciler/openshift/tektonaddon/controller.go index b22dc890ad..026bde3d3f 100644 --- a/pkg/reconciler/openshift/tektonaddon/controller.go +++ b/pkg/reconciler/openshift/tektonaddon/controller.go @@ -114,6 +114,12 @@ func NewExtendedController(generator common.ExtensionGenerator) injection.Contro logger.Fatalf("failed to read console cli from kodata: %v", err) } + communityTaskManifest := &mf.Manifest{} + if err := fetchCommunityTasks(communityTaskManifest); err != nil { + // if unable to fetch community task, don't fail + logger.Errorf("failed to read community task: %v", err) + } + c := &Reconciler{ crdClientSet: crdClient, installerSetClient: client.NewInstallerSetClient(tisClient, version, "addon", v1alpha1.KindTektonAddon, metrics), @@ -129,6 +135,7 @@ func NewExtendedController(generator common.ExtensionGenerator) injection.Contro pipelineTemplateManifest: pipelineTemplateManifest, openShiftConsoleManifest: openShiftConsoleManifest, consoleCLIManifest: consoleCLIManifest, + communityTaskManifest: communityTaskManifest, } impl := tektonAddonreconciler.NewImpl(ctx, c) diff --git a/pkg/reconciler/openshift/tektonaddon/tektonaddon.go b/pkg/reconciler/openshift/tektonaddon/tektonaddon.go index 18735d2381..1eee74c32a 100644 --- a/pkg/reconciler/openshift/tektonaddon/tektonaddon.go +++ b/pkg/reconciler/openshift/tektonaddon/tektonaddon.go @@ -54,6 +54,7 @@ type Reconciler struct { pipelineTemplateManifest *mf.Manifest openShiftConsoleManifest *mf.Manifest consoleCLIManifest *mf.Manifest + communityTaskManifest *mf.Manifest } const ( @@ -64,6 +65,7 @@ const ( providerTypeRedHat = "redhat" installerSetNameForResolverTasks = "addon-versioned-resolvertasks" installerSetNameForResolverStepAction = "addon-versioned-resolverstepactions" + providerTypeCommunity = "community" ) // Check that our Reconciler implements controller.Reconciler @@ -131,6 +133,7 @@ func (r *Reconciler) ReconcileKind(ctx context.Context, ta *v1alpha1.TektonAddon ptVal, _ := findValue(ta.Spec.Params, v1alpha1.PipelineTemplatesParam) rtVal, _ := findValue(ta.Spec.Params, v1alpha1.ResolverTasks) rsaVal, _ := findValue(ta.Spec.Params, v1alpha1.ResolverStepActions) + ctVal, _ := findValue(ta.Spec.Params, v1alpha1.CommunityClusterTasks) if ptVal == "true" && rtVal == "false" { ta.Status.MarkNotReady("pipelineTemplates cannot be true if ResolverTask is false") @@ -203,6 +206,12 @@ func (r *Reconciler) ReconcileKind(ctx context.Context, ta *v1alpha1.TektonAddon } } + if err := r.EnsureCommunityTask(ctx, ctVal, ta); err != nil { + ready = false + errorMsg = fmt.Sprintf("community tasks not yet ready: %v", err) + logger.Error(errorMsg) + } + if !ready { ta.Status.MarkInstallerSetNotReady(errorMsg) return nil diff --git a/test/resources/tektonaddons.go b/test/resources/tektonaddons.go index 900a377f85..0df24b97e9 100644 --- a/test/resources/tektonaddons.go +++ b/test/resources/tektonaddons.go @@ -102,6 +102,7 @@ func AssertTektonInstallerSets(t *testing.T, clients *utils.Clients) { assertInstallerSets(t, clients, tektonaddon.TriggersResourcesInstallerSet) assertInstallerSets(t, clients, tektonaddon.ConsoleCLIInstallerSet) assertInstallerSets(t, clients, tektonaddon.MiscellaneousResourcesInstallerSet) + assertInstallerSets(t, clients, tektonaddon.CommunityTaskInstallerSet) } func assertInstallerSets(t *testing.T, clients *utils.Clients, component string) {