/
environ_network.go
439 lines (390 loc) · 12.6 KB
/
environ_network.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package gce
import (
"fmt"
"strings"
"github.com/juju/collections/set"
"github.com/juju/errors"
"github.com/juju/names/v5"
"google.golang.org/api/compute/v1"
"github.com/juju/juju/core/instance"
corenetwork "github.com/juju/juju/core/network"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/environs/instances"
"github.com/juju/juju/provider/gce/google"
)
type subnetMap map[string]corenetwork.SubnetInfo
type networkMap map[string]*compute.Network
// Subnets implements environs.NetworkingEnviron.
func (e *environ) Subnets(
ctx context.ProviderCallContext, inst instance.Id, subnetIds []corenetwork.Id,
) ([]corenetwork.SubnetInfo, error) {
// In GCE all the subnets are in all AZs.
zones, err := e.zoneNames(ctx)
if err != nil {
return nil, errors.Trace(err)
}
ids := makeIncludeSet(subnetIds)
var results []corenetwork.SubnetInfo
if inst == instance.UnknownId {
results, err = e.getMatchingSubnets(ctx, ids, zones)
} else {
results, err = e.getInstanceSubnets(ctx, inst, ids, zones)
}
if err != nil {
return nil, errors.Trace(err)
}
if missing := ids.Missing(); len(missing) != 0 {
return nil, errors.NotFoundf("subnets %v", formatMissing(missing))
}
return results, nil
}
func (e *environ) zoneNames(ctx context.ProviderCallContext) ([]string, error) {
zones, err := e.AvailabilityZones(ctx)
if err != nil {
return nil, errors.Trace(err)
}
names := make([]string, len(zones))
for i, zone := range zones {
names[i] = zone.Name()
}
return names, nil
}
func (e *environ) networksByURL(ctx context.ProviderCallContext) (networkMap, error) {
networks, err := e.gce.Networks()
if err != nil {
return nil, google.HandleCredentialError(errors.Trace(err), ctx)
}
results := make(networkMap)
for _, network := range networks {
results[network.SelfLink] = network
}
return results, nil
}
func (e *environ) getMatchingSubnets(
ctx context.ProviderCallContext, subnetIds IncludeSet, zones []string,
) ([]corenetwork.SubnetInfo, error) {
allSubnets, err := e.gce.Subnetworks(e.cloud.Region)
if err != nil {
return nil, google.HandleCredentialError(errors.Trace(err), ctx)
}
networks, err := e.networksByURL(ctx)
if err != nil {
return nil, errors.Trace(err)
}
var results []corenetwork.SubnetInfo
for _, subnet := range allSubnets {
netwk, ok := networks[subnet.Network]
if !ok {
return nil, errors.NotFoundf("network %q for subnet %q", subnet.Network, subnet.Name)
}
if subnetIds.Include(subnet.Name) {
results = append(results, makeSubnetInfo(
corenetwork.Id(subnet.Name),
corenetwork.Id(netwk.Name),
subnet.IpCidrRange,
zones,
))
}
}
// We have to include networks in 'LEGACY' mode that do not have subnetworks.
for _, netwk := range networks {
if netwk.IPv4Range != "" && subnetIds.Include(netwk.Name) {
results = append(results, makeSubnetInfo(
corenetwork.Id(netwk.Name),
corenetwork.Id(netwk.Name),
netwk.IPv4Range,
zones,
))
}
}
return results, nil
}
func (e *environ) getInstanceSubnets(
ctx context.ProviderCallContext, inst instance.Id, subnetIds IncludeSet, zones []string,
) ([]corenetwork.SubnetInfo, error) {
ifLists, err := e.NetworkInterfaces(ctx, []instance.Id{inst})
if err != nil {
return nil, errors.Trace(err)
}
ifaces := ifLists[0]
var results []corenetwork.SubnetInfo
for _, iface := range ifaces {
if subnetIds.Include(string(iface.ProviderSubnetId)) {
results = append(results, makeSubnetInfo(
iface.ProviderSubnetId,
iface.ProviderNetworkId,
iface.PrimaryAddress().CIDR,
zones,
))
}
}
return results, nil
}
// NetworkInterfaces implements environs.NetworkingEnviron.
func (e *environ) NetworkInterfaces(ctx context.ProviderCallContext, ids []instance.Id) ([]corenetwork.InterfaceInfos, error) {
if len(ids) == 0 {
return nil, environs.ErrNoInstances
}
// Fetch instance information for the IDs we are interested in.
insts, err := e.Instances(ctx, ids)
partialInfo := err == environs.ErrPartialInstances
if err != nil && err != environs.ErrPartialInstances {
if errors.Cause(err) == environs.ErrNoInstances {
return nil, err
}
return nil, errors.Trace(err)
}
// In GCE all the subnets are in all AZs.
zones, err := e.zoneNames(ctx)
if err != nil {
return nil, errors.Trace(err)
}
networks, err := e.networksByURL(ctx)
if err != nil {
return nil, errors.Trace(err)
}
// Extract the unique list of subnet URLs we are interested in for
// all instances and fetch related subnet information.
uniqueSubnetURLs, err := getUniqueSubnetURLs(ids, insts)
if err != nil {
return nil, errors.Trace(err)
}
subnets, err := e.subnetsByURL(ctx, uniqueSubnetURLs.Values(), networks, zones)
if err != nil {
return nil, errors.Trace(err)
}
infos := make([]corenetwork.InterfaceInfos, len(ids))
for idx, inst := range insts {
if inst == nil {
continue // no instance with this ID known by provider
}
// Note: we have already verified that we can safely cast inst
// to environInstance when we iterated the instance list to
// obtain the unique subnet URLs
for i, iface := range inst.(*environInstance).base.NetworkInterfaces() {
details, err := findNetworkDetails(iface, subnets, networks)
if err != nil {
return nil, errors.Annotatef(err, "instance %q", ids[idx])
}
// Scan the access configs for public addresses
var shadowAddrs corenetwork.ProviderAddresses
for _, accessConf := range iface.AccessConfigs {
// According to the gce docs only ONE_TO_ONE_NAT
// is currently supported for external IPs
if accessConf.Type != "ONE_TO_ONE_NAT" {
continue
}
shadowAddrs = append(shadowAddrs,
corenetwork.NewMachineAddress(accessConf.NatIP, corenetwork.WithScope(corenetwork.ScopePublic)).AsProviderAddress(),
)
}
infos[idx] = append(infos[idx], corenetwork.InterfaceInfo{
DeviceIndex: i,
// The network interface has no id in GCE so it's
// identified by the machine's id + its name.
ProviderId: corenetwork.Id(fmt.Sprintf("%s/%s", ids[idx], iface.Name)),
ProviderSubnetId: details.subnet,
ProviderNetworkId: details.network,
AvailabilityZones: copyStrings(zones),
InterfaceName: iface.Name,
Addresses: corenetwork.ProviderAddresses{corenetwork.NewMachineAddress(
iface.NetworkIP,
corenetwork.WithScope(corenetwork.ScopeCloudLocal),
corenetwork.WithCIDR(details.cidr),
corenetwork.WithConfigType(corenetwork.ConfigDHCP),
).AsProviderAddress()},
ShadowAddresses: shadowAddrs,
InterfaceType: corenetwork.EthernetDevice,
Disabled: false,
NoAutoStart: false,
Origin: corenetwork.OriginProvider,
})
}
}
if partialInfo {
err = environs.ErrPartialInstances
}
return infos, err
}
func getUniqueSubnetURLs(ids []instance.Id, insts []instances.Instance) (set.Strings, error) {
uniqueSet := set.NewStrings()
for idx, inst := range insts {
if inst == nil {
continue // no instance with this ID known by provider
}
envInst, ok := inst.(*environInstance)
if !ok { // This shouldn't happen.
return nil, errors.Errorf("couldn't extract GCE instance for %q", ids[idx])
}
for _, iface := range envInst.base.NetworkInterfaces() {
if iface.Subnetwork != "" {
uniqueSet.Add(iface.Subnetwork)
}
}
}
return uniqueSet, nil
}
type networkDetails struct {
cidr string
subnet corenetwork.Id
network corenetwork.Id
}
// findNetworkDetails looks up the network information we need to
// populate an InterfaceInfo - if the interface is on a legacy network
// we use information from the network because there'll be no subnet
// linked.
func findNetworkDetails(iface compute.NetworkInterface, subnets subnetMap, networks networkMap) (networkDetails, error) {
var result networkDetails
if iface.Subnetwork == "" {
// This interface is on a legacy network.
netwk, ok := networks[iface.Network]
if !ok {
return result, errors.NotFoundf("network %q", iface.Network)
}
result.cidr = netwk.IPv4Range
result.subnet = ""
result.network = corenetwork.Id(netwk.Name)
} else {
subnet, ok := subnets[iface.Subnetwork]
if !ok {
return result, errors.NotFoundf("subnet %q", iface.Subnetwork)
}
result.cidr = subnet.CIDR
result.subnet = subnet.ProviderId
result.network = subnet.ProviderNetworkId
}
return result, nil
}
func (e *environ) subnetsByURL(ctx context.ProviderCallContext, urls []string, networks networkMap, zones []string) (subnetMap, error) {
if len(urls) == 0 {
return make(map[string]corenetwork.SubnetInfo), nil
}
urlSet := includeSet{items: set.NewStrings(urls...)}
allSubnets, err := e.gce.Subnetworks(e.cloud.Region)
if err != nil {
return nil, google.HandleCredentialError(errors.Trace(err), ctx)
}
results := make(map[string]corenetwork.SubnetInfo)
for _, subnet := range allSubnets {
netwk, ok := networks[subnet.Network]
if !ok {
return nil, errors.NotFoundf("network %q for subnet %q", subnet.Network, subnet.Name)
}
if urlSet.Include(subnet.SelfLink) {
results[subnet.SelfLink] = makeSubnetInfo(
corenetwork.Id(subnet.Name),
corenetwork.Id(netwk.Name),
subnet.IpCidrRange,
zones,
)
}
}
if missing := urlSet.Missing(); len(missing) != 0 {
return nil, errors.NotFoundf("subnets %v", formatMissing(missing))
}
return results, nil
}
// SupportsSpaces implements environs.NetworkingEnviron.
func (e *environ) SupportsSpaces(ctx context.ProviderCallContext) (bool, error) {
return false, nil
}
// SupportsContainerAddresses implements environs.NetworkingEnviron.
func (e *environ) SupportsContainerAddresses(ctx context.ProviderCallContext) (bool, error) {
return false, nil
}
// AllocateContainerAddresses implements environs.NetworkingEnviron.
func (e *environ) AllocateContainerAddresses(context.ProviderCallContext, instance.Id, names.MachineTag, corenetwork.InterfaceInfos) (corenetwork.InterfaceInfos, error) {
return nil, errors.NotSupportedf("container addresses")
}
// ReleaseContainerAddresses implements environs.NetworkingEnviron.
func (e *environ) ReleaseContainerAddresses(context.ProviderCallContext, []corenetwork.ProviderInterfaceInfo) error {
return errors.NotSupportedf("container addresses")
}
// AreSpacesRoutable implements environs.NetworkingEnviron.
func (*environ) AreSpacesRoutable(ctx context.ProviderCallContext, space1, space2 *environs.ProviderSpaceInfo) (bool, error) {
return false, nil
}
// SuperSubnets implements environs.SuperSubnets
func (e *environ) SuperSubnets(ctx context.ProviderCallContext) ([]string, error) {
subnets, err := e.Subnets(ctx, "", nil)
if err != nil {
return nil, err
}
cidrs := make([]string, len(subnets))
for i, subnet := range subnets {
cidrs[i] = subnet.CIDR
}
return cidrs, nil
}
func copyStrings(items []string) []string {
if items == nil {
return nil
}
result := make([]string, len(items))
copy(result, items)
return result
}
func makeSubnetInfo(
subnetId corenetwork.Id, networkId corenetwork.Id, cidr string, zones []string,
) corenetwork.SubnetInfo {
return corenetwork.SubnetInfo{
ProviderId: subnetId,
ProviderNetworkId: networkId,
CIDR: cidr,
AvailabilityZones: copyStrings(zones),
VLANTag: 0,
}
}
// IncludeSet represents a set of items that can be crossed off once,
// and when you're finished crossing items off then you can see what's
// left.
type IncludeSet interface {
// Include returns whether this item should be included, and
// crosses it off.
Include(item string) bool
// Missing returns any items that haven't been crossed off (as a
// sorted slice).
Missing() []string
}
// includeAny allows any items and doesn't report any as missing.
type includeAny struct{}
// Include implements IncludeSet.
func (includeAny) Include(string) bool { return true }
// Missing implements IncludeSet.
func (includeAny) Missing() []string { return nil }
// includeSet is a set of items that we want to find in some results.
type includeSet struct {
items set.Strings
}
// Include implements IncludeSet.
func (s *includeSet) Include(item string) bool {
if s.items.Contains(item) {
s.items.Remove(item)
return true
}
return false
}
// Missing implements IncludeSet.
func (s *includeSet) Missing() []string {
return s.items.SortedValues()
}
func makeIncludeSet(ids []corenetwork.Id) IncludeSet {
if len(ids) == 0 {
return &includeAny{}
}
str := set.NewStrings()
for _, id := range ids {
str.Add(string(id))
}
return &includeSet{items: str}
}
func formatMissing(items []string) string {
parts := make([]string, len(items))
for i, item := range items {
parts[i] = fmt.Sprintf("%q", item)
}
return "[" + strings.Join(parts, ", ") + "]"
}