-
Notifications
You must be signed in to change notification settings - Fork 4.7k
kubetest2-kops: drop gsutil for GCS bucket lifecycle #17699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ameukam
wants to merge
2
commits into
kubernetes:master
Choose a base branch
from
ameukam:kubetest2-kops-stop-use-gsutil
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+79
−53
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,16 +17,24 @@ limitations under the License. | |
| package gce | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/rand" | ||
| "encoding/hex" | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "cloud.google.com/go/iam" | ||
| "cloud.google.com/go/storage" | ||
| "k8s.io/klog/v2" | ||
| "sigs.k8s.io/kubetest2/pkg/exec" | ||
| ) | ||
|
|
||
| const ( | ||
| defaultRegion = "us-central1" | ||
| ) | ||
|
|
||
| func GCSBucketName(projectID, prefix string) string { | ||
| var s string | ||
| if jobID := os.Getenv("PROW_JOB_ID"); len(jobID) >= 4 { | ||
|
|
@@ -41,57 +49,69 @@ func GCSBucketName(projectID, prefix string) string { | |
| } | ||
|
|
||
| func EnsureGCSBucket(bucketPath, projectID string, public bool) error { | ||
| lsArgs := []string{ | ||
| "gsutil", "ls", "-b", | ||
| } | ||
| if projectID != "" { | ||
| lsArgs = append(lsArgs, "-p", projectID) | ||
| // TODO: Detect the GCP region used | ||
| return EnsureGCSBucketWithRegion(bucketPath, projectID, defaultRegion, public) | ||
| } | ||
|
|
||
| func EnsureGCSBucketWithRegion(bucketPath, projectID string, region string, public bool) error { | ||
| ctx := context.Background() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: if I'm adding arguments I would probably add |
||
| client, err := storage.NewClient(ctx) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create storage client: %w", err) | ||
| } | ||
| lsArgs = append(lsArgs, bucketPath) | ||
| defer client.Close() | ||
|
|
||
| // Extract bucket name from gs:// path if the bucket's URI is provided | ||
| bucketName := strings.TrimPrefix(bucketPath, "gs://") | ||
| bucketName = strings.TrimSuffix(bucketName, "/") | ||
|
|
||
| klog.Info(strings.Join(lsArgs, " ")) | ||
| cmd := exec.Command(lsArgs[0], lsArgs[1:]...) | ||
| bucket := client.Bucket(bucketName) | ||
|
|
||
| output, err := exec.CombinedOutputLines(cmd) | ||
| // Check if bucket exists | ||
| klog.Infof("Checking if bucket %s exists", bucketName) | ||
| _, err = bucket.Attrs(ctx) | ||
| if err == nil { | ||
| klog.Infof("Bucket %s already exists", bucketName) | ||
| return nil | ||
| } else if len(output) != 1 || !strings.Contains(output[0], "BucketNotFound") { | ||
| klog.Info(output) | ||
| return err | ||
| } | ||
|
|
||
| mbArgs := []string{ | ||
| "gsutil", "mb", | ||
| } | ||
| if projectID != "" { | ||
| mbArgs = append(mbArgs, "-p", projectID) | ||
| // If error is not "bucket doesn't exist", return error | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| if errors.Is(err, storage.ErrBucketNotExist) { | ||
| return fmt.Errorf("error checking bucket: %w", err) | ||
| } | ||
| mbArgs = append(mbArgs, bucketPath) | ||
|
|
||
| klog.Info(strings.Join(mbArgs, " ")) | ||
| cmd = exec.Command(mbArgs[0], mbArgs[1:]...) | ||
| // Create the bucket | ||
| klog.Infof("Creating bucket %s in project %s", bucketName, projectID) | ||
| bucketAttrs := &storage.BucketAttrs{ | ||
| Location: region, | ||
| LocationType: "region", | ||
| UniformBucketLevelAccess: storage.UniformBucketLevelAccess{ | ||
| Enabled: true, | ||
| }, | ||
| SoftDeletePolicy: &storage.SoftDeletePolicy{ | ||
| RetentionDuration: 0, | ||
| }, | ||
| } | ||
|
|
||
| exec.InheritOutput(cmd) | ||
| err = cmd.Run() | ||
| if err != nil { | ||
| return err | ||
| if err := bucket.Create(ctx, projectID, bucketAttrs); err != nil { | ||
| return fmt.Errorf("failed to create bucket: %w", err) | ||
| } | ||
|
|
||
| if public { | ||
| iamArgs := []string{ | ||
| "gsutil", "iam", "ch", "allUsers:objectViewer", | ||
| } | ||
| iamArgs = append(iamArgs, bucketPath) | ||
| klog.Info(strings.Join(iamArgs, " ")) | ||
| // GCS APIs are strongly consistent but this should help with flakes | ||
| time.Sleep(10 * time.Second) | ||
| cmd = exec.Command(iamArgs[0], iamArgs[1:]...) | ||
| exec.InheritOutput(cmd) | ||
| err = cmd.Run() | ||
| klog.Infof("Making bucket %s public", bucketName) | ||
| policy, err := bucket.IAM().Policy(ctx) | ||
| if err != nil { | ||
| return err | ||
| return fmt.Errorf("failed to get bucket IAM policy: %w", err) | ||
| } | ||
|
|
||
| // Add allUsers as objectViewer | ||
| policy.Add(iam.AllUsers, "roles/storage.objectViewer") | ||
|
|
||
| if err := bucket.IAM().SetPolicy(ctx, policy); err != nil { | ||
| return fmt.Errorf("failed to set bucket IAM policy: %w", err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: mark
EnsureGCSBucketas deprecated, if you don't want to just addregionas an argument to EnsureGCSBucket