github.com/jenkins-x/jx/v2@v2.1.155/pkg/cloud/gke/storage/long_term_storage.go (about) 1 package storage 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/google/uuid" 8 "github.com/jenkins-x/jx-logging/pkg/log" 9 "github.com/jenkins-x/jx/v2/pkg/cloud/gke" 10 "github.com/jenkins-x/jx/v2/pkg/kube" 11 "github.com/jenkins-x/jx/v2/pkg/util" 12 "github.com/pkg/errors" 13 ) 14 15 // EnableLongTermStorage will take the cluster install values and a provided bucket name and use it / create a new one for gs 16 func EnableLongTermStorage(gcloud gke.GClouder, installValues map[string]string, providedBucketName string) (string, error) { 17 if providedBucketName != "" { 18 log.Logger().Infof(util.QuestionAnswer("Configured to use long term storage bucket", providedBucketName)) 19 return ensureProvidedBucketExists(gcloud, installValues, providedBucketName) 20 } else { 21 log.Logger().Info("No bucket name provided for long term storage, creating a new one") 22 bucketName, installValues := createUniqueBucketName(installValues) 23 return createBucket(gcloud, bucketName, installValues) 24 } 25 } 26 27 func ensureProvidedBucketExists(gcloud gke.GClouder, installValues map[string]string, providedBucketName string) (string, error) { 28 exists, err := gcloud.BucketExists(installValues[kube.ProjectID], providedBucketName) 29 if err != nil { 30 return "", errors.Wrap(err, "checking if the provided bucket exists") 31 } 32 if exists { 33 return fmt.Sprintf("gs://%s", providedBucketName), nil 34 } 35 36 bucketURL, err := createBucket(gcloud, providedBucketName, installValues) 37 if err == nil { 38 return bucketURL, nil 39 } 40 log.Logger().Warnf("Attempted to create the bucket %s in the project %s but failed, will now create a "+ 41 "random bucket", providedBucketName, installValues[kube.ProjectID]) 42 43 bucketName, installValues := createUniqueBucketName(installValues) 44 return createBucket(gcloud, bucketName, installValues) 45 } 46 47 func createUniqueBucketName(installValues map[string]string) (string, map[string]string) { 48 clusterName := installValues[kube.ClusterName] 49 bucketName := createUniqueBucketNameForCluster(clusterName) 50 return bucketName, installValues 51 } 52 53 func createUniqueBucketNameForCluster(clusterName string) string { 54 uuid4 := uuid.New() 55 bucketName := fmt.Sprintf("%s-lts-%s", clusterName, uuid4.String()) 56 if len(bucketName) > 60 { 57 bucketName = bucketName[:60] 58 } 59 if strings.HasSuffix(bucketName, "-") { 60 bucketName = bucketName[:59] 61 } 62 return bucketName 63 } 64 65 func createBucket(gcloud gke.GClouder, bucketName string, installValues map[string]string) (string, error) { 66 bucketURL := fmt.Sprintf("gs://%s", bucketName) 67 infoBucketURL := util.ColorInfo(bucketURL) 68 log.Logger().Infof("The bucket %s does not exist so lets create it", infoBucketURL) 69 region := gke.GetRegionFromZone(installValues[kube.Zone]) 70 err := gcloud.CreateBucket(installValues[kube.ProjectID], bucketName, region) 71 gcloud.AddBucketLabel(bucketName, gcloud.UserLabel()) 72 if err != nil { 73 return "", errors.Wrapf(err, "there was a problem creating the bucket %s in the GKE Project %s", 74 bucketName, installValues[kube.ProjectID]) 75 } 76 return bucketURL, err 77 }