Skip to content

Commit

Permalink
Streamline stack naming (#145)
Browse files Browse the repository at this point in the history
* Streamline stack naming

This cleans up the stack nameing code a bit and introduces a new naming
format that should work in all cases:

```
ingress-alb-<clusterID>-<UUID>
```

This naming convention will not affect existing ALB stacks, it's only
applied to new stacks.

Fix #144

* Use kube-ingress-aws-controller as stack name prefix

* Add simple tests for normalizeStackName
  • Loading branch information
mikkeloscar authored Apr 9, 2018
1 parent 83b5e37 commit 64b2721
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
20 changes: 10 additions & 10 deletions aws/naming.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@ import (
)

const (
shortHashLen = 7

maxLoadBalancerNameLen = 32
maxStackNameLen = 128

nameSeparator = "-"
maxStackNameLen = 128
uuidLen = 36
nameSeparator = "-"
stackNamePrefix = "kube-ingress-aws-controller"
)

var (
normalizationRegex = regexp.MustCompile("[^A-Za-z0-9-]+")
squeezeDashesRegex = regexp.MustCompile("[-]{2,}")
)

// normalizeStackName normalizes the stackName by normalizing the clusterID and
// adding a uuid suffix.
// normalizeStackName normalizes the stackName by normalizing the clusterID,
// adding a stack name prefix and a uuid suffix.
func normalizeStackName(clusterID string) string {
normalizedClusterID := squeezeDashesRegex.ReplaceAllString(
normalizationRegex.ReplaceAllString(clusterID, nameSeparator), nameSeparator)
lenClusterID := len(normalizedClusterID)
maxClusterIDLen := maxStackNameLen - shortHashLen - 1
// max cluser ID length is the max stack name length except stack name
// prefix, UUID and two separators.
maxClusterIDLen := maxStackNameLen - len(stackNamePrefix) - uuidLen - 2
if lenClusterID > maxClusterIDLen {
normalizedClusterID = normalizedClusterID[lenClusterID-maxClusterIDLen:]
}
normalizedClusterID = strings.Trim(normalizedClusterID, nameSeparator) // trim leading/trailing separators

return fmt.Sprintf("%s%s%s", normalizedClusterID, nameSeparator, uuid.New().String())
return fmt.Sprintf("%s%s%s%s%s", stackNamePrefix, nameSeparator, normalizedClusterID, nameSeparator, uuid.New().String())
}
25 changes: 25 additions & 0 deletions aws/naming_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package aws

import (
"strings"
"testing"
)

func TestNormalizeStackName(t *testing.T) {
// test simple cluster ID
clusterID := "my-cluster"
normalized := normalizeStackName(clusterID)
expectedPrefix := stackNamePrefix + nameSeparator + clusterID + nameSeparator
if !strings.HasPrefix(normalized, expectedPrefix) {
t.Errorf("expected prefix %s, got %s", expectedPrefix, normalized)
}

// test that very long cluster ID gets cut off
longClusterID := strings.Repeat("a", maxStackNameLen)
normalized = normalizeStackName(longClusterID)
expectedClusterID := strings.Repeat("a", maxStackNameLen-len(stackNamePrefix)-uuidLen-2)
expectedPrefix = stackNamePrefix + nameSeparator + expectedClusterID + nameSeparator
if !strings.HasPrefix(normalized, expectedPrefix) {
t.Errorf("expected prefix %s, got %s", expectedPrefix, normalized)
}
}

0 comments on commit 64b2721

Please sign in to comment.