Skip to content

Commit

Permalink
Cleanup some code in artifact storage.
Browse files Browse the repository at this point in the history
- We had some constants in two locations - this removes/dedupes these.
- We were doing some unusual string pointer manipulation- this cleans it up
a bit and adds some comments explaining why.
- Minor cleanups around set manipulation.
  • Loading branch information
dlorenc committed Jul 19, 2020
1 parent 935aecf commit a675257
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
3 changes: 2 additions & 1 deletion pkg/apis/resource/v1alpha1/storage/artifact_pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package storage
import (
"fmt"

"github.com/tektoncd/pipeline/pkg/apis/pipeline"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/pkg/names"
corev1 "k8s.io/api/core/v1"
Expand All @@ -39,7 +40,7 @@ type ArtifactPVC struct {

// GetType returns the type of the artifact storage.
func (p *ArtifactPVC) GetType() string {
return ArtifactStoragePVCType
return pipeline.ArtifactStoragePVCType
}

// StorageBasePath returns the path to be used to store artifacts in a pipelinerun temporary storage.
Expand Down
33 changes: 13 additions & 20 deletions pkg/apis/resource/v1alpha1/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,8 @@ import (
corev1 "k8s.io/api/core/v1"
)

const (
// ArtifactStorageBucketType holds the name of the PipelineResource type for a bucket
ArtifactStorageBucketType = "bucket"

// ArtifactStoragePVCType holds the name of the PipelineResource type for a pvc
ArtifactStoragePVCType = "pvc"
)

// It adds a function to the PipelineResourceInterface for retrieving secrets that are usually
// needed for storage PipelineResources.
// PipelineStorageResourceInterface adds a function to the PipelineResourceInterface for retrieving
// secrets that are usually needed for storage PipelineResources.
type PipelineStorageResourceInterface interface {
v1beta1.PipelineResourceInterface
GetSecretParams() []resource.SecretParam
Expand All @@ -52,9 +44,9 @@ func NewResource(name string, images pipeline.Images, r *resource.PipelineResour
for _, param := range r.Spec.Params {
if strings.EqualFold(param.Name, "type") {
switch {
case strings.EqualFold(param.Value, string(resource.PipelineResourceTypeGCS)):
case strings.EqualFold(param.Value, resource.PipelineResourceTypeGCS):
return NewGCSResource(name, images, r)
case strings.EqualFold(param.Value, string(resource.PipelineResourceTypeBuildGCS)):
case strings.EqualFold(param.Value, resource.PipelineResourceTypeBuildGCS):
return NewBuildGCSResource(name, images, r)
default:
return nil, fmt.Errorf("%s is an invalid or unimplemented PipelineStorageResource", param.Value)
Expand All @@ -66,15 +58,19 @@ func NewResource(name string, images pipeline.Images, r *resource.PipelineResour

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

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

// 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)
if _, ok := mountedSecrets[volName]; ok {
// There is already a volume mounted with this name
continue
}

gcsSecretVolume := corev1.Volume{
Name: volName,
Expand All @@ -84,16 +80,13 @@ func getStorageVolumeSpec(s PipelineStorageResourceInterface, spec v1beta1.TaskS
},
},
}

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

// of the step will include name.
// CreateDirStep returns a Step that creates a given directory with a given name.
func CreateDirStep(shellImage string, name, destinationPath string) v1beta1.Step {
return v1beta1.Step{Container: corev1.Container{
Name: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix(fmt.Sprintf("create-dir-%s", strings.ToLower(name))),
Expand Down
10 changes: 7 additions & 3 deletions pkg/artifacts/artifacts_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,14 @@ func createPVC(pr *v1beta1.PipelineRun, c kubernetes.Interface) (*corev1.Persist
if err != nil {
return nil, fmt.Errorf("failed to create Persistent Volume spec for %q due to error: %w", pr.Name, err)
}

// The storage class name on pod spec has three states. Tekton doesn't support the empty-string case.
// - nil if we don't care
// - "" if we explicity want to have no class names
// - "$name" if we want a specific name
// https://kubernetes.io/docs/concepts/storage/persistent-volumes/#class-1
var pvcStorageClassName *string
if pvcStorageClassNameStr == "" {
pvcStorageClassName = nil
} else {
if pvcStorageClassNameStr != "" {
pvcStorageClassName = &pvcStorageClassNameStr
}

Expand Down

0 comments on commit a675257

Please sign in to comment.