diff --git a/Gopkg.lock b/Gopkg.lock index 6cd7be531bd..32ef85fff38 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -41,7 +41,7 @@ version = "v1.6.7" [[projects]] - digest = "1:a6afc27b2a73a5506832f3c5a1c19a30772cb69e7bd1ced4639eb36a55db224f" + digest = "1:26a5b0d17fc9534a39ab910cbe61ffa5059a9e608e1a0e3c2663d526b05f1081" name = "github.com/gogo/protobuf" packages = [ "proto", @@ -65,7 +65,7 @@ revision = "02826c3e79038b59d737d3b1c0a1d937f71a4433" [[projects]] - digest = "1:0d390d7037c2aecc37e78c2cfbe43d020d6f1fa83fd22266b7ec621189447d57" + digest = "1:62c39e48b3af4ccc5f20819b3fc4b7aee88cf4b7e296c4be0168113e755adfaa" name = "github.com/golang/protobuf" packages = [ "proto", @@ -86,7 +86,7 @@ revision = "e89373fe6b4a7413d7acd6da1725b83ef713e6e4" [[projects]] - digest = "1:d2754cafcab0d22c13541618a8029a70a8959eb3525ff201fe971637e2274cd0" + digest = "1:b787093a6b46cbdb290b2b304d12352eeff1bf7870852c3a0636acf87128c32b" name = "github.com/google/go-cmp" packages = [ "cmp", @@ -101,7 +101,7 @@ [[projects]] branch = "master" - digest = "1:0a518075e503abfe4165f33954bc0dbaf7a6ec47148384084827e4759f208537" + digest = "1:608d3507f689fcab468173d50e46974a862093f73fc92a584b0153c3086dabf5" name = "github.com/google/go-containerregistry" packages = [ "pkg/authn", @@ -125,7 +125,7 @@ [[projects]] branch = "master" - digest = "1:0d5e3798bfa2642ac268341c96710b8def1f3cbc3bc803c421d90704d72107d8" + digest = "1:70dbc44a90fa82c71d6064b88929b2ad9374f4c438cfed3b4cc7dacd77e0eb73" name = "github.com/google/licenseclassifier" packages = [ ".", @@ -139,7 +139,7 @@ revision = "e979a0b10eebe748549c702a25e997c556349da6" [[projects]] - digest = "1:27b4ab41ffdc76ad6db56db327a4db234a59588ef059fc3fd678ba0bc6b9094f" + digest = "1:fc56ff1b4b04575628f199eb0bc60fa6ed8ff63196e290151a5031c6af5eaef4" name = "github.com/googleapis/gnostic" packages = [ "OpenAPIv2", @@ -151,7 +151,7 @@ [[projects]] branch = "master" - digest = "1:7fdf3223c7372d1ced0b98bf53457c5e89d89aecbad9a77ba9fcc6e01f9e5621" + digest = "1:a1db0214936912602a7a8cedc09a2e3211f5c097dc89189fb4b3bc86346c9e89" name = "github.com/gregjones/httpcache" packages = [ ".", @@ -194,7 +194,7 @@ revision = "f2b4162afba35581b6d4a50d3b8f34e33c144682" [[projects]] - digest = "1:109397c6089086e57a6c5c25b641f8d667c43035b3d5d65bcaee83d7700a38b5" + digest = "1:51151e5e2724953551360fb77a67787b5cf9db019a4aefcfe44e6d50c89d5577" name = "github.com/knative/build" packages = [ "pkg/apis/build", @@ -206,7 +206,7 @@ [[projects]] branch = "master" - digest = "1:826576a2dbea52eb350ed5c0fd3e53f1f24cd6bdcfaf89ff89239b8cd426cd0d" + digest = "1:c4996925da5207a2997f504fb9979ea8560c62f5d3307c72a7f4ab8480daf9be" name = "github.com/knative/caching" packages = [ "pkg/apis/caching", @@ -219,7 +219,7 @@ revision = "f0d7bb60956f88d4743f5fea66b5607b96d262c9" [[projects]] - digest = "1:f8800585f2a46c07a4fa252153ce39660786527436cddcc83190d60339fb85ea" + digest = "1:cf625982fcbe364cae5dc44cd59deabafb9125ee350ea487d8f6676c4e6c1843" name = "github.com/knative/pkg" packages = [ "apis", @@ -255,7 +255,7 @@ [[projects]] branch = "master" - digest = "1:adce1d5f23880effb73db34201248efb0e997e2e0fbc24e3e32b8afc2612c5df" + digest = "1:dec39d54426473b232f8d764cd4ff7b71c6ca5c012a805dc604b784fdb800ebf" name = "github.com/knative/test-infra" packages = [ "scripts", @@ -329,7 +329,7 @@ version = "v1.0.3" [[projects]] - digest = "1:fe16cada1d1885b0092d869e39bdd96195d2c65c6f79830a291978f027db1fc6" + digest = "1:27f79b0258eaf8c1ca1f5c806e649e1c4b988b23d7befe667e9457abaaacc8d0" name = "go.opencensus.io" packages = [ ".", @@ -368,7 +368,7 @@ version = "v1.1.0" [[projects]] - digest = "1:6463628b4e2c5680bf069a15bf109ee6dd8ffc54e20447a23639167128c2c4ed" + digest = "1:ac060588b5d3b28b297728ef2db13dc51a77c739e112354ab46694a8eca6a5dc" name = "go.uber.org/zap" packages = [ ".", @@ -392,7 +392,7 @@ revision = "81e90905daefcd6fd217b62423c0908922eadb30" [[projects]] - digest = "1:546efc585d0b3b4ef026faa94ae2d3211c1744d517841a27c83905e18fd0828d" + digest = "1:02ccbca876321f757c25b1cc794cb1c71362aa86c74fa31cf21c4688b114c088" name = "golang.org/x/net" packages = [ "context", @@ -407,7 +407,7 @@ [[projects]] branch = "master" - digest = "1:bc2b221d465bb28ce46e8d472ecdc424b9a9b541bd61d8c311c5f29c8dd75b1b" + digest = "1:acf4103b26c5cc8b83208688a0a5b36adc47072f1bd91e1d886503c9f6fe3f2b" name = "golang.org/x/oauth2" packages = [ ".", @@ -420,7 +420,7 @@ revision = "d2e6202438beef2727060aa7cabdd924d92ebfd9" [[projects]] - digest = "1:a969921c213e6d90554219689f484043e1e897d1936e524cfbb4fd6cb8ae2736" + digest = "1:d80b37b725351dfdd74022614569420d65e377cf9bf1d58dbf91c3aacc1e27c6" name = "golang.org/x/sys" packages = [ "unix", @@ -430,7 +430,7 @@ revision = "95c6576299259db960f6c5b9b69ea52422860fce" [[projects]] - digest = "1:97337ef8cb438f9e3a99ea91a300e916ed9a96fbf3ad50f9a020d30ea9f8692f" + digest = "1:11e5ba605f499e37162cc8ca25fb16d6ff66fbfdbdc43c92d6f1351cde975413" name = "golang.org/x/text" packages = [ "internal/gen", @@ -456,7 +456,7 @@ [[projects]] branch = "master" - digest = "1:2c57a52b1792fad8b668be56c76a9a4959f7b665b8474cfa188c652ef1a2b82d" + digest = "1:c765c0e7646900b1d5f1feecd2d255fcadd04ab9757ef792f5b5a4ffcb633407" name = "golang.org/x/tools" packages = [ "go/ast/astutil", @@ -466,7 +466,7 @@ revision = "66487607e2081c7c2af2281c62c14ee000d5024b" [[projects]] - digest = "1:b63b351b57e64ae8aec1af030dc5e74a2676fcda62102f006ae4411fac1b04c8" + digest = "1:441d97368658ab6125cd88aa973e2bfa0585e4119d437b09f8fd40e5c5c76aa9" name = "google.golang.org/appengine" packages = [ ".", @@ -501,7 +501,7 @@ version = "v2.2.1" [[projects]] - digest = "1:6dd53537a34d9b612e66acd39859953cf7ad70e5b5953937bed5ea998355e7b8" + digest = "1:bc54ff998ad910da88b2e6fe6cddb7800ef9125212491fad41cb94f48e8c8ee4" name = "k8s.io/api" packages = [ "admission/v1beta1", @@ -540,7 +540,7 @@ version = "kubernetes-1.11.3" [[projects]] - digest = "1:e430de0896db98fb93a75f45ace427a4b6d09028777498b7c6a25d33866efdd6" + digest = "1:decfe05ff4223064a9fbba573a95cecf2e5999a5ac75c0f1d7bf69bc1882c623" name = "k8s.io/apimachinery" packages = [ "pkg/api/equality", @@ -574,6 +574,7 @@ "pkg/util/json", "pkg/util/mergepatch", "pkg/util/net", + "pkg/util/rand", "pkg/util/runtime", "pkg/util/sets", "pkg/util/sets/types", @@ -592,7 +593,7 @@ version = "kubernetes-1.11.3" [[projects]] - digest = "1:cf334d5e85a35fe6c9e87dcbf22a8d8a7f42bd16bcb93b90de1128ba854062e2" + digest = "1:d4edaebec066b5aefddcee25a50cef312d0da2e6b885e12053ed68ba26295a80" name = "k8s.io/client-go" packages = [ "discovery", @@ -761,7 +762,7 @@ version = "kubernetes-1.11.3" [[projects]] - digest = "1:3b9c82e73290987c719062ba514f47cc95a7b1e10d9868b1e083e48044c0820b" + digest = "1:e97a3de216110403d31a244dae829652a4cd434430dc823c0f47e258707d909b" name = "k8s.io/code-generator" packages = [ "cmd/client-gen", @@ -790,7 +791,7 @@ [[projects]] branch = "master" - digest = "1:e2844a79caf5a09550077b6f468f4a5e9a5d5a241e34555f2da692064e742184" + digest = "1:cc02b96a3307a2b9534890fb6bc1c0427319aa8f9ffd6797ba97587fc8874f80" name = "k8s.io/gengo" packages = [ "args", @@ -860,6 +861,7 @@ "k8s.io/apimachinery/pkg/runtime/schema", "k8s.io/apimachinery/pkg/runtime/serializer", "k8s.io/apimachinery/pkg/types", + "k8s.io/apimachinery/pkg/util/rand", "k8s.io/apimachinery/pkg/util/sets/types", "k8s.io/apimachinery/pkg/util/validation", "k8s.io/apimachinery/pkg/util/validation/field", diff --git a/pkg/apis/pipeline/v1alpha1/artifact_bucket.go b/pkg/apis/pipeline/v1alpha1/artifact_bucket.go index 45d896d084c..a007f523b32 100644 --- a/pkg/apis/pipeline/v1alpha1/artifact_bucket.go +++ b/pkg/apis/pipeline/v1alpha1/artifact_bucket.go @@ -20,6 +20,7 @@ import ( "fmt" "strings" + "github.com/knative/build-pipeline/pkg/names" corev1 "k8s.io/api/core/v1" ) @@ -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, @@ -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, @@ -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, diff --git a/pkg/apis/pipeline/v1alpha1/artifact_bucket_test.go b/pkg/apis/pipeline/v1alpha1/artifact_bucket_test.go index aa4bebf3dde..1147badad06 100644 --- a/pkg/apis/pipeline/v1alpha1/artifact_bucket_test.go +++ b/pkg/apis/pipeline/v1alpha1/artifact_bucket_test.go @@ -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{{ @@ -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"}}, @@ -51,6 +53,7 @@ func TestBucketGetCopyFromContainerSpec(t *testing.T) { } func TestBucketGetCopyToContainerSpec(t *testing.T) { + names.TestingSeed() bucket := ArtifactBucket{ Location: "gs://fake-bucket", Secrets: []SecretParam{{ @@ -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"}}, @@ -74,6 +77,7 @@ func TestBucketGetCopyToContainerSpec(t *testing.T) { } func TestGetSecretsVolumes(t *testing.T) { + names.TestingSeed() bucket := ArtifactBucket{ Location: "gs://fake-bucket", Secrets: []SecretParam{{ @@ -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", diff --git a/pkg/apis/pipeline/v1alpha1/artifact_pvc.go b/pkg/apis/pipeline/v1alpha1/artifact_pvc.go index dcd326b8f21..3105f8d01dc 100644 --- a/pkg/apis/pipeline/v1alpha1/artifact_pvc.go +++ b/pkg/apis/pipeline/v1alpha1/artifact_pvc.go @@ -21,6 +21,7 @@ import ( "fmt" "strings" + "github.com/knative/build-pipeline/pkg/names" corev1 "k8s.io/api/core/v1" ) @@ -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}, " ")}, }} @@ -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}, " "), @@ -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}, " ")}, } diff --git a/pkg/apis/pipeline/v1alpha1/artifact_pvc_test.go b/pkg/apis/pipeline/v1alpha1/artifact_pvc_test.go index fbf1537f4b3..5d338bcfe69 100644 --- a/pkg/apis/pipeline/v1alpha1/artifact_pvc_test.go +++ b/pkg/apis/pipeline/v1alpha1/artifact_pvc_test.go @@ -20,17 +20,19 @@ import ( "testing" "github.com/google/go-cmp/cmp" + "github.com/knative/build-pipeline/test/names" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) func TestPVCGetCopyFromContainerSpec(t *testing.T) { + names.TestingSeed() pvc := ArtifactPVC{ Name: "pipelinerun-pvc", } want := []corev1.Container{{ - Name: "source-copy-workspace", + Name: "source-copy-workspace-9l9zj", Image: "override-with-bash-noop:latest", Args: []string{"-args", "cp -r src-path/. /workspace/destination"}, }} @@ -42,17 +44,18 @@ func TestPVCGetCopyFromContainerSpec(t *testing.T) { } func TestPVCGetCopyToContainerSpec(t *testing.T) { + names.TestingSeed() pvc := ArtifactPVC{ Name: "pipelinerun-pvc", } want := []corev1.Container{{ - Name: "source-mkdir-workspace", + Name: "source-mkdir-workspace-9l9zj", Image: "override-with-bash-noop:latest", Args: []string{"-args", "mkdir -p /workspace/destination"}, VolumeMounts: []corev1.VolumeMount{{MountPath: "/pvc", Name: "pipelinerun-pvc"}}, }, { - Name: "source-copy-workspace", + Name: "source-copy-workspace-mz4c7", Image: "override-with-bash-noop:latest", Args: []string{"-args", "cp -r src-path/. /workspace/destination"}, VolumeMounts: []corev1.VolumeMount{{MountPath: "/pvc", Name: "pipelinerun-pvc"}}, @@ -65,8 +68,10 @@ func TestPVCGetCopyToContainerSpec(t *testing.T) { } func TestPVCGetMakeDirContainerSpec(t *testing.T) { + names.TestingSeed() + want := corev1.Container{ - Name: "create-dir-workspace", + Name: "create-dir-workspace-9l9zj", Image: "override-with-bash-noop:latest", Args: []string{"-args", "mkdir -p /workspace/destination"}, } diff --git a/pkg/apis/pipeline/v1alpha1/build_gcs_resource.go b/pkg/apis/pipeline/v1alpha1/build_gcs_resource.go index ffa38501f67..6bf1c09ee79 100644 --- a/pkg/apis/pipeline/v1alpha1/build_gcs_resource.go +++ b/pkg/apis/pipeline/v1alpha1/build_gcs_resource.go @@ -21,6 +21,8 @@ import ( "fmt" "strings" + "github.com/knative/build-pipeline/pkg/names" + corev1 "k8s.io/api/core/v1" ) @@ -138,7 +140,7 @@ func (s *BuildGCSResource) GetDownloadContainerSpec() ([]corev1.Container, error return []corev1.Container{ CreateDirContainer(s.Name, s.DestinationDir), { - Name: fmt.Sprintf("storage-fetch-%s", s.Name), + Name: names.SimpleNameGenerator.GenerateName(fmt.Sprintf("storage-fetch-%s", s.Name)), Image: *buildGCSFetcherImage, Args: args, }}, nil @@ -156,7 +158,7 @@ func (s *BuildGCSResource) GetUploadContainerSpec() ([]corev1.Container, error) args := []string{"--location", s.Location, "--dir", s.DestinationDir} return []corev1.Container{{ - Name: fmt.Sprintf("storage-upload-%s", s.Name), + Name: names.SimpleNameGenerator.GenerateName(fmt.Sprintf("storage-upload-%s", s.Name)), Image: *buildGCSUploaderImage, Args: args, }}, nil diff --git a/pkg/apis/pipeline/v1alpha1/build_gcs_resource_test.go b/pkg/apis/pipeline/v1alpha1/build_gcs_resource_test.go index 67be28de722..76e1f8822d5 100644 --- a/pkg/apis/pipeline/v1alpha1/build_gcs_resource_test.go +++ b/pkg/apis/pipeline/v1alpha1/build_gcs_resource_test.go @@ -20,6 +20,7 @@ import ( "testing" "github.com/google/go-cmp/cmp" + "github.com/knative/build-pipeline/test/names" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -220,13 +221,16 @@ func Test_BuildGCSGetDownloadContainerSpec(t *testing.T) { DestinationDir: "/workspace", ArtifactType: "Archive", }, - wantContainers: []corev1.Container{ - CreateDirContainer("gcs-valid", "/workspace"), { - Name: "storage-fetch-gcs-valid", - Image: "gcr.io/cloud-builders/gcs-fetcher:latest", - Args: []string{"--type", "Archive", "--location", "gs://some-bucket", - "--dest_dir", "/workspace"}, - }}, + wantContainers: []corev1.Container{{ + Name: "create-dir-gcs-valid-9l9zj", + Image: "override-with-bash-noop:latest", + Args: []string{"-args", "mkdir -p /workspace"}, + }, { + Name: "storage-fetch-gcs-valid-mz4c7", + Image: "gcr.io/cloud-builders/gcs-fetcher:latest", + Args: []string{"--type", "Archive", "--location", "gs://some-bucket", + "--dest_dir", "/workspace"}, + }}, }, { name: "invalid no destination directory set", resource: &BuildGCSResource{ @@ -238,6 +242,7 @@ func Test_BuildGCSGetDownloadContainerSpec(t *testing.T) { }} for _, tc := range testcases { t.Run(tc.name, func(t *testing.T) { + names.TestingSeed() gotContainers, err := tc.resource.GetDownloadContainerSpec() if tc.wantErr && err == nil { t.Fatalf("Expected error to be %t but got %v:", tc.wantErr, err) @@ -264,7 +269,7 @@ func Test_BuildGCSGetUploadContainerSpec(t *testing.T) { ArtifactType: "Manifest", }, wantContainers: []corev1.Container{{ - Name: "storage-upload-gcs-valid", + Name: "storage-upload-gcs-valid-9l9zj", Image: "gcr.io/cloud-builders/gcs-uploader:latest", Args: []string{"--location", "gs://some-bucket/manifest.json", "--dir", "/workspace"}, }}, diff --git a/pkg/apis/pipeline/v1alpha1/cluster_resource.go b/pkg/apis/pipeline/v1alpha1/cluster_resource.go index cdfbcacbcbf..c18a3ab756b 100644 --- a/pkg/apis/pipeline/v1alpha1/cluster_resource.go +++ b/pkg/apis/pipeline/v1alpha1/cluster_resource.go @@ -24,6 +24,7 @@ import ( "strconv" "strings" + "github.com/knative/build-pipeline/pkg/names" corev1 "k8s.io/api/core/v1" ) @@ -167,7 +168,7 @@ func (s *ClusterResource) GetDownloadContainerSpec() ([]corev1.Container, error) } clusterContainer := corev1.Container{ - Name: "kubeconfig", + Name: names.SimpleNameGenerator.GenerateName("kubeconfig"), Image: *kubeconfigWriterImage, Args: []string{ "-clusterConfig", s.String(), diff --git a/pkg/apis/pipeline/v1alpha1/cluster_resource_test.go b/pkg/apis/pipeline/v1alpha1/cluster_resource_test.go index a5464aca2c2..ce68b480a55 100644 --- a/pkg/apis/pipeline/v1alpha1/cluster_resource_test.go +++ b/pkg/apis/pipeline/v1alpha1/cluster_resource_test.go @@ -17,6 +17,7 @@ import ( "testing" "github.com/google/go-cmp/cmp" + "github.com/knative/build-pipeline/test/names" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -178,6 +179,7 @@ func TestNewClusterResource(t *testing.T) { } func Test_ClusterResource_GetDownloadContainerSpec(t *testing.T) { + names.TestingSeed() testcases := []struct { name string clusterResource *ClusterResource @@ -196,7 +198,7 @@ func Test_ClusterResource_GetDownloadContainerSpec(t *testing.T) { }}, }, wantContainers: []corev1.Container{{ - Name: "kubeconfig", + Name: "kubeconfig-9l9zj", Image: "override-with-kubeconfig-writer:latest", Args: []string{"-clusterConfig", `{"name":"test-cluster-resource","type":"cluster","url":"http://10.10.10.10","revision":"","username":"","password":"","token":"","Insecure":false,"cadata":null,"secrets":[{"fieldName":"cadata","secretKey":"cadatakey","secretName":"secret1"}]}`}, Env: []corev1.EnvVar{{ diff --git a/pkg/apis/pipeline/v1alpha1/gcs_resource.go b/pkg/apis/pipeline/v1alpha1/gcs_resource.go index 3abafaed1dd..cab9494cb35 100644 --- a/pkg/apis/pipeline/v1alpha1/gcs_resource.go +++ b/pkg/apis/pipeline/v1alpha1/gcs_resource.go @@ -22,6 +22,7 @@ import ( "path/filepath" "strings" + "github.com/knative/build-pipeline/pkg/names" corev1 "k8s.io/api/core/v1" ) @@ -118,7 +119,7 @@ func (s *GCSResource) GetUploadContainerSpec() ([]corev1.Container, error) { envVars, secretVolumeMount := getSecretEnvVarsAndVolumeMounts(s.Name, gcsSecretVolumeMountPath, s.Secrets) return []corev1.Container{{ - Name: fmt.Sprintf("storage-upload-%s", s.Name), + Name: names.SimpleNameGenerator.GenerateName(fmt.Sprintf("upload-%s", s.Name)), Image: *gsutilImage, Args: args, VolumeMounts: secretVolumeMount, @@ -141,7 +142,7 @@ func (s *GCSResource) GetDownloadContainerSpec() ([]corev1.Container, error) { envVars, secretVolumeMount := getSecretEnvVarsAndVolumeMounts(s.Name, gcsSecretVolumeMountPath, s.Secrets) return []corev1.Container{ CreateDirContainer(s.Name, s.DestinationDir), { - Name: fmt.Sprintf("storage-fetch-%s", s.Name), + Name: names.SimpleNameGenerator.GenerateName(fmt.Sprintf("fetch-%s", s.Name)), Image: *gsutilImage, Args: args, Env: envVars, diff --git a/pkg/apis/pipeline/v1alpha1/gcs_resource_test.go b/pkg/apis/pipeline/v1alpha1/gcs_resource_test.go index 4d382366c28..edd60cca546 100644 --- a/pkg/apis/pipeline/v1alpha1/gcs_resource_test.go +++ b/pkg/apis/pipeline/v1alpha1/gcs_resource_test.go @@ -20,6 +20,7 @@ import ( "testing" "github.com/google/go-cmp/cmp" + "github.com/knative/build-pipeline/test/names" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -208,6 +209,8 @@ func Test_GetParams(t *testing.T) { } func Test_GetDownloadContainerSpec(t *testing.T) { + names.TestingSeed() + testcases := []struct { name string gcsResource *GCSResource @@ -226,20 +229,23 @@ func Test_GetDownloadContainerSpec(t *testing.T) { SecretKey: "key.json", }}, }, - wantContainers: []corev1.Container{ - CreateDirContainer("gcs-valid", "/workspace"), { - Name: "storage-fetch-gcs-valid", - Image: "override-with-gsutil-image:latest", - Args: []string{"-args", "cp -r gs://some-bucket/** /workspace"}, - Env: []corev1.EnvVar{{ - Name: "GOOGLE_APPLICATION_CREDENTIALS", - Value: "/var/secret/secretName/key.json", - }}, - VolumeMounts: []corev1.VolumeMount{{ - Name: "volume-gcs-valid-secretName", - MountPath: "/var/secret/secretName", - }}, + wantContainers: []corev1.Container{{ + Name: "create-dir-gcs-valid-9l9zj", + Image: "override-with-bash-noop:latest", + Args: []string{"-args", "mkdir -p /workspace"}, + }, { + Name: "fetch-gcs-valid-mz4c7", + Image: "override-with-gsutil-image:latest", + Args: []string{"-args", "cp -r gs://some-bucket/** /workspace"}, + Env: []corev1.EnvVar{{ + Name: "GOOGLE_APPLICATION_CREDENTIALS", + Value: "/var/secret/secretName/key.json", }}, + VolumeMounts: []corev1.VolumeMount{{ + Name: "volume-gcs-valid-secretName", + MountPath: "/var/secret/secretName", + }}, + }}, }, { name: "duplicate secret mount paths", gcsResource: &GCSResource{ @@ -256,20 +262,23 @@ func Test_GetDownloadContainerSpec(t *testing.T) { FieldName: "GOOGLE_APPLICATION_CREDENTIALS", }}, }, - wantContainers: []corev1.Container{ - CreateDirContainer("gcs-valid", "/workspace"), { - Name: "storage-fetch-gcs-valid", - Image: "override-with-gsutil-image:latest", - Args: []string{"-args", "cp gs://some-bucket /workspace"}, - Env: []corev1.EnvVar{{ - Name: "GOOGLE_APPLICATION_CREDENTIALS", - Value: "/var/secret/secretName/key.json", - }}, - VolumeMounts: []corev1.VolumeMount{{ - Name: "volume-gcs-valid-secretName", - MountPath: "/var/secret/secretName", - }}, + wantContainers: []corev1.Container{{ + Name: "create-dir-gcs-valid-mssqb", + Image: "override-with-bash-noop:latest", + Args: []string{"-args", "mkdir -p /workspace"}, + }, { + Name: "fetch-gcs-valid-78c5n", + Image: "override-with-gsutil-image:latest", + Args: []string{"-args", "cp gs://some-bucket /workspace"}, + Env: []corev1.EnvVar{{ + Name: "GOOGLE_APPLICATION_CREDENTIALS", + Value: "/var/secret/secretName/key.json", + }}, + VolumeMounts: []corev1.VolumeMount{{ + Name: "volume-gcs-valid-secretName", + MountPath: "/var/secret/secretName", }}, + }}, }, { name: "invalid no destination directory set", gcsResource: &GCSResource{ @@ -292,6 +301,8 @@ func Test_GetDownloadContainerSpec(t *testing.T) { } func Test_GetUploadContainerSpec(t *testing.T) { + names.TestingSeed() + testcases := []struct { name string gcsResource *GCSResource @@ -311,7 +322,7 @@ func Test_GetUploadContainerSpec(t *testing.T) { }}, }, wantContainers: []corev1.Container{{ - Name: "storage-upload-gcs-valid", + Name: "upload-gcs-valid-9l9zj", Image: "override-with-gsutil-image:latest", Args: []string{"-args", "cp -r /workspace/* gs://some-bucket"}, Env: []corev1.EnvVar{{Name: "GOOGLE_APPLICATION_CREDENTIALS", Value: "/var/secret/secretName/key.json"}}, @@ -337,7 +348,7 @@ func Test_GetUploadContainerSpec(t *testing.T) { }}, }, wantContainers: []corev1.Container{{ - Name: "storage-upload-gcs-valid", + Name: "upload-gcs-valid-mz4c7", Image: "override-with-gsutil-image:latest", Args: []string{"-args", "cp /workspace/* gs://some-bucket"}, Env: []corev1.EnvVar{ @@ -357,7 +368,7 @@ func Test_GetUploadContainerSpec(t *testing.T) { TypeDir: false, }, wantContainers: []corev1.Container{{ - Name: "storage-upload-gcs-valid", + Name: "upload-gcs-valid-mssqb", Image: "override-with-gsutil-image:latest", Args: []string{"-args", "cp /workspace/* gs://some-bucket"}, }}, diff --git a/pkg/apis/pipeline/v1alpha1/git_resource.go b/pkg/apis/pipeline/v1alpha1/git_resource.go index 0cc00eb13c3..d993b286adf 100644 --- a/pkg/apis/pipeline/v1alpha1/git_resource.go +++ b/pkg/apis/pipeline/v1alpha1/git_resource.go @@ -21,6 +21,7 @@ import ( "fmt" "strings" + "github.com/knative/build-pipeline/pkg/names" corev1 "k8s.io/api/core/v1" ) @@ -112,7 +113,7 @@ func (s *GitResource) GetDownloadContainerSpec() ([]corev1.Container, error) { args = append(args, []string{"-path", dPath}...) return []corev1.Container{{ - Name: gitSource + "-" + s.Name, + Name: names.SimpleNameGenerator.GenerateName(gitSource + "-" + s.Name), Image: *gitImage, Args: args, WorkingDir: workspaceDir, diff --git a/pkg/names/generate.go b/pkg/names/generate.go new file mode 100644 index 00000000000..7afbc17e955 --- /dev/null +++ b/pkg/names/generate.go @@ -0,0 +1,54 @@ +/* +Copyright 2018 The Knative 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 names + +import ( + "fmt" + + utilrand "k8s.io/apimachinery/pkg/util/rand" +) + +// NameGenerator generates names for objects. Some backends may have more information +// available to guide selection of new names and this interface hides those details. +type NameGenerator interface { + // GenerateName generates a valid name from the base name, adding a random suffix to the + // the base. If base is valid, the returned name must also be valid. The generator is + // responsible for knowing the maximum valid name length. + GenerateName(base string) string +} + +// simpleNameGenerator generates random names. +type simpleNameGenerator struct{} + +// SimpleNameGenerator is a generator that returns the name plus a random suffix of five alphanumerics +// when a name is requested. The string is guaranteed to not exceed the length of a standard Kubernetes +// name (63 characters) +var SimpleNameGenerator NameGenerator = simpleNameGenerator{} + +const ( + // TODO: make this flexible for non-core resources with alternate naming rules. + maxNameLength = 63 + randomLength = 5 + maxGeneratedNameLength = maxNameLength - randomLength +) + +func (simpleNameGenerator) GenerateName(base string) string { + if len(base) > maxGeneratedNameLength { + base = base[:maxGeneratedNameLength] + } + return fmt.Sprintf("%s-%s", base, utilrand.String(randomLength)) +} diff --git a/pkg/reconciler/v1alpha1/pipelinerun/pipelinerun_test.go b/pkg/reconciler/v1alpha1/pipelinerun/pipelinerun_test.go index 56fcaf62585..c6b36192954 100644 --- a/pkg/reconciler/v1alpha1/pipelinerun/pipelinerun_test.go +++ b/pkg/reconciler/v1alpha1/pipelinerun/pipelinerun_test.go @@ -29,6 +29,7 @@ import ( "github.com/knative/build-pipeline/pkg/system" "github.com/knative/build-pipeline/test" tb "github.com/knative/build-pipeline/test/builder" + "github.com/knative/build-pipeline/test/names" duckv1alpha1 "github.com/knative/pkg/apis/duck/v1alpha1" "github.com/knative/pkg/configmap" "go.uber.org/zap" @@ -73,6 +74,8 @@ func getPipelineRunController(d test.Data, recorder record.EventRecorder) test.T } func TestReconcile(t *testing.T) { + names.TestingSeed() + prs := []*v1alpha1.PipelineRun{ tb.PipelineRun("test-pipeline-run-success", "foo", tb.PipelineRunSpec("test-pipeline", @@ -184,7 +187,7 @@ func TestReconcile(t *testing.T) { // Check that the expected TaskRun was created actual := clients.Pipeline.Actions()[0].(ktesting.CreateAction).GetObject() - expectedTaskRun := tb.TaskRun("test-pipeline-run-success-unit-test-1", "foo", + expectedTaskRun := tb.TaskRun("test-pipeline-run-success-unit-test-1-9l9zj", "foo", tb.TaskRunOwnerReference("PipelineRun", "test-pipeline-run-success", tb.OwnerReferenceAPIVersion("pipeline.knative.dev/v1alpha1"), tb.Controller, tb.BlockOwnerDeletion, @@ -232,7 +235,7 @@ func TestReconcile(t *testing.T) { if len(reconciledRun.Status.TaskRuns) != 1 { t.Errorf("Expected PipelineRun status to include only one TaskRun status item") } - if _, exists := reconciledRun.Status.TaskRuns["test-pipeline-run-success-unit-test-1"]; exists == false { + if _, exists := reconciledRun.Status.TaskRuns["test-pipeline-run-success-unit-test-1-9l9zj"]; exists == false { t.Errorf("Expected PipelineRun status to include TaskRun status") } } @@ -588,6 +591,8 @@ func TestReconcileWithTimeout(t *testing.T) { } func TestReconcilePropagateLabels(t *testing.T) { + names.TestingSeed() + ps := []*v1alpha1.Pipeline{tb.Pipeline("test-pipeline", "foo", tb.PipelineSpec( tb.PipelineTask("hello-world-1", "hello-world"), ))} @@ -629,7 +634,7 @@ func TestReconcilePropagateLabels(t *testing.T) { if actual == nil { t.Errorf("Expected a TaskRun to be created, but it wasn't.") } - expectedTaskRun := tb.TaskRun("test-pipeline-run-with-labels-hello-world-1", "foo", + expectedTaskRun := tb.TaskRun("test-pipeline-run-with-labels-hello-world-1-9l9zj", "foo", tb.TaskRunOwnerReference("PipelineRun", "test-pipeline-run-with-labels", tb.OwnerReferenceAPIVersion("pipeline.knative.dev/v1alpha1"), tb.Controller, tb.BlockOwnerDeletion, diff --git a/pkg/reconciler/v1alpha1/pipelinerun/resources/pipelinerunresolution.go b/pkg/reconciler/v1alpha1/pipelinerun/resources/pipelinerunresolution.go index d7550e2a3a2..ec89a49ebc9 100644 --- a/pkg/reconciler/v1alpha1/pipelinerun/resources/pipelinerunresolution.go +++ b/pkg/reconciler/v1alpha1/pipelinerun/resources/pipelinerunresolution.go @@ -21,6 +21,7 @@ import ( "time" "github.com/knative/build-pipeline/pkg/apis/pipeline/v1alpha1" + "github.com/knative/build-pipeline/pkg/names" "github.com/knative/build-pipeline/pkg/reconciler/v1alpha1/taskrun/list" "github.com/knative/build-pipeline/pkg/reconciler/v1alpha1/taskrun/resources" duckv1alpha1 "github.com/knative/pkg/apis/duck/v1alpha1" @@ -226,7 +227,8 @@ func ResolveTaskRuns(getTaskRun GetTaskRun, state []*ResolvedPipelineRunTask) er // getTaskRunName should return a uniquie name for a `TaskRun`. func getTaskRunName(prName string, pt *v1alpha1.PipelineTask) string { - return fmt.Sprintf("%s-%s", prName, pt.Name) + base := fmt.Sprintf("%s-%s", prName, pt.Name) + return names.SimpleNameGenerator.GenerateName(base) } // GetPipelineConditionStatus will return the Condition that the PipelineRun prName should be diff --git a/pkg/reconciler/v1alpha1/pipelinerun/resources/pipelinerunresolution_test.go b/pkg/reconciler/v1alpha1/pipelinerun/resources/pipelinerunresolution_test.go index 20fda0ca110..57739277a3f 100644 --- a/pkg/reconciler/v1alpha1/pipelinerun/resources/pipelinerunresolution_test.go +++ b/pkg/reconciler/v1alpha1/pipelinerun/resources/pipelinerunresolution_test.go @@ -31,6 +31,7 @@ import ( "github.com/knative/build-pipeline/pkg/apis/pipeline/v1alpha1" "github.com/knative/build-pipeline/pkg/reconciler/v1alpha1/taskrun/resources" tb "github.com/knative/build-pipeline/test/builder" + "github.com/knative/build-pipeline/test/names" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -316,6 +317,8 @@ func TestGetResourcesFromBindings_Extra(t *testing.T) { } } func TestResolvePipelineRun(t *testing.T) { + names.TestingSeed() + p := tb.Pipeline("pipelines", "namespace", tb.PipelineSpec( tb.PipelineDeclaredResource("git-resource", "git"), tb.PipelineTask("mytask1", "task", @@ -355,7 +358,7 @@ func TestResolvePipelineRun(t *testing.T) { } expectedState := []*ResolvedPipelineRunTask{{ PipelineTask: &p.Spec.Tasks[0], - TaskRunName: "pipelinerun-mytask1", + TaskRunName: "pipelinerun-mytask1-9l9zj", TaskRun: nil, ResolvedTaskResources: &resources.ResolvedTaskResources{ TaskName: task.Name, @@ -367,7 +370,7 @@ func TestResolvePipelineRun(t *testing.T) { }, }, { PipelineTask: &p.Spec.Tasks[1], - TaskRunName: "pipelinerun-mytask2", + TaskRunName: "pipelinerun-mytask2-mz4c7", TaskRun: nil, ResolvedTaskResources: &resources.ResolvedTaskResources{ TaskName: task.Name, @@ -379,7 +382,7 @@ func TestResolvePipelineRun(t *testing.T) { }, }, { PipelineTask: &p.Spec.Tasks[2], - TaskRunName: "pipelinerun-mytask3", + TaskRunName: "pipelinerun-mytask3-mssqb", TaskRun: nil, ResolvedTaskResources: &resources.ResolvedTaskResources{ TaskName: task.Name, @@ -390,6 +393,7 @@ func TestResolvePipelineRun(t *testing.T) { }, }, }} + if d := cmp.Diff(pipelineState, expectedState, cmpopts.IgnoreUnexported(v1alpha1.TaskRunSpec{})); d != "" { t.Fatalf("Expected to get current pipeline state %v, but actual differed: %s", expectedState, d) } diff --git a/pkg/reconciler/v1alpha1/taskrun/resources/input_resource_test.go b/pkg/reconciler/v1alpha1/taskrun/resources/input_resource_test.go index d0975d9011e..108dbf70c48 100644 --- a/pkg/reconciler/v1alpha1/taskrun/resources/input_resource_test.go +++ b/pkg/reconciler/v1alpha1/taskrun/resources/input_resource_test.go @@ -22,6 +22,7 @@ import ( informers "github.com/knative/build-pipeline/pkg/client/informers/externalversions" listers "github.com/knative/build-pipeline/pkg/client/listers/pipeline/v1alpha1" "github.com/knative/build-pipeline/pkg/logging" + "github.com/knative/build-pipeline/test/names" buildv1alpha1 "github.com/knative/build/pkg/apis/build/v1alpha1" "go.uber.org/zap" corev1 "k8s.io/api/core/v1" @@ -273,7 +274,7 @@ func TestAddResourceToBuild(t *testing.T) { wantErr: false, want: buildv1alpha1.BuildSpec{ Steps: []corev1.Container{{ - Name: "git-source-the-git", + Name: "git-source-the-git-9l9zj", Image: "override-with-git:latest", Args: []string{"-url", "https://github.com/grafeas/kritis", "-revision", "master", "-path", "/workspace/gitspace"}, WorkingDir: "/workspace", @@ -305,7 +306,7 @@ func TestAddResourceToBuild(t *testing.T) { wantErr: false, want: buildv1alpha1.BuildSpec{ Steps: []corev1.Container{{ - Name: "git-source-the-git-with-branch", + Name: "git-source-the-git-with-branch-9l9zj", Image: "override-with-git:latest", Args: []string{"-url", "https://github.com/grafeas/kritis", "-revision", "branch", "-path", "/workspace/gitspace"}, WorkingDir: "/workspace", @@ -342,12 +343,12 @@ func TestAddResourceToBuild(t *testing.T) { wantErr: false, want: buildv1alpha1.BuildSpec{ Steps: []corev1.Container{{ - Name: "git-source-the-git-with-branch", + Name: "git-source-the-git-with-branch-mz4c7", Image: "override-with-git:latest", Args: []string{"-url", "https://github.com/grafeas/kritis", "-revision", "branch", "-path", "/workspace/git-duplicate-space"}, WorkingDir: "/workspace", }, { - Name: "git-source-the-git-with-branch", + Name: "git-source-the-git-with-branch-9l9zj", Image: "override-with-git:latest", Args: []string{"-url", "https://github.com/grafeas/kritis", "-revision", "branch", "-path", "/workspace/gitspace"}, WorkingDir: "/workspace", @@ -379,7 +380,7 @@ func TestAddResourceToBuild(t *testing.T) { wantErr: false, want: buildv1alpha1.BuildSpec{ Steps: []corev1.Container{{ - Name: "git-source-the-git", + Name: "git-source-the-git-9l9zj", Image: "override-with-git:latest", Args: []string{"-url", "https://github.com/grafeas/kritis", "-revision", "master", "-path", "/workspace/gitspace"}, WorkingDir: "/workspace", @@ -411,7 +412,7 @@ func TestAddResourceToBuild(t *testing.T) { wantErr: false, want: buildv1alpha1.BuildSpec{ Steps: []corev1.Container{{ - Name: "git-source-the-git-with-branch", + Name: "git-source-the-git-with-branch-9l9zj", Image: "override-with-git:latest", Args: []string{"-url", "https://github.com/grafeas/kritis", "-revision", "branch", "-path", "/workspace/gitspace"}, WorkingDir: "/workspace", @@ -445,11 +446,11 @@ func TestAddResourceToBuild(t *testing.T) { wantErr: false, want: buildv1alpha1.BuildSpec{ Steps: []corev1.Container{{ - Name: "create-dir-gitspace", + Name: "create-dir-gitspace-mz4c7", Image: "override-with-bash-noop:latest", Args: []string{"-args", "mkdir -p /workspace/gitspace"}, }, { - Name: "source-copy-gitspace-0", + Name: "source-copy-gitspace-0-9l9zj", Image: "override-with-bash-noop:latest", Args: []string{"-args", "cp -r prev-task-path/. /workspace/gitspace"}, VolumeMounts: []corev1.VolumeMount{{MountPath: "/pvc", Name: "pipelinerun-pvc"}}, @@ -484,11 +485,11 @@ func TestAddResourceToBuild(t *testing.T) { wantErr: false, want: buildv1alpha1.BuildSpec{ Steps: []corev1.Container{{ - Name: "create-dir-storage1", + Name: "create-dir-storage1-9l9zj", Image: "override-with-bash-noop:latest", Args: []string{"-args", "mkdir -p /workspace/gcs-dir"}, }, { - Name: "storage-fetch-storage1", + Name: "fetch-storage1-mz4c7", Image: "override-with-gsutil-image:latest", Args: []string{"-args", "cp gs://fake-bucket/rules.zip /workspace/gcs-dir"}, }}, @@ -521,11 +522,11 @@ func TestAddResourceToBuild(t *testing.T) { wantErr: false, want: buildv1alpha1.BuildSpec{ Steps: []corev1.Container{{ - Name: "create-dir-workspace", + Name: "create-dir-workspace-mz4c7", Image: "override-with-bash-noop:latest", Args: []string{"-args", "mkdir -p /workspace/gcs-dir"}, }, { - Name: "source-copy-workspace-0", + Name: "source-copy-workspace-0-9l9zj", Image: "override-with-bash-noop:latest", Args: []string{"-args", "cp -r prev-task-path/. /workspace/gcs-dir"}, VolumeMounts: []corev1.VolumeMount{{MountPath: "/pvc", Name: "pipelinerun-pvc"}}, @@ -637,7 +638,7 @@ func TestAddResourceToBuild(t *testing.T) { wantErr: false, want: buildv1alpha1.BuildSpec{ Steps: []corev1.Container{{ - Name: "kubeconfig", + Name: "kubeconfig-9l9zj", Image: "override-with-kubeconfig-writer:latest", Args: []string{ "-clusterConfig", `{"name":"cluster3","type":"cluster","url":"http://10.10.10.10","revision":"","username":"","password":"","token":"","Insecure":false,"cadata":"bXktY2EtY2VydAo=","secrets":null}`, @@ -683,7 +684,7 @@ func TestAddResourceToBuild(t *testing.T) { wantErr: false, want: buildv1alpha1.BuildSpec{ Steps: []corev1.Container{{ - Name: "kubeconfig", + Name: "kubeconfig-9l9zj", Image: "override-with-kubeconfig-writer:latest", Args: []string{ "-clusterConfig", `{"name":"cluster2","type":"cluster","url":"http://10.10.10.10","revision":"","username":"","password":"","token":"","Insecure":false,"cadata":null,"secrets":[{"fieldName":"cadata","secretKey":"cadatakey","secretName":"secret1"}]}`, @@ -704,6 +705,7 @@ func TestAddResourceToBuild(t *testing.T) { }} { t.Run(c.desc, func(t *testing.T) { setUp() + names.TestingSeed() fakekubeclient := fakek8s.NewSimpleClientset() got, err := AddInputResource(fakekubeclient, c.build, c.task.Name, &c.task.Spec, c.taskRun, pipelineResourceLister, logger) if (err != nil) != c.wantErr { @@ -841,11 +843,11 @@ func Test_StorageInputResource(t *testing.T) { }, Spec: buildv1alpha1.BuildSpec{ Steps: []corev1.Container{{ - Name: "create-dir-gcs-input-resource", + Name: "create-dir-gcs-input-resource-9l9zj", Image: "override-with-bash-noop:latest", Args: []string{"-args", "mkdir -p /workspace/gcs-input-resource"}, }, { - Name: "storage-fetch-gcs-input-resource", + Name: "fetch-gcs-input-resource-mz4c7", Image: "override-with-gsutil-image:latest", Args: []string{"-args", "cp gs://fake-bucket/rules.zip /workspace/gcs-input-resource"}, }}, @@ -918,11 +920,11 @@ func Test_StorageInputResource(t *testing.T) { }, Spec: buildv1alpha1.BuildSpec{ Steps: []corev1.Container{{ - Name: "create-dir-storage-gcs-keys", + Name: "create-dir-storage-gcs-keys-9l9zj", Image: "override-with-bash-noop:latest", Args: []string{"-args", "mkdir -p /workspace/gcs-input-resource"}, }, { - Name: "storage-fetch-storage-gcs-keys", + Name: "fetch-storage-gcs-keys-mz4c7", Image: "override-with-gsutil-image:latest", Args: []string{"-args", "cp -r gs://fake-bucket/rules.zip/** /workspace/gcs-input-resource"}, VolumeMounts: []corev1.VolumeMount{ @@ -943,6 +945,7 @@ func Test_StorageInputResource(t *testing.T) { }, }} { t.Run(c.desc, func(t *testing.T) { + names.TestingSeed() setUp() fakekubeclient := fakek8s.NewSimpleClientset() got, err := AddInputResource(fakekubeclient, c.build, c.task.Name, &c.task.Spec, c.taskRun, pipelineResourceLister, logger) @@ -1020,15 +1023,14 @@ func TestAddStepsToBuild_WithBucketFromConfigMap(t *testing.T) { build: build(), want: buildv1alpha1.BuildSpec{ Steps: []corev1.Container{{ - Name: "artifact-dest-mkdir-gitspace-0", + Name: "artifact-dest-mkdir-gitspace-0-mssqb", Image: "override-with-bash-noop:latest", Args: []string{"-args", "mkdir -p /workspace/gitspace"}, - }, - { - Name: "artifact-copy-from-gitspace-0", - Image: "override-with-gsutil-image:latest", - Args: []string{"-args", "cp -r gs://fake-bucket/prev-task-path/** /workspace/gitspace"}, - }}, + }, { + Name: "artifact-copy-from-gitspace-0-78c5n", + Image: "override-with-gsutil-image:latest", + Args: []string{"-args", "cp -r gs://fake-bucket/prev-task-path/** /workspace/gitspace"}, + }}, }, }, { desc: "storage resource as input from previous task - copy from bucket", @@ -1057,15 +1059,14 @@ func TestAddStepsToBuild_WithBucketFromConfigMap(t *testing.T) { build: build(), want: buildv1alpha1.BuildSpec{ Steps: []corev1.Container{{ - Name: "artifact-dest-mkdir-workspace-0", + Name: "artifact-dest-mkdir-workspace-0-6nl7g", Image: "override-with-bash-noop:latest", Args: []string{"-args", "mkdir -p /workspace/gcs-dir"}, - }, - { - Name: "artifact-copy-from-workspace-0", - Image: "override-with-gsutil-image:latest", - Args: []string{"-args", "cp -r gs://fake-bucket/prev-task-path/** /workspace/gcs-dir"}, - }}, + }, { + Name: "artifact-copy-from-workspace-0-j2tds", + Image: "override-with-gsutil-image:latest", + Args: []string{"-args", "cp -r gs://fake-bucket/prev-task-path/** /workspace/gcs-dir"}, + }}, }, }} { t.Run(c.desc, func(t *testing.T) { diff --git a/pkg/reconciler/v1alpha1/taskrun/resources/output_resource_test.go b/pkg/reconciler/v1alpha1/taskrun/resources/output_resource_test.go index 356477e7adb..6ab6a38122c 100644 --- a/pkg/reconciler/v1alpha1/taskrun/resources/output_resource_test.go +++ b/pkg/reconciler/v1alpha1/taskrun/resources/output_resource_test.go @@ -24,6 +24,7 @@ import ( fakeclientset "github.com/knative/build-pipeline/pkg/client/clientset/versioned/fake" informers "github.com/knative/build-pipeline/pkg/client/informers/externalversions" listers "github.com/knative/build-pipeline/pkg/client/listers/pipeline/v1alpha1" + "github.com/knative/build-pipeline/test/names" buildv1alpha1 "github.com/knative/build/pkg/apis/build/v1alpha1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -163,7 +164,7 @@ func Test_Valid_OutputResources(t *testing.T) { }, }, wantSteps: []corev1.Container{{ - Name: "source-mkdir-source-git", + Name: "source-mkdir-source-git-9l9zj", Image: "override-with-bash-noop:latest", Args: []string{"-args", "mkdir -p pipeline-task-name"}, VolumeMounts: []corev1.VolumeMount{{ @@ -171,7 +172,7 @@ func Test_Valid_OutputResources(t *testing.T) { MountPath: "/pvc", }}, }, { - Name: "source-copy-source-git", + Name: "source-copy-source-git-mz4c7", Image: "override-with-bash-noop:latest", Args: []string{"-args", "cp -r /workspace/source-workspace/. pipeline-task-name"}, VolumeMounts: []corev1.VolumeMount{{ @@ -219,7 +220,7 @@ func Test_Valid_OutputResources(t *testing.T) { }, }, wantSteps: []corev1.Container{{ - Name: "source-mkdir-source-git", + Name: "source-mkdir-source-git-9l9zj", Image: "override-with-bash-noop:latest", Args: []string{"-args", "mkdir -p pipeline-task-name"}, VolumeMounts: []corev1.VolumeMount{{ @@ -227,7 +228,7 @@ func Test_Valid_OutputResources(t *testing.T) { MountPath: "/pvc", }}, }, { - Name: "source-copy-source-git", + Name: "source-copy-source-git-mz4c7", Image: "override-with-bash-noop:latest", Args: []string{"-args", "cp -r /workspace/output/source-workspace/. pipeline-task-name"}, VolumeMounts: []corev1.VolumeMount{{ @@ -365,7 +366,7 @@ func Test_Valid_OutputResources(t *testing.T) { }, }, wantSteps: []corev1.Container{{ - Name: "storage-upload-source-gcs", + Name: "upload-source-gcs-9l9zj", Image: "override-with-gsutil-image:latest", VolumeMounts: []corev1.VolumeMount{{ Name: "volume-source-gcs-sname", @@ -376,12 +377,12 @@ func Test_Valid_OutputResources(t *testing.T) { Name: "GOOGLE_APPLICATION_CREDENTIALS", Value: "/var/secret/sname/key.json", }}, }, { - Name: "source-mkdir-source-gcs", + Name: "source-mkdir-source-gcs-mz4c7", Image: "override-with-bash-noop:latest", Args: []string{"-args", "mkdir -p pipeline-task-path"}, VolumeMounts: []corev1.VolumeMount{{Name: "pipelinerun-parent-pvc", MountPath: "/pvc"}}, }, { - Name: "source-copy-source-gcs", + Name: "source-copy-source-gcs-mssqb", Image: "override-with-bash-noop:latest", Args: []string{"-args", "cp -r /workspace/faraway-disk/. pipeline-task-path"}, VolumeMounts: []corev1.VolumeMount{{Name: "pipelinerun-parent-pvc", MountPath: "/pvc"}}, @@ -432,7 +433,7 @@ func Test_Valid_OutputResources(t *testing.T) { }, }, wantSteps: []corev1.Container{{ - Name: "storage-upload-source-gcs", + Name: "upload-source-gcs-9l9zj", Image: "override-with-gsutil-image:latest", VolumeMounts: []corev1.VolumeMount{{ Name: "volume-source-gcs-sname", MountPath: "/var/secret/sname", @@ -442,12 +443,12 @@ func Test_Valid_OutputResources(t *testing.T) { }}, Args: []string{"-args", "cp -r /workspace/output/source-workspace/* gs://some-bucket"}, }, { - Name: "source-mkdir-source-gcs", + Name: "source-mkdir-source-gcs-mz4c7", Image: "override-with-bash-noop:latest", Args: []string{"-args", "mkdir -p pipeline-task-path"}, VolumeMounts: []corev1.VolumeMount{{Name: "pipelinerun-pvc", MountPath: "/pvc"}}, }, { - Name: "source-copy-source-gcs", + Name: "source-copy-source-gcs-mssqb", Image: "override-with-bash-noop:latest", Args: []string{"-args", "cp -r /workspace/output/source-workspace/. pipeline-task-path"}, VolumeMounts: []corev1.VolumeMount{{Name: "pipelinerun-pvc", MountPath: "/pvc"}}, @@ -494,7 +495,7 @@ func Test_Valid_OutputResources(t *testing.T) { }, }, wantSteps: []corev1.Container{{ - Name: "storage-upload-source-gcs", + Name: "upload-source-gcs-9l9zj", Image: "override-with-gsutil-image:latest", VolumeMounts: []corev1.VolumeMount{{ Name: "volume-source-gcs-sname", MountPath: "/var/secret/sname", @@ -545,7 +546,7 @@ func Test_Valid_OutputResources(t *testing.T) { }, }, wantSteps: []corev1.Container{{ - Name: "storage-upload-source-gcs", + Name: "upload-source-gcs-9l9zj", Image: "override-with-gsutil-image:latest", VolumeMounts: []corev1.VolumeMount{{ Name: "volume-source-gcs-sname", MountPath: "/var/secret/sname", @@ -665,6 +666,7 @@ func Test_Valid_OutputResources(t *testing.T) { build: build(), }} { t.Run(c.name, func(t *testing.T) { + names.TestingSeed() outputResourceSetup() fakekubeclient := fakek8s.NewSimpleClientset() err := AddOutputResources(fakekubeclient, c.build, c.task.Name, &c.task.Spec, c.taskRun, outputpipelineResourceLister, logger) @@ -758,7 +760,7 @@ func Test_Valid_OutputResources_WithBucketStorage(t *testing.T) { }, }, wantSteps: []corev1.Container{{ - Name: "artifact-copy-to-source-git", + Name: "artifact-copy-to-source-git-9l9zj", Image: "override-with-gsutil-image:latest", Args: []string{"-args", "cp -r /workspace/source-workspace gs://fake-bucket/pipeline-task-name"}, }}, @@ -802,7 +804,7 @@ func Test_Valid_OutputResources_WithBucketStorage(t *testing.T) { }, }, wantSteps: []corev1.Container{{ - Name: "artifact-copy-to-source-git", + Name: "artifact-copy-to-source-git-9l9zj", Image: "override-with-gsutil-image:latest", Args: []string{"-args", "cp -r /workspace/output/source-workspace gs://fake-bucket/pipeline-task-name"}, }}, @@ -844,6 +846,7 @@ func Test_Valid_OutputResources_WithBucketStorage(t *testing.T) { }} { t.Run(c.name, func(t *testing.T) { outputResourceSetup() + names.TestingSeed() fakekubeclient := fakek8s.NewSimpleClientset( &corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ diff --git a/pkg/reconciler/v1alpha1/taskrun/resources/pod.go b/pkg/reconciler/v1alpha1/taskrun/resources/pod.go index 58dcec22981..9e502e0ce5d 100644 --- a/pkg/reconciler/v1alpha1/taskrun/resources/pod.go +++ b/pkg/reconciler/v1alpha1/taskrun/resources/pod.go @@ -35,6 +35,7 @@ import ( "github.com/knative/build-pipeline/pkg/credentials" "github.com/knative/build-pipeline/pkg/credentials/dockercreds" "github.com/knative/build-pipeline/pkg/credentials/gitcreds" + "github.com/knative/build-pipeline/pkg/names" v1alpha1 "github.com/knative/build/pkg/apis/build/v1alpha1" "github.com/knative/pkg/apis" duckv1alpha1 "github.com/knative/pkg/apis/duck/v1alpha1" @@ -122,6 +123,8 @@ func gcsToContainer(source v1alpha1.SourceSpec, index int) (*corev1.Container, e containerName = containerName + strconv.Itoa(index) } + containerName = names.SimpleNameGenerator.GenerateName(containerName) + return &corev1.Container{ Name: containerName, Image: *gcsFetcherImage, @@ -180,7 +183,7 @@ func makeCredentialInitializer(build *v1alpha1.Build, kubeclient kubernetes.Inte } if matched { - name := fmt.Sprintf("secret-volume-%s", secret.Name) + name := names.SimpleNameGenerator.GenerateName(fmt.Sprintf("secret-volume-%s", secret.Name)) volumeMounts = append(volumeMounts, corev1.VolumeMount{ Name: name, MountPath: credentials.VolumeName(secret.Name), @@ -197,7 +200,7 @@ func makeCredentialInitializer(build *v1alpha1.Build, kubeclient kubernetes.Inte } return &corev1.Container{ - Name: initContainerPrefix + credsInit, + Name: names.SimpleNameGenerator.GenerateName(initContainerPrefix + credsInit), Image: *credsImage, Args: args, VolumeMounts: volumeMounts, diff --git a/pkg/reconciler/v1alpha1/taskrun/resources/pod_test.go b/pkg/reconciler/v1alpha1/taskrun/resources/pod_test.go index eedc2dd0a41..587ef410850 100644 --- a/pkg/reconciler/v1alpha1/taskrun/resources/pod_test.go +++ b/pkg/reconciler/v1alpha1/taskrun/resources/pod_test.go @@ -28,6 +28,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" fakek8s "k8s.io/client-go/kubernetes/fake" + "github.com/knative/build-pipeline/test/names" v1alpha1 "github.com/knative/build/pkg/apis/build/v1alpha1" "github.com/knative/build/pkg/system" "github.com/knative/pkg/apis" @@ -60,11 +61,11 @@ func TestMakePod(t *testing.T) { } implicitVolumeMountsWithSecrets := append(implicitVolumeMounts, corev1.VolumeMount{ - Name: "secret-volume-multi-creds", + Name: "secret-volume-multi-creds-9l9zj", MountPath: "/var/build-secrets/multi-creds", }) implicitVolumesWithSecrets := append(implicitVolumes, corev1.Volume{ - Name: "secret-volume-multi-creds", + Name: "secret-volume-multi-creds-9l9zj", VolumeSource: corev1.VolumeSource{Secret: &corev1.SecretVolumeSource{SecretName: "multi-creds"}}, }) @@ -91,7 +92,7 @@ func TestMakePod(t *testing.T) { want: &corev1.PodSpec{ RestartPolicy: corev1.RestartPolicyNever, InitContainers: []corev1.Container{{ - Name: initContainerPrefix + credsInit, + Name: initContainerPrefix + credsInit + "-9l9zj", Image: *credsImage, Args: []string{}, Env: implicitEnvVars, @@ -122,14 +123,14 @@ func TestMakePod(t *testing.T) { want: &corev1.PodSpec{ RestartPolicy: corev1.RestartPolicyNever, InitContainers: []corev1.Container{{ - Name: initContainerPrefix + credsInit, + Name: initContainerPrefix + credsInit + "-9l9zj", Image: *credsImage, Args: []string{}, Env: implicitEnvVars, VolumeMounts: implicitVolumeMounts, // without subpath WorkingDir: workspaceDir, }, { - Name: initContainerPrefix + gcsSource + "-gcs-foo-bar", + Name: initContainerPrefix + gcsSource + "-gcs-foo-bar" + "-mz4c7", Image: *gcsFetcherImage, Args: []string{"--type", "Manifest", "--location", "gs://foo/bar", "--dest_dir", "/workspace/path/foo"}, Env: implicitEnvVars, @@ -152,7 +153,7 @@ func TestMakePod(t *testing.T) { ServiceAccountName: "service-account", RestartPolicy: corev1.RestartPolicyNever, InitContainers: []corev1.Container{{ - Name: initContainerPrefix + credsInit, + Name: initContainerPrefix + credsInit + "-mz4c7", Image: *credsImage, Args: []string{ "-basic-docker=multi-creds=https://docker.io", @@ -175,6 +176,7 @@ func TestMakePod(t *testing.T) { }, }} { t.Run(c.desc, func(t *testing.T) { + names.TestingSeed() cs := fakek8s.NewSimpleClientset( &corev1.ServiceAccount{ObjectMeta: metav1.ObjectMeta{Name: "default"}}, &corev1.ServiceAccount{ObjectMeta: metav1.ObjectMeta{Name: "service-account"}, diff --git a/pkg/reconciler/v1alpha1/taskrun/taskrun_test.go b/pkg/reconciler/v1alpha1/taskrun/taskrun_test.go index e847c25bc57..3872932704a 100644 --- a/pkg/reconciler/v1alpha1/taskrun/taskrun_test.go +++ b/pkg/reconciler/v1alpha1/taskrun/taskrun_test.go @@ -30,6 +30,7 @@ import ( "github.com/knative/build-pipeline/pkg/system" "github.com/knative/build-pipeline/test" tb "github.com/knative/build-pipeline/test/builder" + "github.com/knative/build-pipeline/test/names" buildv1alpha1 "github.com/knative/build/pkg/apis/build/v1alpha1" duckv1alpha1 "github.com/knative/pkg/apis/duck/v1alpha1" "github.com/knative/pkg/configmap" @@ -158,6 +159,7 @@ func getTaskRunController(d test.Data) test.TestAssets { } func TestReconcile(t *testing.T) { + boolTrue := true taskRunSuccess := tb.TaskRun("test-taskrun-run-success", "foo", tb.TaskRunSpec( tb.TaskRunTaskRef(simpleTask.Name, tb.TaskRefAPIVersion("a1")), )) @@ -256,186 +258,902 @@ func TestReconcile(t *testing.T) { PipelineResources: []*v1alpha1.PipelineResource{gitResource, anotherGitResource, imageResource}, } for _, tc := range []struct { - name string - taskRun *v1alpha1.TaskRun - wantBuild *buildv1alpha1.Build + name string + taskRun *v1alpha1.TaskRun + wantPodLabels map[string]string + wantOwnerReferences []metav1.OwnerReference + wantServiceAccountName string + wantPodVolume []corev1.Volume + wantSteps []corev1.Container }{{ - name: "success", - taskRun: taskRunSuccess, - wantBuild: tb.Build("test-taskrun-run-success", "foo", - tb.BuildLabel(taskRunNameLabelKey, "test-taskrun-run-success"), - tb.BuildOwnerReference("TaskRun", "test-taskrun-run-success", - tb.OwnerReferenceAPIVersion("pipeline.knative.dev/v1alpha1")), - tb.BuildSpec( - entrypointCopyStep, - tb.BuildStep("simple-step", "foo", tb.Command(entrypointLocation), - entrypointOptionEnvVar, tb.VolumeMount(toolsMount), - ), - tb.BuildVolume(getToolsVolume(taskRunSuccess.Name)), - ), - ), - }, { - name: "serviceaccount", - taskRun: taskRunWithSaSuccess, - wantBuild: tb.Build("test-taskrun-with-sa-run-success", "foo", - tb.BuildLabel(taskRunNameLabelKey, "test-taskrun-with-sa-run-success"), - tb.BuildOwnerReference("TaskRun", "test-taskrun-with-sa-run-success", - tb.OwnerReferenceAPIVersion("pipeline.knative.dev/v1alpha1")), - tb.BuildSpec( - tb.BuildServiceAccountName("test-sa"), - entrypointCopyStep, - tb.BuildStep("sa-step", "foo", tb.Command(entrypointLocation), - entrypointOptionEnvVar, tb.VolumeMount(toolsMount), - ), - tb.BuildVolume(getToolsVolume(taskRunWithSaSuccess.Name)), - ), - ), + name: "success", + taskRun: taskRunSuccess, + wantPodLabels: map[string]string{"pipeline.knative.dev/taskRun": "test-taskrun-run-success"}, + wantOwnerReferences: []metav1.OwnerReference{{ + APIVersion: "pipeline.knative.dev/v1alpha1", + Kind: "TaskRun", + Name: "test-taskrun-run-success", + Controller: &boolTrue, + BlockOwnerDeletion: &boolTrue, + }}, + wantServiceAccountName: "", + wantPodVolume: getVolumes(), + wantSteps: []corev1.Container{{ + Name: "build-step-credential-initializer-9l9zj", + Image: "override-with-creds:latest", + Args: []string{}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{{ + Name: "HOME", + Value: "/builder/home", + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }, { + Name: "build-step-place-tools", + Image: "gcr.io/k8s-prow/entrypoint@sha256:7c7cd8906ce4982ffee326218e9fc75da2d4896d53cabc9833b9cc8d2d6b2b8f", + Command: []string{"/bin/cp"}, + Args: []string{"/entrypoint", "/tools/entrypoint"}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{ + { + Name: "HOME", + Value: "/builder/home", + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "tools", + ReadOnly: false, + MountPath: "/tools", + }, { + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }, { + Name: "build-step-simple-step", + Image: "foo", + Args: []string{}, + Command: []string{"/tools/entrypoint"}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{{ + Name: "HOME", + Value: "/builder/home", + ValueFrom: (*corev1.EnvVarSource)(nil), + }, { + Name: "ENTRYPOINT_OPTIONS", + Value: "{\"args\":[\"/mycmd\"],\"process_log\":\"/tools/process-log.txt\",\"marker_file\":\"/tools/marker-file.txt\"}", + ValueFrom: (*corev1.EnvVarSource)(nil), + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "tools", + ReadOnly: false, + MountPath: "/tools", + }, { + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }}, }, { - name: "params", - taskRun: taskRunTemplating, - wantBuild: tb.Build("test-taskrun-templating", "foo", - tb.BuildLabel(taskRunNameLabelKey, "test-taskrun-templating"), - tb.BuildOwnerReference("TaskRun", "test-taskrun-templating", - tb.OwnerReferenceAPIVersion("pipeline.knative.dev/v1alpha1")), - tb.BuildSpec( - tb.BuildStep("git-source-git-resource", "override-with-git:latest", - tb.Args("-url", "https://foo.git", "-revision", "master", "-path", "/workspace/workspace"), - tb.VolumeMount(corev1.VolumeMount{ - Name: "workspace", - MountPath: workspaceDir, - }), - tb.VolumeMount(implicitBuilderVolumeMounts), - ), - entrypointCopyStep, - tb.BuildStep("mycontainer", "myimage", tb.Command(entrypointLocation), - tb.EnvVar("ENTRYPOINT_OPTIONS", `{"args":["/mycmd","--my-arg=foo","--my-arg-with-default=bar","--my-arg-with-default2=thedefault","--my-additional-arg=gcr.io/kristoff/sven"],"process_log":"/tools/process-log.txt","marker_file":"/tools/marker-file.txt"}`), - tb.VolumeMount(toolsMount), - ), - tb.BuildStep("myothercontainer", "myotherimage", tb.Command(entrypointLocation), - tb.EnvVar("ENTRYPOINT_OPTIONS", `{"args":["/mycmd","--my-other-arg=https://foo.git"],"process_log":"/tools/process-log.txt","marker_file":"/tools/marker-file.txt"}`), - tb.VolumeMount(toolsMount), - ), - tb.BuildVolume(getToolsVolume(taskRunTemplating.Name)), - ), - ), + name: "serviceaccount", + taskRun: taskRunWithSaSuccess, + wantPodLabels: map[string]string{"pipeline.knative.dev/taskRun": "test-taskrun-with-sa-run-success"}, + wantOwnerReferences: []metav1.OwnerReference{{ + APIVersion: "pipeline.knative.dev/v1alpha1", + Kind: "TaskRun", + Name: "test-taskrun-with-sa-run-success", + Controller: &boolTrue, + BlockOwnerDeletion: &boolTrue, + }}, + wantPodVolume: getVolumes(), + wantServiceAccountName: "test-sa", + wantSteps: []corev1.Container{{ + Name: "build-step-credential-initializer-9l9zj", + Image: "override-with-creds:latest", + Args: []string{}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{{ + Name: "HOME", + Value: "/builder/home", + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }, { + Name: "build-step-place-tools", + Image: "gcr.io/k8s-prow/entrypoint@sha256:7c7cd8906ce4982ffee326218e9fc75da2d4896d53cabc9833b9cc8d2d6b2b8f", + Command: []string{"/bin/cp"}, + Args: []string{"/entrypoint", "/tools/entrypoint"}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{ + { + Name: "HOME", + Value: "/builder/home", + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "tools", + ReadOnly: false, + MountPath: "/tools", + }, { + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }, { + Name: "build-step-sa-step", + Image: "foo", + Args: []string{}, + Command: []string{"/tools/entrypoint"}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{{ + Name: "HOME", + Value: "/builder/home", + ValueFrom: (*corev1.EnvVarSource)(nil), + }, { + Name: "ENTRYPOINT_OPTIONS", + Value: "{\"args\":[\"/mycmd\"],\"process_log\":\"/tools/process-log.txt\",\"marker_file\":\"/tools/marker-file.txt\"}", + ValueFrom: (*corev1.EnvVarSource)(nil), + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "tools", + ReadOnly: false, + MountPath: "/tools", + }, { + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }}, }, { - name: "wrap-steps", - taskRun: taskRunInputOutput, - wantBuild: tb.Build("test-taskrun-input-output", "foo", - tb.BuildLabel(taskRunNameLabelKey, "test-taskrun-input-output"), - tb.BuildOwnerReference("TaskRun", "test-taskrun-input-output", - tb.OwnerReferenceAPIVersion("pipeline.knative.dev/v1alpha1")), - tb.BuildSpec( - tb.BuildStep("create-dir-another-git-resource", "override-with-bash-noop:latest", - tb.Args("-args", "mkdir -p /workspace/another-git-resource"), - ), - tb.BuildStep("source-copy-another-git-resource-0", "override-with-bash-noop:latest", - tb.Args("-args", "cp -r source-folder/. /workspace/another-git-resource"), - tb.VolumeMount(corev1.VolumeMount{Name: "test-pvc", MountPath: "/pvc"}), - ), - tb.BuildStep("create-dir-git-resource", "override-with-bash-noop:latest", - tb.Args("-args", "mkdir -p /workspace/git-resource"), - ), - tb.BuildStep("source-copy-git-resource-0", "override-with-bash-noop:latest", - tb.Args("-args", "cp -r source-folder/. /workspace/git-resource"), - tb.VolumeMount(corev1.VolumeMount{Name: "test-pvc", MountPath: "/pvc"}), - ), - entrypointCopyStep, - tb.BuildStep("simple-step", "foo", tb.Command(entrypointLocation), - entrypointOptionEnvVar, tb.VolumeMount(toolsMount), - ), - tb.BuildStep("source-mkdir-git-resource", "override-with-bash-noop:latest", - tb.Args("-args", "mkdir -p output-folder"), - tb.VolumeMount(corev1.VolumeMount{Name: "test-pvc", MountPath: "/pvc"}), - ), - tb.BuildStep("source-copy-git-resource", "override-with-bash-noop:latest", - tb.Args("-args", "cp -r /workspace/git-resource/. output-folder"), - tb.VolumeMount(corev1.VolumeMount{Name: "test-pvc", MountPath: "/pvc"}), - ), - tb.BuildVolume(getToolsVolume(taskRunInputOutput.Name)), - tb.BuildVolume(resources.GetPVCVolume(taskRunInputOutput.GetPipelineRunPVCName())), - ), - ), + name: "params", + taskRun: taskRunTemplating, + wantPodLabels: map[string]string{"pipeline.knative.dev/taskRun": "test-taskrun-templating"}, + wantOwnerReferences: []metav1.OwnerReference{{ + APIVersion: "pipeline.knative.dev/v1alpha1", + Kind: "TaskRun", + Name: "test-taskrun-templating", + Controller: &boolTrue, + BlockOwnerDeletion: &boolTrue, + }}, + wantServiceAccountName: "", + wantPodVolume: getVolumes(), + wantSteps: []corev1.Container{{ + Name: "build-step-credential-initializer-mz4c7", + Image: "override-with-creds:latest", + Args: []string{}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{{ + Name: "HOME", + Value: "/builder/home", + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }, { + Name: "build-step-git-source-git-resource-9l9zj", + Image: "override-with-git:latest", + Args: []string{"-url", "https://foo.git", "-revision", "master", "-path", "/workspace/workspace"}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{{ + Name: "HOME", + Value: "/builder/home", + ValueFrom: (*corev1.EnvVarSource)(nil), + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }, { + Name: "build-step-place-tools", + Image: "gcr.io/k8s-prow/entrypoint@sha256:7c7cd8906ce4982ffee326218e9fc75da2d4896d53cabc9833b9cc8d2d6b2b8f", + Command: []string{"/bin/cp"}, + Args: []string{"/entrypoint", "/tools/entrypoint"}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{ + { + Name: "HOME", + Value: "/builder/home", + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "tools", + ReadOnly: false, + MountPath: "/tools", + }, { + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }, { + Name: "build-step-mycontainer", + Image: "myimage", + Args: []string{}, + Command: []string{"/tools/entrypoint"}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{ + { + Name: "HOME", + Value: "/builder/home", + }, { + Name: "ENTRYPOINT_OPTIONS", + Value: "{\"args\":[\"/mycmd\",\"--my-arg=foo\",\"--my-arg-with-default=bar\",\"--my-arg-with-default2=thedefault\",\"--my-additional-arg=gcr.io/kristoff/sven\"],\"process_log\":\"/tools/process-log.txt\",\"marker_file\":\"/tools/marker-file.txt\"}", + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "tools", + ReadOnly: false, + MountPath: "/tools", + }, { + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }, { + Name: "build-step-myothercontainer", + Image: "myotherimage", + Args: []string{}, + Command: []string{"/tools/entrypoint"}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{ + { + Name: "HOME", + Value: "/builder/home", + }, { + Name: "ENTRYPOINT_OPTIONS", + Value: "{\"args\":[\"/mycmd\",\"--my-other-arg=https://foo.git\"],\"process_log\":\"/tools/process-log.txt\",\"marker_file\":\"/tools/marker-file.txt\"}", + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "tools", + ReadOnly: false, + MountPath: "/tools", + }, { + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }}, }, { - name: "taskrun-with-taskspec", - taskRun: taskRunWithTaskSpec, - wantBuild: tb.Build("test-taskrun-with-taskspec", "foo", - tb.BuildLabel(taskRunNameLabelKey, "test-taskrun-with-taskspec"), - tb.BuildOwnerReference("TaskRun", "test-taskrun-with-taskspec", - tb.OwnerReferenceAPIVersion("pipeline.knative.dev/v1alpha1")), - tb.BuildSpec( - tb.BuildStep("git-source-git-resource", "override-with-git:latest", - tb.Args("-url", "https://foo.git", "-revision", "master", "-path", "/workspace/workspace"), - tb.VolumeMount(corev1.VolumeMount{ - Name: "workspace", - MountPath: workspaceDir, - }), - tb.VolumeMount(implicitBuilderVolumeMounts), - ), - entrypointCopyStep, - tb.BuildStep("mycontainer", "myimage", tb.Command(entrypointLocation), - tb.EnvVar("ENTRYPOINT_OPTIONS", `{"args":["/mycmd","--my-arg=foo"],"process_log":"/tools/process-log.txt","marker_file":"/tools/marker-file.txt"}`), - tb.VolumeMount(toolsMount), - ), - tb.BuildVolume(getToolsVolume(taskRunWithTaskSpec.Name)), - ), - ), + name: "wrap-steps", + taskRun: taskRunInputOutput, + wantPodLabels: map[string]string{"pipeline.knative.dev/taskRun": "test-taskrun-input-output"}, + wantOwnerReferences: []metav1.OwnerReference{{ + APIVersion: "pipeline.knative.dev/v1alpha1", + Kind: "TaskRun", + Name: "test-taskrun-input-output", + Controller: &boolTrue, + BlockOwnerDeletion: &boolTrue, + }}, + wantServiceAccountName: "", + wantPodVolume: []corev1.Volume{{ + Name: "tools", + VolumeSource: corev1.VolumeSource{ + EmptyDir: &corev1.EmptyDirVolumeSource{}, + }, + }, { + Name: "test-pvc", + VolumeSource: corev1.VolumeSource{ + PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ + ClaimName: "test-pvc", + ReadOnly: false, + }, + }, + }, { + Name: "workspace", + VolumeSource: corev1.VolumeSource{ + EmptyDir: &corev1.EmptyDirVolumeSource{}, + }, + }, { + Name: "home", + VolumeSource: corev1.VolumeSource{ + EmptyDir: &corev1.EmptyDirVolumeSource{}, + }, + }, + }, + wantSteps: []corev1.Container{{ + Name: "build-step-credential-initializer-vr6ds", + Image: "override-with-creds:latest", + Args: []string{}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{{ + Name: "HOME", + Value: "/builder/home", + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }, { + Name: "build-step-create-dir-another-git-resource-78c5n", + Image: "override-with-bash-noop:latest", + Args: []string{"-args", "mkdir -p /workspace/another-git-resource"}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{{ + Name: "HOME", + Value: "/builder/home", + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }, { + Name: "build-step-source-copy-another-git-resource-0-mssqb", + Image: "override-with-bash-noop:latest", + Args: []string{"-args", "cp -r source-folder/. /workspace/another-git-resource"}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{ + { + Name: "HOME", + Value: "/builder/home", + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "test-pvc", + ReadOnly: false, + MountPath: "/pvc", + }, { + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }, { + Name: "build-step-create-dir-git-resource-mz4c7", + Image: "override-with-bash-noop:latest", + Args: []string{"-args", "mkdir -p /workspace/git-resource"}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{ + { + Name: "HOME", + Value: "/builder/home", + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }, { + Name: "build-step-source-copy-git-resource-0-9l9zj", + Image: "override-with-bash-noop:latest", + Args: []string{"-args", "cp -r source-folder/. /workspace/git-resource"}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{ + { + Name: "HOME", + Value: "/builder/home", + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "test-pvc", + ReadOnly: false, + MountPath: "/pvc", + }, { + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }, { + Name: "build-step-place-tools", + Image: "gcr.io/k8s-prow/entrypoint@sha256:7c7cd8906ce4982ffee326218e9fc75da2d4896d53cabc9833b9cc8d2d6b2b8f", + Command: []string{"/bin/cp"}, + Args: []string{"/entrypoint", "/tools/entrypoint"}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{ + { + Name: "HOME", + Value: "/builder/home", + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "tools", + ReadOnly: false, + MountPath: "/tools", + }, { + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }, { + Name: "build-step-simple-step", + Image: "foo", + Args: []string{}, + Command: []string{"/tools/entrypoint"}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{{ + Name: "HOME", + Value: "/builder/home", + ValueFrom: (*corev1.EnvVarSource)(nil), + }, { + Name: "ENTRYPOINT_OPTIONS", + Value: "{\"args\":[\"/mycmd\"],\"process_log\":\"/tools/process-log.txt\",\"marker_file\":\"/tools/marker-file.txt\"}", + ValueFrom: (*corev1.EnvVarSource)(nil), + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "tools", + ReadOnly: false, + MountPath: "/tools", + }, { + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }, { + Name: "build-step-source-mkdir-git-resource-6nl7g", + Image: "override-with-bash-noop:latest", + Args: []string{"-args", "mkdir -p output-folder"}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{ + { + Name: "HOME", + Value: "/builder/home", + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "test-pvc", + ReadOnly: false, + MountPath: "/pvc", + }, { + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }, { + Name: "build-step-source-copy-git-resource-j2tds", + Image: "override-with-bash-noop:latest", + Args: []string{"-args", "cp -r /workspace/git-resource/. output-folder"}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{ + { + Name: "HOME", + Value: "/builder/home", + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "test-pvc", + ReadOnly: false, + MountPath: "/pvc", + }, { + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }}, }, { - name: "success-with-cluster-task", - taskRun: taskRunWithClusterTask, - wantBuild: tb.Build("test-taskrun-with-cluster-task", "foo", - tb.BuildLabel(taskRunNameLabelKey, "test-taskrun-with-cluster-task"), - tb.BuildOwnerReference("TaskRun", "test-taskrun-with-cluster-task", - tb.OwnerReferenceAPIVersion("pipeline.knative.dev/v1alpha1")), - tb.BuildSpec(entrypointCopyStep, - tb.BuildStep("simple-step", "foo", tb.Command(entrypointLocation), - entrypointOptionEnvVar, tb.VolumeMount(toolsMount), - ), - tb.BuildVolume(getToolsVolume(taskRunWithClusterTask.Name)), - ), - ), + name: "taskrun-with-taskspec", + taskRun: taskRunWithTaskSpec, + wantPodLabels: map[string]string{"pipeline.knative.dev/taskRun": "test-taskrun-with-taskspec"}, + wantOwnerReferences: []metav1.OwnerReference{{ + APIVersion: "pipeline.knative.dev/v1alpha1", + Kind: "TaskRun", + Name: "test-taskrun-with-taskspec", + Controller: &boolTrue, + BlockOwnerDeletion: &boolTrue, + }}, + wantServiceAccountName: "", + wantPodVolume: getVolumes(), + wantSteps: []corev1.Container{{ + Name: "build-step-credential-initializer-mz4c7", + Image: "override-with-creds:latest", + Args: []string{}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{{ + Name: "HOME", + Value: "/builder/home", + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }, { + Name: "build-step-git-source-git-resource-9l9zj", + Image: "override-with-git:latest", + Args: []string{"-url", "https://foo.git", "-revision", "master", "-path", "/workspace/workspace"}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{{ + Name: "HOME", + Value: "/builder/home", + ValueFrom: (*corev1.EnvVarSource)(nil), + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }, { + Name: "build-step-place-tools", + Image: "gcr.io/k8s-prow/entrypoint@sha256:7c7cd8906ce4982ffee326218e9fc75da2d4896d53cabc9833b9cc8d2d6b2b8f", + Command: []string{"/bin/cp"}, + Args: []string{"/entrypoint", "/tools/entrypoint"}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{ + { + Name: "HOME", + Value: "/builder/home", + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "tools", + ReadOnly: false, + MountPath: "/tools", + }, { + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }, { + Name: "build-step-mycontainer", + Image: "myimage", + Args: []string{}, + Command: []string{"/tools/entrypoint"}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{ + { + Name: "HOME", + Value: "/builder/home", + }, { + Name: "ENTRYPOINT_OPTIONS", + Value: "{\"args\":[\"/mycmd\",\"--my-arg=foo\"],\"process_log\":\"/tools/process-log.txt\",\"marker_file\":\"/tools/marker-file.txt\"}", + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "tools", + ReadOnly: false, + MountPath: "/tools", + }, { + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }}, }, { - name: "taskrun-with-resource-spec-task-spec", - taskRun: taskRunWithResourceSpecAndTaskSpec, - wantBuild: tb.Build("test-taskrun-with-resource-spec", "foo", - tb.BuildLabel(taskRunNameLabelKey, "test-taskrun-with-resource-spec"), - tb.BuildOwnerReference("TaskRun", "test-taskrun-with-resource-spec", - tb.OwnerReferenceAPIVersion("pipeline.knative.dev/v1alpha1")), - tb.BuildSpec( - tb.BuildStep("git-source-workspace", "override-with-git:latest", - tb.Args("-url", "github.com/build-pipeline.git", "-revision", "rel-can", "-path", "/workspace/workspace"), - tb.VolumeMount(corev1.VolumeMount{ - Name: "workspace", - MountPath: workspaceDir, - }), - tb.VolumeMount(implicitBuilderVolumeMounts), - ), - entrypointCopyStep, - tb.BuildStep("mystep", "ubuntu", tb.Command(entrypointLocation), - tb.EnvVar("ENTRYPOINT_OPTIONS", `{"args":["mycmd"],"process_log":"/tools/process-log.txt","marker_file":"/tools/marker-file.txt"}`), - tb.VolumeMount(toolsMount), - ), - tb.BuildVolume(getToolsVolume(taskRunWithResourceSpecAndTaskSpec.Name)), - ), - ), + name: "success-with-cluster-task", + taskRun: taskRunWithClusterTask, + wantPodLabels: map[string]string{"pipeline.knative.dev/taskRun": "test-taskrun-with-cluster-task"}, + wantOwnerReferences: []metav1.OwnerReference{{ + APIVersion: "pipeline.knative.dev/v1alpha1", + Kind: "TaskRun", + Name: "test-taskrun-with-cluster-task", + Controller: &boolTrue, + BlockOwnerDeletion: &boolTrue, + }}, + wantServiceAccountName: "", + wantPodVolume: getVolumes(), + wantSteps: []corev1.Container{{ + Name: "build-step-credential-initializer-9l9zj", + Image: "override-with-creds:latest", + Args: []string{}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{{ + Name: "HOME", + Value: "/builder/home", + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }, { + Name: "build-step-place-tools", + Image: "gcr.io/k8s-prow/entrypoint@sha256:7c7cd8906ce4982ffee326218e9fc75da2d4896d53cabc9833b9cc8d2d6b2b8f", + Command: []string{"/bin/cp"}, + Args: []string{"/entrypoint", "/tools/entrypoint"}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{ + { + Name: "HOME", + Value: "/builder/home", + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "tools", + ReadOnly: false, + MountPath: "/tools", + }, { + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }, { + Name: "build-step-simple-step", + Image: "foo", + Args: []string{}, + Command: []string{"/tools/entrypoint"}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{{ + Name: "HOME", + Value: "/builder/home", + ValueFrom: (*corev1.EnvVarSource)(nil), + }, { + Name: "ENTRYPOINT_OPTIONS", + Value: "{\"args\":[\"/mycmd\"],\"process_log\":\"/tools/process-log.txt\",\"marker_file\":\"/tools/marker-file.txt\"}", + ValueFrom: (*corev1.EnvVarSource)(nil), + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "tools", + ReadOnly: false, + MountPath: "/tools", + }, { + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }}, }, { - name: "taskrun-with-labels", - taskRun: taskRunWithLabels, - wantBuild: tb.Build("test-taskrun-with-labels", "foo", - tb.BuildLabel("TaskRunLabel", "TaskRunValue"), - tb.BuildLabel(taskRunNameLabelKey, "test-taskrun-with-labels"), - tb.BuildOwnerReference("TaskRun", "test-taskrun-with-labels", - tb.OwnerReferenceAPIVersion("pipeline.knative.dev/v1alpha1")), - tb.BuildSpec( - entrypointCopyStep, - tb.BuildStep("simple-step", "foo", tb.Command(entrypointLocation), - entrypointOptionEnvVar, tb.VolumeMount(toolsMount), - ), - tb.BuildVolume(getToolsVolume(taskRunWithLabels.Name)), - ), - ), - }} { + name: "taskrun-with-resource-spec-task-spec", + taskRun: taskRunWithResourceSpecAndTaskSpec, + wantPodLabels: map[string]string{"pipeline.knative.dev/taskRun": "test-taskrun-with-resource-spec"}, + wantOwnerReferences: []metav1.OwnerReference{{ + APIVersion: "pipeline.knative.dev/v1alpha1", + Kind: "TaskRun", + Name: "test-taskrun-with-resource-spec", + Controller: &boolTrue, + BlockOwnerDeletion: &boolTrue, + }}, + wantServiceAccountName: "", + wantPodVolume: getVolumes(), + wantSteps: []corev1.Container{{ + Name: "build-step-credential-initializer-mz4c7", + Image: "override-with-creds:latest", + Args: []string{}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{{ + Name: "HOME", + Value: "/builder/home", + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }, { + Name: "build-step-git-source-workspace-9l9zj", + Image: "override-with-git:latest", + Args: []string{"-url", "github.com/build-pipeline.git", "-revision", "rel-can", "-path", "/workspace/workspace"}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{{ + Name: "HOME", + Value: "/builder/home", + ValueFrom: (*corev1.EnvVarSource)(nil), + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }, { + Name: "build-step-place-tools", + Image: "gcr.io/k8s-prow/entrypoint@sha256:7c7cd8906ce4982ffee326218e9fc75da2d4896d53cabc9833b9cc8d2d6b2b8f", + Command: []string{"/bin/cp"}, + Args: []string{"/entrypoint", "/tools/entrypoint"}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{ + { + Name: "HOME", + Value: "/builder/home", + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "tools", + ReadOnly: false, + MountPath: "/tools", + }, { + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }, { + Name: "build-step-mystep", + Image: "ubuntu", + Args: []string{}, + Command: []string{"/tools/entrypoint"}, + WorkingDir: "/workspace", + Env: []corev1.EnvVar{ + { + Name: "HOME", + Value: "/builder/home", + }, { + Name: "ENTRYPOINT_OPTIONS", + Value: "{\"args\":[\"mycmd\"],\"process_log\":\"/tools/process-log.txt\",\"marker_file\":\"/tools/marker-file.txt\"}", + }, + }, + VolumeMounts: []corev1.VolumeMount{{ + Name: "tools", + ReadOnly: false, + MountPath: "/tools", + }, { + Name: "workspace", + ReadOnly: false, + MountPath: "/workspace", + }, { + Name: "home", + ReadOnly: false, + MountPath: "/builder/home", + }, + }, + }}, + }, + } { t.Run(tc.name, func(t *testing.T) { + names.TestingSeed() + testAssets := getTaskRunController(d) c := testAssets.Controller clients := testAssets.Clients @@ -482,17 +1200,31 @@ func TestReconcile(t *testing.T) { if err != nil { t.Fatalf("Failed to fetch build pod: %v", err) } - // TODO: Using MakePod means that a diff will not catch issues - // specific to the Build to Pod translation (e.g. if labels are - // not propagated in MakePod). To avoid this issue we should create - // a builder for Pods and use that intead. - wantPod, err := resources.MakePod(tc.wantBuild, clients.Kube) - if err != nil { - t.Fatalf("MakePod: %v", err) + + wantPodSpec := corev1.PodSpec{ + InitContainers: []corev1.Container{}, + Containers: []corev1.Container{{ + Name: "nop", + Image: "override-with-nop:latest", + }, + }, + RestartPolicy: "Never", + } + + wantPodSpec.InitContainers = append(wantPodSpec.InitContainers, tc.wantSteps...) + wantPodSpec.ServiceAccountName = tc.wantServiceAccountName + wantPodSpec.Volumes = tc.wantPodVolume + + if d := cmp.Diff(pod.Spec, wantPodSpec); d != "" { + t.Errorf("pod spec doesn't match, diff: %s", d) + } + + if d := cmp.Diff(pod.Labels, tc.wantPodLabels); d != "" { + t.Errorf("pod spec doesn't match, diff: %s", d) } - if d := cmp.Diff(pod, wantPod, ignoreRandomPodNameSuffix); d != "" { - t.Errorf("pod doesn't match, diff: %s", d) + if d := cmp.Diff(pod.OwnerReferences, tc.wantOwnerReferences); d != "" { + t.Errorf("pod spec doesn't match, diff: %s", d) } if len(clients.Kube.Actions()) == 0 { t.Fatalf("Expected actions to be logged in the kubeclient, got none") @@ -966,3 +1698,23 @@ func TestReconcileOnTimedOutTaskRun(t *testing.T) { t.Fatalf("-want, +got: %v", d) } } + +func getVolumes() []corev1.Volume { + return []corev1.Volume{{ + Name: "tools", + VolumeSource: corev1.VolumeSource{ + EmptyDir: &corev1.EmptyDirVolumeSource{}, + }, + }, { + Name: "workspace", + VolumeSource: corev1.VolumeSource{ + EmptyDir: &corev1.EmptyDirVolumeSource{}, + }, + }, { + Name: "home", + VolumeSource: corev1.VolumeSource{ + EmptyDir: &corev1.EmptyDirVolumeSource{}, + }, + }, + } +} diff --git a/test/README.md b/test/README.md index f3bd2429714..2e8d87ce6f7 100644 --- a/test/README.md +++ b/test/README.md @@ -294,14 +294,14 @@ _See [clients.go](./clients.go)._ #### Generate random names -You can use the function `AppendRandomString` to create random names for `crd`s +You can use the function `GenerateName()` to append a random string for `crd`s or anything else, so that your tests can use unique names each time they run. ```go -namespace := test.AppendRandomString('arendelle') -``` +import "github.com/knative/build-pipeline/pkg/names" -_See [randstring.go](./randstring.go)._ +namespace := names.SimpleNameGenerator.GenerateName("arendelle") +``` #### Poll Pipeline resources diff --git a/test/cancel_test.go b/test/cancel_test.go index b0487da7b84..71bd5310556 100644 --- a/test/cancel_test.go +++ b/test/cancel_test.go @@ -74,21 +74,6 @@ func TestTaskRunPipelineRunCancel(t *testing.T) { t.Fatalf("Error waiting for PipelineRun %s to be running: %s", "pear", err) } - logger.Infof("Waiting for TaskRun %s in namespace %s to be running", "pear-foo", namespace) - if err := WaitForTaskRunState(c, "pear-foo", func(tr *v1alpha1.TaskRun) (bool, error) { - c := tr.Status.GetCondition(duckv1alpha1.ConditionSucceeded) - if c != nil { - if c.Status == corev1.ConditionTrue || c.Status == corev1.ConditionFalse { - return true, fmt.Errorf("taskRun %s already finished!", "pear-foo") - } else if c.Status == corev1.ConditionUnknown && (c.Reason == "Running" || c.Reason == "Pending") { - return true, nil - } - } - return false, nil - }, "TarkRunRunning"); err != nil { - t.Fatalf("Error waiting for TaskRun %s to be running: %s", "pear-foo", err) - } - pr, err := c.PipelineRunClient.Get("pear", metav1.GetOptions{}) if err != nil { t.Fatalf("Failed to get PipelineRun `%s`: %s", "pear", err) @@ -116,22 +101,4 @@ func TestTaskRunPipelineRunCancel(t *testing.T) { }, "PipelineRunCancelled"); err != nil { t.Errorf("Error waiting for PipelineRun `pear` to finish: %s", err) } - - logger.Infof("Waiting for TaskRun `pear-foo` in namespace %s to be cancelled", namespace) - if err := WaitForTaskRunState(c, "pear-foo", func(tr *v1alpha1.TaskRun) (bool, error) { - c := tr.Status.GetCondition(duckv1alpha1.ConditionSucceeded) - if c != nil { - if c.Status == corev1.ConditionFalse { - if c.Reason == "TaskRunCancelled" { - return true, nil - } - return true, fmt.Errorf("taskRun %s completed with the wrong reason: %s", "pear-foo", c.Reason) - } else if c.Status == corev1.ConditionTrue { - return true, fmt.Errorf("taskRun %s completed successfully, should have been cancelled", "pear-foo") - } - } - return false, nil - }, "TaskRunCancelled"); err != nil { - t.Errorf("Error waiting for TaskRun %s to finish: %s", "pear-foo", err) - } } diff --git a/test/helm_task_test.go b/test/helm_task_test.go index 4e253d5ecdd..7a112064116 100644 --- a/test/helm_task_test.go +++ b/test/helm_task_test.go @@ -23,6 +23,7 @@ import ( "time" "github.com/knative/build-pipeline/pkg/apis/pipeline/v1alpha1" + "github.com/knative/build-pipeline/pkg/names" tb "github.com/knative/build-pipeline/test/builder" knativetest "github.com/knative/pkg/test" "github.com/knative/pkg/test/logging" @@ -156,7 +157,7 @@ func getCreateImageTask(namespace string, t *testing.T, logger *logging.BaseLogg t.Fatalf("KO_DOCKER_REPO env variable is required") } - imageName = fmt.Sprintf("%s/%s", dockerRepo, AppendRandomString(sourceImageName)) + imageName = fmt.Sprintf("%s/%s", dockerRepo, names.SimpleNameGenerator.GenerateName(sourceImageName)) logger.Infof("Image to be pusblished: %s", imageName) return tb.Task(createImageTaskName, namespace, tb.TaskSpec( @@ -198,7 +199,7 @@ func getHelmDeployPipeline(namespace string) *v1alpha1.Pipeline { tb.PipelineTask("helm-deploy", helmDeployTaskName, tb.PipelineTaskInputResource("gitsource", "git-repo"), tb.PipelineTaskParam("pathToHelmCharts", "/workspace/gitsource/test/gohelloworld/gohelloworld-chart"), - tb.PipelineTaskParam("chartname", "${params.chartname}"), + tb.PipelineTaskParam("chartname", "gohelloworld"), tb.PipelineTaskParam("image", imageName), ), )) @@ -207,7 +208,6 @@ func getHelmDeployPipeline(namespace string) *v1alpha1.Pipeline { func getHelmDeployPipelineRun(namespace string) *v1alpha1.PipelineRun { return tb.PipelineRun(helmDeployPipelineRunName, namespace, tb.PipelineRunSpec( helmDeployPipelineName, - tb.PipelineRunParam("chartname", "gohelloworld"), tb.PipelineRunResourceBinding("git-repo", tb.PipelineResourceBindingRef(sourceResourceName)), )) } @@ -229,7 +229,7 @@ func setupClusterBindingForHelm(c *clients, t *testing.T, namespace string, logg clusterRoleBindings[0] = &rbacv1.ClusterRoleBinding{ ObjectMeta: metav1.ObjectMeta{ - Name: AppendRandomString("tiller"), + Name: names.SimpleNameGenerator.GenerateName("tiller"), }, RoleRef: rbacv1.RoleRef{ APIGroup: "rbac.authorization.k8s.io", @@ -245,7 +245,7 @@ func setupClusterBindingForHelm(c *clients, t *testing.T, namespace string, logg clusterRoleBindings[1] = &rbacv1.ClusterRoleBinding{ ObjectMeta: metav1.ObjectMeta{ - Name: AppendRandomString("default-tiller"), + Name: names.SimpleNameGenerator.GenerateName("default-tiller"), }, RoleRef: rbacv1.RoleRef{ APIGroup: "rbac.authorization.k8s.io", @@ -261,7 +261,7 @@ func setupClusterBindingForHelm(c *clients, t *testing.T, namespace string, logg clusterRoleBindings[2] = &rbacv1.ClusterRoleBinding{ ObjectMeta: metav1.ObjectMeta{ - Name: AppendRandomString("default-tiller"), + Name: names.SimpleNameGenerator.GenerateName("default-tiller"), }, RoleRef: rbacv1.RoleRef{ APIGroup: "rbac.authorization.k8s.io", diff --git a/test/init_test.go b/test/init_test.go index 96f2e78a684..2a1d8e70a96 100644 --- a/test/init_test.go +++ b/test/init_test.go @@ -27,6 +27,7 @@ import ( "github.com/ghodss/yaml" + "github.com/knative/build-pipeline/pkg/names" knativetest "github.com/knative/pkg/test" "github.com/knative/pkg/test/logging" corev1 "k8s.io/api/core/v1" @@ -52,7 +53,7 @@ func getContextLogger(n string) *logging.BaseLogger { func setup(t *testing.T, logger *logging.BaseLogger) (*clients, string) { t.Helper() - namespace := AppendRandomString("arendelle") + namespace := names.SimpleNameGenerator.GenerateName("arendelle") c := newClients(t, knativetest.Flags.Kubeconfig, knativetest.Flags.Cluster, namespace) createNamespace(namespace, logger, c.KubeClient) diff --git a/test/randstring_test.go b/test/names/generate.go similarity index 71% rename from test/randstring_test.go rename to test/names/generate.go index 4107191a476..7aef7f35a06 100644 --- a/test/randstring_test.go +++ b/test/names/generate.go @@ -11,16 +11,13 @@ See the License for the specific language governing permissions and limitations under the License. */ -package test +package names import ( - "testing" + utilrand "k8s.io/apimachinery/pkg/util/rand" ) -func TestAppendRandomString(t *testing.T) { - s1 := AppendRandomString("same") - s2 := AppendRandomString("same") - if s1 == s2 { - t.Errorf("expected random string. However they are equal %s, %s", s1, s2) - } +// TestingSeed used to set the random name generator to a fixed seed for testing +func TestingSeed() { + utilrand.Seed(12345) } diff --git a/test/pipelinerun_test.go b/test/pipelinerun_test.go index 3740075dca1..57af6cd7d9f 100644 --- a/test/pipelinerun_test.go +++ b/test/pipelinerun_test.go @@ -27,6 +27,7 @@ import ( "github.com/knative/build-pipeline/pkg/apis/pipeline" tb "github.com/knative/build-pipeline/test/builder" + "github.com/knative/build-pipeline/test/names" duckv1alpha1 "github.com/knative/pkg/apis/duck/v1alpha1" knativetest "github.com/knative/pkg/test" corev1 "k8s.io/api/core/v1" @@ -134,6 +135,9 @@ func TestPipelineRun(t *testing.T) { knativetest.CleanupOnInterrupt(func() { tearDown(t, logger, c, namespace) }, logger) defer tearDown(t, logger, c, namespace) + // to use fixed seed for testing name after the namespace is created + names.TestingSeed() + logger.Infof("Setting up test resources for %q test in namespace %s", td.name, namespace) td.testSetup(t, c, namespace, i) diff --git a/test/randstring.go b/test/randstring.go deleted file mode 100644 index 0e29ad2a9fb..00000000000 --- a/test/randstring.go +++ /dev/null @@ -1,63 +0,0 @@ -/* -Copyright 2018 Knative Authors LLC -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. -*/ - -/* -Generate random names - -You can use the function AppendRandomString to create random names for crds -or anything else, so that your tests can use unique names each time they run. -*/ -package test - -import ( - "math/rand" - "strings" - "sync" - "time" -) - -const ( - letterBytes = "abcdefghijklmnopqrstuvwxyz" - randSuffixLen = 8 -) - -// r is used by AppendRandomString to generate a random string. It is seeded with the time -// at import so the strings will be different between test runs. -var ( - r *rand.Rand - rndMutex *sync.Mutex -) - -// once is used to initialize r -var once sync.Once - -func initSeed() { - seed := time.Now().UTC().UnixNano() - r = rand.New(rand.NewSource(seed)) - rndMutex = &sync.Mutex{} -} - -// AppendRandomString will generate a random string that begins with prefix. This is useful -// if you want to make sure that your tests can run at the same time against the same -// environment without conflicting. This method will seed rand with the current time when -// called for the first time. -func AppendRandomString(prefix string) string { - once.Do(initSeed) - suffix := make([]byte, randSuffixLen) - rndMutex.Lock() - for i := range suffix { - suffix[i] = letterBytes[r.Intn(len(letterBytes))] - } - rndMutex.Unlock() - return strings.Join([]string{prefix, string(suffix)}, "-") -} diff --git a/test/timeout_test.go b/test/timeout_test.go index ea7eec61925..9c4e3a3de41 100644 --- a/test/timeout_test.go +++ b/test/timeout_test.go @@ -76,23 +76,6 @@ func TestPipelineRunTimeout(t *testing.T) { t.Fatalf("Error waiting for PipelineRun %s to be running: %s", pipelineRun.Name, err) } - trName := fmt.Sprintf("%s-%s", pipelineRun.Name, task.Spec.Steps[0].Name) - - logger.Infof("Waiting for TaskRun %s in namespace %s to be running", trName, namespace) - if err := WaitForTaskRunState(c, trName, func(tr *v1alpha1.TaskRun) (bool, error) { - c := tr.Status.GetCondition(duckv1alpha1.ConditionSucceeded) - if c != nil { - if c.Status == corev1.ConditionTrue || c.Status == corev1.ConditionFalse { - return true, fmt.Errorf("taskRun %s already finished!", trName) - } else if c.Status == corev1.ConditionUnknown && (c.Reason == "Running" || c.Reason == "Pending") { - return true, nil - } - } - return false, nil - }, "TaskRunRunning"); err != nil { - t.Fatalf("Error waiting for TaskRun %s to be running: %s", trName, err) - } - if _, err := c.PipelineRunClient.Get(pipelineRun.Name, metav1.GetOptions{}); err != nil { t.Fatalf("Failed to get PipelineRun `%s`: %s", pipelineRun.Name, err) } @@ -115,24 +98,6 @@ func TestPipelineRunTimeout(t *testing.T) { t.Errorf("Error waiting for PipelineRun %s to finish: %s", pipelineRun.Name, err) } - logger.Infof("Waiting for TaskRun %s in namespace %s to be cancelled", trName, namespace) - if err := WaitForTaskRunState(c, trName, func(tr *v1alpha1.TaskRun) (bool, error) { - cond := tr.Status.GetCondition(duckv1alpha1.ConditionSucceeded) - if cond != nil { - if cond.Status == corev1.ConditionFalse { - if cond.Reason == "TaskRunTimeout" { - return true, nil - } - return true, fmt.Errorf("taskRun %s completed with the wrong reason: %s", trName, cond.Reason) - } else if cond.Status == corev1.ConditionTrue { - return true, fmt.Errorf("taskRun %s completed successfully, should have been timed out", trName) - } - } - return false, nil - }, "TaskRunTimeout"); err != nil { - t.Errorf("Error waiting for TaskRun %s to finish: %s", trName, err) - } - // Verify that we can create a second Pipeline using the same Task without a Pipeline-level timeout that will not // time out secondPipeline := tb.Pipeline("peppers", namespace, diff --git a/vendor/k8s.io/apimachinery/pkg/util/rand/rand.go b/vendor/k8s.io/apimachinery/pkg/util/rand/rand.go new file mode 100644 index 00000000000..9421edae866 --- /dev/null +++ b/vendor/k8s.io/apimachinery/pkg/util/rand/rand.go @@ -0,0 +1,120 @@ +/* +Copyright 2015 The Kubernetes 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 rand provides utilities related to randomization. +package rand + +import ( + "math/rand" + "sync" + "time" +) + +var rng = struct { + sync.Mutex + rand *rand.Rand +}{ + rand: rand.New(rand.NewSource(time.Now().UTC().UnixNano())), +} + +// Intn generates an integer in range [0,max). +// By design this should panic if input is invalid, <= 0. +func Intn(max int) int { + rng.Lock() + defer rng.Unlock() + return rng.rand.Intn(max) +} + +// IntnRange generates an integer in range [min,max). +// By design this should panic if input is invalid, <= 0. +func IntnRange(min, max int) int { + rng.Lock() + defer rng.Unlock() + return rng.rand.Intn(max-min) + min +} + +// IntnRange generates an int64 integer in range [min,max). +// By design this should panic if input is invalid, <= 0. +func Int63nRange(min, max int64) int64 { + rng.Lock() + defer rng.Unlock() + return rng.rand.Int63n(max-min) + min +} + +// Seed seeds the rng with the provided seed. +func Seed(seed int64) { + rng.Lock() + defer rng.Unlock() + + rng.rand = rand.New(rand.NewSource(seed)) +} + +// Perm returns, as a slice of n ints, a pseudo-random permutation of the integers [0,n) +// from the default Source. +func Perm(n int) []int { + rng.Lock() + defer rng.Unlock() + return rng.rand.Perm(n) +} + +const ( + // We omit vowels from the set of available characters to reduce the chances + // of "bad words" being formed. + alphanums = "bcdfghjklmnpqrstvwxz2456789" + // No. of bits required to index into alphanums string. + alphanumsIdxBits = 5 + // Mask used to extract last alphanumsIdxBits of an int. + alphanumsIdxMask = 1<>= alphanumsIdxBits + remaining-- + } + return string(b) +} + +// SafeEncodeString encodes s using the same characters as rand.String. This reduces the chances of bad words and +// ensures that strings generated from hash functions appear consistent throughout the API. +func SafeEncodeString(s string) string { + r := make([]byte, len(s)) + for i, b := range []rune(s) { + r[i] = alphanums[(int(b) % len(alphanums))] + } + return string(r) +}