diff --git a/pkg/asset/installconfig/clusterid.go b/pkg/asset/installconfig/clusterid.go index 2b5e21f0177..a295d6e37c6 100644 --- a/pkg/asset/installconfig/clusterid.go +++ b/pkg/asset/installconfig/clusterid.go @@ -3,6 +3,7 @@ package installconfig import ( "fmt" "regexp" + "strings" "github.com/pborman/uuid" utilrand "k8s.io/apimachinery/pkg/util/rand" @@ -64,15 +65,21 @@ func (a *ClusterID) Name() string { // - only contains `alphanum` or `-` func generateInfraID(base string, maxLen int) string { maxBaseLen := maxLen - (randomLen + 1) - // truncate to maxBaseLen - if len(base) > maxBaseLen { - base = base[:maxBaseLen] - } // replace all characters that are not `alphanum` or `-` with `-` re := regexp.MustCompile("[^A-Za-z0-9-]") base = re.ReplaceAllString(base, "-") + // replace all multiple dashes in a sequence with single one. + re = regexp.MustCompile(`-{2,}`) + base = re.ReplaceAllString(base, "-") + + // truncate to maxBaseLen + if len(base) > maxBaseLen { + base = base[:maxBaseLen] + } + base = strings.TrimRight(base, "-") + // add random chars to the end to randomize return fmt.Sprintf("%s-%s", base, utilrand.String(randomLen)) } diff --git a/pkg/asset/installconfig/clusterid_test.go b/pkg/asset/installconfig/clusterid_test.go index 9c783f5d6cb..bf3fe945ce7 100644 --- a/pkg/asset/installconfig/clusterid_test.go +++ b/pkg/asset/installconfig/clusterid_test.go @@ -20,10 +20,14 @@ func Test_generateInfraID(t *testing.T) { input: "qwertyuiopasdfghjklzxcvbnm", expLen: 27, expNonRand: "qwertyuiopasdfghjklzx", + }, { + input: "qwertyuiopasdfghjklz-cvbnm", + expLen: 26, + expNonRand: "qwertyuiopasdfghjklz", }, { input: "qwe.rty.@iop!", - expLen: 13 + randomLen + 1, - expNonRand: "qwe-rty--iop-", + expLen: 11 + randomLen + 1, + expNonRand: "qwe-rty-iop", }} for _, test := range tests { t.Run("", func(t *testing.T) { diff --git a/pkg/destroy/gcp/bucket.go b/pkg/destroy/gcp/bucket.go index 6776247a18b..16518429a8f 100644 --- a/pkg/destroy/gcp/bucket.go +++ b/pkg/destroy/gcp/bucket.go @@ -1,12 +1,19 @@ package gcp import ( + "regexp" + "github.com/pkg/errors" "google.golang.org/api/googleapi" storage "google.golang.org/api/storage/v1" ) +var ( + // multiDashes is a regexp matching multiple dashes in a sequence. + multiDashes = regexp.MustCompile(`-{2,}`) +) + func (o *ClusterUninstaller) listBuckets() ([]cloudResource, error) { return o.listBucketsWithFilter("items(name),nextPageToken", o.ClusterID+"-", nil) } @@ -22,6 +29,7 @@ func (o *ClusterUninstaller) listBucketsWithFilter(fields string, prefix string, result := []cloudResource{} req := o.storageSvc.Buckets.List(o.ProjectID).Fields(googleapi.Field(fields)) if len(prefix) > 0 { + prefix = multiDashes.ReplaceAllString(prefix, "-") req = req.Prefix(prefix) } err := req.Pages(ctx, func(list *storage.Buckets) error {