-
Notifications
You must be signed in to change notification settings - Fork 1.5k
*: use a trimmed, unique cluster name for ELB names #1277
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| resource "aws_lb" "api_internal" { | ||
| name = "${var.cluster_name}-int" | ||
| name = "${var.unique_cluster_name}-int" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| load_balancer_type = "network" | ||
| subnets = ["${local.private_subnet_ids}"] | ||
| internal = true | ||
|
|
@@ -12,7 +12,7 @@ resource "aws_lb" "api_internal" { | |
| } | ||
|
|
||
| resource "aws_lb" "api_external" { | ||
| name = "${var.cluster_name}-ext" | ||
| name = "${var.unique_cluster_name}-ext" | ||
| load_balancer_type = "network" | ||
| subnets = ["${local.public_subnet_ids}"] | ||
| internal = false | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package installconfig | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| utilrand "k8s.io/apimachinery/pkg/util/rand" | ||
|
|
||
| "github.com/openshift/installer/pkg/asset" | ||
| ) | ||
|
|
||
| const ( | ||
| // AWS load balancers have a maximum name length of 32. The load balancers | ||
| // have suffixes of "-int" and "-ext", which are 4 characters. | ||
| maxNameLen = 32 - 4 | ||
| randomLen = 5 | ||
| maxBaseLen = maxNameLen - randomLen - 1 | ||
| ) | ||
|
|
||
| // UniqueClusterName is the unique name of the cluster. This combines the name of | ||
| // the cluster supplied by the user with random characters. | ||
| type UniqueClusterName struct { | ||
| ClusterName string | ||
| } | ||
|
|
||
| var _ asset.Asset = (*UniqueClusterName)(nil) | ||
|
|
||
| // Dependencies returns no dependencies. | ||
| func (a *UniqueClusterName) Dependencies() []asset.Asset { | ||
| return []asset.Asset{ | ||
| &InstallConfig{}, | ||
| } | ||
| } | ||
|
|
||
| // Generate generates a random, unique cluster name | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: missing a trailing period here and in some other godocs. |
||
| func (a *UniqueClusterName) Generate(parents asset.Parents) error { | ||
| ic := &InstallConfig{} | ||
| parents.Get(ic) | ||
|
|
||
| a.ClusterName = generateName(ic.Config.ObjectMeta.Name) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // Name returns the human-friendly name of the asset. | ||
| func (a *UniqueClusterName) Name() string { | ||
| return "Unique Cluster Name" | ||
| } | ||
|
|
||
| func generateName(base string) string { | ||
| if len(base) > maxBaseLen { | ||
| base = base[:maxBaseLen] | ||
| } | ||
| return fmt.Sprintf("%s-%s", base, utilrand.String(randomLen)) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package installconfig | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestGenerateName(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| base string | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "empty", | ||
| base: "", | ||
| expected: "^-[[:alnum:]]{5}$", | ||
| }, | ||
| { | ||
| name: "short", | ||
| base: "test-name", | ||
| expected: "^test-name-[[:alnum:]]{5}$", | ||
| }, | ||
| { | ||
| name: "long", | ||
| base: "012345678901234567890123456789", | ||
| expected: "^0123456789012345678901-[[:alnum:]]{5}$", | ||
| }, | ||
| } | ||
| for _, tc := range cases { | ||
| actual := generateName(tc.base) | ||
| assert.Regexp(t, tc.expected, actual) | ||
| assert.True(t, len(actual) <= maxNameLen) | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably include "The value will be at most 28 characters".