Skip to content

Commit f5001f5

Browse files
committed
Add support for Shared VPC Networking
This is an update to the stale PR #991 ** Added support for Host Project for a shared VPC in the Network struct. ** Network Resources will now use the host project name if exists, otherwise the normal project. ** Update the cluster getter interface to include the NetworkProject and Indicator for a shared VPC. ** Update reconcilers for girewall rules, subnets and network. ** Update the services to use the host project for resources when a shared vpc is used.
1 parent f6c2c4f commit f5001f5

14 files changed

+106
-7
lines changed

api/v1beta1/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ type NetworkSpec struct {
112112
// Allow for configuration of load balancer backend (useful for changing apiserver port)
113113
// +optional
114114
LoadBalancerBackendPort *int32 `json:"loadBalancerBackendPort,omitempty"`
115+
116+
// HostProject is the name of the project hosting the shared VPC network resources.
117+
// +optional
118+
HostProject *string `json:"hostProject,omitempty"`
115119
}
116120

117121
// SubnetSpec configures an GCP Subnet.

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 {
@@ -318,7 +323,7 @@ func (m *MachineScope) InstanceAdditionalDiskSpec() []*compute.AttachedDisk {
318323
// InstanceNetworkInterfaceSpec returns compute network interface spec.
319324
func (m *MachineScope) InstanceNetworkInterfaceSpec() *compute.NetworkInterface {
320325
networkInterface := &compute.NetworkInterface{
321-
Network: path.Join("projects", m.ClusterGetter.Project(), "global", "networks", m.ClusterGetter.NetworkName()),
326+
Network: path.Join("projects", m.ClusterGetter.NetworkProject(), "global", "networks", m.ClusterGetter.NetworkName()),
322327
}
323328

324329
if m.GCPMachine.Spec.PublicIP != nil && *m.GCPMachine.Spec.PublicIP {
@@ -331,7 +336,7 @@ func (m *MachineScope) InstanceNetworkInterfaceSpec() *compute.NetworkInterface
331336
}
332337

333338
if m.GCPMachine.Spec.Subnet != nil {
334-
networkInterface.Subnetwork = path.Join("regions", m.ClusterGetter.Region(), "subnetworks", *m.GCPMachine.Spec.Subnet)
339+
networkInterface.Subnetwork = path.Join("projects", m.ClusterGetter.NetworkProject(), "regions", m.ClusterGetter.Region(), "subnetworks", *m.GCPMachine.Spec.Subnet)
335340
}
336341

337342
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
@@ -27,6 +27,10 @@ import (
2727
// Reconcile reconcile cluster firewall compoenents.
2828
func (s *Service) Reconcile(ctx context.Context) error {
2929
log := log.FromContext(ctx)
30+
if s.scope.IsSharedVpc() {
31+
log.Info("VPC enabled. Ignore Reconciling firewall resources")
32+
return nil
33+
}
3034
log.Info("Reconciling firewall resources")
3135
for _, spec := range s.scope.FirewallRulesSpec() {
3236
log.V(2).Info("Looking firewall", "name", spec.Name)
@@ -49,6 +53,10 @@ func (s *Service) Reconcile(ctx context.Context) error {
4953
// Delete delete cluster firewall compoenents.
5054
func (s *Service) Delete(ctx context.Context) error {
5155
log := log.FromContext(ctx)
56+
if s.scope.IsSharedVpc() {
57+
log.Info("VPC enabled. Ignore Deleting firewall resources")
58+
return nil
59+
}
5260
log.Info("Deleting firewall resources")
5361
for _, spec := range s.scope.FirewallRulesSpec() {
5462
log.V(2).Info("Deleting firewall", "name", spec.Name)

cloud/services/compute/networks/reconcile.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ func (s *Service) Reconcile(ctx context.Context) error {
5252
// Delete delete cluster network components.
5353
func (s *Service) Delete(ctx context.Context) error {
5454
log := log.FromContext(ctx)
55+
if s.scope.IsSharedVpc() {
56+
log.Info("VPC enabled. Ignore Deleting network resources")
57+
s.scope.Network().Router = nil
58+
s.scope.Network().SelfLink = nil
59+
return nil
60+
}
5561
log.Info("Deleting network resources")
5662
networkKey := meta.GlobalKey(s.scope.NetworkName())
5763
log.V(2).Info("Looking for network before deleting", "name", networkKey)
@@ -102,6 +108,11 @@ func (s *Service) createOrGetNetwork(ctx context.Context) (*compute.Network, err
102108
return nil, err
103109
}
104110

111+
if s.scope.IsSharedVpc() {
112+
log.Error(err, "VPC is enabled. Error looking for network", "name", s.scope.NetworkName())
113+
return nil, err
114+
}
115+
105116
log.V(2).Info("Creating a network", "name", s.scope.NetworkName())
106117
if err := s.networks.Insert(ctx, networkKey, s.scope.NetworkSpec()); err != nil {
107118
log.Error(err, "Error creating a network", "name", s.scope.NetworkName())

cloud/services/compute/networks/service.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,14 @@ var _ cloud.Reconciler = &Service{}
5454

5555
// New returns Service from given scope.
5656
func New(scope Scope) *Service {
57+
scopeCloud := scope.Cloud()
58+
if scope.IsSharedVpc() {
59+
scopeCloud = scope.NetworkCloud()
60+
}
61+
5762
return &Service{
5863
scope: scope,
59-
networks: scope.Cloud().Networks(),
60-
routers: scope.Cloud().Routers(),
64+
networks: scopeCloud.Networks(),
65+
routers: scopeCloud.Routers(),
6166
}
6267
}

cloud/services/compute/subnets/reconcile.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ func (s *Service) Reconcile(ctx context.Context) error {
4141
// Delete deletes cluster subnetwork components.
4242
func (s *Service) Delete(ctx context.Context) error {
4343
logger := log.FromContext(ctx)
44+
if s.scope.IsSharedVpc() {
45+
logger.Info("VPC enabled. Ignore Deleting subnet resources")
46+
return nil
47+
}
4448
for _, subnetSpec := range s.scope.SubnetSpecs() {
4549
logger.V(2).Info("Deleting a subnet", "name", subnetSpec.Name)
4650
subnetKey := meta.RegionalKey(subnetSpec.Name, s.scope.Region())
@@ -68,6 +72,11 @@ func (s *Service) createOrGetSubnets(ctx context.Context) ([]*compute.Subnetwork
6872
return subnets, err
6973
}
7074

75+
if s.scope.IsSharedVpc() {
76+
logger.Error(err, "VPC is enabled. Error looking for subnetwork", "name", subnetSpec.Name)
77+
return nil, err
78+
}
79+
7180
// Subnet was not found, let's create it
7281
logger.V(2).Info("Creating a subnet", "name", subnetSpec.Name)
7382
if err := s.subnets.Insert(ctx, subnetKey, subnetSpec); err != nil {

0 commit comments

Comments
 (0)