Skip to content

Commit 34a602e

Browse files
committed
create a dedicated staging bucket for kops builds
1 parent d630f9c commit 34a602e

File tree

5 files changed

+50
-12
lines changed

5 files changed

+50
-12
lines changed

tests/e2e/kubetest2-kops/deployer/build.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"strings"
2525

2626
"k8s.io/klog/v2"
27+
"k8s.io/kops/tests/e2e/kubetest2-kops/gce"
2728
"k8s.io/kops/tests/e2e/pkg/util"
2829
"sigs.k8s.io/kubetest2/pkg/exec"
2930
)
@@ -66,6 +67,12 @@ func (d *deployer) verifyBuildFlags() error {
6667
if !strings.HasPrefix(d.StageLocation, "gs://") {
6768
return errors.New("stage-location must be a gs:// path")
6869
}
70+
} else if d.boskos != nil {
71+
d.StageLocation = d.stagingStore()
72+
klog.Infof("creating staging bucket %s to hold kops build artifacts", d.stagingStore())
73+
if err := gce.EnsureGCSBucket(d.StageLocation, d.GCPProject, true); err != nil {
74+
return err
75+
}
6976
} else {
7077
stageLocation, err := defaultStageLocation(d.KopsRoot)
7178
if err != nil {

tests/e2e/kubetest2-kops/deployer/common.go

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ func (d *deployer) init() error {
4141

4242
// initialize should only be called by init(), behind a sync.Once
4343
func (d *deployer) initialize() error {
44-
if d.commonOptions.ShouldBuild() {
45-
if err := d.verifyBuildFlags(); err != nil {
46-
return fmt.Errorf("init failed to check build flags: %v", err)
47-
}
48-
}
4944
if d.commonOptions.ShouldUp() || d.commonOptions.ShouldDown() {
5045
if err := d.verifyKopsFlags(); err != nil {
5146
return fmt.Errorf("init failed to check kops flags: %v", err)
@@ -118,6 +113,12 @@ func (d *deployer) initialize() error {
118113
}
119114
}
120115

116+
if d.commonOptions.ShouldBuild() {
117+
if err := d.verifyBuildFlags(); err != nil {
118+
return fmt.Errorf("init failed to check build flags: %v", err)
119+
}
120+
}
121+
121122
if d.SSHUser == "" {
122123
d.SSHUser = os.Getenv("KUBE_SSH_USER")
123124
}
@@ -272,14 +273,26 @@ func (d *deployer) stateStore() string {
272273
ss = "s3://k8s-kops-prow"
273274
case "gce":
274275
d.createBucket = true
275-
ss = "gs://" + gce.GCSBucketName(d.GCPProject)
276+
ss = "gs://" + gce.GCSBucketName(d.GCPProject, "state")
276277
case "digitalocean":
277278
ss = "do://e2e-kops-space"
278279
}
279280
}
280281
return ss
281282
}
282283

284+
func (d *deployer) stagingStore() string {
285+
sb := os.Getenv("KOPS_STAGING_BUCKET")
286+
if sb == "" {
287+
switch d.CloudProvider {
288+
case "gce":
289+
d.createBucket = true
290+
sb = "gs://" + gce.GCSBucketName(d.GCPProject, "staging")
291+
}
292+
}
293+
return sb
294+
}
295+
283296
// the default is $ARTIFACTS if set, otherwise ./_artifacts
284297
// constructed as an absolute path to help the ginkgo tester because
285298
// for some reason it needs an absolute path to the kubeconfig

tests/e2e/kubetest2-kops/deployer/down.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func (d *deployer) Down() error {
5656

5757
if d.CloudProvider == "gce" && d.createBucket {
5858
gce.DeleteGCSBucket(d.stateStore(), d.GCPProject)
59+
gce.DeleteGCSBucket(d.stagingStore(), d.GCPProject)
5960
}
6061

6162
if d.boskos != nil {

tests/e2e/kubetest2-kops/deployer/up.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (d *deployer) Up() error {
4949
}
5050

5151
if d.CloudProvider == "gce" && d.createBucket {
52-
if err := gce.EnsureGCSBucket(d.stateStore(), d.GCPProject); err != nil {
52+
if err := gce.EnsureGCSBucket(d.stateStore(), d.GCPProject, false); err != nil {
5353
return err
5454
}
5555
}

tests/e2e/kubetest2-kops/gce/gcs.go

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,26 @@ import (
2121
"encoding/hex"
2222
"os"
2323
"strings"
24+
"time"
2425

2526
"k8s.io/klog/v2"
2627
"sigs.k8s.io/kubetest2/pkg/exec"
2728
)
2829

29-
func GCSBucketName(projectID string) string {
30+
func GCSBucketName(projectID, prefix string) string {
3031
var s string
31-
if jobID := os.Getenv("PROW_JOB_ID"); len(jobID) >= 2 {
32-
s = jobID[:2]
32+
if jobID := os.Getenv("PROW_JOB_ID"); len(jobID) >= 4 {
33+
s = jobID[:4]
3334
} else {
3435
b := make([]byte, 2)
3536
rand.Read(b)
3637
s = hex.EncodeToString(b)
3738
}
38-
bucket := strings.Join([]string{projectID, "state", s}, "-")
39+
bucket := strings.Join([]string{projectID, prefix, s}, "-")
3940
return bucket
4041
}
4142

42-
func EnsureGCSBucket(bucketPath, projectID string) error {
43+
func EnsureGCSBucket(bucketPath, projectID string, public bool) error {
4344
lsArgs := []string{
4445
"gsutil", "ls", "-b",
4546
}
@@ -75,6 +76,22 @@ func EnsureGCSBucket(bucketPath, projectID string) error {
7576
if err != nil {
7677
return err
7778
}
79+
80+
if public {
81+
iamArgs := []string{
82+
"gsutil", "iam", "ch", "allUsers:objectViewer",
83+
}
84+
iamArgs = append(iamArgs, bucketPath)
85+
klog.Info(strings.Join(iamArgs, " "))
86+
// GCS APIs are strongly consistent but this should help with flakes
87+
time.Sleep(10 * time.Second)
88+
cmd = exec.Command(iamArgs[0], iamArgs[1:]...)
89+
exec.InheritOutput(cmd)
90+
err = cmd.Run()
91+
if err != nil {
92+
return err
93+
}
94+
}
7895
return nil
7996
}
8097

0 commit comments

Comments
 (0)