Skip to content

Commit 3fb798e

Browse files
authored
Merge pull request #4243 from zac-nixon/main
clear confusion around using ClusterIP service and Instance targets
2 parents 547e0b4 + 7361948 commit 3fb798e

File tree

4 files changed

+60
-5
lines changed

4 files changed

+60
-5
lines changed

pkg/gateway/model/model_build_target_group.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,14 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupSpec(gw *gwv1.Gateway, ro
283283
}
284284
tgPort := builder.buildTargetGroupPort(targetType, *backend.ServicePort)
285285
name := builder.buildTargetGroupName(targetGroupProps, k8s.NamespacedName(gw), route.GetRouteNamespacedName(), k8s.NamespacedName(backend.Service), tgPort, targetType, tgProtocol, tgProtocolVersion)
286+
287+
if tgPort == 0 {
288+
if targetType == elbv2model.TargetTypeIP {
289+
return elbv2model.TargetGroupSpec{}, errors.Errorf("TargetGroup port is empty. Are you using the correct service type?")
290+
}
291+
return elbv2model.TargetGroupSpec{}, errors.Errorf("TargetGroup port is empty. When using Instance targets, your service be must of type 'NodePort' or 'LoadBalancer'")
292+
}
293+
286294
return elbv2model.TargetGroupSpec{
287295
Name: name,
288296
TargetType: targetType,
@@ -359,10 +367,6 @@ func (builder *targetGroupBuilderImpl) buildTargetGroupIPAddressType(svc *corev1
359367
// and we do our best to use the most appropriate port as targetGroup's port to avoid UX confusing.
360368
func (builder *targetGroupBuilderImpl) buildTargetGroupPort(targetType elbv2model.TargetType, svcPort corev1.ServicePort) int32 {
361369
if targetType == elbv2model.TargetTypeInstance {
362-
// Maybe an error? Because the service has no node port, instance type targets don't work.
363-
if svcPort.NodePort == 0 {
364-
return 1
365-
}
366370
return svcPort.NodePort
367371
}
368372
if svcPort.TargetPort.Type == intstr.Int {

pkg/gateway/model/model_build_target_group_test.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,41 @@ func Test_buildTargetGroupSpec(t *testing.T) {
263263
Tags: make(map[string]string),
264264
},
265265
},
266+
{
267+
name: "wrong svc type for instance should produce an error",
268+
tags: make(map[string]string),
269+
lbType: elbv2model.LoadBalancerTypeNetwork,
270+
disableRestrictedSGRules: false,
271+
defaultTargetType: string(elbv2model.TargetTypeInstance),
272+
gateway: &gwv1.Gateway{
273+
ObjectMeta: metav1.ObjectMeta{
274+
Namespace: "my-gw-ns",
275+
Name: "my-gw",
276+
},
277+
},
278+
route: &routeutils.MockRoute{
279+
Kind: routeutils.TCPRouteKind,
280+
Name: "my-route",
281+
Namespace: "my-route-ns",
282+
},
283+
backend: routeutils.Backend{
284+
Service: &corev1.Service{
285+
ObjectMeta: metav1.ObjectMeta{
286+
Namespace: "my-svc-ns",
287+
Name: "my-svc",
288+
},
289+
},
290+
ServicePort: &corev1.ServicePort{
291+
Protocol: corev1.ProtocolTCP,
292+
Port: 80,
293+
TargetPort: intstr.IntOrString{
294+
IntVal: 80,
295+
Type: intstr.Int,
296+
},
297+
},
298+
},
299+
expectErr: true,
300+
},
266301
}
267302

268303
for _, tc := range testCases {
@@ -1207,7 +1242,7 @@ func Test_buildTargetGroupPort(t *testing.T) {
12071242
name: "instance - no node port",
12081243
svcPort: corev1.ServicePort{},
12091244
targetType: elbv2model.TargetTypeInstance,
1210-
expected: 1,
1245+
expected: 0,
12111246
},
12121247
{
12131248
name: "ip",

pkg/ingress/model_build_target_group.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,14 @@ func (t *defaultModelBuildTask) buildTargetGroupSpec(ctx context.Context,
181181
}
182182
tgPort := t.buildTargetGroupPort(ctx, targetType, svcPort)
183183
name := t.buildTargetGroupName(ctx, k8s.NamespacedName(ing.Ing), svc, port, tgPort, targetType, tgProtocol, tgProtocolVersion)
184+
185+
if tgPort == 0 {
186+
if targetType == elbv2model.TargetTypeIP {
187+
return elbv2model.TargetGroupSpec{}, errors.Errorf("TargetGroup port is empty. Are you using the correct service type?")
188+
}
189+
return elbv2model.TargetGroupSpec{}, errors.Errorf("TargetGroup port is empty. When using Instance targets, your service be must of type 'NodePort' or 'LoadBalancer'")
190+
}
191+
184192
return elbv2model.TargetGroupSpec{
185193
Name: name,
186194
TargetType: targetType,

pkg/service/model_build_target_group.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ func (t *defaultModelBuildTask) buildTargetGroupSpec(ctx context.Context, tgProt
7272
if err != nil {
7373
return elbv2model.TargetGroupSpec{}, err
7474
}
75+
76+
if targetPort == 0 {
77+
if targetType == elbv2model.TargetTypeIP {
78+
return elbv2model.TargetGroupSpec{}, errors.Errorf("TargetGroup port is empty. Are you using the correct service type?")
79+
}
80+
return elbv2model.TargetGroupSpec{}, errors.Errorf("TargetGroup port is empty. When using Instance targets, your service be must of type 'NodePort' or 'LoadBalancer'")
81+
}
82+
7583
return elbv2model.TargetGroupSpec{
7684
Name: tgName,
7785
TargetType: targetType,

0 commit comments

Comments
 (0)