Skip to content

Commit 579f404

Browse files
Merge pull request openshift#8421 from bfournie/capg-internal-lb
CORS-3296: CAPG - configure Internal LoadBalancer in the provider
2 parents cd88964 + 1422bf7 commit 579f404

File tree

3 files changed

+13
-179
lines changed

3 files changed

+13
-179
lines changed

pkg/asset/manifests/gcp/cluster.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
gcpic "github.com/openshift/installer/pkg/asset/installconfig/gcp"
2222
"github.com/openshift/installer/pkg/asset/manifests/capiutils"
2323
gcpconsts "github.com/openshift/installer/pkg/constants/gcp"
24+
"github.com/openshift/installer/pkg/types"
2425
"github.com/openshift/installer/pkg/types/gcp"
2526
)
2627

@@ -122,6 +123,11 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
122123
labels[label.Key] = label.Value
123124
}
124125

126+
capgLoadBalancerType := capg.InternalExternal
127+
if installConfig.Config.Publish == types.InternalPublishingStrategy {
128+
capgLoadBalancerType = capg.Internal
129+
}
130+
125131
gcpCluster := &capg.GCPCluster{
126132
ObjectMeta: metav1.ObjectMeta{
127133
Name: clusterID.InfraID,
@@ -142,6 +148,7 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
142148
FailureDomains: findFailureDomains(installConfig),
143149
LoadBalancer: capg.LoadBalancerSpec{
144150
APIServerInstanceGroupTagOverride: ptr.To(InstanceGroupRoleTag),
151+
LoadBalancerType: ptr.To(capgLoadBalancerType),
145152
},
146153
},
147154
}

pkg/infrastructure/gcp/clusterapi/clusterapi.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,6 @@ func (p Provider) InfraReady(ctx context.Context, in clusterapi.InfraReadyInput)
188188
return fmt.Errorf("could not find master subnet %s in subnets %v", masterSubnetName, subnets)
189189
}
190190

191-
zones := gcpCluster.Status.FailureDomains.GetIDs()
192-
193-
// Currently, the internal/private load balancer is not created by CAPG. The load balancer will be created
194-
// by the installer for now.
195-
// TODO: remove the creation of the LB and health check here when supported by CAPG.
196-
// https://github.com/kubernetes-sigs/cluster-api-provider-gcp/issues/903
197-
apiIntIPAddress, err := createInternalLB(ctx, in, masterSubnetSelflink, networkSelfLink, zones)
198-
if err != nil {
199-
return fmt.Errorf("failed to create internal load balancer address: %w", err)
200-
}
201-
202191
// The firewall for masters, aka control-plane, is created by CAPG
203192
// Create the ones needed for worker to master communication
204193
if err = createFirewallRules(ctx, in, *gcpCluster.Status.Network.SelfLink); err != nil {
@@ -216,6 +205,11 @@ func (p Provider) InfraReady(ctx context.Context, in clusterapi.InfraReadyInput)
216205
return fmt.Errorf("failed to create the private managed zone: %w", err)
217206
}
218207

208+
apiIntIPAddress, err := getInternalLBAddress(ctx, in.InstallConfig.Config.GCP.ProjectID, in.InstallConfig.Config.GCP.Region, getAPIAddressName(in.InfraID))
209+
if err != nil {
210+
return fmt.Errorf("failed to get the internal load balancer address: %w", err)
211+
}
212+
219213
// Create the public (optional) and private dns records
220214
if err := createDNSRecords(ctx, in.InstallConfig, in.InfraID, apiIPAddress, apiIntIPAddress); err != nil {
221215
return fmt.Errorf("failed to create DNS records: %w", err)

pkg/infrastructure/gcp/clusterapi/network.go

Lines changed: 1 addition & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,10 @@ package clusterapi
33
import (
44
"context"
55
"fmt"
6-
"time"
7-
8-
"github.com/sirupsen/logrus"
9-
"google.golang.org/api/compute/v1"
10-
11-
"github.com/openshift/installer/pkg/asset/manifests/gcp"
12-
"github.com/openshift/installer/pkg/infrastructure/clusterapi"
136
)
147

15-
func getAPIInternalResourceName(infraID string) string {
16-
return fmt.Sprintf("%s-api-internal", infraID)
17-
}
18-
198
func getAPIAddressName(infraID string) string {
20-
return fmt.Sprintf("%s-cluster-ip", infraID)
9+
return fmt.Sprintf("%s-api-internal", infraID)
2110
}
2211

2312
func getInternalLBAddress(ctx context.Context, project, region, name string) (string, error) {
@@ -32,159 +21,3 @@ func getInternalLBAddress(ctx context.Context, project, region, name string) (st
3221
}
3322
return addrOutput.Address, nil
3423
}
35-
36-
// createInternalLB creates a static ip address for the internal load balancer.
37-
// Returns the IP address of the created load balancer.
38-
func createInternalLB(ctx context.Context, in clusterapi.InfraReadyInput, subnetSelfLink, networkSelfLink string, zones []*string) (string, error) {
39-
projectID := in.InstallConfig.Config.GCP.ProjectID
40-
region := in.InstallConfig.Config.GCP.Region
41-
name := getAPIAddressName(in.InfraID)
42-
labels := mergeLabels(in.InstallConfig, in.InfraID)
43-
44-
service, err := NewComputeService()
45-
if err != nil {
46-
return "", err
47-
}
48-
49-
ctx, cancel := context.WithTimeout(ctx, time.Minute*3)
50-
defer cancel()
51-
52-
// Patch the balancing mode on CAPG proxy classic load balancer backends
53-
// to match the CONNECTION balancing mode used by installer-created
54-
// internal passthrough LB, because:
55-
// "all backend services that reference the instance group must use the same balancing mode"
56-
// cf: https://cloud.google.com/load-balancing/docs/backend-service
57-
logrus.Debug("Patching external load balancer")
58-
extBesvcName := fmt.Sprintf("%s-apiserver", in.InfraID)
59-
extBesvc, err := service.BackendServices.Get(projectID, extBesvcName).Context(ctx).Do()
60-
if err != nil {
61-
return "", fmt.Errorf("failed to get backend service: %w", err)
62-
}
63-
64-
for _, be := range extBesvc.Backends {
65-
be.BalancingMode = "CONNECTION"
66-
be.MaxConnections = int64(2 ^ 32)
67-
}
68-
69-
op, err := service.BackendServices.Patch(projectID, extBesvcName, extBesvc).Context(ctx).Do()
70-
if err != nil {
71-
return "", fmt.Errorf("failed to patch external load balancer: %w", err)
72-
}
73-
74-
if err := WaitForOperationGlobal(ctx, projectID, op); err != nil {
75-
return "", fmt.Errorf("failed to wait for patching external load balancer: %w", err)
76-
}
77-
logrus.Debug("Successfully patched external load balancer")
78-
79-
logrus.Debug("Creating internal load balancer")
80-
addr := &compute.Address{
81-
Name: name,
82-
AddressType: "INTERNAL",
83-
Subnetwork: subnetSelfLink,
84-
Description: resourceDescription,
85-
Labels: labels,
86-
Region: region,
87-
}
88-
89-
op, err = service.Addresses.Insert(projectID, region, addr).Context(ctx).Do()
90-
if err != nil {
91-
return "", fmt.Errorf("failed to create internal compute address: %w", err)
92-
}
93-
94-
if err := WaitForOperationRegional(ctx, projectID, region, op); err != nil {
95-
return "", fmt.Errorf("failed to wait for compute address creation: %w", err)
96-
}
97-
98-
ipAddress, err := getInternalLBAddress(ctx, projectID, region, name)
99-
if err != nil {
100-
return "", fmt.Errorf("failed to get internal load balancer IP address: %w", err)
101-
}
102-
103-
hcName := getAPIInternalResourceName(in.InfraID)
104-
healthCheck := &compute.HealthCheck{
105-
Region: region,
106-
Name: hcName,
107-
Description: resourceDescription,
108-
HealthyThreshold: 3,
109-
UnhealthyThreshold: 3,
110-
CheckIntervalSec: 2,
111-
TimeoutSec: 2,
112-
Type: "HTTPS",
113-
HttpsHealthCheck: &compute.HTTPSHealthCheck{
114-
Port: 6443,
115-
RequestPath: "/readyz",
116-
},
117-
}
118-
119-
_, err = service.RegionHealthChecks.Insert(projectID, region, healthCheck).Context(ctx).Do()
120-
if err != nil {
121-
return "", fmt.Errorf("failed to create api-internal health check: %w", err)
122-
}
123-
124-
if err := WaitForOperationRegional(ctx, projectID, region, op); err != nil {
125-
return "", fmt.Errorf("failed to wait for health check creation: %w", err)
126-
}
127-
128-
hc, err := service.RegionHealthChecks.Get(projectID, region, hcName).Context(ctx).Do()
129-
if err != nil {
130-
return "", fmt.Errorf("error getting health check: %w", err)
131-
}
132-
backends := []*compute.Backend{}
133-
for _, zone := range zones {
134-
igName := fmt.Sprintf("%s-%s-%s", in.InfraID, gcp.InstanceGroupRoleTag, *zone)
135-
ig, err := service.InstanceGroups.Get(projectID, *zone, igName).Context(ctx).Do()
136-
if err != nil {
137-
return "", fmt.Errorf("error getting instance group %s in zone %s: %w", igName, *zone, err)
138-
}
139-
backends = append(backends, &compute.Backend{
140-
BalancingMode: "CONNECTION",
141-
Group: ig.SelfLink,
142-
})
143-
}
144-
145-
besvcName := fmt.Sprintf("%s-api-internal", in.InfraID)
146-
op, err = service.RegionBackendServices.Insert(projectID, region, &compute.BackendService{
147-
Backends: backends,
148-
Name: besvcName,
149-
LoadBalancingScheme: "INTERNAL",
150-
Protocol: "TCP",
151-
TimeoutSec: int64((10 * time.Minute).Seconds()),
152-
HealthChecks: []string{hc.SelfLink},
153-
Region: region,
154-
Network: networkSelfLink,
155-
}).Context(ctx).Do()
156-
if err != nil {
157-
return "", fmt.Errorf("failed to create internal backend service: %w", err)
158-
}
159-
160-
if err := WaitForOperationRegional(ctx, projectID, region, op); err != nil {
161-
return "", fmt.Errorf("failed to wait for internal backend service creation: %w", err)
162-
}
163-
164-
besvc, err := service.RegionBackendServices.Get(projectID, region, besvcName).Context(ctx).Do()
165-
if err != nil {
166-
return "", fmt.Errorf("failed to get backend service: %w", err)
167-
}
168-
169-
op, err = service.ForwardingRules.Insert(projectID, region, &compute.ForwardingRule{
170-
Name: fmt.Sprintf("%s-api-internal", in.InfraID),
171-
IPProtocol: "TCP",
172-
IPAddress: ipAddress,
173-
LoadBalancingScheme: "INTERNAL",
174-
Ports: []string{"6443", "22623"},
175-
BackendService: besvc.SelfLink,
176-
Network: networkSelfLink,
177-
Subnetwork: subnetSelfLink,
178-
Region: region,
179-
Labels: labels,
180-
}).Context(ctx).Do()
181-
if err != nil {
182-
return "", fmt.Errorf("failed to create forwarding rule: %w", err)
183-
}
184-
185-
if err := WaitForOperationRegional(ctx, projectID, region, op); err != nil {
186-
return "", fmt.Errorf("failed to wait for forwarding rule creation: %w", err)
187-
}
188-
logrus.Debug("Successfully created internal load balancer")
189-
return ipAddress, nil
190-
}

0 commit comments

Comments
 (0)