Skip to content

Commit

Permalink
Add a PipelineResourceBase struct that implements some stub versions …
Browse files Browse the repository at this point in the history
…of the interfaces.

This struct can be embedded to simplify and reduce boilerplate across the different resource
types, making further refactors easier.
  • Loading branch information
dlorenc committed Aug 13, 2019
1 parent 5e95307 commit 1c3270a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 51 deletions.
13 changes: 1 addition & 12 deletions pkg/apis/pipeline/v1alpha1/cluster_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -138,10 +139,6 @@ func (s ClusterResource) String() string {
return string(json)
}

func (s *ClusterResource) GetUploadContainerSpec(_ string) ([]corev1.Container, error) {
return nil, nil
}

func (s *ClusterResource) GetDownloadContainerSpec(sourcePath string) ([]corev1.Container, error) {
var envVars []corev1.EnvVar
for _, sec := range s.Secrets {
Expand Down Expand Up @@ -171,11 +168,3 @@ func (s *ClusterResource) GetDownloadContainerSpec(sourcePath string) ([]corev1.

return []corev1.Container{clusterContainer}, nil
}

func (s *ClusterResource) GetUploadVolumeSpec(spec *TaskSpec) ([]corev1.Volume, error) {
return nil, nil
}

func (s *ClusterResource) GetDownloadVolumeSpec(spec *TaskSpec) ([]corev1.Volume, error) {
return nil, nil
}
13 changes: 1 addition & 12 deletions pkg/apis/pipeline/v1alpha1/git_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -110,15 +111,3 @@ func (s *GitResource) GetDownloadContainerSpec(sourcePath string) ([]corev1.Cont
WorkingDir: WorkspaceDir,
}}, nil
}

func (s *GitResource) GetUploadContainerSpec(sourcePath string) ([]corev1.Container, 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
}
20 changes: 1 addition & 19 deletions pkg/apis/pipeline/v1alpha1/image_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"strings"

"golang.org/x/xerrors"
corev1 "k8s.io/api/core/v1"
)

// NewImageResource creates a new ImageResource from a PipelineResource.
Expand Down Expand Up @@ -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
Expand All @@ -75,16 +75,6 @@ func (s *ImageResource) Replacements() map[string]string {
}
}

// GetUploadContainerSpec returns the spec for the upload container
func (s *ImageResource) GetUploadContainerSpec(_ string) ([]corev1.Container, error) {
return nil, nil
}

// GetDownloadContainerSpec returns the spec for the download container
func (s *ImageResource) GetDownloadContainerSpec(sourcePath string) ([]corev1.Container, error) {
return nil, nil
}

// GetOutputImageDir return the path to get the index.json file
func (s *ImageResource) GetOutputImageDir() string {
return s.OutputImageDir
Expand All @@ -97,11 +87,3 @@ func (s ImageResource) String() string {
json, _ := json.Marshal(s)
return string(json)
}

func (s *ImageResource) GetUploadVolumeSpec(spec *TaskSpec) ([]corev1.Volume, error) {
return nil, nil
}

func (s *ImageResource) GetDownloadVolumeSpec(spec *TaskSpec) ([]corev1.Volume, error) {
return nil, nil
}
9 changes: 1 addition & 8 deletions pkg/apis/pipeline/v1alpha1/pull_request_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -132,11 +133,3 @@ func (s *PullRequestResource) getContainerSpec(mode string, sourcePath string) (
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
}
20 changes: 20 additions & 0 deletions pkg/apis/pipeline/v1alpha1/resource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) GetDownloadContainerSpec(sourcePath string) ([]corev1.Container, error) {
return nil, nil
}

func (p *PipelineResourceBase) GetUploadContainerSpec(sourcePath string) ([]corev1.Container, 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"`
Expand Down

0 comments on commit 1c3270a

Please sign in to comment.