-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Multi Network support for the ILB L4 controller.
- Loading branch information
Showing
12 changed files
with
435 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package backends | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud" | ||
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta" | ||
v1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/cloud-provider-gcp/providers/gce" | ||
"k8s.io/ingress-gce/pkg/multinetwork" | ||
"k8s.io/ingress-gce/pkg/utils" | ||
namer "k8s.io/ingress-gce/pkg/utils/namer" | ||
) | ||
|
||
const ( | ||
kubeSystemUID = "ksuid123" | ||
) | ||
|
||
func TestEnsureL4BackendService(t *testing.T) { | ||
serviceName := types.NamespacedName{Name: "test-service", Namespace: "test-ns"} | ||
fakeGCE := gce.NewFakeGCECloud(gce.DefaultTestClusterValues()) | ||
l4namer := namer.NewL4Namer(kubeSystemUID, nil) | ||
backendPool := NewPool(fakeGCE, l4namer) | ||
|
||
hcLink := l4namer.L4HealthCheck(serviceName.Namespace, serviceName.Name, false) | ||
bsName := l4namer.L4Backend(serviceName.Namespace, serviceName.Name) | ||
network := &multinetwork.NetworkInfo{NetworkURL: "https://www.googleapis.com/compute/v1/projects/test-poject/global/networks/test-vpc"} | ||
bs, err := backendPool.EnsureL4BackendService(bsName, hcLink, "TCP", string(v1.ServiceAffinityNone), string(cloud.SchemeInternal), serviceName, network) | ||
if err != nil { | ||
t.Errorf("EnsureL4BackendService failed") | ||
} | ||
|
||
if bs.SessionAffinity != strings.ToUpper(string(v1.ServiceAffinityNone)) { | ||
t.Errorf("BackendService.SessionAffinity was not populated correctly want=%q, got=%q", strings.ToUpper(string(v1.ServiceAffinityNone)), bs.SessionAffinity) | ||
} | ||
if bs.Network != network.NetworkURL { | ||
t.Errorf("BackendService.Network was not populated correctly, want=%q, got=%q", network.NetworkURL, bs.Network) | ||
} | ||
if len(bs.HealthChecks) != 1 || bs.HealthChecks[0] != hcLink { | ||
t.Errorf("BackendService.HealthChecks was not populated correctly, want=%q, got=%q", hcLink, bs.HealthChecks) | ||
} | ||
description, err := utils.MakeL4LBServiceDescription(serviceName.String(), "", meta.VersionGA, false, utils.ILB) | ||
if err != nil { | ||
t.Errorf("utils.MakeL4LBServiceDescription() failed %v", err) | ||
} | ||
if bs.Description != description { | ||
t.Errorf("BackendService.Description was not populated correctly, want=%q, got=%q", description, bs.Description) | ||
} | ||
if bs.Protocol != "TCP" { | ||
t.Errorf("BackendService.Protocol was not populated correctly, want=%q, got=%q", "TCP", bs.Protocol) | ||
} | ||
if bs.LoadBalancingScheme != string(cloud.SchemeInternal) { | ||
t.Errorf("BackendService.LoadBalancingScheme was not populated correctly, want=%q, got=%q", string(cloud.SchemeInternal), bs.LoadBalancingScheme) | ||
} | ||
if bs.ConnectionDraining == nil || bs.ConnectionDraining.DrainingTimeoutSec != DefaultConnectionDrainingTimeoutSeconds { | ||
t.Errorf("BackendService.ConnectionDraining was not populated correctly, want=connection draining with %q, got=%q", DefaultConnectionDrainingTimeoutSeconds, bs.ConnectionDraining) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package firewalls | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
compute "google.golang.org/api/compute/v1" | ||
"k8s.io/cloud-provider-gcp/providers/gce" | ||
"k8s.io/ingress-gce/pkg/multinetwork" | ||
"k8s.io/ingress-gce/pkg/utils" | ||
) | ||
|
||
func TestEnsureL4FirewallRule(t *testing.T) { | ||
firewallDescription, err := utils.MakeL4LBFirewallDescription(utils.ServiceKeyFunc("test-ns", "test-name"), "10.0.0.1", meta.VersionGA, false) | ||
if err != nil { | ||
t.Errorf("Failed making the description, err=%v", err) | ||
} | ||
tests := []struct { | ||
desc string | ||
nsName string | ||
params *FirewallParams | ||
shared bool | ||
want *compute.Firewall | ||
}{ | ||
{ | ||
desc: "default setup", | ||
nsName: utils.ServiceKeyFunc("test-ns", "test-name"), | ||
params: &FirewallParams{ | ||
Name: "test-firewall", | ||
IP: "10.0.0.1", | ||
SourceRanges: []string{ | ||
"10.1.2.8/29", | ||
}, | ||
DestinationRanges: []string{ | ||
"10.1.2.16/29", | ||
}, | ||
PortRanges: []string{"8080"}, | ||
NodeNames: []string{"k8s-test-node"}, | ||
Protocol: "TCP", | ||
L4Type: utils.ILB, | ||
Network: nil, | ||
}, | ||
shared: false, | ||
want: &compute.Firewall{ | ||
Name: "test-firewall", | ||
Network: "", | ||
SourceRanges: []string{ | ||
"10.1.2.8/29", | ||
}, | ||
TargetTags: []string{"k8s-test"}, | ||
Description: firewallDescription, | ||
Allowed: []*compute.FirewallAllowed{ | ||
{ | ||
IPProtocol: "tcp", | ||
Ports: []string{"8080"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
desc: "non default network", | ||
nsName: utils.ServiceKeyFunc("test-ns", "test-name"), | ||
params: &FirewallParams{ | ||
Name: "test-firewall", | ||
IP: "10.0.0.1", | ||
SourceRanges: []string{ | ||
"10.1.2.8/29", | ||
}, | ||
PortRanges: []string{"8080"}, | ||
NodeNames: []string{"k8s-test-node"}, | ||
Protocol: "TCP", | ||
L4Type: utils.ILB, | ||
Network: &multinetwork.NetworkInfo{ | ||
NetworkURL: "https://www.googleapis.com/compute/v1/projects/test-poject/global/networks/test-vpc", | ||
}, | ||
}, | ||
shared: false, | ||
want: &compute.Firewall{ | ||
Name: "test-firewall", | ||
Network: "https://www.googleapis.com/compute/v1/projects/test-poject/global/networks/test-vpc", | ||
SourceRanges: []string{ | ||
"10.1.2.8/29", | ||
}, | ||
TargetTags: []string{"k8s-test"}, | ||
Description: firewallDescription, | ||
Allowed: []*compute.FirewallAllowed{ | ||
{ | ||
IPProtocol: "tcp", | ||
Ports: []string{"8080"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tc := range tests { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
fakeGCE := gce.NewFakeGCECloud(gce.DefaultTestClusterValues()) | ||
// Add some instance to act as the node so that target tags in the firewall can be resolved. | ||
err = fakeGCE.Compute().Instances().Insert(context.Background(), | ||
meta.ZonalKey("k8s-test-node", "us-central1-b"), | ||
&compute.Instance{ | ||
Name: "test-node", | ||
Zone: "us-central1-b", | ||
Tags: &compute.Tags{ | ||
Items: []string{"k8s-test"}, | ||
}, | ||
}) | ||
if err != nil { | ||
t.Errorf("failed to create instance err=%v", err) | ||
} | ||
if err := EnsureL4FirewallRule(fakeGCE, tc.nsName, tc.params, tc.shared); err != nil { | ||
t.Errorf("EnsureL4FirewallRule() failed, err=%v", err) | ||
} | ||
firewall, err := fakeGCE.GetFirewall(tc.params.Name) | ||
if err != nil { | ||
t.Errorf("failed to get firewall err=%v", err) | ||
} | ||
if diff := cmp.Diff(tc.want, firewall, cmpopts.IgnoreFields(compute.Firewall{}, "SelfLink")); diff != "" { | ||
t.Errorf("EnsureL4FirewallRule() diff -want +got\n%v\n", diff) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.