Skip to content

Commit

Permalink
feat(RHTAPREL-717): use new PipelineRun builder
Browse files Browse the repository at this point in the history
This commit uses the new builder to generate the managed PipelineRun.

Signed-off-by: David Moreno García <[email protected]>
  • Loading branch information
davidmogar committed Feb 6, 2024
1 parent 244fdf1 commit 810781f
Show file tree
Hide file tree
Showing 15 changed files with 153 additions and 698 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ DEFAULT_RELEASE_PVC ?= release-pvc
# DEFAULT_WORKSPACE_NAME defines the default name for the workspace that will be used in the managed Release Pipeline.
DEFAULT_RELEASE_WORKSPACE_NAME ?= release-workspace

# DEFAULT_WORKSPACE_SIZE defines the default size for the workspace that will be used in the managed Release Pipeline.
DEFAULT_RELEASE_WORKSPACE_SIZE ?= 1Gi

# CHANNELS define the bundle channels used in the bundle.
# Add a new line here if you would like to change its default config. (E.g CHANNELS = "candidate,fast,stable")
# To re-generate a bundle for other specific channels without changing the standard setup, you can:
Expand Down Expand Up @@ -158,6 +161,7 @@ deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
DEFAULT_RELEASE_PVC=${DEFAULT_RELEASE_PVC} \
DEFAULT_RELEASE_WORKSPACE_NAME=${DEFAULT_RELEASE_WORKSPACE_NAME} \
DEFAULT_RELEASE_WORKSPACE_SIZE=${DEFAULT_RELEASE_WORKSPACE_SIZE} \
$(KUSTOMIZE) build config/default | kubectl apply -f -

.PHONY: undeploy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,6 @@ spec:
the execution of the Pipeline
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?$
type: string
timeout:
default: "0"
description: "Timeout is a value to use to override the tekton
default Pipelinerun timeout \n This field is DEPRECATED and
will be replaced by Timeouts in a future change."
type: string
timeouts:
description: Timeouts defines the different Timeouts to use in
the PipelineRun execution
Expand Down
1 change: 1 addition & 0 deletions config/manager/manager.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
DEFAULT_RELEASE_PVC
DEFAULT_RELEASE_WORKSPACE_NAME
DEFAULT_RELEASE_WORKSPACE_SIZE
6 changes: 6 additions & 0 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ spec:
key: DEFAULT_RELEASE_WORKSPACE_NAME
name: manager-properties
optional: true
- name: DEFAULT_RELEASE_WORKSPACE_SIZE
valueFrom:
configMapKeyRef:
key: DEFAULT_RELEASE_WORKSPACE_SIZE
name: manager-properties
optional: true
- name: SERVICE_NAMESPACE
valueFrom:
fieldRef:
Expand Down
50 changes: 31 additions & 19 deletions controllers/release/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@ package release
import (
"context"
"fmt"
"os"

"github.com/redhat-appstudio/operator-toolkit/controller"

"github.com/go-logr/logr"
applicationapiv1alpha1 "github.com/redhat-appstudio/application-api/api/v1alpha1"
integrationgitops "github.com/redhat-appstudio/integration-service/gitops"
"github.com/redhat-appstudio/operator-toolkit/controller"
"github.com/redhat-appstudio/release-service/api/v1alpha1"
"github.com/redhat-appstudio/release-service/gitops"
"github.com/redhat-appstudio/release-service/loader"
"github.com/redhat-appstudio/release-service/metadata"
"github.com/redhat-appstudio/release-service/syncer"
"github.com/redhat-appstudio/release-service/tekton"

applicationapiv1alpha1 "github.com/redhat-appstudio/application-api/api/v1alpha1"
"github.com/redhat-appstudio/release-service/tekton/utils"
"os"
"strings"

libhandler "github.com/operator-framework/operator-lib/handler"
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
Expand Down Expand Up @@ -336,21 +335,34 @@ func (a *adapter) EnsureReleaseProcessingIsTracked() (controller.OperationResult
// will be extracted from the given ReleaseStrategy. The Release's Snapshot will also be passed to the release
// PipelineRun.
func (a *adapter) createManagedPipelineRun(resources *loader.ProcessingResources) (*tektonv1.PipelineRun, error) {
pipelineRun := tekton.NewReleasePipelineRun("managed-release", resources.ReleasePlanAdmission.Namespace).
WithObjectReferences(a.release, resources.ReleasePlan,
resources.ReleasePlanAdmission, resources.Snapshot).
pipelineRun, err := utils.NewPipelineRunBuilder("managed-pr", resources.ReleasePlanAdmission.Namespace).
WithAnnotations(metadata.GetAnnotationsWithPrefix(a.release, integrationgitops.PipelinesAsCodePrefix)).
WithFinalizer(metadata.ReleaseFinalizer).
WithLabels(map[string]string{
metadata.ApplicationNameLabel: resources.ReleasePlan.Spec.Application,
metadata.PipelinesTypeLabel: strings.ToLower(a.release.Kind),
metadata.ReleaseNameLabel: a.release.Name,
metadata.ReleaseNamespaceLabel: a.release.Namespace,
metadata.ReleaseSnapshotLabel: a.release.Spec.Snapshot,
}).
WithObjectReferences(a.release, resources.ReleasePlan, resources.ReleasePlanAdmission, resources.Snapshot).
WithObjectSpecsAsJson(resources.EnterpriseContractPolicy).
WithOwner(a.release).
WithReleaseAndApplicationMetadata(a.release, resources.Snapshot.Spec.Application).
WithWorkspace(os.Getenv("DEFAULT_RELEASE_WORKSPACE_NAME"), os.Getenv("DEFAULT_RELEASE_PVC")).
WithServiceAccount(resources.ReleasePlanAdmission.Spec.Pipeline.ServiceAccount).
WithTimeout(resources.ReleasePlanAdmission.Spec.Pipeline.Timeout).
WithParamsFromConfigMap(resources.EnterpriseContractConfigMap, []string{"verify_ec_task_bundle"}).
WithPipelineRef(resources.ReleasePlanAdmission.Spec.Pipeline.PipelineRef.ToTektonPipelineRef()).
WithTaskGitPipelineParameters(&resources.ReleasePlanAdmission.Spec.Pipeline.PipelineRef).
WithEnterpriseContractConfigMap(resources.EnterpriseContractConfigMap).
WithEnterpriseContractPolicy(resources.EnterpriseContractPolicy).
AsPipelineRun()
WithServiceAccount(resources.ReleasePlanAdmission.Spec.Pipeline.ServiceAccount).
WithTimeouts(&resources.ReleasePlanAdmission.Spec.Pipeline.Timeouts).
WithWorkspaceFromVolumeTemplate(
os.Getenv("DEFAULT_RELEASE_WORKSPACE_NAME"),
os.Getenv("DEFAULT_RELEASE_WORKSPACE_SIZE"),
).
Build()

if err != nil {
return nil, err
}

err := a.client.Create(a.ctx, pipelineRun)
err = a.client.Create(a.ctx, pipelineRun)
if err != nil {
return nil, err
}
Expand Down
25 changes: 19 additions & 6 deletions controllers/release/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"os"
"reflect"
"strings"
"time"
"unicode"

tektonutils "github.com/redhat-appstudio/release-service/tekton/utils"

Expand Down Expand Up @@ -70,6 +72,9 @@ var _ = Describe("Release adapter", Ordered, func() {
})

BeforeAll(func() {
Expect(os.Setenv("DEFAULT_RELEASE_WORKSPACE_NAME", "release-workspace")).To(Succeed())
Expect(os.Setenv("DEFAULT_RELEASE_WORKSPACE_SIZE", "1Gi")).To(Succeed())

createResources()
})

Expand Down Expand Up @@ -834,7 +839,7 @@ var _ = Describe("Release adapter", Ordered, func() {

It("returns a PipelineRun with the right prefix", func() {
Expect(reflect.TypeOf(pipelineRun)).To(Equal(reflect.TypeOf(&tektonv1.PipelineRun{})))
Expect(pipelineRun.Name).To(HavePrefix("managed-release"))
Expect(pipelineRun.Name).To(HavePrefix("managed-pr"))
})

It("has the release reference", func() {
Expand All @@ -844,13 +849,19 @@ var _ = Describe("Release adapter", Ordered, func() {
})

It("has the releasePlan reference", func() {
Expect(pipelineRun.Spec.Params).Should(ContainElement(HaveField("Name", strings.ToLower(releasePlan.Kind))))
name := []rune(releasePlanAdmission.Kind)
name[0] = unicode.ToLower(name[0])

Expect(pipelineRun.Spec.Params).Should(ContainElement(HaveField("Name", string(name))))
Expect(pipelineRun.Spec.Params).Should(ContainElement(HaveField("Value.StringVal",
fmt.Sprintf("%s%c%s", releasePlan.Namespace, types.Separator, releasePlan.Name))))
})

It("has the releasePlanAdmission reference", func() {
Expect(pipelineRun.Spec.Params).Should(ContainElement(HaveField("Name", strings.ToLower(releasePlanAdmission.Kind))))
name := []rune(releasePlanAdmission.Kind)
name[0] = unicode.ToLower(name[0])

Expect(pipelineRun.Spec.Params).Should(ContainElement(HaveField("Name", string(name))))
Expect(pipelineRun.Spec.Params).Should(ContainElement(HaveField("Value.StringVal",
fmt.Sprintf("%s%c%s", releasePlanAdmission.Namespace, types.Separator, releasePlanAdmission.Name))))
})
Expand Down Expand Up @@ -909,8 +920,7 @@ var _ = Describe("Release adapter", Ordered, func() {
})

It("contains the proper timeout value", func() {
timeout := releasePlanAdmission.Spec.Pipeline.Timeout
Expect(pipelineRun.Spec.Timeouts.Pipeline.Duration.String()).To(Equal(string(timeout)))
Expect(pipelineRun.Spec.Timeouts.Pipeline).To(Equal(releasePlanAdmission.Spec.Pipeline.Timeouts.Pipeline))
})

It("contains a parameter with the verify ec task bundle", func() {
Expand Down Expand Up @@ -1695,6 +1705,7 @@ var _ = Describe("Release adapter", Ordered, func() {
},
}
Expect(k8sClient.Create(ctx, releasePlan)).To(Succeed())
releasePlan.Kind = "Snapshot"

releaseServiceConfig = &v1alpha1.ReleaseServiceConfig{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -1725,7 +1736,9 @@ var _ = Describe("Release adapter", Ordered, func() {
{Name: "pathInRepo", Value: "my-path"},
},
},
Timeout: "2h0m0s",
Timeouts: tektonv1.TimeoutFields{
Pipeline: &metav1.Duration{Duration: 1 * time.Hour},
},
},
Policy: enterpriseContractPolicy.Name,
},
Expand Down
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ func main() {
}
}

// Set a default value for the DEFAULT_RELEASE_WORKSPACE_SIZE environment variable
if os.Getenv("DEFAULT_RELEASE_WORKSPACE_SIZE") == "" {
err := os.Setenv("DEFAULT_RELEASE_WORKSPACE_SIZE", "1Gi")
if err != nil {
setupLog.Error(err, "unable to setup DEFAULT_RELEASE_WORKSPACE_SIZE environment variable")
os.Exit(1)
}
}

setUpControllers(mgr)
setUpWebhooks(mgr)

Expand Down
Loading

0 comments on commit 810781f

Please sign in to comment.