Skip to content

Commit

Permalink
Provide default bucket name if bucket is not set (#1855)
Browse files Browse the repository at this point in the history
(cherry picked from commit 7e08141)
  • Loading branch information
vishalbollu committed Feb 3, 2021
1 parent 44d8d3b commit c6d356b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 deletions.
25 changes: 21 additions & 4 deletions cli/cmd/cluster_gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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)
}

Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion cli/cmd/lib_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pkg/lib/gcp/gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/operator/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/types/clusterconfig/cluster_config_gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c6d356b

Please sign in to comment.