Skip to content

Commit

Permalink
Merge pull request #2694 from abhinavdahiya/fix_infra_id_repeating_da…
Browse files Browse the repository at this point in the history
…shes

Bug 1764812: pkg/destroy/gcp/bucket.go: join multipe dashes when looking for cluster bucket prefix
  • Loading branch information
openshift-merge-robot authored Dec 3, 2019
2 parents c82a585 + 8dc27fd commit fefa982
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
15 changes: 11 additions & 4 deletions pkg/asset/installconfig/clusterid.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package installconfig
import (
"fmt"
"regexp"
"strings"

"github.com/pborman/uuid"
utilrand "k8s.io/apimachinery/pkg/util/rand"
Expand Down Expand Up @@ -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))
}
8 changes: 6 additions & 2 deletions pkg/asset/installconfig/clusterid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions pkg/destroy/gcp/bucket.go
Original file line number Diff line number Diff line change
@@ -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)
}
Expand All @@ -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 {
Expand Down

0 comments on commit fefa982

Please sign in to comment.