Skip to content

Commit

Permalink
Move Resources implementation to their package 🎗
Browse files Browse the repository at this point in the history
GitResource, ImageResource, … are PipelineResource implementation and
are not really part of the API. At least, they shouldn't be part of
the `pkg/apis/pipeline` package.

This moves those to their own packages under
`pkg/apis/resource/v1alpha1`:
- `cloudevent` for `CloudEventResource`
- `cluster` for `ClusterResource`
- `git` for `GitResource`
- `image` for `ImageResource`
- `pullrequest` for `PullRequestResource`
- `storage` for all the storage resources `GCSResource`,
  `BuildGCSResource` and `ArtifactBucket`, `ArtifactPVC`

This renames the struct (to make lint and I happy 😌) and moves
`v1alpha1.ResourceFromType` to `resource.FromType`

Signed-off-by: Vincent Demeester <[email protected]>
  • Loading branch information
vdemeester authored and tekton-robot committed Feb 28, 2020
1 parent 7e31f1a commit 1e25e91
Show file tree
Hide file tree
Showing 51 changed files with 2,214 additions and 645 deletions.
3 changes: 2 additions & 1 deletion cmd/imagedigestexporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/google/go-containerregistry/pkg/v1/layout"
v1alpha1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1/image"
)

var (
Expand All @@ -46,7 +47,7 @@ func main() {
_ = logger.Sync()
}()

imageResources := []*v1alpha1.ImageResource{}
imageResources := []*image.Resource{}
if err := json.Unmarshal([]byte(*images), &imageResources); err != nil {
logger.Fatalf("Error reading images array: %v", err)
}
Expand Down
11 changes: 5 additions & 6 deletions cmd/kubeconfigwriter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ import (
"os"
"strings"

"github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1/cluster"
"github.com/tektoncd/pipeline/pkg/logging"
"go.uber.org/zap"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"

v1alpha1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"github.com/tektoncd/pipeline/pkg/logging"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
)

var (
Expand All @@ -44,15 +43,15 @@ func main() {
_ = logger.Sync()
}()

cr := v1alpha1.ClusterResource{}
cr := cluster.Resource{}
err := json.Unmarshal([]byte(*clusterConfig), &cr)
if err != nil {
logger.Fatalf("Error reading cluster config: %v", err)
}
createKubeconfigFile(&cr, logger)
}

func createKubeconfigFile(resource *v1alpha1.ClusterResource, logger *zap.SugaredLogger) {
func createKubeconfigFile(resource *cluster.Resource, logger *zap.SugaredLogger) {
cluster := &clientcmdapi.Cluster{
Server: resource.URL,
InsecureSkipTLSVerify: resource.Insecure,
Expand Down
7 changes: 6 additions & 1 deletion hack/update-codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ ${GOPATH}/bin/deepcopy-gen \
${GOPATH}/bin/deepcopy-gen \
-O zz_generated.deepcopy \
--go-header-file ${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt \
-i github.com/tektoncd/pipeline/pkg/apis/pipeline/pod
-i github.com/tektoncd/pipeline/pkg/apis/pipeline/pod

${GOPATH}/bin/deepcopy-gen \
-O zz_generated.deepcopy \
--go-header-file ${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.go.txt \
-i github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1/storage

# Knative Injection
# This generates the knative injection packages for the resource package (v1alpha1).
Expand Down
22 changes: 0 additions & 22 deletions pkg/apis/pipeline/v1alpha1/resource_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"

"github.com/google/go-cmp/cmp"
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha2"
)

Expand Down Expand Up @@ -108,24 +107,3 @@ type PipelineResourceResult = v1alpha2.PipelineResourceResult

// ResultType used to find out whether a PipelineResourceResult is from a task result or not
type ResultType = v1alpha2.ResultType

// ResourceFromType returns an instance of the correct PipelineResource object type which can be
// used to add input and output containers as well as volumes to a TaskRun's pod in order to realize
// a PipelineResource in a pod.
func ResourceFromType(r *PipelineResource, images pipeline.Images) (PipelineResourceInterface, error) {
switch r.Spec.Type {
case PipelineResourceTypeGit:
return NewGitResource(images.GitImage, r)
case PipelineResourceTypeImage:
return NewImageResource(r)
case PipelineResourceTypeCluster:
return NewClusterResource(images.KubeconfigWriterImage, r)
case PipelineResourceTypeStorage:
return NewStorageResource(images, r)
case PipelineResourceTypePullRequest:
return NewPullRequestResource(images.PRImage, r)
case PipelineResourceTypeCloudEvent:
return NewCloudEventResource(r)
}
return nil, fmt.Errorf("%s is an invalid or unimplemented PipelineResource", r.Spec.Type)
}
68 changes: 1 addition & 67 deletions pkg/apis/pipeline/v1alpha1/storage_resource.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 The Tekton Authors
Copyright 2019-2020 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -17,16 +17,9 @@ limitations under the License.
package v1alpha1

import (
"fmt"
"strings"

"github.com/tektoncd/pipeline/pkg/apis/pipeline"
resource "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1"
corev1 "k8s.io/api/core/v1"
)

type PipelineResourceStorageType string

const (
// PipelineResourceTypeGCS is the subtype for the GCSResources, which is backed by a GCS blob/directory.
PipelineResourceTypeGCS PipelineResourceType = resource.PipelineResourceTypeGCS
Expand All @@ -35,62 +28,3 @@ const (
// with additional functionality that was added to be compatible with knative build.
PipelineResourceTypeBuildGCS PipelineResourceType = resource.PipelineResourceTypeBuildGCS
)

// PipelineStorageResourceInterface is the interface for subtypes of the storage type.
// It adds a function to the PipelineResourceInterface for retrieving secrets that are usually
// needed for storage PipelineResources.
type PipelineStorageResourceInterface interface {
PipelineResourceInterface
GetSecretParams() []SecretParam
}

// NewStorageResource returns an instance of the requested storage subtype, which can be used
// to add input and output steps and volumes to an executing pod.
func NewStorageResource(images pipeline.Images, r *PipelineResource) (PipelineStorageResourceInterface, error) {
if r.Spec.Type != PipelineResourceTypeStorage {
return nil, fmt.Errorf("StoreResource: Cannot create a storage resource from a %s Pipeline Resource", r.Spec.Type)
}

for _, param := range r.Spec.Params {
if strings.EqualFold(param.Name, "type") {
switch {
case strings.EqualFold(param.Value, string(PipelineResourceTypeGCS)):
return NewGCSResource(images, r)
case strings.EqualFold(param.Value, string(PipelineResourceTypeBuildGCS)):
return NewBuildGCSResource(images, r)
default:
return nil, fmt.Errorf("%s is an invalid or unimplemented PipelineStorageResource", param.Value)
}
}
}
return nil, fmt.Errorf("StoreResource: Cannot create a storage resource without type %s in spec", r.Name)
}

func getStorageVolumeSpec(s PipelineStorageResourceInterface, spec TaskSpec) []corev1.Volume {
var storageVol []corev1.Volume
mountedSecrets := map[string]string{}

for _, volume := range spec.Volumes {
mountedSecrets[volume.Name] = ""
}

// Map holds list of secrets that are mounted as volumes
for _, secretParam := range s.GetSecretParams() {
volName := fmt.Sprintf("volume-%s-%s", s.GetName(), secretParam.SecretName)

gcsSecretVolume := corev1.Volume{
Name: volName,
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: secretParam.SecretName,
},
},
}

if _, ok := mountedSecrets[volName]; !ok {
storageVol = append(storageVol, gcsSecretVolume)
mountedSecrets[volName] = ""
}
}
return storageVol
}
Loading

0 comments on commit 1e25e91

Please sign in to comment.