-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
api_loadbalancer.go
278 lines (247 loc) · 8.66 KB
/
api_loadbalancer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gcemodel
import (
"fmt"
"strconv"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/wellknownports"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/gce"
"k8s.io/kops/upup/pkg/fi/cloudup/gcetasks"
"k8s.io/utils/strings/slices"
)
// APILoadBalancerBuilder builds a LoadBalancer for accessing the API
type APILoadBalancerBuilder struct {
*GCEModelContext
Lifecycle fi.Lifecycle
}
var _ fi.CloudupModelBuilder = &APILoadBalancerBuilder{}
// createPublicLB validates the existence of a target pool with the given name,
// and creates an IP address and forwarding rule pointing to that target pool.
func (b *APILoadBalancerBuilder) createPublicLB(c *fi.CloudupModelBuilderContext) error {
healthCheck := &gcetasks.HTTPHealthcheck{
Name: s(b.NameForHealthcheck("api")),
Port: i64(wellknownports.KubeAPIServerHealthCheck),
RequestPath: s("/healthz"),
Lifecycle: b.Lifecycle,
}
c.AddTask(healthCheck)
// TODO: point target pool to instance group managers, as done in internal LB.
targetPool := &gcetasks.TargetPool{
Name: s(b.NameForTargetPool("api")),
HealthCheck: healthCheck,
Lifecycle: b.Lifecycle,
}
c.AddTask(targetPool)
poolHealthCheck := &gcetasks.PoolHealthCheck{
Name: s(b.NameForPoolHealthcheck("api")),
Healthcheck: healthCheck,
Pool: targetPool,
Lifecycle: b.Lifecycle,
}
c.AddTask(poolHealthCheck)
ipAddress := &gcetasks.Address{
Name: s(b.NameForIPAddress("api")),
ForAPIServer: true,
Lifecycle: b.Lifecycle,
}
c.AddTask(ipAddress)
clusterLabel := gce.LabelForCluster(b.ClusterName())
c.AddTask(&gcetasks.ForwardingRule{
Name: s(b.NameForForwardingRule("api")),
Lifecycle: b.Lifecycle,
PortRange: s(strconv.Itoa(wellknownports.KubeAPIServer) + "-" + strconv.Itoa(wellknownports.KubeAPIServer)),
TargetPool: targetPool,
IPAddress: ipAddress,
IPProtocol: "TCP",
LoadBalancingScheme: s("EXTERNAL"),
Labels: map[string]string{
clusterLabel.Key: clusterLabel.Value,
"name": "api",
},
})
if b.Cluster.UsesNoneDNS() {
c.AddTask(&gcetasks.ForwardingRule{
Name: s(b.NameForForwardingRule("kops-controller")),
Lifecycle: b.Lifecycle,
PortRange: s(strconv.Itoa(wellknownports.KopsControllerPort) + "-" + strconv.Itoa(wellknownports.KopsControllerPort)),
TargetPool: targetPool,
IPAddress: ipAddress,
IPProtocol: "TCP",
LoadBalancingScheme: s("EXTERNAL"),
Labels: map[string]string{
clusterLabel.Key: clusterLabel.Value,
"name": "kops-controller",
},
})
}
return b.addFirewallRules(c)
}
func (b *APILoadBalancerBuilder) addFirewallRules(c *fi.CloudupModelBuilderContext) error {
// Allow traffic into the API from KubernetesAPIAccess CIDRs
{
network, err := b.LinkToNetwork()
if err != nil {
return err
}
b.AddFirewallRulesTasks(c, "https-api", &gcetasks.FirewallRule{
Lifecycle: b.Lifecycle,
Network: network,
SourceRanges: b.Cluster.Spec.API.Access,
TargetTags: []string{b.GCETagForRole(kops.InstanceGroupRoleControlPlane)},
Allowed: []string{"tcp:" + strconv.Itoa(wellknownports.KubeAPIServer)},
})
if b.NetworkingIsIPAlias() {
c.AddTask(&gcetasks.FirewallRule{
Name: s(b.NameForFirewallRule("pod-cidrs-to-https-api")),
Lifecycle: b.Lifecycle,
Network: network,
Family: gcetasks.AddressFamilyIPv4, // ip alias is always ipv4
SourceRanges: []string{b.Cluster.Spec.Networking.PodCIDR},
TargetTags: []string{b.GCETagForRole(kops.InstanceGroupRoleControlPlane)},
Allowed: []string{"tcp:" + strconv.Itoa(wellknownports.KubeAPIServer)},
})
}
if b.Cluster.UsesNoneDNS() {
b.AddFirewallRulesTasks(c, "kops-controller", &gcetasks.FirewallRule{
Lifecycle: b.Lifecycle,
Network: network,
SourceRanges: b.Cluster.Spec.API.Access,
TargetTags: []string{b.GCETagForRole(kops.InstanceGroupRoleControlPlane)},
Allowed: []string{"tcp:" + strconv.Itoa(wellknownports.KopsControllerPort)},
})
}
}
return nil
}
// createInternalLB creates an internal load balancer for the cluster. In
// GCP this entails creating a health check, backend service, and one forwarding rule
// per specified subnet pointing to that backend service.
func (b *APILoadBalancerBuilder) createInternalLB(c *fi.CloudupModelBuilderContext) error {
clusterLabel := gce.LabelForCluster(b.ClusterName())
hc := &gcetasks.HealthCheck{
Name: s(b.NameForHealthCheck("api")),
Port: wellknownports.KubeAPIServer,
Lifecycle: b.Lifecycle,
}
c.AddTask(hc)
var igms []*gcetasks.InstanceGroupManager
for _, ig := range b.InstanceGroups {
if ig.Spec.Role != kops.InstanceGroupRoleControlPlane {
continue
}
if len(ig.Spec.Zones) > 1 {
return fmt.Errorf("instance group %q has %d zones, which is not yet supported for GCP", ig.GetName(), len(ig.Spec.Zones))
}
if len(ig.Spec.Zones) == 0 {
return fmt.Errorf("instance group %q must specify exactly one zone", ig.GetName())
}
zone := ig.Spec.Zones[0]
igms = append(igms, &gcetasks.InstanceGroupManager{Name: s(gce.NameForInstanceGroupManager(b.Cluster, ig, zone)), Zone: s(zone)})
}
bs := &gcetasks.BackendService{
Name: s(b.NameForBackendService("api")),
Protocol: s("TCP"),
HealthChecks: []*gcetasks.HealthCheck{hc},
Lifecycle: b.Lifecycle,
LoadBalancingScheme: s("INTERNAL"),
InstanceGroupManagers: igms,
}
c.AddTask(bs)
network, err := b.LinkToNetwork()
if err != nil {
return err
}
for _, sn := range b.Cluster.Spec.Networking.Subnets {
var subnet *gcetasks.Subnet
for _, ig := range b.InstanceGroups {
if ig.HasAPIServer() && slices.Contains(ig.Spec.Subnets, sn.Name) {
subnet = b.LinkToSubnet(&sn)
break
}
}
if subnet == nil {
continue
}
ipAddress := &gcetasks.Address{
Name: s(b.NameForIPAddress("api-" + sn.Name)),
IPAddressType: s("INTERNAL"),
Purpose: s("SHARED_LOADBALANCER_VIP"),
Subnetwork: subnet,
ForAPIServer: true,
Lifecycle: b.Lifecycle,
}
c.AddTask(ipAddress)
c.AddTask(&gcetasks.ForwardingRule{
Name: s(b.NameForForwardingRule("api-" + sn.Name)),
Lifecycle: b.Lifecycle,
BackendService: bs,
Ports: []string{strconv.Itoa(wellknownports.KubeAPIServer)},
IPAddress: ipAddress,
IPProtocol: "TCP",
LoadBalancingScheme: s("INTERNAL"),
Network: network,
Subnetwork: subnet,
Labels: map[string]string{
clusterLabel.Key: clusterLabel.Value,
"name": "api-" + sn.Name,
},
})
if b.Cluster.UsesNoneDNS() {
c.AddTask(&gcetasks.ForwardingRule{
Name: s(b.NameForForwardingRule("kops-controller-" + sn.Name)),
Lifecycle: b.Lifecycle,
BackendService: bs,
Ports: []string{strconv.Itoa(wellknownports.KopsControllerPort)},
IPAddress: ipAddress,
IPProtocol: "TCP",
LoadBalancingScheme: s("INTERNAL"),
Network: network,
Subnetwork: subnet,
Labels: map[string]string{
clusterLabel.Key: clusterLabel.Value,
"name": "kops-controller-" + sn.Name,
},
})
}
}
return b.addFirewallRules(c)
}
func (b *APILoadBalancerBuilder) Build(c *fi.CloudupModelBuilderContext) error {
if !b.UseLoadBalancerForAPI() {
return nil
}
lbSpec := b.Cluster.Spec.API.LoadBalancer
if lbSpec == nil {
// Skipping API LB creation; not requested in Spec
return nil
}
switch lbSpec.Type {
case kops.LoadBalancerTypePublic:
return b.createPublicLB(c)
case kops.LoadBalancerTypeInternal:
return b.createInternalLB(c)
default:
return fmt.Errorf("unhandled LoadBalancer type %q", lbSpec.Type)
}
}
// subnetNotSpecified returns true if the given LB subnet is not listed in the list of cluster subnets.
func subnetNotSpecified(sn kops.LoadBalancerSubnetSpec, subnets []kops.ClusterSubnetSpec) bool {
for _, csn := range subnets {
if csn.Name == sn.Name || csn.ID == sn.Name {
return false
}
}
return true
}