@@ -107,6 +107,16 @@ func getBaseClusterScopeWithLabels() (*scope.ClusterScope, error) {
107
107
return clusterScope , nil
108
108
}
109
109
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
+
110
120
func TestService_createOrGetInstanceGroup (t * testing.T ) {
111
121
tests := []struct {
112
122
name string
@@ -451,6 +461,7 @@ func TestService_createOrGetAddress(t *testing.T) {
451
461
mockAddress * cloud.MockGlobalAddresses
452
462
want * compute.Address
453
463
wantErr bool
464
+ sharedVPC bool
454
465
}{
455
466
{
456
467
name : "address does not exist for external load balancer (should create address)" ,
@@ -467,11 +478,33 @@ func TestService_createOrGetAddress(t *testing.T) {
467
478
AddressType : "EXTERNAL" ,
468
479
},
469
480
},
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
+ },
470
497
}
471
498
for _ , tt := range tests {
472
499
t .Run (tt .name , func (t * testing.T ) {
473
500
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
+ }
475
508
if err != nil {
476
509
t .Fatal (err )
477
510
}
@@ -498,6 +531,7 @@ func TestService_createOrGetInternalAddress(t *testing.T) {
498
531
mockSubnetworks * cloud.MockSubnetworks
499
532
want * compute.Address
500
533
wantErr bool
534
+ sharedVPC bool
501
535
}{
502
536
{
503
537
name : "address does not exist for internal load balancer (should create address)" ,
@@ -527,11 +561,46 @@ func TestService_createOrGetInternalAddress(t *testing.T) {
527
561
Purpose : "GCE_ENDPOINT" ,
528
562
},
529
563
},
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
+ },
530
593
}
531
594
for _ , tt := range tests {
532
595
t .Run (tt .name , func (t * testing.T ) {
533
596
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
+ }
535
604
if err != nil {
536
605
t .Fatal (err )
537
606
}
0 commit comments