Skip to content

Commit

Permalink
append random string to generated names
Browse files Browse the repository at this point in the history
- generated names for taskruns and build steps get a random suffix
of five alphanumerics. The string is guaranteed to not exceed the
length of a standard Kubernetes name (63 characters)
- updated tests using fixed seed for the name generation
- remove rand string func from test folder
- use new rand string already used for generated names
  • Loading branch information
nader-ziada committed Feb 12, 2019
1 parent b52f7a9 commit 8dae438
Show file tree
Hide file tree
Showing 30 changed files with 1,352 additions and 480 deletions.
50 changes: 26 additions & 24 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions pkg/apis/pipeline/v1alpha1/artifact_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"strings"

"github.com/knative/build-pipeline/pkg/names"
corev1 "k8s.io/api/core/v1"
)

Expand Down Expand Up @@ -81,13 +82,13 @@ func (b *ArtifactBucket) GetCopyFromContainerSpec(name, sourcePath, destinationP
envVars, secretVolumeMount := getSecretEnvVarsAndVolumeMounts("bucket", secretVolumeMountPath, b.Secrets)

return []corev1.Container{{
Name: fmt.Sprintf("artifact-dest-mkdir-%s", name),
Name: names.SimpleNameGenerator.GenerateName(fmt.Sprintf("artifact-dest-mkdir-%s", name)),
Image: *bashNoopImage,
Args: []string{
"-args", strings.Join([]string{"mkdir", "-p", destinationPath}, " "),
},
}, {
Name: fmt.Sprintf("artifact-copy-from-%s", name),
Name: names.SimpleNameGenerator.GenerateName(fmt.Sprintf("artifact-copy-from-%s", name)),
Image: *gsutilImage,
Args: args,
Env: envVars,
Expand All @@ -102,7 +103,7 @@ func (b *ArtifactBucket) GetCopyToContainerSpec(name, sourcePath, destinationPat
envVars, secretVolumeMount := getSecretEnvVarsAndVolumeMounts("bucket", secretVolumeMountPath, b.Secrets)

return []corev1.Container{{
Name: fmt.Sprintf("artifact-copy-to-%s", name),
Name: names.SimpleNameGenerator.GenerateName(fmt.Sprintf("artifact-copy-to-%s", name)),
Image: *gsutilImage,
Args: args,
Env: envVars,
Expand All @@ -116,7 +117,7 @@ func (b *ArtifactBucket) GetSecretsVolumes() []corev1.Volume {
volumes := []corev1.Volume{}
for _, sec := range b.Secrets {
volumes = append(volumes, corev1.Volume{
Name: fmt.Sprintf("bucket-secret-volume-%s", sec.SecretName),
Name: names.SimpleNameGenerator.GenerateName(fmt.Sprintf("bucket-secret-%s", sec.SecretName)),
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: sec.SecretName,
Expand Down
12 changes: 8 additions & 4 deletions pkg/apis/pipeline/v1alpha1/artifact_bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/knative/build-pipeline/test/names"
corev1 "k8s.io/api/core/v1"
)

func TestBucketGetCopyFromContainerSpec(t *testing.T) {
names.TestingSeed()
bucket := ArtifactBucket{
Location: "gs://fake-bucket",
Secrets: []SecretParam{{
Expand All @@ -33,11 +35,11 @@ func TestBucketGetCopyFromContainerSpec(t *testing.T) {
}},
}
want := []corev1.Container{{
Name: "artifact-dest-mkdir-workspace",
Name: "artifact-dest-mkdir-workspace-9l9zj",
Image: "override-with-bash-noop:latest",
Args: []string{"-args", "mkdir -p /workspace/destination"},
}, {
Name: "artifact-copy-from-workspace",
Name: "artifact-copy-from-workspace-mz4c7",
Image: "override-with-gsutil-image:latest",
Args: []string{"-args", "cp -r gs://fake-bucket/src-path/** /workspace/destination"},
Env: []corev1.EnvVar{{Name: "GOOGLE_APPLICATION_CREDENTIALS", Value: "/var/bucketsecret/secret1/serviceaccount"}},
Expand All @@ -51,6 +53,7 @@ func TestBucketGetCopyFromContainerSpec(t *testing.T) {
}

func TestBucketGetCopyToContainerSpec(t *testing.T) {
names.TestingSeed()
bucket := ArtifactBucket{
Location: "gs://fake-bucket",
Secrets: []SecretParam{{
Expand All @@ -60,7 +63,7 @@ func TestBucketGetCopyToContainerSpec(t *testing.T) {
}},
}
want := []corev1.Container{{
Name: "artifact-copy-to-workspace",
Name: "artifact-copy-to-workspace-9l9zj",
Image: "override-with-gsutil-image:latest",
Args: []string{"-args", "cp -r src-path gs://fake-bucket/workspace/destination"},
Env: []corev1.EnvVar{{Name: "GOOGLE_APPLICATION_CREDENTIALS", Value: "/var/bucketsecret/secret1/serviceaccount"}},
Expand All @@ -74,6 +77,7 @@ func TestBucketGetCopyToContainerSpec(t *testing.T) {
}

func TestGetSecretsVolumes(t *testing.T) {
names.TestingSeed()
bucket := ArtifactBucket{
Location: "gs://fake-bucket",
Secrets: []SecretParam{{
Expand All @@ -83,7 +87,7 @@ func TestGetSecretsVolumes(t *testing.T) {
}},
}
want := []corev1.Volume{{
Name: "bucket-secret-volume-secret1",
Name: "bucket-secret-secret1-9l9zj",
VolumeSource: corev1.VolumeSource{
Secret: &corev1.SecretVolumeSource{
SecretName: "secret1",
Expand Down
9 changes: 5 additions & 4 deletions pkg/apis/pipeline/v1alpha1/artifact_pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"strings"

"github.com/knative/build-pipeline/pkg/names"
corev1 "k8s.io/api/core/v1"
)

Expand Down Expand Up @@ -48,7 +49,7 @@ func (p *ArtifactPVC) StorageBasePath(pr *PipelineRun) string {
// GetCopyFromContainerSpec returns a container used to download artifacts from temporary storage
func (p *ArtifactPVC) GetCopyFromContainerSpec(name, sourcePath, destinationPath string) []corev1.Container {
return []corev1.Container{{
Name: fmt.Sprintf("source-copy-%s", name),
Name: names.SimpleNameGenerator.GenerateName(fmt.Sprintf("source-copy-%s", name)),
Image: *bashNoopImage,
Args: []string{"-args", strings.Join([]string{"cp", "-r", fmt.Sprintf("%s/.", sourcePath), destinationPath}, " ")},
}}
Expand All @@ -57,14 +58,14 @@ func (p *ArtifactPVC) GetCopyFromContainerSpec(name, sourcePath, destinationPath
// GetCopyToContainerSpec returns a container used to upload artifacts for temporary storage
func (p *ArtifactPVC) GetCopyToContainerSpec(name, sourcePath, destinationPath string) []corev1.Container {
return []corev1.Container{{
Name: fmt.Sprintf("source-mkdir-%s", name),
Name: names.SimpleNameGenerator.GenerateName(fmt.Sprintf("source-mkdir-%s", name)),
Image: *bashNoopImage,
Args: []string{
"-args", strings.Join([]string{"mkdir", "-p", destinationPath}, " "),
},
VolumeMounts: []corev1.VolumeMount{getPvcMount(p.Name)},
}, {
Name: fmt.Sprintf("source-copy-%s", name),
Name: names.SimpleNameGenerator.GenerateName(fmt.Sprintf("source-copy-%s", name)),
Image: *bashNoopImage,
Args: []string{
"-args", strings.Join([]string{"cp", "-r", fmt.Sprintf("%s/.", sourcePath), destinationPath}, " "),
Expand All @@ -83,7 +84,7 @@ func getPvcMount(name string) corev1.VolumeMount {
// CreateDirContainer returns a container step to create a dir
func CreateDirContainer(name, destinationPath string) corev1.Container {
return corev1.Container{
Name: fmt.Sprintf("create-dir-%s", name),
Name: names.SimpleNameGenerator.GenerateName(fmt.Sprintf("create-dir-%s", name)),
Image: *bashNoopImage,
Args: []string{"-args", strings.Join([]string{"mkdir", "-p", destinationPath}, " ")},
}
Expand Down
Loading

0 comments on commit 8dae438

Please sign in to comment.