Skip to content

Commit 5a947cf

Browse files
authored
Merge pull request #1189 from barbacbd/add-shared-vpc-support
Add support for Shared VPC Networking
2 parents ef2e7f1 + cd4236f commit 5a947cf

17 files changed

+893
-8
lines changed

api/v1beta1/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ type NetworkSpec struct {
132132
// Allow for configuration of load balancer backend (useful for changing apiserver port)
133133
// +optional
134134
LoadBalancerBackendPort *int32 `json:"loadBalancerBackendPort,omitempty"`
135+
136+
// HostProject is the name of the project hosting the shared VPC network resources.
137+
// +optional
138+
HostProject *string `json:"hostProject,omitempty"`
135139
}
136140

137141
// LoadBalancerType defines the Load Balancer that should be created.

api/v1beta1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cloud/interfaces.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type ReconcilerWithResult interface {
4646
// Client is an interface which can get cloud client.
4747
type Client interface {
4848
Cloud() Cloud
49+
NetworkCloud() Cloud
4950
}
5051

5152
// ClusterGetter is an interface which can get cluster information.
@@ -56,6 +57,8 @@ type ClusterGetter interface {
5657
Name() string
5758
Namespace() string
5859
NetworkName() string
60+
NetworkProject() string
61+
IsSharedVpc() bool
5962
Network() *infrav1.Network
6063
AdditionalLabels() infrav1.Labels
6164
FailureDomains() clusterv1.FailureDomains

cloud/scope/cluster.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,27 @@ func (s *ClusterScope) Cloud() cloud.Cloud {
9090
return newCloud(s.Project(), s.GCPServices)
9191
}
9292

93+
// NetworkCloud returns initialized cloud.
94+
func (s *ClusterScope) NetworkCloud() cloud.Cloud {
95+
return newCloud(s.NetworkProject(), s.GCPServices)
96+
}
97+
9398
// Project returns the current project name.
9499
func (s *ClusterScope) Project() string {
95100
return s.GCPCluster.Spec.Project
96101
}
97102

103+
// NetworkProject returns the project name where network resources should exist.
104+
// The network project defaults to the Project when one is not supplied.
105+
func (s *ClusterScope) NetworkProject() string {
106+
return ptr.Deref(s.GCPCluster.Spec.Network.HostProject, s.Project())
107+
}
108+
109+
// IsSharedVpc returns true If sharedVPC used else , returns false.
110+
func (s *ClusterScope) IsSharedVpc() bool {
111+
return s.NetworkProject() != s.Project()
112+
}
113+
98114
// Region returns the cluster region.
99115
func (s *ClusterScope) Region() string {
100116
return s.GCPCluster.Spec.Region
@@ -117,7 +133,7 @@ func (s *ClusterScope) NetworkName() string {
117133

118134
// NetworkLink returns the partial URL for the network.
119135
func (s *ClusterScope) NetworkLink() string {
120-
return fmt.Sprintf("projects/%s/global/networks/%s", s.Project(), s.NetworkName())
136+
return fmt.Sprintf("projects/%s/global/networks/%s", s.NetworkProject(), s.NetworkName())
121137
}
122138

123139
// Network returns the cluster network object.

cloud/scope/machine.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ func (m *MachineScope) Cloud() cloud.Cloud {
9494
return m.ClusterGetter.Cloud()
9595
}
9696

97+
// NetworkCloud returns initialized network cloud.
98+
func (m *MachineScope) NetworkCloud() cloud.Cloud {
99+
return m.ClusterGetter.NetworkCloud()
100+
}
101+
97102
// Zone returns the FailureDomain for the GCPMachine.
98103
func (m *MachineScope) Zone() string {
99104
if m.Machine.Spec.FailureDomain == nil {
@@ -319,7 +324,7 @@ func (m *MachineScope) InstanceAdditionalDiskSpec() []*compute.AttachedDisk {
319324
// InstanceNetworkInterfaceSpec returns compute network interface spec.
320325
func (m *MachineScope) InstanceNetworkInterfaceSpec() *compute.NetworkInterface {
321326
networkInterface := &compute.NetworkInterface{
322-
Network: path.Join("projects", m.ClusterGetter.Project(), "global", "networks", m.ClusterGetter.NetworkName()),
327+
Network: path.Join("projects", m.ClusterGetter.NetworkProject(), "global", "networks", m.ClusterGetter.NetworkName()),
323328
}
324329

325330
if m.GCPMachine.Spec.PublicIP != nil && *m.GCPMachine.Spec.PublicIP {
@@ -332,7 +337,7 @@ func (m *MachineScope) InstanceNetworkInterfaceSpec() *compute.NetworkInterface
332337
}
333338

334339
if m.GCPMachine.Spec.Subnet != nil {
335-
networkInterface.Subnetwork = path.Join("regions", m.ClusterGetter.Region(), "subnetworks", *m.GCPMachine.Spec.Subnet)
340+
networkInterface.Subnetwork = path.Join("projects", m.ClusterGetter.NetworkProject(), "regions", m.ClusterGetter.Region(), "subnetworks", *m.GCPMachine.Spec.Subnet)
336341
}
337342

338343
return networkInterface

cloud/scope/managedcluster.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ func (s *ManagedClusterScope) Cloud() cloud.Cloud {
9393
return newCloud(s.Project(), s.GCPServices)
9494
}
9595

96+
// NetworkCloud returns initialized cloud.
97+
func (s *ManagedClusterScope) NetworkCloud() cloud.Cloud {
98+
return newCloud(s.NetworkProject(), s.GCPServices)
99+
}
100+
96101
// Project returns the current project name.
97102
func (s *ManagedClusterScope) Project() string {
98103
return s.GCPManagedCluster.Spec.Project
@@ -118,9 +123,20 @@ func (s *ManagedClusterScope) NetworkName() string {
118123
return ptr.Deref(s.GCPManagedCluster.Spec.Network.Name, "default")
119124
}
120125

126+
// NetworkProject returns the project name where network resources should exist.
127+
// The network project defaults to the Project when one is not supplied.
128+
func (s *ManagedClusterScope) NetworkProject() string {
129+
return ptr.Deref(s.GCPManagedCluster.Spec.Network.HostProject, s.Project())
130+
}
131+
132+
// IsSharedVpc returns true If sharedVPC used else , returns false.
133+
func (s *ManagedClusterScope) IsSharedVpc() bool {
134+
return s.NetworkProject() != s.Project()
135+
}
136+
121137
// NetworkLink returns the partial URL for the network.
122138
func (s *ManagedClusterScope) NetworkLink() string {
123-
return fmt.Sprintf("projects/%s/global/networks/%s", s.Project(), s.NetworkName())
139+
return fmt.Sprintf("projects/%s/global/networks/%s", s.NetworkProject(), s.NetworkName())
124140
}
125141

126142
// Network returns the cluster network object.

cloud/services/compute/firewalls/reconcile.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ import (
2828
// Reconcile reconcile cluster firewall compoenents.
2929
func (s *Service) Reconcile(ctx context.Context) error {
3030
log := log.FromContext(ctx)
31+
if s.scope.IsSharedVpc() {
32+
log.V(2).Info("Shared VPC enabled. Ignore Reconciling firewall resources")
33+
return nil
34+
}
3135
log.Info("Reconciling firewall resources")
3236
for _, spec := range s.scope.FirewallRulesSpec() {
3337
log.V(2).Info("Looking firewall", "name", spec.Name)
@@ -50,6 +54,10 @@ func (s *Service) Reconcile(ctx context.Context) error {
5054
// Delete delete cluster firewall compoenents.
5155
func (s *Service) Delete(ctx context.Context) error {
5256
log := log.FromContext(ctx)
57+
if s.scope.IsSharedVpc() {
58+
log.V(2).Info("Shared VPC enabled. Ignore Deleting firewall resources")
59+
return nil
60+
}
5361
log.Info("Deleting firewall resources")
5462
for _, spec := range s.scope.FirewallRulesSpec() {
5563
log.V(2).Info("Deleting firewall", "name", spec.Name)

0 commit comments

Comments
 (0)