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 <damoreno@redhat.com>
  • Loading branch information
davidmogar committed Feb 26, 2024
1 parent b8e29a4 commit 50c7c7b
Show file tree
Hide file tree
Showing 23 changed files with 237 additions and 706 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@v5
with:
go-version: 1.20
go-version: 1.21
- name: Run tests
run: make test
- name: Codecov
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Set up Go 1.x
uses: actions/setup-go@v5
with:
go-version: "1.20"
go-version: "1.21"
- name: Check out code into the Go module directory
uses: actions/checkout@v4
with:
Expand Down
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
5 changes: 5 additions & 0 deletions api/v1alpha1/releaseserviceconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1alpha1

import (
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand All @@ -32,6 +33,10 @@ type ReleaseServiceConfigSpec struct {
// AdvisoryRepo is the repo to create advisories in during the managed release PipelineRun
// +optional
AdvisoryRepo string `json:"advisoryRepo,omitempty"`

// DefaultTimeouts contain the default Tekton timeouts to be used in case they are
// not specified in the ReleasePlanAdmission resource.
DefaultTimeouts tektonv1.TimeoutFields `json:"defaultTimeouts,omitempty"`
}

// ReleaseServiceConfigStatus defines the observed state of ReleaseServiceConfig.
Expand Down
3 changes: 2 additions & 1 deletion api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
19 changes: 19 additions & 0 deletions config/crd/bases/appstudio.redhat.com_releaseserviceconfigs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@ spec:
description: Debug is the boolean that specifies whether or not the
Release Service should run in debug mode
type: boolean
defaultTimeouts:
description: DefaultTimeouts contain the default Tekton timeouts to
be used in case they are not specified in the ReleasePlanAdmission
resource.
properties:
finally:
description: Finally sets the maximum allowed duration of this
pipeline's finally
type: string
pipeline:
description: Pipeline sets the maximum allowed duration for execution
of the entire pipeline. The sum of individual timeouts for tasks
and finally must not exceed this value.
type: string
tasks:
description: Tasks sets the maximum allowed duration of this pipeline's
tasks
type: string
type: object
type: object
status:
description: ReleaseServiceConfigStatus defines the observed state of
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
49 changes: 30 additions & 19 deletions controllers/release/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,17 @@ import (
"os"
"time"

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

"github.com/go-logr/logr"
libhandler "github.com/operator-framework/operator-lib/handler"
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"

libhandler "github.com/operator-framework/operator-lib/handler"
"github.com/redhat-appstudio/release-service/tekton/utils"
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
rbac "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -381,21 +379,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", 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: metadata.ManagedPipelineType,
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, &a.releaseServiceConfig.Spec.DefaultTimeouts).
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
29 changes: 22 additions & 7 deletions controllers/release/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"reflect"
"strings"
"time"
"unicode"

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

Expand Down Expand Up @@ -73,6 +74,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 @@ -564,6 +568,7 @@ var _ = Describe("Release adapter", Ordered, func() {

BeforeEach(func() {
adapter = createReleaseAndAdapter()
adapter.releaseServiceConfig = releaseServiceConfig
})

It("should do nothing if the Release is already processed", func() {
Expand Down Expand Up @@ -715,6 +720,8 @@ var _ = Describe("Release adapter", Ordered, func() {
Policy: enterpriseContractPolicy.Name,
},
}
newReleasePlanAdmission.Kind = "ReleasePlanAdmission"

adapter.ctx = toolkit.GetMockedContext(ctx, []toolkit.MockData{
{
ContextKey: loader.ProcessingResourcesContextKey,
Expand Down Expand Up @@ -1085,7 +1092,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"))
})

It("has the release reference", func() {
Expand All @@ -1095,13 +1102,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(releasePlan.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 All @@ -1118,7 +1131,7 @@ var _ = Describe("Release adapter", Ordered, func() {
})

It("has release labels", func() {
Expect(pipelineRun.GetLabels()[metadata.PipelinesTypeLabel]).To(Equal("release"))
Expect(pipelineRun.GetLabels()[metadata.PipelinesTypeLabel]).To(Equal(metadata.ManagedPipelineType))
Expect(pipelineRun.GetLabels()[metadata.ReleaseNameLabel]).To(Equal(adapter.release.Name))
Expect(pipelineRun.GetLabels()[metadata.ReleaseNamespaceLabel]).To(Equal(testNamespace))
Expect(pipelineRun.GetLabels()[metadata.ReleaseSnapshotLabel]).To(Equal(adapter.release.Spec.Snapshot))
Expand Down Expand Up @@ -1160,8 +1173,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 @@ -2024,6 +2036,7 @@ var _ = Describe("Release adapter", Ordered, func() {
},
}
Expect(k8sClient.Create(ctx, releasePlan)).To(Succeed())
releasePlan.Kind = "ReleasePlan"

releaseServiceConfig = &v1alpha1.ReleaseServiceConfig{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -2054,8 +2067,10 @@ var _ = Describe("Release adapter", Ordered, func() {
{Name: "pathInRepo", Value: "my-path"},
},
},
Timeout: "2h0m0s",
ServiceAccount: "service-account",
Timeouts: tektonv1.TimeoutFields{
Pipeline: &metav1.Duration{Duration: 1 * time.Hour},
},
},
Policy: enterpriseContractPolicy.Name,
},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/redhat-appstudio/release-service

go 1.20
go 1.21

require (
github.com/enterprise-contract/enterprise-contract-controller/api v0.1.35
Expand Down
Loading

0 comments on commit 50c7c7b

Please sign in to comment.