Skip to content

Commit 74f6aa6

Browse files
committed
fix aws loadbalancer nodePort cannot change issue
1 parent 903f1e6 commit 74f6aa6

File tree

2 files changed

+128
-3
lines changed

2 files changed

+128
-3
lines changed

staging/src/k8s.io/legacy-cloud-providers/aws/aws_loadbalancer.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -452,13 +452,14 @@ var invalidELBV2NameRegex = regexp.MustCompile("[^[:alnum:]]")
452452

453453
// buildTargetGroupName will build unique name for targetGroup of service & port.
454454
// the name is in format k8s-{namespace:8}-{name:8}-{uuid:10} (chosen to benefit most common use cases).
455-
// Note: targetProtocol & targetType are included since they cannot be modified on existing targetGroup.
456-
func (c *Cloud) buildTargetGroupName(serviceName types.NamespacedName, servicePort int64, targetProtocol string, targetType string) string {
455+
// Note: nodePort & targetProtocol & targetType are included since they cannot be modified on existing targetGroup.
456+
func (c *Cloud) buildTargetGroupName(serviceName types.NamespacedName, servicePort int64, nodePort int64, targetProtocol string, targetType string) string {
457457
hasher := sha1.New()
458458
_, _ = hasher.Write([]byte(c.tagging.clusterID()))
459459
_, _ = hasher.Write([]byte(serviceName.Namespace))
460460
_, _ = hasher.Write([]byte(serviceName.Name))
461461
_, _ = hasher.Write([]byte(strconv.FormatInt(servicePort, 10)))
462+
_, _ = hasher.Write([]byte(strconv.FormatInt(nodePort, 10)))
462463
_, _ = hasher.Write([]byte(targetProtocol))
463464
_, _ = hasher.Write([]byte(targetType))
464465
tgUUID := hex.EncodeToString(hasher.Sum(nil))
@@ -527,7 +528,7 @@ func (c *Cloud) ensureTargetGroup(targetGroup *elbv2.TargetGroup, serviceName ty
527528
dirty := false
528529
if targetGroup == nil {
529530
targetType := "instance"
530-
name := c.buildTargetGroupName(serviceName, mapping.FrontendPort, mapping.TrafficProtocol, targetType)
531+
name := c.buildTargetGroupName(serviceName, mapping.FrontendPort, mapping.TrafficPort, mapping.TrafficProtocol, targetType)
531532
klog.Infof("Creating load balancer target group for %v with name: %s", serviceName, name)
532533
input := &elbv2.CreateTargetGroupInput{
533534
VpcId: aws.String(vpcID),

staging/src/k8s.io/legacy-cloud-providers/aws/aws_loadbalancer_test.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ limitations under the License.
1919
package aws
2020

2121
import (
22+
"k8s.io/apimachinery/pkg/types"
2223
"testing"
2324

2425
"github.com/aws/aws-sdk-go/aws"
@@ -296,3 +297,126 @@ func TestElbListenersAreEqual(t *testing.T) {
296297
})
297298
}
298299
}
300+
301+
func TestBuildTargetGroupName(t *testing.T) {
302+
type args struct {
303+
serviceName types.NamespacedName
304+
servicePort int64
305+
nodePort int64
306+
targetProtocol string
307+
targetType string
308+
}
309+
tests := []struct {
310+
name string
311+
clusterID string
312+
args args
313+
want string
314+
}{
315+
{
316+
name: "base case",
317+
clusterID: "cluster-a",
318+
args: args{
319+
serviceName: types.NamespacedName{Namespace: "default", Name: "service-a"},
320+
servicePort: 80,
321+
nodePort: 8080,
322+
targetProtocol: "TCP",
323+
targetType: "instance",
324+
},
325+
want: "k8s-default-servicea-0aeb5b75af",
326+
},
327+
{
328+
name: "base case & clusterID changed",
329+
clusterID: "cluster-b",
330+
args: args{
331+
serviceName: types.NamespacedName{Namespace: "default", Name: "service-a"},
332+
servicePort: 80,
333+
nodePort: 8080,
334+
targetProtocol: "TCP",
335+
targetType: "instance",
336+
},
337+
want: "k8s-default-servicea-5d3a0a69a8",
338+
},
339+
{
340+
name: "base case & serviceNamespace changed",
341+
clusterID: "cluster-a",
342+
args: args{
343+
serviceName: types.NamespacedName{Namespace: "another", Name: "service-a"},
344+
servicePort: 80,
345+
nodePort: 8080,
346+
targetProtocol: "TCP",
347+
targetType: "instance",
348+
},
349+
want: "k8s-another-servicea-f3a3263315",
350+
},
351+
{
352+
name: "base case & serviceName changed",
353+
clusterID: "cluster-a",
354+
args: args{
355+
serviceName: types.NamespacedName{Namespace: "default", Name: "service-b"},
356+
servicePort: 80,
357+
nodePort: 8080,
358+
targetProtocol: "TCP",
359+
targetType: "instance",
360+
},
361+
want: "k8s-default-serviceb-9a3c03b25e",
362+
},
363+
{
364+
name: "base case & servicePort changed",
365+
clusterID: "cluster-a",
366+
args: args{
367+
serviceName: types.NamespacedName{Namespace: "default", Name: "service-a"},
368+
servicePort: 9090,
369+
nodePort: 8080,
370+
targetProtocol: "TCP",
371+
targetType: "instance",
372+
},
373+
want: "k8s-default-servicea-6e07474ff4",
374+
},
375+
{
376+
name: "base case & nodePort changed",
377+
clusterID: "cluster-a",
378+
args: args{
379+
serviceName: types.NamespacedName{Namespace: "default", Name: "service-a"},
380+
servicePort: 80,
381+
nodePort: 9090,
382+
targetProtocol: "TCP",
383+
targetType: "instance",
384+
},
385+
want: "k8s-default-servicea-6cb2d0201c",
386+
},
387+
{
388+
name: "base case & targetProtocol changed",
389+
clusterID: "cluster-a",
390+
args: args{
391+
serviceName: types.NamespacedName{Namespace: "default", Name: "service-a"},
392+
servicePort: 80,
393+
nodePort: 8080,
394+
targetProtocol: "UDP",
395+
targetType: "instance",
396+
},
397+
want: "k8s-default-servicea-70495e628e",
398+
},
399+
{
400+
name: "base case & targetType changed",
401+
clusterID: "cluster-a",
402+
args: args{
403+
serviceName: types.NamespacedName{Namespace: "default", Name: "service-a"},
404+
servicePort: 80,
405+
nodePort: 8080,
406+
targetProtocol: "TCP",
407+
targetType: "ip",
408+
},
409+
want: "k8s-default-servicea-fff6dd8028",
410+
},
411+
}
412+
for _, tt := range tests {
413+
t.Run(tt.name, func(t *testing.T) {
414+
c := &Cloud{
415+
tagging: awsTagging{ClusterID: tt.clusterID},
416+
}
417+
if got := c.buildTargetGroupName(tt.args.serviceName, tt.args.servicePort, tt.args.nodePort, tt.args.targetProtocol, tt.args.targetType); got != tt.want {
418+
assert.Equal(t, tt.want, got)
419+
}
420+
})
421+
}
422+
}

0 commit comments

Comments
 (0)