Skip to content

Commit

Permalink
feat: add sharedvpc support for lb subnet
Browse files Browse the repository at this point in the history
fix: set cloudScope for regionalhc and tgttcpproxies

add test for lb w/ shared vpc

add more test cases

revert regionlhc and targettcpproxies
  • Loading branch information
swarren83 authored and Shawn Warren committed Jul 11, 2024
1 parent 3c2e7de commit 0207069
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
73 changes: 71 additions & 2 deletions cloud/services/compute/loadbalancers/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ func getBaseClusterScopeWithLabels() (*scope.ClusterScope, error) {
return clusterScope, nil
}

func getBaseClusterScopeWithSharedVPC() (*scope.ClusterScope, error) {
clusterScope, err := getBaseClusterScope()
if err != nil {
return nil, err
}

clusterScope.GCPCluster.Spec.Network.HostProject = ptr.To("my-shared-vpc-project")
return clusterScope, nil
}

func TestService_createOrGetInstanceGroup(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -451,6 +461,7 @@ func TestService_createOrGetAddress(t *testing.T) {
mockAddress *cloud.MockGlobalAddresses
want *compute.Address
wantErr bool
sharedVPC bool
}{
{
name: "address does not exist for external load balancer (should create address)",
Expand All @@ -467,11 +478,33 @@ func TestService_createOrGetAddress(t *testing.T) {
AddressType: "EXTERNAL",
},
},
{
name: "address does not exist for external load balancer in shared VPC (should create address)",
scope: func(s *scope.ClusterScope) Scope { return s },
lbName: infrav1.APIServerRoleTagValue,
mockAddress: &cloud.MockGlobalAddresses{
ProjectRouter: &cloud.SingleProjectRouter{ID: "proj-id"},
Objects: map[meta.Key]*cloud.MockGlobalAddressesObj{},
},
want: &compute.Address{
IpVersion: "IPV4",
Name: "my-cluster-apiserver",
SelfLink: "https://www.googleapis.com/compute/v1/projects/proj-id/global/addresses/my-cluster-apiserver",
AddressType: "EXTERNAL",
},
sharedVPC: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.TODO()
clusterScope, err := getBaseClusterScope()
var err error
var clusterScope *scope.ClusterScope
if tt.sharedVPC {
clusterScope, err = getBaseClusterScopeWithSharedVPC()
} else {
clusterScope, err = getBaseClusterScope()
}
if err != nil {
t.Fatal(err)
}
Expand All @@ -498,6 +531,7 @@ func TestService_createOrGetInternalAddress(t *testing.T) {
mockSubnetworks *cloud.MockSubnetworks
want *compute.Address
wantErr bool
sharedVPC bool
}{
{
name: "address does not exist for internal load balancer (should create address)",
Expand Down Expand Up @@ -527,11 +561,46 @@ func TestService_createOrGetInternalAddress(t *testing.T) {
Purpose: "GCE_ENDPOINT",
},
},
{
name: "address does not exist for internal load balancer using SharedVPC subnet (should create address)",
scope: func(s *scope.ClusterScope) Scope {
s.GCPCluster.Spec.LoadBalancer = infrav1.LoadBalancerSpec{
LoadBalancerType: &lbTypeInternal,
}
return s
},
lbName: infrav1.InternalRoleTagValue,
mockAddress: &cloud.MockAddresses{
ProjectRouter: &cloud.SingleProjectRouter{ID: "proj-id"},
Objects: map[meta.Key]*cloud.MockAddressesObj{},
},
mockSubnetworks: &cloud.MockSubnetworks{
ProjectRouter: &cloud.SingleProjectRouter{ID: "proj-id"},
Objects: map[meta.Key]*cloud.MockSubnetworksObj{
*meta.RegionalKey("control-plane", "us-central1"): {},
},
},
want: &compute.Address{
IpVersion: "IPV4",
Name: "my-cluster-api-internal",
Region: "us-central1",
SelfLink: "https://www.googleapis.com/compute/v1/projects/proj-id/regions/us-central1/addresses/my-cluster-api-internal",
AddressType: "INTERNAL",
Purpose: "GCE_ENDPOINT",
},
sharedVPC: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.TODO()
clusterScope, err := getBaseClusterScope()
var err error
var clusterScope *scope.ClusterScope
if tt.sharedVPC {
clusterScope, err = getBaseClusterScopeWithSharedVPC()
} else {
clusterScope, err = getBaseClusterScope()
}
if err != nil {
t.Fatal(err)
}
Expand Down
7 changes: 6 additions & 1 deletion cloud/services/compute/loadbalancers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ var _ cloud.Reconciler = &Service{}

// New returns Service from given scope.
func New(scope Scope) *Service {
cloudScope := scope.Cloud()
if scope.IsSharedVpc() {
cloudScope = scope.NetworkCloud()
}

return &Service{
scope: scope,
addresses: scope.Cloud().GlobalAddresses(),
Expand All @@ -113,6 +118,6 @@ func New(scope Scope) *Service {
regionalhealthchecks: scope.Cloud().RegionHealthChecks(),
instancegroups: scope.Cloud().InstanceGroups(),
targettcpproxies: scope.Cloud().TargetTcpProxies(),
subnets: scope.Cloud().Subnetworks(),
subnets: cloudScope.Subnetworks(),
}
}

0 comments on commit 0207069

Please sign in to comment.