From a585d6ff74fdb1309d0fcead5f1dab046d2727ad Mon Sep 17 00:00:00 2001 From: Dan Lorenc Date: Tue, 13 Aug 2019 11:17:41 -0500 Subject: [PATCH] Add a PipelineResourceBase struct that implements some stub versions of the interfaces. This struct can be embedded to simplify and reduce boilerplate across the different resource types, making further refactors easier. --- .../pipeline/v1alpha1/cloud_event_resource.go | 10 +--------- .../pipeline/v1alpha1/cluster_resource.go | 4 +--- pkg/apis/pipeline/v1alpha1/git_resource.go | 5 +---- pkg/apis/pipeline/v1alpha1/image_resource.go | 7 +------ .../v1alpha1/pull_request_resource.go | 9 +-------- pkg/apis/pipeline/v1alpha1/resource_types.go | 20 +++++++++++++++++++ 6 files changed, 25 insertions(+), 30 deletions(-) diff --git a/pkg/apis/pipeline/v1alpha1/cloud_event_resource.go b/pkg/apis/pipeline/v1alpha1/cloud_event_resource.go index 71d53d593b2..c7faf6f1a52 100644 --- a/pkg/apis/pipeline/v1alpha1/cloud_event_resource.go +++ b/pkg/apis/pipeline/v1alpha1/cloud_event_resource.go @@ -19,8 +19,6 @@ package v1alpha1 import ( "fmt" "strings" - - corev1 "k8s.io/api/core/v1" ) // CloudEventResource is an event sink to which events are delivered when a TaskRun has finished @@ -31,6 +29,7 @@ type CloudEventResource struct { Type PipelineResourceType `json:"type"` // TargetURI is the URI of the sink which the cloud event is develired to TargetURI string `json:"targetURI"` + PipelineResourceBase } // NewCloudEventResource creates a new CloudEvent resource to pass to a Task @@ -79,10 +78,3 @@ func (s *CloudEventResource) Replacements() map[string]string { "target-uri": s.TargetURI, } } - -func (s *CloudEventResource) GetUploadSteps(string) ([]Step, error) { return nil, nil } -func (s *CloudEventResource) GetDownloadSteps(string) ([]Step, error) { return nil, nil } -func (s *CloudEventResource) GetUploadVolumeSpec(*TaskSpec) ([]corev1.Volume, error) { return nil, nil } -func (s *CloudEventResource) GetDownloadVolumeSpec(*TaskSpec) ([]corev1.Volume, error) { - return nil, nil -} diff --git a/pkg/apis/pipeline/v1alpha1/cluster_resource.go b/pkg/apis/pipeline/v1alpha1/cluster_resource.go index 3ae7194f989..467a6b41ce4 100644 --- a/pkg/apis/pipeline/v1alpha1/cluster_resource.go +++ b/pkg/apis/pipeline/v1alpha1/cluster_resource.go @@ -54,6 +54,7 @@ type ClusterResource struct { CAData []byte `json:"cadata"` //Secrets holds a struct to indicate a field name and corresponding secret name to populate it Secrets []SecretParam `json:"secrets"` + PipelineResourceBase } // NewClusterResource create a new k8s cluster resource to pass to a pipeline task @@ -166,6 +167,3 @@ func (s *ClusterResource) GetDownloadSteps(sourcePath string) ([]Step, error) { Env: envVars, }}}, nil } - -func (s *ClusterResource) GetUploadVolumeSpec(*TaskSpec) ([]corev1.Volume, error) { return nil, nil } -func (s *ClusterResource) GetDownloadVolumeSpec(*TaskSpec) ([]corev1.Volume, error) { return nil, nil } diff --git a/pkg/apis/pipeline/v1alpha1/git_resource.go b/pkg/apis/pipeline/v1alpha1/git_resource.go index 5885aa09ce7..8b7fca8f5bf 100644 --- a/pkg/apis/pipeline/v1alpha1/git_resource.go +++ b/pkg/apis/pipeline/v1alpha1/git_resource.go @@ -44,6 +44,7 @@ type GitResource struct { // https://git-scm.com/docs/gitrevisions#_specifying_revisions for more // information. Revision string `json:"revision"` + PipelineResourceBase } // NewGitResource creates a new git resource to pass to a Task @@ -110,7 +111,3 @@ func (s *GitResource) GetDownloadSteps(sourcePath string) ([]Step, error) { WorkingDir: WorkspaceDir, }}}, nil } - -func (s *GitResource) GetUploadSteps(sourcePath string) ([]Step, error) { return nil, nil } -func (s *GitResource) GetUploadVolumeSpec(spec *TaskSpec) ([]corev1.Volume, error) { return nil, nil } -func (s *GitResource) GetDownloadVolumeSpec(spec *TaskSpec) ([]corev1.Volume, error) { return nil, nil } diff --git a/pkg/apis/pipeline/v1alpha1/image_resource.go b/pkg/apis/pipeline/v1alpha1/image_resource.go index f5066ab6b68..6fe6cf2bd75 100644 --- a/pkg/apis/pipeline/v1alpha1/image_resource.go +++ b/pkg/apis/pipeline/v1alpha1/image_resource.go @@ -21,7 +21,6 @@ import ( "strings" "golang.org/x/xerrors" - corev1 "k8s.io/api/core/v1" ) // NewImageResource creates a new ImageResource from a PipelineResource. @@ -53,6 +52,7 @@ type ImageResource struct { URL string `json:"url"` Digest string `json:"digest"` OutputImageDir string + PipelineResourceBase } // GetName returns the name of the resource @@ -75,11 +75,6 @@ func (s *ImageResource) Replacements() map[string]string { } } -func (s *ImageResource) GetUploadSteps(string) ([]Step, error) { return nil, nil } -func (s *ImageResource) GetDownloadSteps(string) ([]Step, error) { return nil, nil } -func (s *ImageResource) GetUploadVolumeSpec(*TaskSpec) ([]corev1.Volume, error) { return nil, nil } -func (s *ImageResource) GetDownloadVolumeSpec(*TaskSpec) ([]corev1.Volume, error) { return nil, nil } - // GetOutputImageDir return the path to get the index.json file func (s *ImageResource) GetOutputImageDir() string { return s.OutputImageDir diff --git a/pkg/apis/pipeline/v1alpha1/pull_request_resource.go b/pkg/apis/pipeline/v1alpha1/pull_request_resource.go index b202fd19d97..6fc3f5a21b2 100644 --- a/pkg/apis/pipeline/v1alpha1/pull_request_resource.go +++ b/pkg/apis/pipeline/v1alpha1/pull_request_resource.go @@ -47,6 +47,7 @@ type PullRequestResource struct { URL string `json:"url"` // Secrets holds a struct to indicate a field name and corresponding secret name to populate it. Secrets []SecretParam `json:"secrets"` + PipelineResourceBase } // NewPullRequestResource create a new git resource to pass to a Task @@ -131,11 +132,3 @@ func (s *PullRequestResource) getSteps(mode string, sourcePath string) ([]Step, Env: evs, }}}, nil } - -func (s *PullRequestResource) GetUploadVolumeSpec(spec *TaskSpec) ([]corev1.Volume, error) { - return nil, nil -} - -func (s *PullRequestResource) GetDownloadVolumeSpec(spec *TaskSpec) ([]corev1.Volume, error) { - return nil, nil -} diff --git a/pkg/apis/pipeline/v1alpha1/resource_types.go b/pkg/apis/pipeline/v1alpha1/resource_types.go index 8cab9f52313..dde8b3adf3c 100644 --- a/pkg/apis/pipeline/v1alpha1/resource_types.go +++ b/pkg/apis/pipeline/v1alpha1/resource_types.go @@ -62,6 +62,26 @@ type PipelineResourceInterface interface { GetDownloadVolumeSpec(spec *TaskSpec) ([]corev1.Volume, error) } +// PipelineResourceBase is a struct that can be used to simplify implementations. +type PipelineResourceBase struct { +} + +func (p *PipelineResourceBase) GetDownloadSteps(sourcePath string) ([]Step, error) { + return nil, nil +} + +func (p *PipelineResourceBase) GetUploadSteps(sourcePath string) ([]Step, error) { + return nil, nil +} + +func (p *PipelineResourceBase) GetUploadVolumeSpec(spec *TaskSpec) ([]corev1.Volume, error) { + return nil, nil +} + +func (p *PipelineResourceBase) GetDownloadVolumeSpec(spec *TaskSpec) ([]corev1.Volume, error) { + return nil, nil +} + // SecretParam indicates which secret can be used to populate a field of the resource type SecretParam struct { FieldName string `json:"fieldName"`