Repository: kubernetes-sigs/aws-alb-ingress-controller
Pull request #1523 (2020-10-19 22:12:01)

use numeric target group if available
For ALB target group port, use numeric value if available. If not, use 1.

k8s-ci-robot: Hi @kishorj. Thanks for your PR.

I'm waiting for a [kubernetes-sigs](https://github.com/orgs/kubernetes-sigs/people) member to verify that this patch is reasonable to test. If it is, they should reply with `/ok-to-test` on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should [join the org](https://git.k8s.io/community/community-membership.md#member) to skip this step.

Once the patch is verified, the new status will be reflected by the `ok-to-test` label.

I understand the commands that are listed [here](https://go.k8s.io/bot-commands?repo=kubernetes-sigs%2Faws-alb-ingress-controller).

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
</details>

k8s-ci-robot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubernetes-sigs/aws-alb-ingress-controller/pull/1523#" title="Author self-approved">kishorj</a>*
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign  after the PR has been reviewed.
You can assign the PR to them by writing `/assign ` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=kubernetes-sigs%2Faws-alb-ingress-controller).

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/kubernetes-sigs/aws-alb-ingress-controller/blob/main/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/pkg/ingress/model_build_target_group.go b/pkg/ingress/model_build_target_group.go
index 4a1b43405..e41c8e051 100644
--- a/pkg/ingress/model_build_target_group.go
+++ b/pkg/ingress/model_build_target_group.go
@@ -117,11 +117,19 @@ func (t *defaultModelBuildTask) buildTargetGroupSpec(ctx context.Context,
 	if err != nil {
 		return elbv2model.TargetGroupSpec{}, err
 	}
-	name := t.buildTargetGroupName(ctx, k8s.NamespacedName(ing), svc, port, targetType, tgProtocol)
+	svcPort, err := k8s.LookupServicePort(svc, port)
+	if err != nil {
+		return elbv2model.TargetGroupSpec{}, err
+	}
+	targetGroupPort := 1
+	if svcPort.TargetPort.Type == intstr.Int {
+		targetGroupPort = svcPort.TargetPort.IntValue()
+	}
+	name := t.buildTargetGroupName(ctx, k8s.NamespacedName(ing), svc, svcPort.TargetPort, targetType, tgProtocol)
 	return elbv2model.TargetGroupSpec{
 		Name:                  name,
 		TargetType:            targetType,
-		Port:                  1,
+		Port:                  int64(targetGroupPort),
 		Protocol:              tgProtocol,
 		HealthCheckConfig:     &healthCheckConfig,
 		TargetGroupAttributes: targetGroupAttributes,


Test patch:
diff --git a/pkg/ingress/model_builder_test.go b/pkg/ingress/model_builder_test.go
index 065dfa1eb..d6f18ea12 100644
--- a/pkg/ingress/model_builder_test.go
+++ b/pkg/ingress/model_builder_test.go
@@ -402,9 +402,9 @@ func Test_defaultModelBuilder_Build(t *testing.T) {
         "AWS::ElasticLoadBalancingV2::TargetGroup":{
             "ns-1/ing-1-svc-1:http":{
                 "spec":{
-                    "name":"k8s-ns1-svc1-2718fcb5d7",
+                    "name":"k8s-ns1-svc1-1939f42801",
                     "targetType":"instance",
-                    "port":1,
+                    "port":8080,
                     "protocol":"HTTP",
                     "healthCheckConfig":{
                         "port":"traffic-port",
@@ -422,9 +422,9 @@ func Test_defaultModelBuilder_Build(t *testing.T) {
             },
             "ns-1/ing-1-svc-2:http":{
                 "spec":{
-                    "name":"k8s-ns1-svc2-2718fcb5d7",
+                    "name":"k8s-ns1-svc2-1939f42801",
                     "targetType":"instance",
-                    "port":1,
+                    "port":8080,
                     "protocol":"HTTP",
                     "healthCheckConfig":{
                         "port":"traffic-port",
@@ -442,9 +442,9 @@ func Test_defaultModelBuilder_Build(t *testing.T) {
             },
             "ns-1/ing-1-svc-3:https":{
                 "spec":{
-                    "name":"k8s-ns1-svc3-da71e18041",
+                    "name":"k8s-ns1-svc3-59d2f49fb4",
                     "targetType":"ip",
-                    "port":1,
+                    "port":8443,
                     "protocol":"HTTPS",
                     "healthCheckConfig":{
                         "port":9090,
@@ -466,7 +466,7 @@ func Test_defaultModelBuilder_Build(t *testing.T) {
                 "spec":{
                     "template":{
                         "metadata":{
-                            "name":"k8s-ns1-svc1-2718fcb5d7",
+                            "name":"k8s-ns1-svc1-1939f42801",
                             "namespace":"ns-1",
                             "creationTimestamp":null
                         },
@@ -507,7 +507,7 @@ func Test_defaultModelBuilder_Build(t *testing.T) {
                 "spec":{
                     "template":{
                         "metadata":{
-                            "name":"k8s-ns1-svc2-2718fcb5d7",
+                            "name":"k8s-ns1-svc2-1939f42801",
                             "namespace":"ns-1",
                             "creationTimestamp":null
                         },
@@ -548,7 +548,7 @@ func Test_defaultModelBuilder_Build(t *testing.T) {
                 "spec":{
                     "template":{
                         "metadata":{
-                            "name":"k8s-ns1-svc3-da71e18041",
+                            "name":"k8s-ns1-svc3-59d2f49fb4",
                             "namespace":"ns-1",
                             "creationTimestamp":null
                         },
@@ -848,9 +848,9 @@ func Test_defaultModelBuilder_Build(t *testing.T) {
         "AWS::ElasticLoadBalancingV2::TargetGroup":{
             "ns-1/ing-1-svc-1:http":{
                 "spec":{
-                    "name":"k8s-ns1-svc1-2718fcb5d7",
+                    "name":"k8s-ns1-svc1-1939f42801",
                     "targetType":"instance",
-                    "port":1,
+                    "port":8080,
                     "protocol":"HTTP",
                     "healthCheckConfig":{
                         "port":"traffic-port",
@@ -868,9 +868,9 @@ func Test_defaultModelBuilder_Build(t *testing.T) {
             },
             "ns-1/ing-1-svc-2:http":{
                 "spec":{
-                    "name":"k8s-ns1-svc2-2718fcb5d7",
+                    "name":"k8s-ns1-svc2-1939f42801",
                     "targetType":"instance",
-                    "port":1,
+                    "port":8080,
                     "protocol":"HTTP",
                     "healthCheckConfig":{
                         "port":"traffic-port",
@@ -888,9 +888,9 @@ func Test_defaultModelBuilder_Build(t *testing.T) {
             },
             "ns-1/ing-1-svc-3:https":{
                 "spec":{
-                    "name":"k8s-ns1-svc3-da71e18041",
+                    "name":"k8s-ns1-svc3-59d2f49fb4",
                     "targetType":"ip",
-                    "port":1,
+                    "port":8443,
                     "protocol":"HTTPS",
                     "healthCheckConfig":{
                         "port":9090,
@@ -912,7 +912,7 @@ func Test_defaultModelBuilder_Build(t *testing.T) {
                 "spec":{
                     "template":{
                         "metadata":{
-                            "name":"k8s-ns1-svc1-2718fcb5d7",
+                            "name":"k8s-ns1-svc1-1939f42801",
                             "namespace":"ns-1",
                             "creationTimestamp":null
                         },
@@ -953,7 +953,7 @@ func Test_defaultModelBuilder_Build(t *testing.T) {
                 "spec":{
                     "template":{
                         "metadata":{
-                            "name":"k8s-ns1-svc2-2718fcb5d7",
+                            "name":"k8s-ns1-svc2-1939f42801",
                             "namespace":"ns-1",
                             "creationTimestamp":null
                         },
@@ -994,7 +994,7 @@ func Test_defaultModelBuilder_Build(t *testing.T) {
                 "spec":{
                     "template":{
                         "metadata":{
-                            "name":"k8s-ns1-svc3-da71e18041",
+                            "name":"k8s-ns1-svc3-59d2f49fb4",
                             "namespace":"ns-1",
                             "creationTimestamp":null
                         },