-
Notifications
You must be signed in to change notification settings - Fork 247
/
fixture.go
350 lines (298 loc) · 10.7 KB
/
fixture.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
package fixture
import (
"time"
"github.com/go-logr/logr"
logz "sigs.k8s.io/controller-runtime/pkg/log/zap"
metal3api "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
"github.com/metal3-io/baremetal-operator/pkg/hardwareutils/bmc"
"github.com/metal3-io/baremetal-operator/pkg/provisioner"
)
var log = logz.New().WithName("provisioner").WithName("fixture")
var deprovisionRequeueDelay = time.Second * 10
var provisionRequeueDelay = time.Second * 10
type fixtureHostConfigData struct {
userData string
networkData string
metaData string
}
// NewHostConfigData creates new host configuration data
func NewHostConfigData(userData string, networkData string, metaData string) provisioner.HostConfigData {
return &fixtureHostConfigData{
userData: userData,
networkData: networkData,
metaData: metaData,
}
}
func (cd *fixtureHostConfigData) UserData() (string, error) {
return cd.userData, nil
}
func (cd *fixtureHostConfigData) NetworkData() (string, error) {
return cd.networkData, nil
}
func (cd *fixtureHostConfigData) MetaData() (string, error) {
return cd.metaData, nil
}
// fixtureProvisioner implements the provisioning.fixtureProvisioner interface
// and uses Ironic to manage the host.
type fixtureProvisioner struct {
// the provisioning ID for this host
provID string
// the bmc credentials
bmcCreds bmc.Credentials
// a logger configured for this host
log logr.Logger
// an event publisher for recording significant events
publisher provisioner.EventPublisher
// state storage for the Host
state *Fixture
}
// Fixture contains persistent state for a particular host
type Fixture struct {
// counter to set the provisioner as ready
BecomeReadyCounter int
// state to manage deletion
Deleted bool
// state to manage the two-step adopt process
adopted bool
// state to manage provisioning
image metal3api.Image
// state to manage power
poweredOn bool
validateError string
customDeploy *metal3api.CustomDeploy
}
// NewProvisioner returns a new Fixture Provisioner
func (f *Fixture) NewProvisioner(hostData provisioner.HostData, publisher provisioner.EventPublisher) (provisioner.Provisioner, error) {
p := &fixtureProvisioner{
provID: hostData.ProvisionerID,
bmcCreds: hostData.BMCCredentials,
log: log.WithValues("host", hostData.ObjectMeta.Name),
publisher: publisher,
state: f,
}
return p, nil
}
func (f *Fixture) SetValidateError(message string) {
f.validateError = message
}
func (p *fixtureProvisioner) HasCapacity() (result bool, err error) {
return true, nil
}
// ValidateManagementAccess tests the connection information for the
// host to verify that the location and credentials work.
func (p *fixtureProvisioner) ValidateManagementAccess(_ provisioner.ManagementAccessData, _, _ bool) (result provisioner.Result, provID string, err error) {
p.log.Info("testing management access")
if p.state.validateError != "" {
result.ErrorMessage = p.state.validateError
return
}
// Fill in the ID of the host in the provisioning system
if p.provID == "" {
provID = "temporary-fake-id"
result.Dirty = true
result.RequeueAfter = time.Second * 5
p.publisher("Registered", "Registered new host")
return
}
return
}
func (p *fixtureProvisioner) PreprovisioningImageFormats() ([]metal3api.ImageFormat, error) {
return nil, nil
}
// InspectHardware updates the HardwareDetails field of the host with
// details of devices discovered on the hardware. It may be called
// multiple times, and should return true for its dirty flag until the
// inspection is completed.
func (p *fixtureProvisioner) InspectHardware(_ provisioner.InspectData, _, _, _ bool) (result provisioner.Result, started bool, details *metal3api.HardwareDetails, err error) {
// The inspection is ongoing. We'll need to check the fixture
// status for the server here until it is ready for us to get the
// inspection details. Simulate that for now by creating the
// hardware details struct as part of a second pass.
p.log.Info("continuing inspection by setting details")
started = true
details =
&metal3api.HardwareDetails{
RAMMebibytes: 128 * 1024,
NIC: []metal3api.NIC{
{
Name: "nic-1",
Model: "virt-io",
MAC: "ab:cd:12:34:56:78",
IP: "192.168.100.1",
SpeedGbps: 1,
PXE: true,
},
{
Name: "nic-2",
Model: "e1000",
MAC: "12:34:56:78:ab:cd",
IP: "192.168.100.2",
SpeedGbps: 1,
PXE: false,
},
},
Storage: []metal3api.Storage{
{
Name: "disk-1 (boot)",
Rotational: false,
SizeBytes: metal3api.TebiByte * 93,
Model: "Dell CFJ61",
},
{
Name: "disk-2",
Rotational: false,
SizeBytes: metal3api.TebiByte * 93,
Model: "Dell CFJ61",
},
},
CPU: metal3api.CPU{
Arch: "x86_64",
Model: "FancyPants CPU",
ClockMegahertz: 3.0 * metal3api.GigaHertz,
Flags: []string{"fpu", "hypervisor", "sse", "vmx"},
Count: 1,
},
}
p.publisher("InspectionComplete", "Hardware inspection completed")
return
}
// UpdateHardwareState fetches the latest hardware state of the server
// and updates the HardwareDetails field of the host with details. It
// is expected to do this in the least expensive way possible, such as
// reading from a cache.
func (p *fixtureProvisioner) UpdateHardwareState() (hwState provisioner.HardwareState, err error) {
hwState.PoweredOn = &p.state.poweredOn
p.log.Info("updating hardware state")
return
}
// Prepare remove existing configuration and set new configuration
func (p *fixtureProvisioner) Prepare(_ provisioner.PrepareData, unprepared bool, _ bool) (result provisioner.Result, started bool, err error) {
p.log.Info("preparing host")
started = unprepared
return
}
// Adopt notifies the provisioner that the state machine believes the host
// to be currently provisioned, and that it should be managed as such.
func (p *fixtureProvisioner) Adopt(_ provisioner.AdoptData, _ bool) (result provisioner.Result, err error) {
p.log.Info("adopting host")
if !p.state.adopted {
p.state.adopted = true
result.Dirty = true
result.RequeueAfter = provisionRequeueDelay
}
return
}
// Provision writes the image from the host spec to the host. It may
// be called multiple times, and should return true for its dirty flag
// until the provisioning operation is completed.
func (p *fixtureProvisioner) Provision(data provisioner.ProvisionData, _ bool) (result provisioner.Result, err error) {
p.log.Info("provisioning image to host")
if data.CustomDeploy != nil && p.state.customDeploy == nil {
p.publisher("ProvisioningComplete", "Custom deploy provisioning completed")
p.log.Info("moving to done")
p.state.customDeploy = data.CustomDeploy.DeepCopy()
result.Dirty = true
result.RequeueAfter = provisionRequeueDelay
return result, nil
}
if data.Image.URL != "" && p.state.image.URL == "" {
p.publisher("ProvisioningComplete", "Image provisioning completed")
p.log.Info("moving to done")
p.state.image = data.Image
result.Dirty = true
result.RequeueAfter = provisionRequeueDelay
}
return result, nil
}
// Deprovision removes the host from the image. It may be called
// multiple times, and should return true for its dirty flag until the
// deprovisioning operation is completed.
func (p *fixtureProvisioner) Deprovision(_ bool) (result provisioner.Result, err error) {
p.log.Info("ensuring host is deprovisioned")
result.RequeueAfter = deprovisionRequeueDelay
// NOTE(dhellmann): In order to simulate a multi-step process,
// modify some of the status data structures. This is likely not
// necessary once we really have Fixture doing the deprovisioning
// and we can monitor it's status.
if p.state.image.URL != "" {
p.publisher("DeprovisionStarted", "Image deprovisioning started")
p.log.Info("clearing hardware details")
p.state.image = metal3api.Image{}
result.Dirty = true
return result, nil
}
if p.state.customDeploy != nil {
p.publisher("DeprovisionStarted", "Custom deploy deprovisioning started")
p.log.Info("clearing hardware details")
p.state.customDeploy = nil
result.Dirty = true
return result, nil
}
p.publisher("DeprovisionComplete", "Image deprovisioning completed")
return result, nil
}
// Delete removes the host from the provisioning system. It may be
// called multiple times, and should return true for its dirty flag
// until the deprovisioning operation is completed.
func (p *fixtureProvisioner) Delete() (result provisioner.Result, err error) {
p.log.Info("deleting host")
if !p.state.Deleted {
p.log.Info("clearing provisioning id")
p.state.Deleted = true
result.Dirty = true
return result, nil
}
return result, nil
}
// Detach removes the host from the provisioning system.
// Similar to Delete, but ensures non-interruptive behavior
// for the target system. It may be called multiple times,
// and should return true for its dirty flag until the
// deletion operation is completed.
func (p *fixtureProvisioner) Detach() (result provisioner.Result, err error) {
return p.Delete()
}
// PowerOn ensures the server is powered on independently of any image
// provisioning operation.
func (p *fixtureProvisioner) PowerOn(_ bool) (result provisioner.Result, err error) {
p.log.Info("ensuring host is powered on")
if !p.state.poweredOn {
p.publisher("PowerOn", "Host powered on")
p.log.Info("changing status")
p.state.poweredOn = true
result.Dirty = true
return result, nil
}
return result, nil
}
// PowerOff ensures the server is powered off independently of any image
// provisioning operation.
func (p *fixtureProvisioner) PowerOff(_ metal3api.RebootMode, _ bool) (result provisioner.Result, err error) {
p.log.Info("ensuring host is powered off")
if p.state.poweredOn {
p.publisher("PowerOff", "Host powered off")
p.log.Info("changing status")
p.state.poweredOn = false
result.Dirty = true
return result, nil
}
return result, nil
}
// IsReady returns the current availability status of the provisioner
func (p *fixtureProvisioner) IsReady() (result bool, err error) {
p.log.Info("checking provisioner status")
if p.state.BecomeReadyCounter > 0 {
p.state.BecomeReadyCounter--
}
return p.state.BecomeReadyCounter == 0, nil
}
func (p *fixtureProvisioner) GetFirmwareSettings(_ bool) (settings metal3api.SettingsMap, schema map[string]metal3api.SettingSchema, err error) {
p.log.Info("getting BIOS settings")
return
}
func (p *fixtureProvisioner) AddBMCEventSubscriptionForNode(_ *metal3api.BMCEventSubscription, _ provisioner.HTTPHeaders) (result provisioner.Result, err error) {
return result, nil
}
func (p *fixtureProvisioner) RemoveBMCEventSubscriptionForNode(_ metal3api.BMCEventSubscription) (result provisioner.Result, err error) {
return result, nil
}