11package gcp
22
33import (
4+ "context"
45 "fmt"
56 "net"
7+ "time"
68
79 "github.com/apparentlymart/go-cidr/cidr"
10+ "google.golang.org/api/compute/v1"
11+ "google.golang.org/api/option"
812 corev1 "k8s.io/api/core/v1"
913 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1014 "k8s.io/apimachinery/pkg/util/sets"
@@ -14,6 +18,7 @@ import (
1418
1519 "github.com/openshift/installer/pkg/asset"
1620 "github.com/openshift/installer/pkg/asset/installconfig"
21+ gcpic "github.com/openshift/installer/pkg/asset/installconfig/gcp"
1722 "github.com/openshift/installer/pkg/asset/manifests/capiutils"
1823 gcpconsts "github.com/openshift/installer/pkg/constants/gcp"
1924 "github.com/openshift/installer/pkg/types/gcp"
@@ -33,26 +38,42 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
3338 networkName = installConfig .Config .GCP .Network
3439 }
3540
36- masterSubnet := gcp .DefaultSubnetName (clusterID .InfraID , "master" )
41+ controlPlaneSubnetName := gcp .DefaultSubnetName (clusterID .InfraID , "master" )
42+ controlPlaneSubnetCidr := ""
3743 if installConfig .Config .GCP .ControlPlaneSubnet != "" {
38- masterSubnet = installConfig .Config .GCP .ControlPlaneSubnet
44+ controlPlaneSubnetName = installConfig .Config .GCP .ControlPlaneSubnet
45+
46+ controlPlaneSubnet , err := getSubnet (context .TODO (), installConfig .Config .GCP .NetworkProjectID , installConfig .Config .GCP .Region , controlPlaneSubnetName )
47+ if err != nil {
48+ return nil , fmt .Errorf ("failed to get control plane subnet: %w" , err )
49+ }
50+ // IpCidr is the IPv4 version, the IPv6 version can be accessed as well
51+ controlPlaneSubnetCidr = controlPlaneSubnet .IpCidrRange
3952 }
4053
41- master := capg.SubnetSpec {
42- Name : masterSubnet ,
43- CidrBlock : "" ,
54+ controlPlane := capg.SubnetSpec {
55+ Name : controlPlaneSubnetName ,
56+ CidrBlock : controlPlaneSubnetCidr ,
4457 Description : ptr .To (description ),
4558 Region : installConfig .Config .GCP .Region ,
4659 }
4760
48- workerSubnet := gcp .DefaultSubnetName (clusterID .InfraID , "worker" )
61+ computeSubnetName := gcp .DefaultSubnetName (clusterID .InfraID , "worker" )
62+ computeSubnetCidr := ""
4963 if installConfig .Config .GCP .ComputeSubnet != "" {
50- workerSubnet = installConfig .Config .GCP .ComputeSubnet
64+ computeSubnetName = installConfig .Config .GCP .ComputeSubnet
65+
66+ computeSubnet , err := getSubnet (context .TODO (), installConfig .Config .GCP .NetworkProjectID , installConfig .Config .GCP .Region , computeSubnetName )
67+ if err != nil {
68+ return nil , fmt .Errorf ("failed to get compute subnet: %w" , err )
69+ }
70+ // IpCidr is the IPv4 version, the IPv6 version can be accessed as well
71+ computeSubnetCidr = computeSubnet .IpCidrRange
5172 }
5273
53- worker := capg.SubnetSpec {
54- Name : workerSubnet ,
55- CidrBlock : "" ,
74+ compute := capg.SubnetSpec {
75+ Name : computeSubnetName ,
76+ CidrBlock : computeSubnetCidr ,
5677 Description : ptr .To (description ),
5778 Region : installConfig .Config .GCP .Region ,
5879 }
@@ -79,18 +100,18 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
79100 if err != nil {
80101 return nil , fmt .Errorf ("failed to create the master subnet %w" , err )
81102 }
82- master .CidrBlock = masterCIDR .String ()
103+ controlPlane .CidrBlock = masterCIDR .String ()
83104 }
84105
85106 if installConfig .Config .GCP .ComputeSubnet == "" {
86- workerCIDR , err := cidr .Subnet (ipv4Net , 1 , 1 )
107+ computeCIDR , err := cidr .Subnet (ipv4Net , 1 , 1 )
87108 if err != nil {
88- return nil , fmt .Errorf ("failed to create the worker subnet %w" , err )
109+ return nil , fmt .Errorf ("failed to create the compute subnet %w" , err )
89110 }
90- worker .CidrBlock = workerCIDR .String ()
111+ compute .CidrBlock = computeCIDR .String ()
91112 }
92113
93- subnets := []capg.SubnetSpec {master , worker }
114+ subnets := []capg.SubnetSpec {controlPlane , compute }
94115 // Subnets should never be auto created, even in shared VPC installs
95116 autoCreateSubnets := false
96117
@@ -126,6 +147,11 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
126147 }
127148 gcpCluster .SetGroupVersionKind (capg .GroupVersion .WithKind ("GCPCluster" ))
128149
150+ // Set the network project during shared vpc installs
151+ if installConfig .Config .GCP .NetworkProjectID != "" {
152+ gcpCluster .Spec .Network .HostProject = ptr .To (installConfig .Config .GCP .NetworkProjectID )
153+ }
154+
129155 manifests = append (manifests , & asset.RuntimeFile {
130156 Object : gcpCluster ,
131157 File : asset.File {Filename : "02_gcp-cluster.yaml" },
@@ -182,3 +208,30 @@ func findFailureDomains(installConfig *installconfig.InstallConfig) []string {
182208
183209 return zones .UnsortedList ()
184210}
211+
212+ // getSubnet will find a subnet in a project by the name. The matching subnet structure will be returned if
213+ // one is found.
214+ func getSubnet (ctx context.Context , project , region , subnetName string ) (* compute.Subnetwork , error ) {
215+ ctx , cancel := context .WithTimeout (ctx , time .Minute * 1 )
216+ defer cancel ()
217+
218+ ssn , err := gcpic .GetSession (ctx )
219+ if err != nil {
220+ return nil , fmt .Errorf ("failed to get session: %w" , err )
221+ }
222+
223+ computeService , err := compute .NewService (ctx , option .WithCredentials (ssn .Credentials ))
224+ if err != nil {
225+ return nil , fmt .Errorf ("failed to create compute service: %w" , err )
226+ }
227+
228+ subnetService := compute .NewSubnetworksService (computeService )
229+ subnet , err := subnetService .Get (project , region , subnetName ).Context (ctx ).Do ()
230+ if err != nil {
231+ return nil , fmt .Errorf ("failed to find subnet %s: %w" , subnetName , err )
232+ } else if subnet == nil {
233+ return nil , fmt .Errorf ("subnet %s is empty" , subnetName )
234+ }
235+
236+ return subnet , nil
237+ }
0 commit comments