Skip to content

Commit c6d356b

Browse files
committed
Provide default bucket name if bucket is not set (#1855)
(cherry picked from commit 7e08141)
1 parent 44d8d3b commit c6d356b

File tree

5 files changed

+29
-8
lines changed

5 files changed

+29
-8
lines changed

cli/cmd/cluster_gcp.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ var _clusterGCPUpCmd = &cobra.Command{
141141
}
142142

143143
gkeClusterName := fmt.Sprintf("projects/%s/locations/%s/clusters/%s", *clusterConfig.Project, *clusterConfig.Zone, clusterConfig.ClusterName)
144-
bucketName := clusterconfig.GCPBucketName(clusterConfig.ClusterName, *clusterConfig.Project, *clusterConfig.Zone)
145144

146145
clusterExists, err := gcpClient.ClusterExists(gkeClusterName)
147146
if err != nil {
@@ -151,7 +150,7 @@ var _clusterGCPUpCmd = &cobra.Command{
151150
exit.Error(ErrorGCPClusterAlreadyExists(clusterConfig.ClusterName, *clusterConfig.Zone, *clusterConfig.Project))
152151
}
153152

154-
err = gcpClient.CreateBucket(bucketName, gcp.ZoneToRegion(*accessConfig.Zone), true)
153+
err = createGSBucketIfNotFound(gcpClient, clusterConfig.Bucket, gcp.ZoneToRegion(*accessConfig.Zone))
155154
if err != nil {
156155
exit.Error(err)
157156
}
@@ -161,9 +160,8 @@ var _clusterGCPUpCmd = &cobra.Command{
161160
exit.Error(err)
162161
}
163162

164-
_, _, err = runGCPManagerWithClusterConfig("/root/install.sh", clusterConfig, bucketName, nil, nil)
163+
_, _, err = runGCPManagerWithClusterConfig("/root/install.sh", clusterConfig, nil, nil)
165164
if err != nil {
166-
gcpClient.DeleteBucket(bucketName)
167165
exit.Error(err)
168166
}
169167

@@ -596,3 +594,22 @@ func getGCPOperatorLoadBalancerIP(clusterName string, gcpClient *gcp.Client) (st
596594

597595
return service.Status.LoadBalancer.Ingress[0].IP, nil
598596
}
597+
598+
func createGSBucketIfNotFound(gcpClient *gcp.Client, bucket string, location string) error {
599+
bucketFound, err := gcpClient.DoesBucketExist(bucket)
600+
if err != nil {
601+
return err
602+
}
603+
if !bucketFound {
604+
fmt.Print("○ creating a new gs bucket: ", bucket)
605+
err = gcpClient.CreateBucket(bucket, location, false)
606+
if err != nil {
607+
fmt.Print("\n\n")
608+
return err
609+
}
610+
} else {
611+
fmt.Print("○ using existing gs bucket: ", bucket)
612+
}
613+
fmt.Println(" ✓")
614+
return nil
615+
}

cli/cmd/lib_manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func runManagerWithClusterConfig(entrypoint string, clusterConfig *clusterconfig
212212
return output, exitCode, nil
213213
}
214214

215-
func runGCPManagerWithClusterConfig(entrypoint string, clusterConfig *clusterconfig.GCPConfig, bucketName string, copyToPaths []dockerCopyToPath, copyFromPaths []dockerCopyFromPath) (string, *int, error) {
215+
func runGCPManagerWithClusterConfig(entrypoint string, clusterConfig *clusterconfig.GCPConfig, copyToPaths []dockerCopyToPath, copyFromPaths []dockerCopyFromPath) (string, *int, error) {
216216
clusterConfigBytes, err := yaml.Marshal(clusterConfig)
217217
if err != nil {
218218
return "", nil, errors.WithStack(err)

pkg/lib/gcp/gcs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func IsValidGCSPath(gcsPath string) bool {
6363
return true
6464
}
6565

66-
func (c *Client) DoesBucketExist(bucket string, location string) (bool, error) {
66+
func (c *Client) DoesBucketExist(bucket string) (bool, error) {
6767
gcsClient, err := c.GCS()
6868
if err != nil {
6969
return false, err
@@ -74,7 +74,7 @@ func (c *Client) DoesBucketExist(bucket string, location string) (bool, error) {
7474
if IsBucketDoesNotExist(err) {
7575
return false, nil
7676
}
77-
return false, errors.WithStack(err)
77+
return false, errors.Wrap(err, "bucket", bucket)
7878
}
7979
return true, nil
8080
}

pkg/operator/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func Init() error {
179179
istioNamespace = GCPCoreConfig.IstioNamespace
180180

181181
// If the bucket is specified double check that it exists and the operator has access to it
182-
exists, err := GCP.DoesBucketExist(GCPCoreConfig.Bucket, gcp.ZoneToRegion(*GCPCoreConfig.Zone))
182+
exists, err := GCP.DoesBucketExist(GCPCoreConfig.Bucket)
183183
if err != nil {
184184
return err
185185
}

pkg/types/clusterconfig/cluster_config_gcp.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,10 @@ func (cc *GCPConfig) Validate(GCP *gcp.Client) error {
448448
return ErrorGCPInvalidZone(*cc.Zone, availableZones...)
449449
}
450450

451+
if cc.Bucket == "" {
452+
cc.Bucket = GCPBucketName(cc.ClusterName, *cc.Project, *cc.Zone)
453+
}
454+
451455
if validInstanceType, err := GCP.IsInstanceTypeAvailable(*cc.InstanceType, *cc.Zone); err != nil {
452456
return err
453457
} else if !validInstanceType {

0 commit comments

Comments
 (0)