Skip to content

Commit 398776e

Browse files
authored
New error generation/message when ServiceType is ClusterIP and LoadBalancerTargetType is instance (#2094)
1 parent ed92ddf commit 398776e

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

pkg/service/model_build_target_group.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ func (t *defaultModelBuildTask) buildTargetGroupHealthCheckUnhealthyThresholdCou
327327
}
328328

329329
func (t *defaultModelBuildTask) buildTargetType(_ context.Context) (elbv2model.TargetType, error) {
330+
svcType := t.service.Spec.Type
330331
var lbType string
331332
_ = t.annotationParser.ParseStringAnnotation(annotations.SvcLBSuffixLoadBalancerType, &lbType, t.service.Annotations)
332333
var lbTargetType string
@@ -335,6 +336,9 @@ func (t *defaultModelBuildTask) buildTargetType(_ context.Context) (elbv2model.T
335336
return elbv2model.TargetTypeIP, nil
336337
}
337338
if lbType == LoadBalancerTypeExternal && lbTargetType == LoadBalancerTargetTypeInstance {
339+
if svcType == corev1.ServiceTypeClusterIP {
340+
return "", errors.Errorf("unsupported service type \"%v\" for load balancer target type \"%v\"", svcType, lbTargetType)
341+
}
338342
return elbv2model.TargetTypeInstance, nil
339343
}
340344
return "", errors.Errorf("unsupported target type \"%v\" for load balancer type \"%v\"", lbTargetType, lbType)

pkg/service/model_build_target_group_test.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ package service
33
import (
44
"context"
55
"errors"
6+
"sort"
7+
"strconv"
8+
"testing"
9+
610
"github.com/aws/aws-sdk-go/aws"
711
"github.com/aws/aws-sdk-go/service/ec2"
812
"github.com/stretchr/testify/assert"
@@ -12,9 +16,6 @@ import (
1216
elbv2api "sigs.k8s.io/aws-load-balancer-controller/apis/elbv2/v1beta1"
1317
"sigs.k8s.io/aws-load-balancer-controller/pkg/annotations"
1418
"sigs.k8s.io/aws-load-balancer-controller/pkg/model/elbv2"
15-
"sort"
16-
"strconv"
17-
"testing"
1819
)
1920

2021
func Test_defaultModelBuilderTask_targetGroupAttrs(t *testing.T) {
@@ -1044,6 +1045,21 @@ func Test_defaultModelBuilder_buildTargetType(t *testing.T) {
10441045
},
10451046
wantErr: errors.New("unsupported target type \"unknown\" for load balancer type \"external\""),
10461047
},
1048+
{
1049+
testName: "external, ClusterIP with target type instance",
1050+
svc: &corev1.Service{
1051+
ObjectMeta: metav1.ObjectMeta{
1052+
Annotations: map[string]string{
1053+
"service.beta.kubernetes.io/aws-load-balancer-type": "external",
1054+
"service.beta.kubernetes.io/aws-load-balancer-nlb-target-type": "instance",
1055+
},
1056+
},
1057+
Spec: corev1.ServiceSpec{
1058+
Type: corev1.ServiceTypeClusterIP,
1059+
},
1060+
},
1061+
wantErr: errors.New("unsupported service type \"ClusterIP\" for load balancer target type \"instance\""),
1062+
},
10471063
}
10481064
for _, tt := range tests {
10491065
t.Run(tt.testName, func(t *testing.T) {

0 commit comments

Comments
 (0)