Skip to content

Commit 1a8192b

Browse files
committed
Create an Internal Load Balancer if configured
Provide the ability to configure the types of Load Balancers to be created (Internal and/or External). By default, an External Proxy Load Balancer will be created per the current implementation. If set for an Internal Load Balancer, an Internal Passthrough Load Balancer will be created using resources in the specified region.
1 parent 6cc9af0 commit 1a8192b

12 files changed

+1315
-100
lines changed

api/v1beta1/gcpcluster_webhook.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ func (c *GCPCluster) ValidateUpdate(oldRaw runtime.Object) (admission.Warnings,
8585
)
8686
}
8787

88+
if !reflect.DeepEqual(c.Spec.LoadBalancer, old.Spec.LoadBalancer) {
89+
allErrs = append(allErrs,
90+
field.Invalid(field.NewPath("spec", "LoadBalancer"),
91+
c.Spec.LoadBalancer, "field is immutable"),
92+
)
93+
}
94+
8895
if len(allErrs) == 0 {
8996
return nil, nil
9097
}

api/v1beta1/labels.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ const (
111111

112112
// APIServerRoleTagValue describes the value for the apiserver role.
113113
APIServerRoleTagValue = "apiserver"
114+
115+
// InternalRoleTagValue describes the value for the internal role.
116+
InternalRoleTagValue = "api-internal"
114117
)
115118

116119
// ClusterTagKey generates the key for resources associated with a cluster.

api/v1beta1/types.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,26 @@ type Network struct {
8585
// created for the API Server.
8686
// +optional
8787
APIServerForwardingRule *string `json:"apiServerForwardingRule,omitempty"`
88+
89+
// APIInternalAddress is the IPV4 regional address assigned to the
90+
// internal Load Balancer.
91+
// +optional
92+
APIInternalAddress *string `json:"apiInternalIpAddress,omitempty"`
93+
94+
// APIInternalHealthCheck is the full reference to the health check
95+
// created for the internal Load Balancer.
96+
// +optional
97+
APIInternalHealthCheck *string `json:"apiInternalHealthCheck,omitempty"`
98+
99+
// APIInternalBackendService is the full reference to the backend service
100+
// created for the internal Load Balancer.
101+
// +optional
102+
APIInternalBackendService *string `json:"apiInternalBackendService,omitempty"`
103+
104+
// APIInternalForwardingRule is the full reference to the forwarding rule
105+
// created for the internal Load Balancer.
106+
// +optional
107+
APIInternalForwardingRule *string `json:"apiInternalForwardingRule,omitempty"`
88108
}
89109

90110
// NetworkSpec encapsulates all things related to a GCP network.
@@ -114,6 +134,24 @@ type NetworkSpec struct {
114134
LoadBalancerBackendPort *int32 `json:"loadBalancerBackendPort,omitempty"`
115135
}
116136

137+
// LoadBalancerType defines the Load Balancer that should be created.
138+
type LoadBalancerType string
139+
140+
var (
141+
// External creates a Global External Proxy Load Balancer
142+
// to manage traffic to backends in multiple regions. This is the default Load
143+
// Balancer and will be created if no LoadBalancerType is defined.
144+
External = LoadBalancerType("External")
145+
146+
// Internal creates a Regional Internal Passthrough Load
147+
// Balancer to manage traffic to backends in the configured region.
148+
Internal = LoadBalancerType("Internal")
149+
150+
// InternalExternal creates both External and Internal Load Balancers to provide
151+
// separate endpoints for managing both external and internal traffic.
152+
InternalExternal = LoadBalancerType("InternalExternal")
153+
)
154+
117155
// LoadBalancerSpec contains configuration for one or more LoadBalancers.
118156
type LoadBalancerSpec struct {
119157
// APIServerInstanceGroupTagOverride overrides the default setting for the
@@ -123,6 +161,15 @@ type LoadBalancerSpec struct {
123161
// +kubebuilder:validation:Pattern=`(^[1-9][0-9]{0,31}$)|(^[a-z][a-z0-9-]{4,28}[a-z0-9]$)`
124162
// +optional
125163
APIServerInstanceGroupTagOverride *string `json:"apiServerInstanceGroupTagOverride,omitempty"`
164+
165+
// LoadBalancerType defines the type of Load Balancer that should be created.
166+
// If not set, a Global External Proxy Load Balancer will be created by default.
167+
// +optional
168+
LoadBalancerType *LoadBalancerType `json:"loadBalancerType,omitempty"`
169+
170+
// InternalLoadBalancer is the configuration for an Internal Passthrough Network Load Balancer.
171+
// +optional
172+
InternalLoadBalancer *LoadBalancer `json:"internalLoadBalancer,omitempty"`
126173
}
127174

128175
// SubnetSpec configures an GCP Subnet.
@@ -278,3 +325,19 @@ type ObjectReference struct {
278325
// +kubebuilder:validation:Required
279326
Name string `json:"name"`
280327
}
328+
329+
// LoadBalancer specifies the configuration of a LoadBalancer.
330+
type LoadBalancer struct {
331+
// Name is the name of the Load Balancer. If not set a default name
332+
// will be used. For an Internal Load Balancer service the default
333+
// name is "api-internal".
334+
// +kubebuilder:validation:Optional
335+
// +kubebuilder:validation:Pattern=`(^[1-9][0-9]{0,31}$)|(^[a-z][a-z0-9-]{4,28}[a-z0-9]$)`
336+
// +optional
337+
Name *string `json:"name,omitempty"`
338+
339+
// Subnet is the name of the subnet to use for a regional Load Balancer. A subnet is
340+
// required for the Load Balancer, if not defined the first configured subnet will be
341+
// used.
342+
Subnet *string `json:"subnet,omitempty"`
343+
}

api/v1beta1/zz_generated.deepcopy.go

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

cloud/scope/cluster.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -290,18 +290,18 @@ func (s *ClusterScope) FirewallRulesSpec() []*compute.Firewall {
290290
// ANCHOR: ClusterControlPlaneSpec
291291

292292
// AddressSpec returns google compute address spec.
293-
func (s *ClusterScope) AddressSpec() *compute.Address {
293+
func (s *ClusterScope) AddressSpec(lbname string) *compute.Address {
294294
return &compute.Address{
295-
Name: fmt.Sprintf("%s-%s", s.Name(), infrav1.APIServerRoleTagValue),
295+
Name: fmt.Sprintf("%s-%s", s.Name(), lbname),
296296
AddressType: "EXTERNAL",
297297
IpVersion: "IPV4",
298298
}
299299
}
300300

301301
// BackendServiceSpec returns google compute backend-service spec.
302-
func (s *ClusterScope) BackendServiceSpec() *compute.BackendService {
302+
func (s *ClusterScope) BackendServiceSpec(lbname string) *compute.BackendService {
303303
return &compute.BackendService{
304-
Name: fmt.Sprintf("%s-%s", s.Name(), infrav1.APIServerRoleTagValue),
304+
Name: fmt.Sprintf("%s-%s", s.Name(), lbname),
305305
LoadBalancingScheme: "EXTERNAL",
306306
PortName: "apiserver",
307307
Protocol: "TCP",
@@ -310,24 +310,24 @@ func (s *ClusterScope) BackendServiceSpec() *compute.BackendService {
310310
}
311311

312312
// ForwardingRuleSpec returns google compute forwarding-rule spec.
313-
func (s *ClusterScope) ForwardingRuleSpec() *compute.ForwardingRule {
313+
func (s *ClusterScope) ForwardingRuleSpec(lbname string) *compute.ForwardingRule {
314314
port := int32(443)
315315
if c := s.Cluster.Spec.ClusterNetwork; c != nil {
316316
port = ptr.Deref(c.APIServerPort, 443)
317317
}
318318
portRange := fmt.Sprintf("%d-%d", port, port)
319319
return &compute.ForwardingRule{
320-
Name: fmt.Sprintf("%s-%s", s.Name(), infrav1.APIServerRoleTagValue),
320+
Name: fmt.Sprintf("%s-%s", s.Name(), lbname),
321321
IPProtocol: "TCP",
322322
LoadBalancingScheme: "EXTERNAL",
323323
PortRange: portRange,
324324
}
325325
}
326326

327327
// HealthCheckSpec returns google compute health-check spec.
328-
func (s *ClusterScope) HealthCheckSpec() *compute.HealthCheck {
328+
func (s *ClusterScope) HealthCheckSpec(lbname string) *compute.HealthCheck {
329329
return &compute.HealthCheck{
330-
Name: fmt.Sprintf("%s-%s", s.Name(), infrav1.APIServerRoleTagValue),
330+
Name: fmt.Sprintf("%s-%s", s.Name(), lbname),
331331
Type: "HTTPS",
332332
HttpsHealthCheck: &compute.HTTPSHealthCheck{
333333
Port: 6443,

0 commit comments

Comments
 (0)