@@ -20,6 +20,7 @@ import (
2020 "context"
2121 "errors"
2222 "fmt"
23+ "strconv"
2324 "testing"
2425 "time"
2526
@@ -296,6 +297,7 @@ func TestFirewallController_createReconciledFirewallRequest(t *testing.T) {
296297 firewallRequest * godo.FirewallRequest
297298 firewallController FirewallController
298299 serviceList []* v1.Service
300+ expectedError error
299301 }{
300302 {
301303 name : "nothing to reconcile when there are no changes" ,
@@ -425,21 +427,21 @@ func TestFirewallController_createReconciledFirewallRequest(t *testing.T) {
425427 InboundRules : []godo.InboundRule {
426428 {
427429 Protocol : "tcp" ,
428- PortRange : "31000 " ,
430+ PortRange : "30000 " ,
429431 Sources : & godo.Sources {
430432 Addresses : []string {"0.0.0.0/0" , "::/0" },
431433 },
432434 },
433435 {
434- Protocol : "udp " ,
436+ Protocol : "tcp " ,
435437 PortRange : "31000" ,
436438 Sources : & godo.Sources {
437439 Addresses : []string {"0.0.0.0/0" , "::/0" },
438440 },
439441 },
440442 {
441443 Protocol : "tcp" ,
442- PortRange : "30000 " ,
444+ PortRange : "32727 " ,
443445 Sources : & godo.Sources {
444446 Addresses : []string {"0.0.0.0/0" , "::/0" },
445447 },
@@ -452,8 +454,8 @@ func TestFirewallController_createReconciledFirewallRequest(t *testing.T) {
452454 },
453455 },
454456 {
455- Protocol : "tcp " ,
456- PortRange : "32727 " ,
457+ Protocol : "udp " ,
458+ PortRange : "31000 " ,
457459 Sources : & godo.Sources {
458460 Addresses : []string {"0.0.0.0/0" , "::/0" },
459461 },
@@ -524,6 +526,119 @@ func TestFirewallController_createReconciledFirewallRequest(t *testing.T) {
524526 },
525527 },
526528 },
529+ {
530+ name : "reconcile firewall with REGIONAL_NETWORK LB w/ externalTrafficPolicy=Cluster" ,
531+ firewallRequest : & godo.FirewallRequest {
532+ Name : testWorkerFWName ,
533+ InboundRules : []godo.InboundRule {
534+ {
535+ Protocol : "tcp" ,
536+ PortRange : strconv .Itoa (kubeProxyHealthPort ),
537+ Sources : & godo.Sources {
538+ Addresses : []string {"0.0.0.0/0" , "::/0" },
539+ },
540+ },
541+ {
542+ Protocol : "tcp" ,
543+ PortRange : strconv .Itoa (443 ),
544+ Sources : & godo.Sources {
545+ Addresses : []string {"0.0.0.0/0" , "::/0" },
546+ },
547+ },
548+ {
549+ Protocol : "tcp" ,
550+ PortRange : strconv .Itoa (80 ),
551+ Sources : & godo.Sources {
552+ Addresses : []string {"0.0.0.0/0" , "::/0" },
553+ },
554+ },
555+ },
556+ OutboundRules : testOutboundRules ,
557+ Tags : testWorkerFWTags ,
558+ },
559+ serviceList : []* v1.Service {
560+ {
561+ ObjectMeta : metav1.ObjectMeta {
562+ Name : "regional_network" ,
563+ UID : "abc123" ,
564+ Annotations : map [string ]string {
565+ annDOType : godo .LoadBalancerTypeRegionalNetwork ,
566+ },
567+ },
568+ Spec : v1.ServiceSpec {
569+ Type : v1 .ServiceTypeLoadBalancer ,
570+ ExternalTrafficPolicy : v1 .ServiceExternalTrafficPolicyCluster ,
571+ Ports : []v1.ServicePort {
572+ {
573+ Protocol : v1 .ProtocolTCP ,
574+ Port : 80 ,
575+ },
576+ {
577+ Protocol : v1 .ProtocolTCP ,
578+ Port : 443 ,
579+ },
580+ },
581+ },
582+ },
583+ },
584+ },
585+ {
586+ name : "reconcile firewall with REGIONAL_NETWORK LB w/ externalTrafficPolicy=Local" ,
587+ firewallRequest : & godo.FirewallRequest {
588+ Name : testWorkerFWName ,
589+ InboundRules : []godo.InboundRule {
590+ {
591+ Protocol : "tcp" ,
592+ PortRange : strconv .Itoa (15000 ),
593+ Sources : & godo.Sources {
594+ Addresses : []string {"0.0.0.0/0" , "::/0" },
595+ },
596+ },
597+ {
598+ Protocol : "tcp" ,
599+ PortRange : strconv .Itoa (443 ),
600+ Sources : & godo.Sources {
601+ Addresses : []string {"0.0.0.0/0" , "::/0" },
602+ },
603+ },
604+ {
605+ Protocol : "tcp" ,
606+ PortRange : strconv .Itoa (80 ),
607+ Sources : & godo.Sources {
608+ Addresses : []string {"0.0.0.0/0" , "::/0" },
609+ },
610+ },
611+ },
612+ OutboundRules : testOutboundRules ,
613+ Tags : testWorkerFWTags ,
614+ },
615+ serviceList : []* v1.Service {
616+ {
617+ ObjectMeta : metav1.ObjectMeta {
618+ Name : "regional_network" ,
619+ UID : "abc123" ,
620+ Annotations : map [string ]string {
621+ annDOType : godo .LoadBalancerTypeRegionalNetwork ,
622+ },
623+ },
624+ Spec : v1.ServiceSpec {
625+ Type : v1 .ServiceTypeLoadBalancer ,
626+ ExternalTrafficPolicy : v1 .ServiceExternalTrafficPolicyLocal ,
627+ HealthCheckNodePort : 15000 ,
628+ Ports : []v1.ServicePort {
629+ {
630+ Protocol : v1 .ProtocolTCP ,
631+ Port : 80 ,
632+ },
633+ {
634+ Protocol : v1 .ProtocolTCP ,
635+ Port : 443 ,
636+ },
637+ },
638+ },
639+ },
640+ },
641+ },
527642 {
528643 name : "reconcile firewall with management flag" ,
529644 firewallRequest : & godo.FirewallRequest {
@@ -638,7 +753,11 @@ func TestFirewallController_createReconciledFirewallRequest(t *testing.T) {
638753 workerFirewallTags : testWorkerFWTags ,
639754 workerFirewallName : testWorkerFWName ,
640755 }
641- fwReq := fm .createReconciledFirewallRequest (test .serviceList )
756+ fwReq , err := fm .createReconciledFirewallRequest (test .serviceList )
757+
758+ if (err != nil && test .expectedError == nil ) || (err == nil && test .expectedError != nil ) {
759+ t .Fatalf ("expected error %q, got %q" , test .expectedError , err )
760+ }
642761 if diff := cmp .Diff (test .firewallRequest , fwReq ); diff != "" {
643762 t .Errorf ("createReconciledFirewallRequest() mismatch (-want +got):\n %s" , diff )
644763 }
@@ -675,15 +794,18 @@ func TestFirewallController_ensureReconciledFirewall(t *testing.T) {
675794 }
676795 }
677796
678- serviceToFirewall := func (fm * firewallManager , svc * v1.Service ) * godo.Firewall {
679- fr := fm .createReconciledFirewallRequest ([]* v1.Service {svc })
797+ serviceToFirewall := func (fm * firewallManager , svc * v1.Service ) (* godo.Firewall , error ) {
798+ fr , err := fm .createReconciledFirewallRequest ([]* v1.Service {svc })
799+ if err != nil {
800+ return nil , err
801+ }
680802 return & godo.Firewall {
681803 ID : "id" ,
682804 Name : fr .Name ,
683805 InboundRules : fr .InboundRules ,
684806 OutboundRules : fr .OutboundRules ,
685807 Tags : fr .Tags ,
686- }
808+ }, nil
687809 }
688810
689811 tests := []struct {
@@ -757,7 +879,10 @@ func TestFirewallController_ensureReconciledFirewall(t *testing.T) {
757879
758880 // Populate the firewall cache.
759881 if test .nodePortForCachedFirewall != nil {
760- fw := serviceToFirewall (fwManager , nodePortToService (* test .nodePortForCachedFirewall ))
882+ fw , err := serviceToFirewall (fwManager , nodePortToService (* test .nodePortForCachedFirewall ))
883+ if err != nil {
884+ t .Fatal (err )
885+ }
761886 fwManager .fwCache .updateCache (fw )
762887 }
763888
0 commit comments