@@ -2372,6 +2372,61 @@ func TestServiceToMeshServices(t *testing.T) {
2372
2372
},
2373
2373
},
2374
2374
},
2375
+ {
2376
+ name : "k8s headless service with single port and endpoint (no hostname), no appProtocol set" ,
2377
+ // Single port on the service maps to a single MeshService.
2378
+ // Since no appProtocol is specified, MeshService.Protocol should default
2379
+ // to http because Port.Protocol=TCP
2380
+ svc : corev1.Service {
2381
+ ObjectMeta : metav1.ObjectMeta {
2382
+ Namespace : "ns1" ,
2383
+ Name : "s1" ,
2384
+ },
2385
+ Spec : corev1.ServiceSpec {
2386
+ Ports : []corev1.ServicePort {
2387
+ {
2388
+ Name : "p1" ,
2389
+ Port : 80 ,
2390
+ Protocol : corev1 .ProtocolTCP ,
2391
+ },
2392
+ },
2393
+ ClusterIP : corev1 .ClusterIPNone ,
2394
+ },
2395
+ },
2396
+ svcEndpoints : []runtime.Object {
2397
+ & corev1.Endpoints {
2398
+ ObjectMeta : metav1.ObjectMeta {
2399
+ // Should match svc.Name and svc.Namespace
2400
+ Namespace : "ns1" ,
2401
+ Name : "s1" ,
2402
+ },
2403
+ Subsets : []corev1.EndpointSubset {
2404
+ {
2405
+ Addresses : []corev1.EndpointAddress {
2406
+ {
2407
+ IP : "10.1.0.1" ,
2408
+ },
2409
+ },
2410
+ Ports : []corev1.EndpointPort {
2411
+ {
2412
+ // Must match the port of 'svc.Spec.Ports[0]'
2413
+ Port : 8080 , // TargetPort
2414
+ },
2415
+ },
2416
+ },
2417
+ },
2418
+ },
2419
+ },
2420
+ expected : []service.MeshService {
2421
+ {
2422
+ Namespace : "ns1" ,
2423
+ Name : "s1" ,
2424
+ Port : 80 ,
2425
+ TargetPort : 8080 ,
2426
+ Protocol : "http" ,
2427
+ },
2428
+ },
2429
+ },
2375
2430
{
2376
2431
name : "multiple ports on k8s service with appProtocol specified" ,
2377
2432
svc : corev1.Service {
@@ -2514,6 +2569,78 @@ func TestServiceToMeshServices(t *testing.T) {
2514
2569
},
2515
2570
},
2516
2571
},
2572
+ {
2573
+ name : "multiple ports on k8s headless service with appProtocol specified and no hostname in the endpoints" ,
2574
+ svc : corev1.Service {
2575
+ ObjectMeta : metav1.ObjectMeta {
2576
+ Namespace : "ns1" ,
2577
+ Name : "s1" ,
2578
+ },
2579
+ Spec : corev1.ServiceSpec {
2580
+ ClusterIP : corev1 .ClusterIPNone ,
2581
+ Ports : []corev1.ServicePort {
2582
+ {
2583
+ Name : "p1" ,
2584
+ Port : 80 ,
2585
+ AppProtocol : pointer .StringPtr ("http" ),
2586
+ },
2587
+ {
2588
+ Name : "p2" ,
2589
+ Port : 90 ,
2590
+ AppProtocol : pointer .StringPtr ("tcp" ),
2591
+ },
2592
+ },
2593
+ },
2594
+ },
2595
+ svcEndpoints : []runtime.Object {
2596
+ & corev1.Endpoints {
2597
+ ObjectMeta : metav1.ObjectMeta {
2598
+ // Should match svc.Name and svc.Namespace
2599
+ Namespace : "ns1" ,
2600
+ Name : "s1" ,
2601
+ },
2602
+ Subsets : []corev1.EndpointSubset {
2603
+ {
2604
+ Addresses : []corev1.EndpointAddress {
2605
+ {
2606
+ IP : "10.1.0.1" ,
2607
+ },
2608
+ },
2609
+ Ports : []corev1.EndpointPort {
2610
+ {
2611
+ // Must match the port of 'svc.Spec.Ports[0]'
2612
+ Name : "p1" ,
2613
+ Port : 8080 , // TargetPort
2614
+ AppProtocol : pointer .StringPtr ("http" ),
2615
+ },
2616
+ {
2617
+ // Must match the port of 'svc.Spec.Ports[1]'
2618
+ Name : "p2" ,
2619
+ Port : 9090 , // TargetPort
2620
+ AppProtocol : pointer .StringPtr ("tcp" ),
2621
+ },
2622
+ },
2623
+ },
2624
+ },
2625
+ },
2626
+ },
2627
+ expected : []service.MeshService {
2628
+ {
2629
+ Namespace : "ns1" ,
2630
+ Name : "s1" ,
2631
+ Port : 80 ,
2632
+ TargetPort : 8080 ,
2633
+ Protocol : "http" ,
2634
+ },
2635
+ {
2636
+ Namespace : "ns1" ,
2637
+ Name : "s1" ,
2638
+ Port : 90 ,
2639
+ TargetPort : 9090 ,
2640
+ Protocol : "tcp" ,
2641
+ },
2642
+ },
2643
+ },
2517
2644
{
2518
2645
name : "duplicate ports on k8s service with different protocols" ,
2519
2646
svc : corev1.Service {
@@ -2682,7 +2809,7 @@ func TestGetMeshService(t *testing.T) {
2682
2809
expectErr : true ,
2683
2810
},
2684
2811
{
2685
- name : "TargetPort not found as Endpoint does not exist" ,
2812
+ name : "TargetPort not found as matching Endpoint does not exist" ,
2686
2813
svc : & corev1.Service {
2687
2814
ObjectMeta : metav1.ObjectMeta {
2688
2815
Name : "s1" ,
@@ -2693,6 +2820,7 @@ func TestGetMeshService(t *testing.T) {
2693
2820
Name : "p1" ,
2694
2821
Port : 80 ,
2695
2822
}},
2823
+ ClusterIP : corev1 .ClusterIPNone ,
2696
2824
},
2697
2825
},
2698
2826
endpoints : & corev1.Endpoints {
@@ -2707,14 +2835,18 @@ func TestGetMeshService(t *testing.T) {
2707
2835
Name : "invalid" , // does not match svc port
2708
2836
Port : 8080 ,
2709
2837
},
2838
+ {
2839
+ Name : "invalid2" ,
2840
+ Port : 8081 , // also does not match svc port; having 2 ports triggers the name filter logic
2841
+ },
2710
2842
},
2711
2843
},
2712
2844
},
2713
2845
},
2714
2846
namespacedSvc : types.NamespacedName {Namespace : "ns1" , Name : "s1" }, // matches svc
2715
2847
port : 80 , // matches svc
2716
- expectedTargetPort : 0 , // matches endpoint's 'p1' port
2717
- expectErr : true ,
2848
+ expectedTargetPort : 0 , // does not match either of the endpoint's ports
2849
+ expectErr : false , // port matches, so no error should be expected
2718
2850
},
2719
2851
}
2720
2852
0 commit comments