forked from juju/juju
/
broker.go
223 lines (195 loc) · 7.22 KB
/
broker.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
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package provisioner
import (
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/utils/arch"
"github.com/juju/version"
"gopkg.in/juju/names.v2"
apiprovisioner "github.com/juju/juju/api/provisioner"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/cloudconfig"
"github.com/juju/juju/container"
"github.com/juju/juju/core/instance"
"github.com/juju/juju/network"
"github.com/juju/juju/tools"
)
//go:generate mockgen -package mocks -destination mocks/apicalls_mock.go github.com/juju/juju/worker/provisioner APICalls
type APICalls interface {
ContainerConfig() (params.ContainerConfig, error)
PrepareContainerInterfaceInfo(names.MachineTag) ([]network.InterfaceInfo, error)
GetContainerInterfaceInfo(names.MachineTag) ([]network.InterfaceInfo, error)
GetContainerProfileInfo(names.MachineTag) ([]*apiprovisioner.LXDProfileResult, error)
ReleaseContainerAddresses(names.MachineTag) error
SetHostMachineNetworkConfig(names.MachineTag, []params.NetworkConfig) error
HostChangesForContainer(containerTag names.MachineTag) ([]network.DeviceToBridge, int, error)
}
type hostArchToolsFinder struct {
f ToolsFinder
}
// FindTools is defined on the ToolsFinder interface.
func (h hostArchToolsFinder) FindTools(v version.Number, series, _ string) (tools.List, error) {
// Override the arch constraint with the arch of the host.
return h.f.FindTools(v, series, arch.HostArch())
}
// resolvConf contains the full path to common resolv.conf files on the local
// system. Defined here so it can be overridden for testing.
var resolvConfFiles = []string{"/etc/resolv.conf", "/etc/systemd/resolved.conf", "/run/systemd/resolve/resolv.conf"}
func prepareOrGetContainerInterfaceInfo(
api APICalls,
machineID string,
allocateOrMaintain bool,
log loggo.Logger,
) ([]network.InterfaceInfo, error) {
maintain := !allocateOrMaintain
if maintain {
// TODO(jam): 2016-12-14 The function is called
// 'prepareOrGet', but the only time we would handle the 'Get'
// side, we explicitly abort. Something seems wrong.
log.Debugf("not running maintenance for machine %q", machineID)
return nil, nil
}
log.Debugf("using multi-bridge networking for container %q", machineID)
containerTag := names.NewMachineTag(machineID)
preparedInfo, err := api.PrepareContainerInterfaceInfo(containerTag)
if err != nil {
return nil, errors.Trace(err)
}
log.Tracef("PrepareContainerInterfaceInfo returned %+v", preparedInfo)
return preparedInfo, nil
}
// finishNetworkConfig populates the ParentInterfaceName, DNSServers, and
// DNSSearchDomains fields on each element, when they are not set. The given
// bridgeDevice is used for ParentInterfaceName, while the DNS config is
// discovered using network.ParseResolvConf(). If interfaces has zero length,
// container.FallbackInterfaceInfo() is used as fallback.
func finishNetworkConfig(bridgeDevice string, interfaces []network.InterfaceInfo) ([]network.InterfaceInfo, error) {
haveNameservers, haveSearchDomains := false, false
if len(interfaces) == 0 {
// Use the fallback network config as a last resort.
interfaces = container.FallbackInterfaceInfo()
}
results := make([]network.InterfaceInfo, len(interfaces))
for i, info := range interfaces {
if info.ParentInterfaceName == "" {
info.ParentInterfaceName = bridgeDevice
}
if len(info.DNSServers) > 0 {
haveNameservers = true
}
if len(info.DNSSearchDomains) > 0 {
haveSearchDomains = true
}
results[i] = info
}
if !haveNameservers || !haveSearchDomains {
warnMissing := func(s string) { logger.Warningf("no %s supplied by provider, using host's %s.", s, s) }
if !haveNameservers {
warnMissing("name servers")
}
if !haveSearchDomains {
warnMissing("search domains")
}
logger.Warningf("incomplete DNS config found, discovering host's DNS config")
dnsConfig, err := findDNSServerConfig()
if err != nil {
return nil, errors.Trace(err)
}
// Since the result is sorted, the first entry is the primary NIC. Also,
// results always contains at least one element.
results[0].DNSServers = dnsConfig.Nameservers
results[0].DNSSearchDomains = dnsConfig.SearchDomains
logger.Debugf(
"setting DNS servers %+v and domains %+v on container interface %q",
results[0].DNSServers, results[0].DNSSearchDomains, results[0].InterfaceName,
)
}
return results, nil
}
// findDNSServerConfig is a heuristic method to find an adequate DNS
// configuration. Currently the only rule that is implemented is that common
// configuration files are parsed until a configuration is found that is not a
// loopback address (i.e systemd/resolved stub address).
func findDNSServerConfig() (*network.DNSConfig, error) {
for _, dnsConfigFile := range resolvConfFiles {
dnsConfig, err := network.ParseResolvConf(dnsConfigFile)
if err != nil {
return nil, errors.Trace(err)
}
for _, nameServer := range dnsConfig.Nameservers {
if nameServer.Scope != network.ScopeMachineLocal {
logger.Debugf("The DNS configuration from %s has been selected for use", dnsConfigFile)
return dnsConfig, nil
}
}
}
return nil, errors.New("A DNS configuration could not be found.")
}
func releaseContainerAddresses(
api APICalls,
instanceID instance.Id,
namespace instance.Namespace,
log loggo.Logger,
) {
containerTag, err := namespace.MachineTag(string(instanceID))
if err != nil {
// Not a reason to cause StopInstances to fail though..
log.Warningf("unexpected container tag %q: %v", instanceID, err)
return
}
err = api.ReleaseContainerAddresses(containerTag)
switch {
case err == nil:
log.Infof("released all addresses for container %q", containerTag.Id())
case errors.IsNotSupported(err):
log.Warningf("not releasing all addresses for container %q: %v", containerTag.Id(), err)
default:
log.Warningf(
"unexpected error trying to release container %q addreses: %v",
containerTag.Id(), err,
)
}
}
// matchHostArchTools filters the given list of tools to the host architecture.
func matchHostArchTools(allTools tools.List) (tools.List, error) {
arch := arch.HostArch()
archTools, err := allTools.Match(tools.Filter{Arch: arch})
if err == tools.ErrNoMatches {
return nil, errors.Errorf(
"need agent binaries for arch %s, only found %s",
arch, allTools.Arches(),
)
} else if err != nil {
return nil, errors.Trace(err)
}
return archTools, nil
}
// GetMachineCloudInitData is for testing purposes.
var GetMachineCloudInitData = cloudconfig.GetMachineCloudInitData
// combinedCloudInitData returns a combined map of the given cloudInitData
// and instance cloud init properties provided.
func combinedCloudInitData(
cloudInitData map[string]interface{},
containerInheritProperties, series string,
log loggo.Logger,
) (map[string]interface{}, error) {
if containerInheritProperties == "" {
return cloudInitData, nil
}
machineData, err := GetMachineCloudInitData(series)
if err != nil {
return nil, err
}
if machineData == nil {
return cloudInitData, nil
}
if cloudInitData == nil {
cloudInitData = make(map[string]interface{})
}
resultsMap := cloudconfig.CloudConfigByVersionFunc(series)(containerInheritProperties, machineData, log)
for k, v := range resultsMap {
cloudInitData[k] = v
}
return cloudInitData, nil
}