Skip to content
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

Fix firewall cr creation #2808

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pkg/firewalls/firewalls_l7_cr.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ func NewFirewallCR(name string, ports, srcRanges, dstRanges []string, enforced b
}
protocolPorts = append(protocolPorts, protocolPort)
}

// TCP:all is the default if the ports' list is empty.
if len(protocolPorts) == 0 {
protocolPort := gcpfirewallv1.ProtocolPort{
Protocol: gcpfirewallv1.ProtocolTCP,
}
protocolPorts = append(protocolPorts, protocolPort)
}

firewallCR.Spec.Ports = protocolPorts

var src_cidrs, dst_cidrs []gcpfirewallv1.CIDR
Expand Down
29 changes: 26 additions & 3 deletions pkg/firewalls/firewalls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,30 @@ func TestFirewallPoolSyncSrcRanges(t *testing.T) {
func TestFirewallPoolSyncPorts(t *testing.T) {
fwp := NewFakeFirewallsProvider(false, false)
fwClient := firewallclient.NewSimpleClientset()
fp := NewFirewallPool(fwp, defaultNamer, srcRanges, portRanges(), klog.TODO())
fcrp := NewFirewallCRPool(fwClient, fwp, defaultNamer, srcRanges, portRanges(), true, klog.TODO())
nodes := []string{"node-a", "node-b", "node-c"}
emptyPortRanges := make([]string, 0)

// Verify empty ports' list
fp := NewFirewallPool(fwp, defaultNamer, srcRanges, emptyPortRanges, klog.TODO())
fcrp := NewFirewallCRPool(fwClient, fwp, defaultNamer, srcRanges, emptyPortRanges, true, klog.TODO())

if err := fp.Sync(nodes, nil, nil, true); err != nil {
t.Fatal(err)
}
verifyFirewallRule(fwp, ruleName, nodes, srcRanges, emptyPortRanges, t)

if err := fcrp.Sync(nodes, nil, nil, true); err != nil {
t.Fatal(err)
}
verifyFirewallCR(fwClient, ruleName, srcRanges, emptyPortRanges, true, t)

// Verify a preset ports' list
fp = NewFirewallPool(fwp, defaultNamer, srcRanges, portRanges(), klog.TODO())
fcrp = NewFirewallCRPool(fwClient, fwp, defaultNamer, srcRanges, portRanges(), true, klog.TODO())

if err := fp.Sync(nodes, nil, nil, true); err != nil {
t.Errorf("unexpected err when syncing firewall, err: %v", err)
}
verifyFirewallRule(fwp, ruleName, nodes, srcRanges, portRanges(), t)

if err := fcrp.Sync(nodes, nil, nil, true); err != nil {
Expand Down Expand Up @@ -398,14 +415,20 @@ func verifyFirewallCR(firewallclient *firewallclient.Clientset, ruleName string,
ports := sets.NewString(expectedPorts...)
srcranges := sets.NewString(sourceRanges...)

// Empty ports' list would mean that all protocols are permitted
// (not only TCP)
if len(actualFW.Spec.Ports) == 0 {
t.Errorf("Empty list of allowed protocols is not permited")
}

actualPorts := sets.NewString()
for _, protocolports := range actualFW.Spec.Ports {
if protocolports.Protocol != "TCP" {
t.Errorf("Protocol isn't TCP")
}
if protocolports.EndPort != nil {
actualPorts.Insert(fmt.Sprintf("%d-%d", *protocolports.StartPort, *protocolports.EndPort))
} else {
} else if protocolports.StartPort != nil {
actualPorts.Insert(fmt.Sprintf("%d", *protocolports.StartPort))
}

Expand Down