From c6d356ba50530eec4d800c4c47f4b844b7ec3afd Mon Sep 17 00:00:00 2001 From: Vishal Bollu Date: Wed, 3 Feb 2021 09:41:34 -0500 Subject: [PATCH] Provide default bucket name if bucket is not set (#1855) (cherry picked from commit 7e081411ea5a04da3ddf6dce1329249d629a8b22) --- cli/cmd/cluster_gcp.go | 25 ++++++++++++++++--- cli/cmd/lib_manager.go | 2 +- pkg/lib/gcp/gcs.go | 4 +-- pkg/operator/config/config.go | 2 +- pkg/types/clusterconfig/cluster_config_gcp.go | 4 +++ 5 files changed, 29 insertions(+), 8 deletions(-) diff --git a/cli/cmd/cluster_gcp.go b/cli/cmd/cluster_gcp.go index f2f3d53928..a70c20a955 100644 --- a/cli/cmd/cluster_gcp.go +++ b/cli/cmd/cluster_gcp.go @@ -141,7 +141,6 @@ var _clusterGCPUpCmd = &cobra.Command{ } gkeClusterName := fmt.Sprintf("projects/%s/locations/%s/clusters/%s", *clusterConfig.Project, *clusterConfig.Zone, clusterConfig.ClusterName) - bucketName := clusterconfig.GCPBucketName(clusterConfig.ClusterName, *clusterConfig.Project, *clusterConfig.Zone) clusterExists, err := gcpClient.ClusterExists(gkeClusterName) if err != nil { @@ -151,7 +150,7 @@ var _clusterGCPUpCmd = &cobra.Command{ exit.Error(ErrorGCPClusterAlreadyExists(clusterConfig.ClusterName, *clusterConfig.Zone, *clusterConfig.Project)) } - err = gcpClient.CreateBucket(bucketName, gcp.ZoneToRegion(*accessConfig.Zone), true) + err = createGSBucketIfNotFound(gcpClient, clusterConfig.Bucket, gcp.ZoneToRegion(*accessConfig.Zone)) if err != nil { exit.Error(err) } @@ -161,9 +160,8 @@ var _clusterGCPUpCmd = &cobra.Command{ exit.Error(err) } - _, _, err = runGCPManagerWithClusterConfig("/root/install.sh", clusterConfig, bucketName, nil, nil) + _, _, err = runGCPManagerWithClusterConfig("/root/install.sh", clusterConfig, nil, nil) if err != nil { - gcpClient.DeleteBucket(bucketName) exit.Error(err) } @@ -596,3 +594,22 @@ func getGCPOperatorLoadBalancerIP(clusterName string, gcpClient *gcp.Client) (st return service.Status.LoadBalancer.Ingress[0].IP, nil } + +func createGSBucketIfNotFound(gcpClient *gcp.Client, bucket string, location string) error { + bucketFound, err := gcpClient.DoesBucketExist(bucket) + if err != nil { + return err + } + if !bucketFound { + fmt.Print("○ creating a new gs bucket: ", bucket) + err = gcpClient.CreateBucket(bucket, location, false) + if err != nil { + fmt.Print("\n\n") + return err + } + } else { + fmt.Print("○ using existing gs bucket: ", bucket) + } + fmt.Println(" ✓") + return nil +} diff --git a/cli/cmd/lib_manager.go b/cli/cmd/lib_manager.go index 34c14c53cf..3a2bcdd36e 100644 --- a/cli/cmd/lib_manager.go +++ b/cli/cmd/lib_manager.go @@ -212,7 +212,7 @@ func runManagerWithClusterConfig(entrypoint string, clusterConfig *clusterconfig return output, exitCode, nil } -func runGCPManagerWithClusterConfig(entrypoint string, clusterConfig *clusterconfig.GCPConfig, bucketName string, copyToPaths []dockerCopyToPath, copyFromPaths []dockerCopyFromPath) (string, *int, error) { +func runGCPManagerWithClusterConfig(entrypoint string, clusterConfig *clusterconfig.GCPConfig, copyToPaths []dockerCopyToPath, copyFromPaths []dockerCopyFromPath) (string, *int, error) { clusterConfigBytes, err := yaml.Marshal(clusterConfig) if err != nil { return "", nil, errors.WithStack(err) diff --git a/pkg/lib/gcp/gcs.go b/pkg/lib/gcp/gcs.go index ae0034f43f..45bb3283b6 100644 --- a/pkg/lib/gcp/gcs.go +++ b/pkg/lib/gcp/gcs.go @@ -63,7 +63,7 @@ func IsValidGCSPath(gcsPath string) bool { return true } -func (c *Client) DoesBucketExist(bucket string, location string) (bool, error) { +func (c *Client) DoesBucketExist(bucket string) (bool, error) { gcsClient, err := c.GCS() if err != nil { return false, err @@ -74,7 +74,7 @@ func (c *Client) DoesBucketExist(bucket string, location string) (bool, error) { if IsBucketDoesNotExist(err) { return false, nil } - return false, errors.WithStack(err) + return false, errors.Wrap(err, "bucket", bucket) } return true, nil } diff --git a/pkg/operator/config/config.go b/pkg/operator/config/config.go index 31dd368dfe..c1dee22565 100644 --- a/pkg/operator/config/config.go +++ b/pkg/operator/config/config.go @@ -179,7 +179,7 @@ func Init() error { istioNamespace = GCPCoreConfig.IstioNamespace // If the bucket is specified double check that it exists and the operator has access to it - exists, err := GCP.DoesBucketExist(GCPCoreConfig.Bucket, gcp.ZoneToRegion(*GCPCoreConfig.Zone)) + exists, err := GCP.DoesBucketExist(GCPCoreConfig.Bucket) if err != nil { return err } diff --git a/pkg/types/clusterconfig/cluster_config_gcp.go b/pkg/types/clusterconfig/cluster_config_gcp.go index 3302bd96d8..6348d7260e 100644 --- a/pkg/types/clusterconfig/cluster_config_gcp.go +++ b/pkg/types/clusterconfig/cluster_config_gcp.go @@ -448,6 +448,10 @@ func (cc *GCPConfig) Validate(GCP *gcp.Client) error { return ErrorGCPInvalidZone(*cc.Zone, availableZones...) } + if cc.Bucket == "" { + cc.Bucket = GCPBucketName(cc.ClusterName, *cc.Project, *cc.Zone) + } + if validInstanceType, err := GCP.IsInstanceTypeAvailable(*cc.InstanceType, *cc.Zone); err != nil { return err } else if !validInstanceType {