Skip to content

Commit

Permalink
feat(RELEASE-1434): allow to override volume type (#666)
Browse files Browse the repository at this point in the history
This is a provisional change that will be used during the
transition to using Trusted Artificts in the
release-service-catalog pipelines.

Trusted Artifacts requireis to use an emptyDir in opposition
to the PVCs used today. This change allow to override
the volume type used by a given pipeline by defining a list
of EmptyDirOverride items which contain the details of the
pipeline to be overriden. Regex is supported in the url and
revision values.

Signed-off-by: David Moreno García <[email protected]>
  • Loading branch information
davidmogar authored Feb 10, 2025
1 parent 11f89cf commit 5954f10
Show file tree
Hide file tree
Showing 10 changed files with 405 additions and 9 deletions.
41 changes: 41 additions & 0 deletions api/v1alpha1/releaseserviceconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package v1alpha1
import (
tektonv1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"regexp"
)

const ReleaseServiceConfigResourceName string = "release-service-config"
Expand All @@ -33,6 +34,25 @@ type ReleaseServiceConfigSpec struct {
// 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"`

// VolumeOverrides is a map containing the volume type for specific Pipeline git refs
// +optional
EmptyDirOverrides []EmptyDirOverrides `json:"EmptyDirOverrides,omitempty"`
}

// EmptyDirOverrides defines the values usually set in a PipelineRef using a git resolver.
type EmptyDirOverrides struct {
// Url is the url to the git repo
// +required
Url string `json:"url"`

// Revision is the git revision where the Pipeline definition can be found
// +required
Revision string `json:"revision"`

// PathInRepo is the path within the git repository where the Pipeline definition can be found
// +required
PathInRepo string `json:"pathInRepo"`
}

// ReleaseServiceConfigStatus defines the observed state of ReleaseServiceConfig.
Expand All @@ -52,6 +72,27 @@ type ReleaseServiceConfig struct {
Status ReleaseServiceConfigStatus `json:"status,omitempty"`
}

// IsPipelineOverridden checks whether there is a EmptyDirOverride matching the given url, revision and pathInRepo.
func (rsc *ReleaseServiceConfig) IsPipelineOverridden(url, revision, pathInRepo string) bool {
for _, override := range rsc.Spec.EmptyDirOverrides {
urlRegex, err := regexp.Compile(override.Url)
if err != nil || !urlRegex.MatchString(url) {
continue
}

revisionRegex, err := regexp.Compile(override.Revision)
if err != nil || !revisionRegex.MatchString(revision) {
continue
}

if override.PathInRepo == pathInRepo {
return true
}
}

return false
}

//+kubebuilder:object:root=true

// ReleaseServiceConfigList contains a list of ReleaseServiceConfig
Expand Down
71 changes: 71 additions & 0 deletions api/v1alpha1/releaseserviceconfig_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright 2022.
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 v1alpha1

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var _ = Describe("ReleaseServiceConfig type", func() {
When("IsPipelineOverridden method is called", func() {
var releaseServiceConfig *ReleaseServiceConfig

BeforeEach(func() {
releaseServiceConfig = &ReleaseServiceConfig{
ObjectMeta: metav1.ObjectMeta{
Name: "config",
Namespace: "default",
},
}
})

It("should return false if the resource is not overridden", func() {
Expect(releaseServiceConfig.IsPipelineOverridden("foo", "bar", "baz")).To(BeFalse())
})

It("should return true if the resource is overridden", func() {
releaseServiceConfig.Spec.EmptyDirOverrides = []EmptyDirOverrides{
{"foo", "bar", "baz"},
}
Expect(releaseServiceConfig.IsPipelineOverridden("foo", "bar", "baz")).To(BeTrue())
})

It("should return true if the resource is overridden using a regex expression in the url field", func() {
releaseServiceConfig.Spec.EmptyDirOverrides = []EmptyDirOverrides{
{".*", "bar", "baz"},
}
Expect(releaseServiceConfig.IsPipelineOverridden("foo", "bar", "baz")).To(BeTrue())
})

It("should return true if the resource is overridden using a regex expression in the revision field", func() {
releaseServiceConfig.Spec.EmptyDirOverrides = []EmptyDirOverrides{
{"foo", ".*", "baz"},
}
Expect(releaseServiceConfig.IsPipelineOverridden("foo", "bar", "baz")).To(BeTrue())
})

It("should return false if the resource is overridden using a regex expression in the pathInRepo field", func() {
releaseServiceConfig.Spec.EmptyDirOverrides = []EmptyDirOverrides{
{"foo", "bar", ".*"},
}
Expect(releaseServiceConfig.IsPipelineOverridden("foo", "bar", "baz")).To(BeFalse())
})
})
})
20 changes: 20 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

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

24 changes: 24 additions & 0 deletions config/crd/bases/appstudio.redhat.com_releaseserviceconfigs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ spec:
spec:
description: ReleaseServiceConfigSpec defines the desired state of ReleaseServiceConfig.
properties:
EmptyDirOverrides:
description: VolumeOverrides is a map containing the volume type for
specific Pipeline git refs
items:
description: EmptyDirOverrides defines the values usually set in
a PipelineRef using a git resolver.
properties:
pathInRepo:
description: PathInRepo is the path within the git repository
where the Pipeline definition can be found
type: string
revision:
description: Revision is the git revision where the Pipeline
definition can be found
type: string
url:
description: Url is the url to the git repo
type: string
required:
- pathInRepo
- revision
- url
type: object
type: array
debug:
description: |-
Debug is the boolean that specifies whether or not the Release Service should run
Expand Down
20 changes: 15 additions & 5 deletions controllers/release/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ func (a *adapter) createFinalPipelineRun(releasePlan *v1alpha1.ReleasePlan, snap
// will be extracted from the given ReleasePlanAdmission. The Release's Snapshot will also be passed to the release
// PipelineRun.
func (a *adapter) createManagedPipelineRun(resources *loader.ProcessingResources) (*tektonv1.PipelineRun, error) {
pipelineRun, err := utils.NewPipelineRunBuilder(metadata.ManagedPipelineType, resources.ReleasePlanAdmission.Namespace).
builder := utils.NewPipelineRunBuilder(metadata.ManagedPipelineType, resources.ReleasePlanAdmission.Namespace).
WithAnnotations(metadata.GetAnnotationsWithPrefix(a.release, integrationgitops.PipelinesAsCodePrefix)).
WithFinalizer(metadata.ReleaseFinalizer).
WithLabels(map[string]string{
Expand All @@ -870,13 +870,23 @@ func (a *adapter) createManagedPipelineRun(resources *loader.ProcessingResources
WithParamsFromConfigMap(resources.EnterpriseContractConfigMap, []string{"verify_ec_task_bundle"}).
WithPipelineRef(resources.ReleasePlanAdmission.Spec.Pipeline.PipelineRef.ToTektonPipelineRef()).
WithServiceAccount(resources.ReleasePlanAdmission.Spec.Pipeline.ServiceAccountName).
WithTimeouts(&resources.ReleasePlanAdmission.Spec.Pipeline.Timeouts, &a.releaseServiceConfig.Spec.DefaultTimeouts).
WithWorkspaceFromVolumeTemplate(
WithTimeouts(&resources.ReleasePlanAdmission.Spec.Pipeline.Timeouts, &a.releaseServiceConfig.Spec.DefaultTimeouts)

url, revision, pathInRepo, err := resources.ReleasePlanAdmission.Spec.Pipeline.PipelineRef.GetGitResolverParams()
if err == nil && a.releaseServiceConfig.IsPipelineOverridden(url, revision, pathInRepo) {
builder.WithEmptyDirVolume(
os.Getenv("DEFAULT_RELEASE_WORKSPACE_NAME"),
os.Getenv("DEFAULT_RELEASE_WORKSPACE_SIZE"),
).
Build()
)
} else {
builder.WithWorkspaceFromVolumeTemplate(
os.Getenv("DEFAULT_RELEASE_WORKSPACE_NAME"),
os.Getenv("DEFAULT_RELEASE_WORKSPACE_SIZE"),
)
}

var pipelineRun *tektonv1.PipelineRun
pipelineRun, err = builder.Build()
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 5954f10

Please sign in to comment.