|
7 | 7 | "testing" |
8 | 8 |
|
9 | 9 | gwv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 10 | + gwv1alpha2 "sigs.k8s.io/gateway-api/apis/v1alpha2" |
10 | 11 |
|
11 | 12 | mock_client "github.com/aws/aws-application-networking-k8s/mocks/controller-runtime/client" |
12 | 13 | "github.com/aws/aws-application-networking-k8s/pkg/config" |
@@ -1274,3 +1275,253 @@ func Test_TGModelByHTTPRouteBuild_AdditionalTags(t *testing.T) { |
1274 | 1275 | }) |
1275 | 1276 | } |
1276 | 1277 | } |
| 1278 | + |
| 1279 | +func Test_TGModelByServiceExportWithExportedPorts_TargetGroupPolicy(t *testing.T) { |
| 1280 | + config.VpcID = "vpc-id" |
| 1281 | + config.ClusterName = "cluster-name" |
| 1282 | + |
| 1283 | + tests := []struct { |
| 1284 | + name string |
| 1285 | + svcExport *anv1alpha1.ServiceExport |
| 1286 | + svc *corev1.Service |
| 1287 | + tgp *anv1alpha1.TargetGroupPolicy |
| 1288 | + wantErrIsNil bool |
| 1289 | + wantProtocol string |
| 1290 | + wantProtocolVersion string |
| 1291 | + wantHealthCheckProto *string |
| 1292 | + description string |
| 1293 | + }{ |
| 1294 | + { |
| 1295 | + name: "ServiceExport with HTTP routeType and HTTPS protocol override from TargetGroupPolicy", |
| 1296 | + svcExport: &anv1alpha1.ServiceExport{ |
| 1297 | + ObjectMeta: metav1.ObjectMeta{ |
| 1298 | + Name: "https-override", |
| 1299 | + Namespace: "bar", |
| 1300 | + }, |
| 1301 | + Spec: anv1alpha1.ServiceExportSpec{ |
| 1302 | + ExportedPorts: []anv1alpha1.ExportedPort{ |
| 1303 | + { |
| 1304 | + Port: 80, |
| 1305 | + RouteType: "HTTP", |
| 1306 | + }, |
| 1307 | + }, |
| 1308 | + }, |
| 1309 | + }, |
| 1310 | + svc: &corev1.Service{ |
| 1311 | + ObjectMeta: metav1.ObjectMeta{ |
| 1312 | + Name: "https-override", |
| 1313 | + Namespace: "bar", |
| 1314 | + }, |
| 1315 | + Spec: corev1.ServiceSpec{ |
| 1316 | + Ports: []corev1.ServicePort{ |
| 1317 | + {Port: 80}, |
| 1318 | + }, |
| 1319 | + IPFamilies: []corev1.IPFamily{corev1.IPv4Protocol}, |
| 1320 | + }, |
| 1321 | + }, |
| 1322 | + tgp: &anv1alpha1.TargetGroupPolicy{ |
| 1323 | + ObjectMeta: metav1.ObjectMeta{ |
| 1324 | + Name: "https-override", |
| 1325 | + Namespace: "bar", |
| 1326 | + }, |
| 1327 | + Spec: anv1alpha1.TargetGroupPolicySpec{ |
| 1328 | + TargetRef: &gwv1alpha2.NamespacedPolicyTargetReference{ |
| 1329 | + Group: "application-networking.k8s.aws", |
| 1330 | + Kind: "ServiceExport", |
| 1331 | + Name: "https-override", |
| 1332 | + }, |
| 1333 | + Protocol: func() *string { s := vpclattice.TargetGroupProtocolHttps; return &s }(), |
| 1334 | + ProtocolVersion: func() *string { s := vpclattice.TargetGroupProtocolVersionHttp2; return &s }(), |
| 1335 | + HealthCheck: &anv1alpha1.HealthCheckConfig{ |
| 1336 | + Enabled: func() *bool { b := true; return &b }(), |
| 1337 | + Protocol: func() *anv1alpha1.HealthCheckProtocol { p := anv1alpha1.HealthCheckProtocolHTTPS; return &p }(), |
| 1338 | + ProtocolVersion: func() *anv1alpha1.HealthCheckProtocolVersion { |
| 1339 | + v := anv1alpha1.HealthCheckProtocolVersionHTTP2 |
| 1340 | + return &v |
| 1341 | + }(), |
| 1342 | + Port: func() *int64 { p := int64(80); return &p }(), |
| 1343 | + Path: func() *string { s := "/env"; return &s }(), |
| 1344 | + IntervalSeconds: func() *int64 { i := int64(10); return &i }(), |
| 1345 | + TimeoutSeconds: func() *int64 { t := int64(5); return &t }(), |
| 1346 | + HealthyThresholdCount: func() *int64 { c := int64(2); return &c }(), |
| 1347 | + UnhealthyThresholdCount: func() *int64 { c := int64(3); return &c }(), |
| 1348 | + StatusMatch: func() *string { s := "200-299"; return &s }(), |
| 1349 | + }, |
| 1350 | + }, |
| 1351 | + }, |
| 1352 | + wantErrIsNil: true, |
| 1353 | + wantProtocol: vpclattice.TargetGroupProtocolHttps, |
| 1354 | + wantProtocolVersion: vpclattice.TargetGroupProtocolVersionHttp2, |
| 1355 | + wantHealthCheckProto: func() *string { s := vpclattice.TargetGroupProtocolHttps; return &s }(), |
| 1356 | + description: "should use HTTPS protocol and HTTP2 from TargetGroupPolicy when specified", |
| 1357 | + }, |
| 1358 | + { |
| 1359 | + name: "ServiceExport with GRPC routeType and no TargetGroupPolicy override", |
| 1360 | + svcExport: &anv1alpha1.ServiceExport{ |
| 1361 | + ObjectMeta: metav1.ObjectMeta{ |
| 1362 | + Name: "grpc-default", |
| 1363 | + Namespace: "bar", |
| 1364 | + }, |
| 1365 | + Spec: anv1alpha1.ServiceExportSpec{ |
| 1366 | + ExportedPorts: []anv1alpha1.ExportedPort{ |
| 1367 | + { |
| 1368 | + Port: 8081, |
| 1369 | + RouteType: "GRPC", |
| 1370 | + }, |
| 1371 | + }, |
| 1372 | + }, |
| 1373 | + }, |
| 1374 | + svc: &corev1.Service{ |
| 1375 | + ObjectMeta: metav1.ObjectMeta{ |
| 1376 | + Name: "grpc-default", |
| 1377 | + Namespace: "bar", |
| 1378 | + }, |
| 1379 | + Spec: corev1.ServiceSpec{ |
| 1380 | + Ports: []corev1.ServicePort{ |
| 1381 | + {Port: 8081}, |
| 1382 | + }, |
| 1383 | + IPFamilies: []corev1.IPFamily{corev1.IPv4Protocol}, |
| 1384 | + }, |
| 1385 | + }, |
| 1386 | + wantErrIsNil: true, |
| 1387 | + wantProtocol: vpclattice.TargetGroupProtocolHttp, |
| 1388 | + wantProtocolVersion: vpclattice.TargetGroupProtocolVersionGrpc, |
| 1389 | + description: "should use default GRPC protocol when no TargetGroupPolicy", |
| 1390 | + }, |
| 1391 | + { |
| 1392 | + name: "ServiceExport with TLS routeType and TCP protocol (no protocolVersion)", |
| 1393 | + svcExport: &anv1alpha1.ServiceExport{ |
| 1394 | + ObjectMeta: metav1.ObjectMeta{ |
| 1395 | + Name: "tls-tcp", |
| 1396 | + Namespace: "bar", |
| 1397 | + }, |
| 1398 | + Spec: anv1alpha1.ServiceExportSpec{ |
| 1399 | + ExportedPorts: []anv1alpha1.ExportedPort{ |
| 1400 | + { |
| 1401 | + Port: 443, |
| 1402 | + RouteType: "TLS", |
| 1403 | + }, |
| 1404 | + }, |
| 1405 | + }, |
| 1406 | + }, |
| 1407 | + svc: &corev1.Service{ |
| 1408 | + ObjectMeta: metav1.ObjectMeta{ |
| 1409 | + Name: "tls-tcp", |
| 1410 | + Namespace: "bar", |
| 1411 | + }, |
| 1412 | + Spec: corev1.ServiceSpec{ |
| 1413 | + Ports: []corev1.ServicePort{ |
| 1414 | + {Port: 443}, |
| 1415 | + }, |
| 1416 | + IPFamilies: []corev1.IPFamily{corev1.IPv4Protocol}, |
| 1417 | + }, |
| 1418 | + }, |
| 1419 | + tgp: &anv1alpha1.TargetGroupPolicy{ |
| 1420 | + ObjectMeta: metav1.ObjectMeta{ |
| 1421 | + Name: "tls-tcp", |
| 1422 | + Namespace: "bar", |
| 1423 | + }, |
| 1424 | + Spec: anv1alpha1.TargetGroupPolicySpec{ |
| 1425 | + TargetRef: &gwv1alpha2.NamespacedPolicyTargetReference{ |
| 1426 | + Group: "application-networking.k8s.aws", |
| 1427 | + Kind: "ServiceExport", |
| 1428 | + Name: "tls-tcp", |
| 1429 | + }, |
| 1430 | + Protocol: func() *string { s := vpclattice.TargetGroupProtocolTcp; return &s }(), |
| 1431 | + }, |
| 1432 | + }, |
| 1433 | + wantErrIsNil: true, |
| 1434 | + wantProtocol: vpclattice.TargetGroupProtocolTcp, |
| 1435 | + wantProtocolVersion: "", |
| 1436 | + description: "should use TCP protocol with empty protocolVersion", |
| 1437 | + }, |
| 1438 | + { |
| 1439 | + name: "ServiceExport with HTTP routeType and HTTPS protocol with HTTP1 override", |
| 1440 | + svcExport: &anv1alpha1.ServiceExport{ |
| 1441 | + ObjectMeta: metav1.ObjectMeta{ |
| 1442 | + Name: "https-http1", |
| 1443 | + Namespace: "bar", |
| 1444 | + }, |
| 1445 | + Spec: anv1alpha1.ServiceExportSpec{ |
| 1446 | + ExportedPorts: []anv1alpha1.ExportedPort{ |
| 1447 | + { |
| 1448 | + Port: 80, |
| 1449 | + RouteType: "HTTP", |
| 1450 | + }, |
| 1451 | + }, |
| 1452 | + }, |
| 1453 | + }, |
| 1454 | + svc: &corev1.Service{ |
| 1455 | + ObjectMeta: metav1.ObjectMeta{ |
| 1456 | + Name: "https-http1", |
| 1457 | + Namespace: "bar", |
| 1458 | + }, |
| 1459 | + Spec: corev1.ServiceSpec{ |
| 1460 | + Ports: []corev1.ServicePort{ |
| 1461 | + {Port: 80}, |
| 1462 | + }, |
| 1463 | + IPFamilies: []corev1.IPFamily{corev1.IPv4Protocol}, |
| 1464 | + }, |
| 1465 | + }, |
| 1466 | + tgp: &anv1alpha1.TargetGroupPolicy{ |
| 1467 | + ObjectMeta: metav1.ObjectMeta{ |
| 1468 | + Name: "https-http1", |
| 1469 | + Namespace: "bar", |
| 1470 | + }, |
| 1471 | + Spec: anv1alpha1.TargetGroupPolicySpec{ |
| 1472 | + TargetRef: &gwv1alpha2.NamespacedPolicyTargetReference{ |
| 1473 | + Group: "application-networking.k8s.aws", |
| 1474 | + Kind: "ServiceExport", |
| 1475 | + Name: "https-http1", |
| 1476 | + }, |
| 1477 | + Protocol: func() *string { s := vpclattice.TargetGroupProtocolHttps; return &s }(), |
| 1478 | + ProtocolVersion: func() *string { s := vpclattice.TargetGroupProtocolVersionHttp1; return &s }(), |
| 1479 | + }, |
| 1480 | + }, |
| 1481 | + wantErrIsNil: true, |
| 1482 | + wantProtocol: vpclattice.TargetGroupProtocolHttps, |
| 1483 | + wantProtocolVersion: vpclattice.TargetGroupProtocolVersionHttp1, |
| 1484 | + description: "should use HTTPS protocol with HTTP1 from TargetGroupPolicy", |
| 1485 | + }, |
| 1486 | + } |
| 1487 | + |
| 1488 | + for _, tt := range tests { |
| 1489 | + t.Run(tt.name, func(t *testing.T) { |
| 1490 | + ctx := context.TODO() |
| 1491 | + |
| 1492 | + k8sSchema := runtime.NewScheme() |
| 1493 | + clientgoscheme.AddToScheme(k8sSchema) |
| 1494 | + anv1alpha1.Install(k8sSchema) |
| 1495 | + k8sClient := testclient.NewClientBuilder().WithScheme(k8sSchema).Build() |
| 1496 | + |
| 1497 | + assert.NoError(t, k8sClient.Create(ctx, tt.svc.DeepCopy())) |
| 1498 | + |
| 1499 | + if tt.tgp != nil { |
| 1500 | + assert.NoError(t, k8sClient.Create(ctx, tt.tgp.DeepCopy())) |
| 1501 | + } |
| 1502 | + |
| 1503 | + builder := NewSvcExportTargetGroupBuilder(gwlog.FallbackLogger, k8sClient) |
| 1504 | + |
| 1505 | + stack, err := builder.Build(ctx, tt.svcExport) |
| 1506 | + if !tt.wantErrIsNil { |
| 1507 | + assert.NotNil(t, err, tt.description) |
| 1508 | + return |
| 1509 | + } |
| 1510 | + assert.Nil(t, err, tt.description) |
| 1511 | + |
| 1512 | + var targetGroups []*model.TargetGroup |
| 1513 | + err = stack.ListResources(&targetGroups) |
| 1514 | + assert.Nil(t, err) |
| 1515 | + assert.Equal(t, 1, len(targetGroups)) |
| 1516 | + |
| 1517 | + tg := targetGroups[0] |
| 1518 | + assert.Equal(t, tt.wantProtocol, tg.Spec.Protocol, tt.description) |
| 1519 | + assert.Equal(t, tt.wantProtocolVersion, tg.Spec.ProtocolVersion, tt.description) |
| 1520 | + |
| 1521 | + if tt.wantHealthCheckProto != nil { |
| 1522 | + assert.NotNil(t, tg.Spec.HealthCheckConfig, tt.description) |
| 1523 | + assert.Equal(t, *tt.wantHealthCheckProto, *tg.Spec.HealthCheckConfig.Protocol, tt.description) |
| 1524 | + } |
| 1525 | + }) |
| 1526 | + } |
| 1527 | +} |
0 commit comments