-
Notifications
You must be signed in to change notification settings - Fork 0
/
artifact_bucket.go
103 lines (87 loc) · 3.79 KB
/
artifact_bucket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
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.
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 storage
import (
"fmt"
"github.com/ouyang-xlauncher/pipeline/pkg/apis/pipeline"
"github.com/ouyang-xlauncher/pipeline/pkg/apis/pipeline/v1beta1"
resource "github.com/ouyang-xlauncher/pipeline/pkg/apis/resource/v1alpha1"
"github.com/ouyang-xlauncher/pipeline/pkg/names"
corev1 "k8s.io/api/core/v1"
)
// For some reason gosec thinks this string has enough entropy to be a potential secret.
// The nosec comment disables it for this line.
/* #nosec */
const secretVolumeMountPath = "/var/bucketsecret"
// ArtifactBucket contains the Storage bucket configuration defined in the
// Bucket config map.
// +k8s:deepcopy-gen=true
type ArtifactBucket struct {
Name string
Location string
Secrets []resource.SecretParam
ShellImage string
GsutilImage string
}
// GetType returns the type of the artifact storage
func (b *ArtifactBucket) GetType() string {
return pipeline.ArtifactStorageBucketType
}
// StorageBasePath returns the path to be used to store artifacts in a pipelinerun temporary storage
func (b *ArtifactBucket) StorageBasePath(pr *v1beta1.PipelineRun) string {
return fmt.Sprintf("%s-%s-bucket", pr.Name, pr.Namespace)
}
// GetCopyFromStorageToSteps returns a container used to download artifacts from temporary storage
func (b *ArtifactBucket) GetCopyFromStorageToSteps(name, sourcePath, destinationPath string) []v1beta1.Step {
envVars, secretVolumeMount := getSecretEnvVarsAndVolumeMounts("bucket", secretVolumeMountPath, b.Secrets)
return []v1beta1.Step{{Container: corev1.Container{
Name: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix(fmt.Sprintf("artifact-dest-mkdir-%s", name)),
Image: b.ShellImage,
Command: []string{"mkdir", "-p", destinationPath},
}}, {Container: corev1.Container{
Name: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix(fmt.Sprintf("artifact-copy-from-%s", name)),
Image: b.GsutilImage,
Command: []string{"gsutil"},
Args: []string{"cp", "-P", "-r", fmt.Sprintf("%s/%s/*", b.Location, sourcePath), destinationPath},
Env: envVars,
VolumeMounts: secretVolumeMount,
}}}
}
// GetCopyToStorageFromSteps returns a container used to upload artifacts for temporary storage
func (b *ArtifactBucket) GetCopyToStorageFromSteps(name, sourcePath, destinationPath string) []v1beta1.Step {
envVars, secretVolumeMount := getSecretEnvVarsAndVolumeMounts("bucket", secretVolumeMountPath, b.Secrets)
return []v1beta1.Step{{Container: corev1.Container{
Name: names.SimpleNameGenerator.RestrictLengthWithRandomSuffix(fmt.Sprintf("artifact-copy-to-%s", name)),
Image: b.GsutilImage,
Command: []string{"gsutil"},
Args: []string{"cp", "-P", "-r", sourcePath, fmt.Sprintf("%s/%s", b.Location, destinationPath)},
Env: envVars,
VolumeMounts: secretVolumeMount,
}}}
}
// GetSecretsVolumes returns the list of volumes for secrets to be mounted
// on pod
func (b *ArtifactBucket) GetSecretsVolumes() []corev1.Volume {
volumes := []corev1.Volume{}
for _, sec := range b.Secrets {
volumes = append(volumes, corev1.Volume{
Name: fmt.Sprintf("volume-bucket-%s", sec.SecretName),
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: sec.SecretName,
},
},
})
}
return volumes
}