Skip to content

Commit 4c5b811

Browse files
authored
Merge pull request #4948 from r4f4/target-group-name-fix
🐛 Fix Target Group's name exceeding 32 characters
2 parents 63c6635 + 077734c commit 4c5b811

File tree

5 files changed

+9
-73
lines changed

5 files changed

+9
-73
lines changed

api/v1beta2/network_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ var (
223223
// This is created first, and the ARN is then passed to the listener.
224224
type TargetGroupSpec struct {
225225
// Name of the TargetGroup. Must be unique over the same group of listeners.
226+
// +kubebuilder:validation:MaxLength=32
226227
Name string `json:"name"`
227228
// Port is the exposed port
228229
Port int64 `json:"port"`

config/crd/bases/controlplane.cluster.x-k8s.io_awsmanagedcontrolplanes.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,7 @@ spec:
14671467
name:
14681468
description: Name of the TargetGroup. Must be unique
14691469
over the same group of listeners.
1470+
maxLength: 32
14701471
type: string
14711472
port:
14721473
description: Port is the exposed port
@@ -1686,6 +1687,7 @@ spec:
16861687
name:
16871688
description: Name of the TargetGroup. Must be unique
16881689
over the same group of listeners.
1690+
maxLength: 32
16891691
type: string
16901692
port:
16911693
description: Port is the exposed port
@@ -3415,6 +3417,7 @@ spec:
34153417
name:
34163418
description: Name of the TargetGroup. Must be unique
34173419
over the same group of listeners.
3420+
maxLength: 32
34183421
type: string
34193422
port:
34203423
description: Port is the exposed port
@@ -3634,6 +3637,7 @@ spec:
36343637
name:
36353638
description: Name of the TargetGroup. Must be unique
36363639
over the same group of listeners.
3640+
maxLength: 32
36373641
type: string
36383642
port:
36393643
description: Port is the exposed port

config/crd/bases/infrastructure.cluster.x-k8s.io_awsclusters.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2413,6 +2413,7 @@ spec:
24132413
name:
24142414
description: Name of the TargetGroup. Must be unique
24152415
over the same group of listeners.
2416+
maxLength: 32
24162417
type: string
24172418
port:
24182419
description: Port is the exposed port
@@ -2632,6 +2633,7 @@ spec:
26322633
name:
26332634
description: Name of the TargetGroup. Must be unique
26342635
over the same group of listeners.
2636+
maxLength: 32
26352637
type: string
26362638
port:
26372639
description: Port is the exposed port

pkg/cloud/services/elb/loadbalancer.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -239,18 +239,6 @@ func (s *Service) getAdditionalTargetGroupHealthCheck(ln infrav1.AdditionalListe
239239
return healthCheck
240240
}
241241

242-
// getTargetGroupName creates the target group name based on LB Name, when defined, otherwise return
243-
// the standard name created from the timestamp.
244-
func (s *Service) getTargetGroupName(lbSpec *infrav1.AWSLoadBalancerSpec, defaultPrefix string, port int64) string {
245-
targetName := fmt.Sprintf("%s-%d", defaultPrefix, time.Now().Unix())
246-
247-
if lbSpec != nil && lbSpec.Name != nil {
248-
targetName = fmt.Sprintf("%s-%d", *lbSpec.Name, port)
249-
}
250-
251-
return targetName
252-
}
253-
254242
func (s *Service) getAPIServerLBSpec(elbName string, lbSpec *infrav1.AWSLoadBalancerSpec) (*infrav1.LoadBalancer, error) {
255243
var securityGroupIDs []string
256244
if lbSpec != nil {
@@ -266,7 +254,6 @@ func (s *Service) getAPIServerLBSpec(elbName string, lbSpec *infrav1.AWSLoadBala
266254

267255
// The default API health check is TCP, allowing customization to HTTP or HTTPS when HealthCheckProtocol is set.
268256
apiHealthCheck := s.getAPITargetGroupHealthCheck(lbSpec)
269-
apiTargetGroupName := s.getTargetGroupName(lbSpec, "apiserver-target", infrav1.DefaultAPIServerPort)
270257
res := &infrav1.LoadBalancer{
271258
Name: elbName,
272259
Scheme: scheme,
@@ -276,7 +263,7 @@ func (s *Service) getAPIServerLBSpec(elbName string, lbSpec *infrav1.AWSLoadBala
276263
Protocol: infrav1.ELBProtocolTCP,
277264
Port: infrav1.DefaultAPIServerPort,
278265
TargetGroup: infrav1.TargetGroupSpec{
279-
Name: apiTargetGroupName,
266+
Name: fmt.Sprintf("apiserver-target-%d", time.Now().Unix()),
280267
Port: infrav1.DefaultAPIServerPort,
281268
Protocol: infrav1.ELBProtocolTCP,
282269
VpcID: s.scope.VPC().ID,
@@ -289,7 +276,6 @@ func (s *Service) getAPIServerLBSpec(elbName string, lbSpec *infrav1.AWSLoadBala
289276

290277
if lbSpec != nil {
291278
for _, listener := range lbSpec.AdditionalListeners {
292-
targetGroupName := s.getTargetGroupName(lbSpec, "additional-listener", listener.Port)
293279
lnHealthCheck := &infrav1.TargetGroupHealthCheck{
294280
Protocol: aws.String(string(listener.Protocol)),
295281
Port: aws.String(strconv.FormatInt(listener.Port, 10)),
@@ -302,7 +288,7 @@ func (s *Service) getAPIServerLBSpec(elbName string, lbSpec *infrav1.AWSLoadBala
302288
Protocol: listener.Protocol,
303289
Port: listener.Port,
304290
TargetGroup: infrav1.TargetGroupSpec{
305-
Name: targetGroupName,
291+
Name: fmt.Sprintf("additional-listener-%d", time.Now().Unix()),
306292
Port: listener.Port,
307293
Protocol: listener.Protocol,
308294
VpcID: s.scope.VPC().ID,

pkg/cloud/services/elb/loadbalancer_test.go

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3341,63 +3341,6 @@ func stubGetBaseService(t *testing.T, clusterName string) *Service {
33413341
}
33423342
}
33433343

3344-
func TestService_getTargetGroupName(t *testing.T) {
3345-
type argWant struct {
3346-
value string
3347-
prefixOnly bool
3348-
}
3349-
type args struct {
3350-
lbSpec *infrav1.AWSLoadBalancerSpec
3351-
defaultPrefix string
3352-
port int64
3353-
}
3354-
tests := []struct {
3355-
name string
3356-
args args
3357-
want argWant
3358-
}{
3359-
{
3360-
name: "default name",
3361-
args: args{
3362-
lbSpec: &infrav1.AWSLoadBalancerSpec{},
3363-
defaultPrefix: "apiserver-target",
3364-
port: 6443,
3365-
},
3366-
want: argWant{
3367-
value: "apiserver-target-",
3368-
prefixOnly: true,
3369-
},
3370-
},
3371-
{
3372-
name: "custom name",
3373-
args: args{
3374-
lbSpec: &infrav1.AWSLoadBalancerSpec{
3375-
Name: ptr.To("foo"),
3376-
},
3377-
defaultPrefix: "api",
3378-
port: 6443,
3379-
},
3380-
want: argWant{
3381-
value: "foo-6443",
3382-
},
3383-
},
3384-
}
3385-
for _, tt := range tests {
3386-
t.Run(tt.name, func(t *testing.T) {
3387-
s := stubGetBaseService(t, "foo")
3388-
if tt.want.prefixOnly {
3389-
if got := s.getTargetGroupName(tt.args.lbSpec, tt.args.defaultPrefix, tt.args.port); !strings.HasPrefix(got, tt.want.value) {
3390-
t.Errorf("Service.getTargetGroupName() = %v, wantPrefix %v", got, tt.want.value)
3391-
}
3392-
return
3393-
}
3394-
if got := s.getTargetGroupName(tt.args.lbSpec, tt.args.defaultPrefix, tt.args.port); got != tt.want.value {
3395-
t.Errorf("Service.getTargetGroupName() = %v, want %v", got, tt.want)
3396-
}
3397-
})
3398-
}
3399-
}
3400-
34013344
func TestService_getAPITargetGroupHealthCheck(t *testing.T) {
34023345
tests := []struct {
34033346
name string

0 commit comments

Comments
 (0)