Skip to content

Commit 0207069

Browse files
swarren83Shawn Warren
authored andcommitted
feat: add sharedvpc support for lb subnet
fix: set cloudScope for regionalhc and tgttcpproxies add test for lb w/ shared vpc add more test cases revert regionlhc and targettcpproxies
1 parent 3c2e7de commit 0207069

File tree

2 files changed

+77
-3
lines changed

2 files changed

+77
-3
lines changed

cloud/services/compute/loadbalancers/reconcile_test.go

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ func getBaseClusterScopeWithLabels() (*scope.ClusterScope, error) {
107107
return clusterScope, nil
108108
}
109109

110+
func getBaseClusterScopeWithSharedVPC() (*scope.ClusterScope, error) {
111+
clusterScope, err := getBaseClusterScope()
112+
if err != nil {
113+
return nil, err
114+
}
115+
116+
clusterScope.GCPCluster.Spec.Network.HostProject = ptr.To("my-shared-vpc-project")
117+
return clusterScope, nil
118+
}
119+
110120
func TestService_createOrGetInstanceGroup(t *testing.T) {
111121
tests := []struct {
112122
name string
@@ -451,6 +461,7 @@ func TestService_createOrGetAddress(t *testing.T) {
451461
mockAddress *cloud.MockGlobalAddresses
452462
want *compute.Address
453463
wantErr bool
464+
sharedVPC bool
454465
}{
455466
{
456467
name: "address does not exist for external load balancer (should create address)",
@@ -467,11 +478,33 @@ func TestService_createOrGetAddress(t *testing.T) {
467478
AddressType: "EXTERNAL",
468479
},
469480
},
481+
{
482+
name: "address does not exist for external load balancer in shared VPC (should create address)",
483+
scope: func(s *scope.ClusterScope) Scope { return s },
484+
lbName: infrav1.APIServerRoleTagValue,
485+
mockAddress: &cloud.MockGlobalAddresses{
486+
ProjectRouter: &cloud.SingleProjectRouter{ID: "proj-id"},
487+
Objects: map[meta.Key]*cloud.MockGlobalAddressesObj{},
488+
},
489+
want: &compute.Address{
490+
IpVersion: "IPV4",
491+
Name: "my-cluster-apiserver",
492+
SelfLink: "https://www.googleapis.com/compute/v1/projects/proj-id/global/addresses/my-cluster-apiserver",
493+
AddressType: "EXTERNAL",
494+
},
495+
sharedVPC: true,
496+
},
470497
}
471498
for _, tt := range tests {
472499
t.Run(tt.name, func(t *testing.T) {
473500
ctx := context.TODO()
474-
clusterScope, err := getBaseClusterScope()
501+
var err error
502+
var clusterScope *scope.ClusterScope
503+
if tt.sharedVPC {
504+
clusterScope, err = getBaseClusterScopeWithSharedVPC()
505+
} else {
506+
clusterScope, err = getBaseClusterScope()
507+
}
475508
if err != nil {
476509
t.Fatal(err)
477510
}
@@ -498,6 +531,7 @@ func TestService_createOrGetInternalAddress(t *testing.T) {
498531
mockSubnetworks *cloud.MockSubnetworks
499532
want *compute.Address
500533
wantErr bool
534+
sharedVPC bool
501535
}{
502536
{
503537
name: "address does not exist for internal load balancer (should create address)",
@@ -527,11 +561,46 @@ func TestService_createOrGetInternalAddress(t *testing.T) {
527561
Purpose: "GCE_ENDPOINT",
528562
},
529563
},
564+
{
565+
name: "address does not exist for internal load balancer using SharedVPC subnet (should create address)",
566+
scope: func(s *scope.ClusterScope) Scope {
567+
s.GCPCluster.Spec.LoadBalancer = infrav1.LoadBalancerSpec{
568+
LoadBalancerType: &lbTypeInternal,
569+
}
570+
return s
571+
},
572+
lbName: infrav1.InternalRoleTagValue,
573+
mockAddress: &cloud.MockAddresses{
574+
ProjectRouter: &cloud.SingleProjectRouter{ID: "proj-id"},
575+
Objects: map[meta.Key]*cloud.MockAddressesObj{},
576+
},
577+
mockSubnetworks: &cloud.MockSubnetworks{
578+
ProjectRouter: &cloud.SingleProjectRouter{ID: "proj-id"},
579+
Objects: map[meta.Key]*cloud.MockSubnetworksObj{
580+
*meta.RegionalKey("control-plane", "us-central1"): {},
581+
},
582+
},
583+
want: &compute.Address{
584+
IpVersion: "IPV4",
585+
Name: "my-cluster-api-internal",
586+
Region: "us-central1",
587+
SelfLink: "https://www.googleapis.com/compute/v1/projects/proj-id/regions/us-central1/addresses/my-cluster-api-internal",
588+
AddressType: "INTERNAL",
589+
Purpose: "GCE_ENDPOINT",
590+
},
591+
sharedVPC: true,
592+
},
530593
}
531594
for _, tt := range tests {
532595
t.Run(tt.name, func(t *testing.T) {
533596
ctx := context.TODO()
534-
clusterScope, err := getBaseClusterScope()
597+
var err error
598+
var clusterScope *scope.ClusterScope
599+
if tt.sharedVPC {
600+
clusterScope, err = getBaseClusterScopeWithSharedVPC()
601+
} else {
602+
clusterScope, err = getBaseClusterScope()
603+
}
535604
if err != nil {
536605
t.Fatal(err)
537606
}

cloud/services/compute/loadbalancers/service.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ var _ cloud.Reconciler = &Service{}
101101

102102
// New returns Service from given scope.
103103
func New(scope Scope) *Service {
104+
cloudScope := scope.Cloud()
105+
if scope.IsSharedVpc() {
106+
cloudScope = scope.NetworkCloud()
107+
}
108+
104109
return &Service{
105110
scope: scope,
106111
addresses: scope.Cloud().GlobalAddresses(),
@@ -113,6 +118,6 @@ func New(scope Scope) *Service {
113118
regionalhealthchecks: scope.Cloud().RegionHealthChecks(),
114119
instancegroups: scope.Cloud().InstanceGroups(),
115120
targettcpproxies: scope.Cloud().TargetTcpProxies(),
116-
subnets: scope.Cloud().Subnetworks(),
121+
subnets: cloudScope.Subnetworks(),
117122
}
118123
}

0 commit comments

Comments
 (0)