Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Shared VPC Networking #1189

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/v1beta1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ type NetworkSpec struct {
// Allow for configuration of load balancer backend (useful for changing apiserver port)
// +optional
LoadBalancerBackendPort *int32 `json:"loadBalancerBackendPort,omitempty"`

// HostProject is the name of the project hosting the shared VPC network resources.
// +optional
HostProject *string `json:"hostProject,omitempty"`
Comment on lines +116 to +118
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this field only used for shared VPC / networking resources?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vincepri yes that is correct.

}

// LoadBalancerSpec contains configuration for one or more LoadBalancers.
Expand Down
5 changes: 5 additions & 0 deletions api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions cloud/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type ReconcilerWithResult interface {
// Client is an interface which can get cloud client.
type Client interface {
Cloud() Cloud
NetworkCloud() Cloud
}

// ClusterGetter is an interface which can get cluster information.
Expand All @@ -56,6 +57,8 @@ type ClusterGetter interface {
Name() string
Namespace() string
NetworkName() string
NetworkProject() string
IsSharedVpc() bool
Network() *infrav1.Network
AdditionalLabels() infrav1.Labels
FailureDomains() clusterv1.FailureDomains
Expand Down
18 changes: 17 additions & 1 deletion cloud/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,27 @@ func (s *ClusterScope) Cloud() cloud.Cloud {
return newCloud(s.Project(), s.GCPServices)
}

// NetworkCloud returns initialized cloud.
func (s *ClusterScope) NetworkCloud() cloud.Cloud {
return newCloud(s.NetworkProject(), s.GCPServices)
}

// Project returns the current project name.
func (s *ClusterScope) Project() string {
return s.GCPCluster.Spec.Project
}

// NetworkProject returns the project name where network resources should exist.
// The network project defaults to the Project when one is not supplied.
func (s *ClusterScope) NetworkProject() string {
return ptr.Deref(s.GCPCluster.Spec.Network.HostProject, s.Project())
}

// IsSharedVpc returns true If sharedVPC used else , returns false.
func (s *ClusterScope) IsSharedVpc() bool {
return s.NetworkProject() != s.Project()
}

// Region returns the cluster region.
func (s *ClusterScope) Region() string {
return s.GCPCluster.Spec.Region
Expand All @@ -117,7 +133,7 @@ func (s *ClusterScope) NetworkName() string {

// NetworkLink returns the partial URL for the network.
func (s *ClusterScope) NetworkLink() string {
return fmt.Sprintf("projects/%s/global/networks/%s", s.Project(), s.NetworkName())
return fmt.Sprintf("projects/%s/global/networks/%s", s.NetworkProject(), s.NetworkName())
}

// Network returns the cluster network object.
Expand Down
9 changes: 7 additions & 2 deletions cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ func (m *MachineScope) Cloud() cloud.Cloud {
return m.ClusterGetter.Cloud()
}

// NetworkCloud returns initialized network cloud.
func (m *MachineScope) NetworkCloud() cloud.Cloud {
return m.ClusterGetter.NetworkCloud()
}

// Zone returns the FailureDomain for the GCPMachine.
func (m *MachineScope) Zone() string {
if m.Machine.Spec.FailureDomain == nil {
Expand Down Expand Up @@ -319,7 +324,7 @@ func (m *MachineScope) InstanceAdditionalDiskSpec() []*compute.AttachedDisk {
// InstanceNetworkInterfaceSpec returns compute network interface spec.
func (m *MachineScope) InstanceNetworkInterfaceSpec() *compute.NetworkInterface {
networkInterface := &compute.NetworkInterface{
Network: path.Join("projects", m.ClusterGetter.Project(), "global", "networks", m.ClusterGetter.NetworkName()),
Network: path.Join("projects", m.ClusterGetter.NetworkProject(), "global", "networks", m.ClusterGetter.NetworkName()),
barbacbd marked this conversation as resolved.
Show resolved Hide resolved
}

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

if m.GCPMachine.Spec.Subnet != nil {
networkInterface.Subnetwork = path.Join("regions", m.ClusterGetter.Region(), "subnetworks", *m.GCPMachine.Spec.Subnet)
networkInterface.Subnetwork = path.Join("projects", m.ClusterGetter.NetworkProject(), "regions", m.ClusterGetter.Region(), "subnetworks", *m.GCPMachine.Spec.Subnet)
}

return networkInterface
Expand Down
18 changes: 17 additions & 1 deletion cloud/scope/managedcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ func (s *ManagedClusterScope) Cloud() cloud.Cloud {
return newCloud(s.Project(), s.GCPServices)
}

// NetworkCloud returns initialized cloud.
func (s *ManagedClusterScope) NetworkCloud() cloud.Cloud {
return newCloud(s.NetworkProject(), s.GCPServices)
}

// Project returns the current project name.
func (s *ManagedClusterScope) Project() string {
return s.GCPManagedCluster.Spec.Project
Expand All @@ -118,9 +123,20 @@ func (s *ManagedClusterScope) NetworkName() string {
return ptr.Deref(s.GCPManagedCluster.Spec.Network.Name, "default")
}

// NetworkProject returns the project name where network resources should exist.
// The network project defaults to the Project when one is not supplied.
func (s *ManagedClusterScope) NetworkProject() string {
return ptr.Deref(s.GCPManagedCluster.Spec.Network.HostProject, s.Project())
}

// IsSharedVpc returns true If sharedVPC used else , returns false.
func (s *ManagedClusterScope) IsSharedVpc() bool {
return s.NetworkProject() != s.Project()
}

// NetworkLink returns the partial URL for the network.
func (s *ManagedClusterScope) NetworkLink() string {
return fmt.Sprintf("projects/%s/global/networks/%s", s.Project(), s.NetworkName())
return fmt.Sprintf("projects/%s/global/networks/%s", s.NetworkProject(), s.NetworkName())
}

// Network returns the cluster network object.
Expand Down
8 changes: 8 additions & 0 deletions cloud/services/compute/firewalls/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import (
// Reconcile reconcile cluster firewall compoenents.
func (s *Service) Reconcile(ctx context.Context) error {
log := log.FromContext(ctx)
if s.scope.IsSharedVpc() {
log.V(2).Info("Shared VPC enabled. Ignore Reconciling firewall resources")
return nil
}
log.Info("Reconciling firewall resources")
for _, spec := range s.scope.FirewallRulesSpec() {
log.V(2).Info("Looking firewall", "name", spec.Name)
Expand All @@ -50,6 +54,10 @@ func (s *Service) Reconcile(ctx context.Context) error {
// Delete delete cluster firewall compoenents.
func (s *Service) Delete(ctx context.Context) error {
log := log.FromContext(ctx)
if s.scope.IsSharedVpc() {
log.V(2).Info("Shared VPC enabled. Ignore Deleting firewall resources")
return nil
}
log.Info("Deleting firewall resources")
for _, spec := range s.scope.FirewallRulesSpec() {
log.V(2).Info("Deleting firewall", "name", spec.Name)
Expand Down
Loading