@@ -159,7 +159,7 @@ func Test_defaultModelBuilderTask_buildListenerConfig(t *testing.T) {
159159 },
160160 want : & listenerConfig {
161161 certificates : ([]elbv2model.Certificate )(nil ),
162- tlsPortsSet : sets .NewString ("83" ),
162+ tlsPortsSet : sets.New [ string ] ("83" ),
163163 sslPolicy : new (string ),
164164 backendProtocol : "" ,
165165 },
@@ -301,3 +301,238 @@ func Test_defaultModelBuilderTask_buildListenerAttributes(t *testing.T) {
301301 })
302302 }
303303}
304+
305+ func Test_mergeServicePortsForListener (t * testing.T ) {
306+ tests := []struct {
307+ name string
308+ ports []corev1.ServicePort
309+ want corev1.ServicePort
310+ success bool
311+ }{
312+ {
313+ name : "one port" ,
314+ ports : []corev1.ServicePort {
315+ {
316+ Name : "p1" ,
317+ Port : 80 ,
318+ TargetPort : intstr .FromInt (80 ),
319+ Protocol : corev1 .ProtocolTCP ,
320+ NodePort : 31223 ,
321+ },
322+ },
323+ want : corev1.ServicePort {
324+ Name : "p1" ,
325+ Port : 80 ,
326+ TargetPort : intstr .FromInt (80 ),
327+ Protocol : corev1 .ProtocolTCP ,
328+ NodePort : 31223 ,
329+ },
330+ success : true ,
331+ },
332+ {
333+ name : "two tcp ports, different target and node ports" ,
334+ ports : []corev1.ServicePort {
335+ {
336+ Name : "p1" ,
337+ Port : 80 ,
338+ TargetPort : intstr .FromInt (80 ),
339+ Protocol : corev1 .ProtocolTCP ,
340+ NodePort : 31223 ,
341+ },
342+ {
343+ Name : "p2" ,
344+ Port : 80 ,
345+ TargetPort : intstr .FromInt (8888 ),
346+ Protocol : corev1 .ProtocolTCP ,
347+ NodePort : 31224 ,
348+ },
349+ },
350+ want : corev1.ServicePort {
351+ Name : "p1" ,
352+ Port : 80 ,
353+ TargetPort : intstr .FromInt (80 ),
354+ Protocol : corev1 .ProtocolTCP ,
355+ NodePort : 31223 ,
356+ },
357+ success : true ,
358+ },
359+ {
360+ name : "two udp ports, different target and node ports" ,
361+ ports : []corev1.ServicePort {
362+ {
363+ Name : "p1" ,
364+ Port : 80 ,
365+ TargetPort : intstr .FromInt (80 ),
366+ Protocol : corev1 .ProtocolUDP ,
367+ NodePort : 31223 ,
368+ },
369+ {
370+ Name : "p2" ,
371+ Port : 80 ,
372+ TargetPort : intstr .FromInt (8888 ),
373+ Protocol : corev1 .ProtocolUDP ,
374+ NodePort : 31224 ,
375+ },
376+ },
377+ want : corev1.ServicePort {
378+ Name : "p1" ,
379+ Port : 80 ,
380+ TargetPort : intstr .FromInt (80 ),
381+ Protocol : corev1 .ProtocolUDP ,
382+ NodePort : 31223 ,
383+ },
384+ success : true ,
385+ },
386+ {
387+ name : "one tcp and one udp, different target and node ports" ,
388+ ports : []corev1.ServicePort {
389+ {
390+ Name : "p1" ,
391+ Port : 80 ,
392+ TargetPort : intstr .FromInt (80 ),
393+ Protocol : corev1 .ProtocolTCP ,
394+ NodePort : 31223 ,
395+ },
396+ {
397+ Name : "p2" ,
398+ Port : 80 ,
399+ TargetPort : intstr .FromInt (8888 ),
400+ Protocol : corev1 .ProtocolUDP ,
401+ NodePort : 31224 ,
402+ },
403+ },
404+ want : corev1.ServicePort {
405+ Name : "p1" ,
406+ Port : 80 ,
407+ TargetPort : intstr .FromInt (80 ),
408+ Protocol : corev1 .ProtocolTCP ,
409+ NodePort : 31223 ,
410+ },
411+ success : true ,
412+ },
413+ {
414+ name : "one tcp and one udp, same target and node ports" ,
415+ ports : []corev1.ServicePort {
416+ {
417+ Name : "p1" ,
418+ Port : 80 ,
419+ TargetPort : intstr .FromInt (80 ),
420+ Protocol : corev1 .ProtocolTCP ,
421+ NodePort : 31223 ,
422+ },
423+ {
424+ Name : "p2" ,
425+ Port : 80 ,
426+ TargetPort : intstr .FromInt (80 ),
427+ Protocol : corev1 .ProtocolUDP ,
428+ NodePort : 31223 ,
429+ },
430+ },
431+ want : corev1.ServicePort {
432+ Name : "p1" ,
433+ Port : 80 ,
434+ TargetPort : intstr .FromInt (80 ),
435+ Protocol : corev1 .Protocol ("TCP_UDP" ),
436+ NodePort : 31223 ,
437+ },
438+ success : true ,
439+ },
440+ {
441+ name : "one udp and one tcp, same target and node ports" ,
442+ ports : []corev1.ServicePort {
443+ {
444+ Name : "p1" ,
445+ Port : 80 ,
446+ TargetPort : intstr .FromInt (80 ),
447+ Protocol : corev1 .ProtocolUDP ,
448+ NodePort : 31223 ,
449+ },
450+ {
451+ Name : "p2" ,
452+ Port : 80 ,
453+ TargetPort : intstr .FromInt (80 ),
454+ Protocol : corev1 .ProtocolTCP ,
455+ NodePort : 31223 ,
456+ },
457+ },
458+ want : corev1.ServicePort {
459+ Name : "p1" ,
460+ Port : 80 ,
461+ TargetPort : intstr .FromInt (80 ),
462+ Protocol : corev1 .Protocol ("TCP_UDP" ),
463+ NodePort : 31223 ,
464+ },
465+ success : true ,
466+ },
467+ {
468+ name : "one tcp and one udp, same node port, different target port" ,
469+ ports : []corev1.ServicePort {
470+ {
471+ Name : "p1" ,
472+ Port : 80 ,
473+ TargetPort : intstr .FromInt (80 ),
474+ Protocol : corev1 .ProtocolTCP ,
475+ NodePort : 31223 ,
476+ },
477+ {
478+ Name : "p2" ,
479+ Port : 80 ,
480+ TargetPort : intstr .FromInt (80 ),
481+ Protocol : corev1 .ProtocolUDP ,
482+ NodePort : 31223 ,
483+ },
484+ },
485+ want : corev1.ServicePort {
486+ Name : "p1" ,
487+ Port : 80 ,
488+ TargetPort : intstr .FromInt (80 ),
489+ Protocol : corev1 .Protocol ("TCP_UDP" ),
490+ NodePort : 31223 ,
491+ },
492+ success : true ,
493+ },
494+ {
495+ name : "one tcp and one udp, same node port, different target port" ,
496+ ports : []corev1.ServicePort {
497+ {
498+ Name : "p1" ,
499+ Port : 80 ,
500+ TargetPort : intstr .FromInt (80 ),
501+ Protocol : corev1 .ProtocolTCP ,
502+ NodePort : 31223 ,
503+ },
504+ {
505+ Name : "p2" ,
506+ Port : 80 ,
507+ TargetPort : intstr .FromInt (80 ),
508+ Protocol : corev1 .ProtocolUDP ,
509+ NodePort : 31223 ,
510+ },
511+ {
512+ Name : "p2" ,
513+ Port : 80 ,
514+ TargetPort : intstr .FromInt (80 ),
515+ Protocol : corev1 .ProtocolSCTP ,
516+ NodePort : 31223 ,
517+ },
518+ },
519+ want : corev1.ServicePort {},
520+ success : false ,
521+ },
522+ }
523+
524+ for _ , tt := range tests {
525+ t .Run (tt .name , func (t * testing.T ) {
526+ port , err := mergeServicePortsForListener (tt .ports )
527+ if ! tt .success {
528+ assert .NotNil (t , err )
529+ return
530+ }
531+ assert .Equal (t , port .Name , tt .want .Name )
532+ assert .Equal (t , port .Port , tt .want .Port )
533+ assert .Equal (t , port .TargetPort .IntVal , tt .want .TargetPort .IntVal )
534+ assert .Equal (t , port .Protocol , tt .want .Protocol )
535+ assert .Equal (t , port .NodePort , tt .want .NodePort )
536+ })
537+ }
538+ }
0 commit comments