@@ -41,7 +41,10 @@ type FirewallCR struct {
4141 // all the port ranges to open with each call to Sync()
4242 nodePortRanges []string
4343 firewallClient firewallclient.Interface
44- dryRun bool
44+ // If set to true, CRs are marked as "disabled" and errors are not
45+ // propagated. Firewalls should be still created/updated by l7LB, not the
46+ // Platform Firewall
47+ dryRun bool
4548
4649 logger klog.Logger
4750}
@@ -81,17 +84,17 @@ func (fr *FirewallCR) Sync(nodeNames, additionalPorts, additionalRanges []string
8184 ranges .Insert (additionalRanges ... )
8285
8386 fr .logger .V (3 ).Info ("Firewall CR is enabled." )
84- expectedFirewallCR , err := NewFirewallCR (name , ports .List (), ranges .UnsortedList (), []string {}, fr .dryRun )
87+ expectedFirewallCR , err := NewFirewallCR (name , ports .List (), ranges .UnsortedList (), []string {}, ! fr .dryRun )
8588 if err != nil {
8689 return err
8790 }
88- return ensureFirewallCR (fr .firewallClient , expectedFirewallCR , fr .logger )
91+ return ensureFirewallCR (fr .firewallClient , expectedFirewallCR , fr .logger , fr . dryRun )
8992}
9093
9194// ensureFirewallCR creates/updates the firewall CR
9295// On CR update, it will read the conditions to see if there are errors updated by PFW controller.
9396// If the Spec was updated by others, it will reconcile the Spec.
94- func ensureFirewallCR (client firewallclient.Interface , expectedFWCR * gcpfirewallv1.GCPFirewall , logger klog.Logger ) error {
97+ func ensureFirewallCR (client firewallclient.Interface , expectedFWCR * gcpfirewallv1.GCPFirewall , logger klog.Logger , dryRun bool ) error {
9598 fw := client .NetworkingV1 ().GCPFirewalls ()
9699 currentFWCR , err := fw .Get (context .Background (), expectedFWCR .Name , metav1.GetOptions {})
97100 logger .V (3 ).Info ("ensureFirewallCR Get CR" , "currentFirewallCR" , fmt .Sprintf ("%+v" , currentFWCR ), "err" , err )
@@ -103,6 +106,10 @@ func ensureFirewallCR(client firewallclient.Interface, expectedFWCR *gcpfirewall
103106 }
104107 return err
105108 }
109+ if currentFWCR .DeletionTimestamp != nil {
110+ logger .V (3 ).Info ("ensureFirewallCR: The CR contains DeletionTimestamp. Skipping Ensure" , "currentFirewallDeletionTimestamp" , fmt .Sprintf ("%+v" , currentFWCR .DeletionTimestamp ))
111+ return nil
112+ }
106113 if ! reflect .DeepEqual (currentFWCR .Spec , expectedFWCR .Spec ) {
107114 // Update the current firewall CR
108115 logger .V (3 ).Info ("ensureFirewallCR Update CR" , "currentFirewallCR" , fmt .Sprintf ("%+v" , currentFWCR .Spec ), "expectedFirewallCR" , fmt .Sprintf ("%+v" , expectedFWCR .Spec ))
@@ -116,7 +123,11 @@ func ensureFirewallCR(client firewallclient.Interface, expectedFWCR *gcpfirewall
116123 con .Reason == string (gcpfirewallv1 .FirewallRuleReasonSyncError ) {
117124 // Use recorder to emit the cmd in Sync()
118125 logger .V (3 ).Info ("ensureFirewallCR: Could not enforce Firewall CR" , "currentFirewallCRName" , currentFWCR .Name , "reason" , con .Reason )
119- return fmt .Errorf (con .Reason )
126+ if dryRun {
127+ return nil
128+ } else {
129+ return fmt .Errorf (con .Reason )
130+ }
120131 }
121132 }
122133 return nil
@@ -169,6 +180,15 @@ func NewFirewallCR(name string, ports, srcRanges, dstRanges []string, enforced b
169180 }
170181 protocolPorts = append (protocolPorts , protocolPort )
171182 }
183+
184+ // TCP:all is the default if the ports' list is empty.
185+ if len (protocolPorts ) == 0 {
186+ protocolPort := gcpfirewallv1.ProtocolPort {
187+ Protocol : gcpfirewallv1 .ProtocolTCP ,
188+ }
189+ protocolPorts = append (protocolPorts , protocolPort )
190+ }
191+
172192 firewallCR .Spec .Ports = protocolPorts
173193
174194 var src_cidrs , dst_cidrs []gcpfirewallv1.CIDR
0 commit comments