-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
subnet.go
344 lines (283 loc) · 8.8 KB
/
subnet.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
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 gcetasks
import (
"fmt"
"reflect"
compute "google.golang.org/api/compute/v1"
"k8s.io/klog/v2"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/gce"
"k8s.io/kops/upup/pkg/fi/cloudup/terraform"
"k8s.io/kops/upup/pkg/fi/cloudup/terraformWriter"
)
// +kops:fitask
type Subnet struct {
Name *string
Lifecycle fi.Lifecycle
Network *Network
Region *string
CIDR *string
// StackType indicates the address families supported (IPV4_IPV6 or IPV4_ONLY)
StackType *string
// Ipv6AccessType indicates whether the IPv6 addresses are accessible externally
Ipv6AccessType *string
SecondaryIpRanges map[string]string
Shared *bool
}
var _ fi.CompareWithID = &Subnet{}
func (e *Subnet) CompareWithID() *string {
return e.Name
}
func (e *Subnet) Find(c *fi.CloudupContext) (*Subnet, error) {
cloud := c.T.Cloud.(gce.GCECloud)
_, project, err := gce.ParseNameAndProjectFromNetworkID(c.T.Cluster.Spec.Networking.NetworkID)
if err != nil {
return nil, fmt.Errorf("error parsing network name from cluster spec: %w", err)
} else if project == "" {
project = cloud.Project()
}
s, err := cloud.Compute().Subnetworks().Get(project, cloud.Region(), *e.Name)
if err != nil {
if gce.IsNotFound(err) {
return nil, nil
}
return nil, fmt.Errorf("error listing Subnets: %w", err)
}
actual := &Subnet{}
actual.Name = &s.Name
actual.Network = &Network{Name: fi.PtrTo(lastComponent(s.Network))}
actual.Region = fi.PtrTo(lastComponent(s.Region))
actual.CIDR = &s.IpCidrRange
actual.StackType = &s.StackType
actual.Ipv6AccessType = &s.Ipv6AccessType
shared := fi.ValueOf(e.Shared)
{
actual.SecondaryIpRanges = make(map[string]string)
for _, r := range s.SecondaryIpRanges {
if shared {
// In the shared case, only show differences on the ranges we specified
if _, found := e.SecondaryIpRanges[r.RangeName]; !found {
continue
}
}
actual.SecondaryIpRanges[r.RangeName] = r.IpCidrRange
}
}
// Prevent spurious changes
actual.Lifecycle = e.Lifecycle
actual.Name = e.Name
actual.Shared = e.Shared
return actual, nil
}
func (e *Subnet) Run(c *fi.CloudupContext) error {
return fi.CloudupDefaultDeltaRunMethod(e, c)
}
func (_ *Subnet) CheckChanges(a, e, changes *Subnet) error {
return nil
}
func (_ *Subnet) RenderGCE(t *gce.GCEAPITarget, a, e, changes *Subnet) error {
shared := fi.ValueOf(e.Shared)
if shared {
// Verify the subnet was found
if a == nil {
return fmt.Errorf("Subnet with name %q not found", fi.ValueOf(e.Name))
}
}
cloud := t.Cloud
project := cloud.Project()
if a == nil {
klog.V(2).Infof("Creating Subnet with CIDR: %q", fi.ValueOf(e.CIDR))
subnet := &compute.Subnetwork{
IpCidrRange: fi.ValueOf(e.CIDR),
Name: *e.Name,
Network: e.Network.URL(project),
StackType: fi.ValueOf(e.StackType),
Ipv6AccessType: fi.ValueOf(e.Ipv6AccessType),
}
for k, v := range e.SecondaryIpRanges {
subnet.SecondaryIpRanges = append(subnet.SecondaryIpRanges, &compute.SubnetworkSecondaryRange{
RangeName: k,
IpCidrRange: v,
})
}
op, err := cloud.Compute().Subnetworks().Insert(t.Cloud.Project(), t.Cloud.Region(), subnet)
if err != nil {
return fmt.Errorf("error creating Subnet: %v", err)
}
if err := t.Cloud.WaitForOp(op); err != nil {
return fmt.Errorf("error waiting for Subnet creation to complete: %w", err)
}
} else {
if changes.SecondaryIpRanges != nil {
// Update is split into two calls as GCE does not allow us to add and remove ranges in the same call
if err := updateSecondaryRanges(cloud, "add", e); err != nil {
return err
}
if !shared {
if err := updateSecondaryRanges(cloud, "remove", e); err != nil {
return err
}
}
changes.SecondaryIpRanges = nil
}
if changes.StackType != nil {
if err := updateStackTypeAndIPv6AccessType(cloud, e); err != nil {
return err
}
changes.StackType = nil
changes.Ipv6AccessType = nil
}
empty := &Subnet{}
if !reflect.DeepEqual(empty, changes) {
return fmt.Errorf("cannot apply changes to Subnet: %v", changes)
}
}
return nil
}
func updateSecondaryRanges(cloud gce.GCECloud, op string, e *Subnet) error {
// We need to refetch to patch it
subnet, err := cloud.Compute().Subnetworks().Get(cloud.Project(), cloud.Region(), *e.Name)
if err != nil {
return fmt.Errorf("error fetching subnet for patch: %w", err)
}
expectedRanges := e.SecondaryIpRanges
actualRanges := make(map[string]string)
for _, r := range subnet.SecondaryIpRanges {
actualRanges[r.RangeName] = r.IpCidrRange
}
// Cannot add and remove ranges in the same call
if op == "add" {
patch := false
for k, v := range expectedRanges {
if actualRanges[k] != v {
actualRanges[k] = v
subnet.SecondaryIpRanges = append(subnet.SecondaryIpRanges, &compute.SubnetworkSecondaryRange{
RangeName: k,
IpCidrRange: v,
})
patch = true
}
}
if !patch {
return nil
}
} else if op == "remove" {
patch := false
if len(actualRanges) != len(expectedRanges) {
patch = true
} else {
for k := range expectedRanges {
if actualRanges[k] != e.SecondaryIpRanges[k] {
patch = true
}
}
}
if !patch {
return nil
}
subnet.SecondaryIpRanges = nil
for k, v := range expectedRanges {
subnet.SecondaryIpRanges = append(subnet.SecondaryIpRanges, &compute.SubnetworkSecondaryRange{
RangeName: k,
IpCidrRange: v,
})
}
}
patchOp, err := cloud.Compute().Subnetworks().Patch(cloud.Project(), cloud.Region(), subnet.Name, subnet)
if err != nil {
return fmt.Errorf("error patching Subnet: %w", err)
}
if err := cloud.WaitForOp(patchOp); err != nil {
return fmt.Errorf("error waiting for Subnet patch to complete: %w", err)
}
return nil
}
func updateStackTypeAndIPv6AccessType(cloud gce.GCECloud, e *Subnet) error {
// We need to refetch to patch it
subnet, err := cloud.Compute().Subnetworks().Get(cloud.Project(), cloud.Region(), *e.Name)
if err != nil {
return fmt.Errorf("error fetching subnet for patch: %w", err)
}
subnet.StackType = fi.ValueOf(e.StackType)
subnet.Ipv6AccessType = fi.ValueOf(e.Ipv6AccessType)
patchOp, err := cloud.Compute().Subnetworks().Patch(cloud.Project(), cloud.Region(), subnet.Name, subnet)
if err != nil {
return fmt.Errorf("error patching Subnet: %w", err)
}
if err := cloud.WaitForOp(patchOp); err != nil {
return fmt.Errorf("error waiting for Subnet patch to complete: %w", err)
}
return nil
}
func (e *Subnet) URL(project string, region string) string {
u := gce.GoogleCloudURL{
Version: "v1",
Project: project,
Name: *e.Name,
Type: "subnetworks",
Region: region,
}
return u.BuildURL()
}
type terraformSubnet struct {
Name *string `cty:"name"`
Network *terraformWriter.Literal `cty:"network"`
Region *string `cty:"region"`
CIDR *string `cty:"ip_cidr_range"`
// SecondaryIPRange defines additional IP ranges
SecondaryIPRange []terraformSubnetRange `cty:"secondary_ip_range"`
StackType *string `cty:"stack_type"`
Ipv6AccessType *string `cty:"ipv6_access_type"`
}
type terraformSubnetRange struct {
Name string `cty:"range_name"`
CIDR string `cty:"ip_cidr_range"`
}
func (_ *Subnet) RenderSubnet(t *terraform.TerraformTarget, a, e, changes *Subnet) error {
shared := fi.ValueOf(e.Shared)
if shared {
// Not terraform owned / managed
return nil
}
tf := &terraformSubnet{
Name: e.Name,
Network: e.Network.TerraformLink(),
Region: e.Region,
CIDR: e.CIDR,
StackType: e.StackType,
Ipv6AccessType: e.Ipv6AccessType,
}
for k, v := range e.SecondaryIpRanges {
tf.SecondaryIPRange = append(tf.SecondaryIPRange, terraformSubnetRange{
Name: k,
CIDR: v,
})
}
return t.RenderResource("google_compute_subnetwork", *e.Name, tf)
}
func (e *Subnet) TerraformLink() *terraformWriter.Literal {
shared := fi.ValueOf(e.Shared)
if shared {
if e.Name == nil {
klog.Fatalf("GCEName must be set, if subnet is shared: %#v", e)
}
name := *e.Name
if e.Network != nil && e.Network.Project != nil {
name = *e.Network.Project + "/" + name
}
klog.V(4).Infof("reusing existing subnet with name %q", name)
return terraformWriter.LiteralFromStringValue(name)
}
return terraformWriter.LiteralProperty("google_compute_subnetwork", *e.Name, "name")
}