-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[GLBC] Update firewall source ranges if outdated #574
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,17 +75,29 @@ func (fr *FirewallRules) Sync(nodePorts []int64, nodeNames []string) error { | |
existingPorts.Insert(p) | ||
} | ||
} | ||
if requiredPorts.Equal(existingPorts) { | ||
|
||
requiredCIDRs := sets.NewString(l7SrcRanges...) | ||
existingCIDRs := sets.NewString(rule.SourceRanges...) | ||
|
||
// Do not update if ports and source cidrs are not outdated. | ||
// NOTE: We are not checking if nodeNames matches the firwall targetTags | ||
if requiredPorts.Equal(existingPorts) && requiredCIDRs.Equal(existingCIDRs) { | ||
return nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. glog.V(4).Infof("No changes... |
||
} | ||
|
||
glog.V(3).Infof("Firewall rule %v already exists, updating nodeports %v", name, nodePorts) | ||
return fr.cloud.UpdateFirewall(suffix, "GCE L7 firewall rule", fr.srcRanges, nodePorts, nodeNames) | ||
} | ||
|
||
// Shutdown shuts down this firewall rules manager. | ||
func (fr *FirewallRules) Shutdown() error { | ||
glog.Infof("Deleting firewall rule with suffix %v", fr.namer.FrSuffix()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [minor] technically multiple rule(s)? |
||
return fr.cloud.DeleteFirewall(fr.namer.FrSuffix()) | ||
err := fr.cloud.DeleteFirewall(fr.namer.FrSuffix()) | ||
if err != nil && utils.IsHTTPErrorCode(err, 404) { | ||
glog.Infof("Firewall with suffix %v didn't exist at Shutdown", fr.namer.FrSuffix()) | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
// GetFirewall just returns the firewall object corresponding to the given name. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package firewalls | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. copyright |
||
|
||
import ( | ||
"strconv" | ||
"testing" | ||
|
||
"k8s.io/apimachinery/pkg/util/sets" | ||
"k8s.io/ingress/controllers/gce/utils" | ||
netset "k8s.io/kubernetes/pkg/util/net/sets" | ||
) | ||
|
||
func TestSyncFirewallPool(t *testing.T) { | ||
namer := utils.NewNamer("ABC", "XYZ") | ||
fwp := NewFakeFirewallsProvider(namer) | ||
fp := NewFirewallPool(fwp, namer) | ||
ruleName := namer.FrName(namer.FrSuffix()) | ||
|
||
// Test creating a firewall rule via Sync | ||
nodePorts := []int64{80, 443, 3000} | ||
nodes := []string{"node-a", "node-b", "node-c"} | ||
err := fp.Sync(nodePorts, nodes) | ||
if err != nil { | ||
t.Errorf("unexpected err when syncing firewall, err: %v", err) | ||
} | ||
verifyFirewallRule(fwp, ruleName, nodePorts, nodes, l7SrcRanges, t) | ||
|
||
// Sync to fewer ports | ||
nodePorts = []int64{80, 443} | ||
err = fp.Sync(nodePorts, nodes) | ||
if err != nil { | ||
t.Errorf("unexpected err when syncing firewall, err: %v", err) | ||
} | ||
verifyFirewallRule(fwp, ruleName, nodePorts, nodes, l7SrcRanges, t) | ||
|
||
all := "0.0.0.0/0" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. const |
||
srcRanges, _ := netset.ParseIPNets(all) | ||
err = fwp.UpdateFirewall(namer.FrSuffix(), "", srcRanges, nodePorts, nodes) | ||
if err != nil { | ||
t.Errorf("failed to update firewall rule, err: %v", err) | ||
} | ||
verifyFirewallRule(fwp, ruleName, nodePorts, nodes, []string{all}, t) | ||
|
||
// Run Sync and expect l7 src ranges to be returned | ||
err = fp.Sync(nodePorts, nodes) | ||
if err != nil { | ||
t.Errorf("unexpected err when syncing firewall, err: %v", err) | ||
} | ||
verifyFirewallRule(fwp, ruleName, nodePorts, nodes, l7SrcRanges, t) | ||
|
||
// Add node and expect firwall to remain the same | ||
// NOTE: See computeHostTag(..) in gce cloudprovider | ||
nodes = []string{"node-a", "node-b", "node-c", "node-d"} | ||
err = fp.Sync(nodePorts, nodes) | ||
if err != nil { | ||
t.Errorf("unexpected err when syncing firewall, err: %v", err) | ||
} | ||
verifyFirewallRule(fwp, ruleName, nodePorts, nodes, l7SrcRanges, t) | ||
|
||
// Remove all ports and expect firewall rule to disappear | ||
nodePorts = []int64{} | ||
err = fp.Sync(nodePorts, nodes) | ||
if err != nil { | ||
t.Errorf("unexpected err when syncing firewall, err: %v", err) | ||
} | ||
|
||
err = fp.Shutdown() | ||
if err != nil { | ||
t.Errorf("unexpected err when deleting firewall, err: %v", err) | ||
} | ||
} | ||
|
||
func verifyFirewallRule(fwp *fakeFirewallsProvider, ruleName string, expectedPorts []int64, expectedNodes, expectedCIDRs []string, t *testing.T) { | ||
var strPorts []string | ||
for _, v := range expectedPorts { | ||
strPorts = append(strPorts, strconv.FormatInt(v, 10)) | ||
} | ||
|
||
// Verify firewall rule was created | ||
f, err := fwp.GetFirewall(ruleName) | ||
if err != nil { | ||
t.Errorf("could not retrieve firewall via cloud api, err %v", err) | ||
} | ||
|
||
// Verify firwall rule has correct ports | ||
if !sets.NewString(f.Allowed[0].Ports...).Equal(sets.NewString(strPorts...)) { | ||
t.Errorf("allowed ports doesn't equal expected ports, Actual: %v, Expected: %v", f.Allowed[0].Ports, strPorts) | ||
} | ||
|
||
// Verify firwall rule has correct CIDRs | ||
if !sets.NewString(f.SourceRanges...).Equal(sets.NewString(expectedCIDRs...)) { | ||
t.Errorf("source CIDRs doesn't equal expected CIDRs. Actual: %v, Expected: %v", f.SourceRanges, expectedCIDRs) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sp. firwall