/
lxd-broker.go
216 lines (189 loc) · 6.85 KB
/
lxd-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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package broker
import (
"fmt"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/names/v5"
"github.com/juju/juju/agent"
"github.com/juju/juju/cloudconfig/instancecfg"
"github.com/juju/juju/container"
"github.com/juju/juju/core/instance"
"github.com/juju/juju/core/lxdprofile"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/environs/instances"
)
var lxdLogger = loggo.GetLogger("juju.container.broker.lxd")
type PrepareHostFunc func(containerTag names.MachineTag, log loggo.Logger, abort <-chan struct{}) error
// NewLXDBroker creates a Broker that can be used to start LXD containers in a
// similar fashion to normal StartInstance requests.
// prepareHost is a callback that will be called when a new container is about
// to be started. It provides the intersection point where the host can update
// itself to be ready for whatever changes are necessary to have a functioning
// container. (such as bridging host devices.)
// manager is the infrastructure to actually launch the container.
// agentConfig is currently only used to find out the 'default' bridge to use
// when a specific network device is not specified in StartInstanceParams. This
// should be deprecated. And hopefully removed in the future.
func NewLXDBroker(
prepareHost PrepareHostFunc,
api APICalls,
manager container.Manager,
agentConfig agent.Config,
) (environs.InstanceBroker, error) {
return &lxdBroker{
prepareHost: prepareHost,
manager: manager,
api: api,
agentConfig: agentConfig,
}, nil
}
type lxdBroker struct {
prepareHost PrepareHostFunc
manager container.Manager
api APICalls
agentConfig agent.Config
}
func (broker *lxdBroker) StartInstance(ctx context.ProviderCallContext, args environs.StartInstanceParams) (*environs.StartInstanceResult, error) {
containerMachineID := args.InstanceConfig.MachineId
config, err := broker.api.ContainerConfig()
if err != nil {
lxdLogger.Errorf("failed to get container config: %v", err)
return nil, err
}
if err := broker.prepareHost(names.NewMachineTag(containerMachineID), lxdLogger, args.Abort); err != nil {
return nil, errors.Trace(err)
}
preparedInfo, err := prepareContainerInterfaceInfo(broker.api, containerMachineID, lxdLogger)
if err != nil {
return nil, errors.Trace(err)
}
interfaces, err := finishNetworkConfig(preparedInfo)
if err != nil {
return nil, errors.Trace(err)
}
net := container.BridgeNetworkConfig(0, interfaces)
pNames, err := broker.writeProfiles(containerMachineID)
if err != nil {
err = fmt.Errorf("cannot write charm profile: %w", err)
return nil, errors.WithType(err, environs.ErrAvailabilityZoneIndependent)
}
// The provisioner worker will provide all tools it knows about
// (after applying explicitly specified constraints), which may
// include tools for architectures other than the host's. We
// must constrain to the host's architecture for LXD.
archTools, err := matchHostArchTools(args.Tools)
if err != nil {
return nil, errors.Trace(err)
}
args.InstanceConfig.MachineContainerType = instance.LXD
if err := args.InstanceConfig.SetTools(archTools); err != nil {
return nil, errors.Trace(err)
}
cloudInitUserData, err := combinedCloudInitData(
config.CloudInitUserData,
config.ContainerInheritProperties,
args.InstanceConfig.Base, lxdLogger)
if err != nil {
return nil, errors.Trace(err)
}
if err := instancecfg.PopulateInstanceConfig(
args.InstanceConfig,
config.ProviderType,
config.AuthorizedKeys,
config.SSLHostnameVerification,
proxyConfigurationFromContainerCfg(config),
config.EnableOSRefreshUpdate,
config.EnableOSUpgrade,
cloudInitUserData,
append([]string{"default"}, pNames...),
); err != nil {
lxdLogger.Errorf("failed to populate machine config: %v", err)
return nil, err
}
storageConfig := &container.StorageConfig{}
inst, hardware, err := broker.manager.CreateContainer(
ctx, args.InstanceConfig, args.Constraints, args.InstanceConfig.Base, net, storageConfig, args.StatusCallback,
)
if err != nil {
return nil, err
}
return &environs.StartInstanceResult{
Instance: inst,
Hardware: hardware,
}, nil
}
func (broker *lxdBroker) StopInstances(ctx context.ProviderCallContext, ids ...instance.Id) error {
// TODO: potentially parallelise.
for _, id := range ids {
lxdLogger.Infof("stopping lxd container for instance: %s", id)
if err := broker.manager.DestroyContainer(id); err != nil {
lxdLogger.Errorf("container did not stop: %v", err)
return err
}
releaseContainerAddresses(broker.api, id, broker.manager.Namespace(), lxdLogger)
}
return nil
}
// AllInstances returns all containers.
func (broker *lxdBroker) AllInstances(ctx context.ProviderCallContext) (result []instances.Instance, err error) {
return broker.manager.ListContainers()
}
// AllRunningInstances only returns running containers.
func (broker *lxdBroker) AllRunningInstances(ctx context.ProviderCallContext) (result []instances.Instance, err error) {
return broker.manager.ListContainers()
}
// LXDProfileNames returns all the profiles for a container that the broker
// currently is aware of.
// LXDProfileNames implements environs.LXDProfiler.
func (broker *lxdBroker) LXDProfileNames(containerName string) ([]string, error) {
nameRetriever, ok := broker.manager.(container.LXDProfileNameRetriever)
if !ok {
return make([]string, 0), nil
}
return nameRetriever.LXDProfileNames(containerName)
}
func (broker *lxdBroker) writeProfiles(machineID string) ([]string, error) {
containerTag := names.NewMachineTag(machineID)
profileInfo, err := broker.api.GetContainerProfileInfo(containerTag)
if err != nil {
return nil, err
}
var names []string
for _, profile := range profileInfo {
if profile == nil {
continue
}
if profile.Name == "" {
return nil, errors.Errorf("request to write LXD profile for machine %s with no profile name", machineID)
}
err := broker.MaybeWriteLXDProfile(profile.Name, lxdprofile.Profile{
Config: profile.Config,
Description: profile.Description,
Devices: profile.Devices,
})
if err != nil {
return nil, err
}
names = append(names, profile.Name)
}
return names, nil
}
// MaybeWriteLXDProfile implements environs.LXDProfiler.
func (broker *lxdBroker) MaybeWriteLXDProfile(pName string, put lxdprofile.Profile) error {
profileMgr, ok := broker.manager.(container.LXDProfileManager)
if !ok {
return nil
}
return profileMgr.MaybeWriteLXDProfile(pName, put)
}
// AssignLXDProfiles implements environs.LXDProfiler.
func (broker *lxdBroker) AssignLXDProfiles(instID string, profilesNames []string, profilePosts []lxdprofile.ProfilePost) ([]string, error) {
profileMgr, ok := broker.manager.(container.LXDProfileManager)
if !ok {
return []string{}, nil
}
return profileMgr.AssignLXDProfiles(instID, profilesNames, profilePosts)
}