-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathhost_config.go
402 lines (347 loc) · 10.7 KB
/
host_config.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
package agentbasedinstaller
import (
"context"
"fmt"
"net"
"os"
"path"
"path/filepath"
"strings"
"github.com/go-openapi/strfmt"
bmh_v1alpha1 "github.com/metal3-io/baremetal-operator/apis/metal3.io/v1alpha1"
"github.com/openshift/assisted-service/client"
"github.com/openshift/assisted-service/client/installer"
"github.com/openshift/assisted-service/internal/host/hostutil"
"github.com/openshift/assisted-service/models"
errorutil "github.com/openshift/assisted-service/pkg/error"
log "github.com/sirupsen/logrus"
"sigs.k8s.io/yaml"
)
// AgentWorkflowType defines the supported
// agent workflows.
type AgentWorkflowType string
const (
// AgentWorkflowTypeInstall identifies the install workflow.
AgentWorkflowTypeInstall AgentWorkflowType = "install"
// AgentWorkflowTypeAddNodes identifies the add nodes workflow.
AgentWorkflowTypeAddNodes AgentWorkflowType = "addnodes"
)
func ApplyHostConfigs(ctx context.Context, log *log.Logger, bmInventory *client.AssistedInstall, hostConfigs HostConfigs, infraEnvID strfmt.UUID) ([]Failure, error) {
hostList, err := bmInventory.Installer.V2ListHosts(ctx, installer.NewV2ListHostsParams().WithInfraEnvID(infraEnvID))
if err != nil {
return nil, fmt.Errorf("Failed to list hosts: %w", errorutil.GetAssistedError(err))
}
failures := []Failure{}
for _, host := range hostList.Payload {
if err := applyHostConfig(ctx, log, bmInventory, host, hostConfigs); err != nil {
if fail, ok := err.(Failure); ok {
failures = append(failures, fail)
log.Error(err.Error())
} else {
return failures, err
}
}
}
missing := hostConfigs.missing(log)
if len(missing) > 0 {
log.Info("Not all hosts present yet")
for _, mh := range missing {
failures = append(failures, mh)
}
} else {
log.Info("All expected hosts found")
}
return failures, nil
}
func applyHostConfig(ctx context.Context, log *log.Logger, bmInventory *client.AssistedInstall, host *models.Host, hostConfigs HostConfigs) error {
log.Infof("Checking configuration for host %s", *host.ID)
if len(host.Inventory) == 0 {
log.Info("Inventory information not yet available")
return nil
}
inventory := &models.Inventory{}
err := inventory.UnmarshalBinary([]byte(host.Inventory))
if err != nil {
return fmt.Errorf("failed to unmarshal host inventory: %w", err)
}
config := hostConfigs.findHostConfig(*host.ID, inventory)
if config == nil {
return nil
}
updateParams := &models.HostUpdateParams{}
changed := false
rdh, err := config.RootDeviceHints()
if err != nil {
return err
}
if applyRootDeviceHints(log, host, inventory, rdh, updateParams) {
changed = true
}
role, err := config.Role()
if err != nil {
return err
}
if applyRole(log, host, inventory, role, updateParams) {
changed = true
}
if !changed {
log.Info("No configuration changes needed")
return nil
}
log.Info("Updating host")
params := installer.NewV2UpdateHostParams().
WithHostID(*host.ID).
WithInfraEnvID(host.InfraEnvID).
WithHostUpdateParams(updateParams)
_, err = bmInventory.Installer.V2UpdateHost(ctx, params)
if err != nil {
if errorResponse, ok := err.(errorutil.AssistedServiceErrorAPI); ok {
return &UpdateFailure{
response: errorResponse,
params: updateParams,
host: host,
inventory: inventory,
}
}
return fmt.Errorf("failed to update Host: %w", err)
}
return nil
}
func applyRootDeviceHints(log *log.Logger, host *models.Host, inventory *models.Inventory, rdh *bmh_v1alpha1.RootDeviceHints, updateParams *models.HostUpdateParams) bool {
acceptableDisks := hostutil.GetAcceptableDisksWithHints(inventory.Disks, rdh)
if host.InstallationDiskID != "" {
for _, disk := range acceptableDisks {
if disk.ID == host.InstallationDiskID {
log.Infof("Selected disk %s already matches root device hints", host.InstallationDiskID)
return false
}
}
}
diskID := "/dev/not-found-by-hints"
if len(acceptableDisks) > 0 {
diskID = acceptableDisks[0].ID
log.Infof("Selecting disk %s for installation", diskID)
} else {
log.Info("No disk found matching root device hints")
possibleDisks := []string{}
for _, disk := range inventory.Disks {
if !disk.InstallationEligibility.Eligible {
log.Infof("Disk %s is not eligible due to %s", disk.Path, disk.InstallationEligibility.NotEligibleReasons)
continue
}
diskStr := fmt.Sprintf("Disk - path: %s, by-path: %s, wwn: %s", disk.Path, disk.ByPath, disk.Wwn)
possibleDisks = append(possibleDisks, diskStr)
}
log.Info("Eligible disks: ", possibleDisks)
}
updateParams.DisksSelectedConfig = []*models.DiskConfigParams{
{ID: &diskID, Role: models.DiskRoleInstall},
}
return true
}
func applyRole(log *log.Logger, host *models.Host, inventory *models.Inventory, role *string, updateParams *models.HostUpdateParams) bool {
if role == nil {
log.Info("No role configured")
return false
}
if host.SuggestedRole == models.HostRole(*role) {
log.Infof("Host role %s already configured", *role)
return false
}
updateParams.HostRole = role
return true
}
func LoadHostConfigs(hostConfigDir string, workflowType AgentWorkflowType) (HostConfigs, error) {
log.Infof("Loading host configurations from disk in %s", hostConfigDir)
configs := HostConfigs{}
entries, err := os.ReadDir(hostConfigDir)
if err != nil {
if os.IsNotExist(err) {
log.Infof("No host configuration directory found %s", hostConfigDir)
return nil, nil
}
return nil, fmt.Errorf("failed to read config directory %s: %w", hostConfigDir, err)
}
for _, e := range entries {
if !e.IsDir() {
continue
}
hostPath := path.Join(hostConfigDir, e.Name())
log.Infof("Reading directory %s", hostPath)
macs, err := os.ReadFile(filepath.Join(hostPath, "mac_addresses"))
if os.IsNotExist(err) {
log.Info("No MAC Addresses file found")
continue
}
if err != nil {
return nil, fmt.Errorf("failed to read MAC Addresses file: %w", err)
}
lines := strings.Split(string(macs), "\n")
addresses := []string{}
for _, l := range lines {
mac := strings.TrimSpace(l)
if len(mac) > 0 {
addresses = append(addresses, mac)
}
}
if workflowType == AgentWorkflowTypeAddNodes {
// In the addnodes workflow, the only host config we want to load is the
// current host's. Multiple HostConfigs could exist in hostConfigDir
// if multiple day-2 nodes are being added using the same day-2 ISO.
// Filter otu the other HostConfig entries because each day-2
// node is added in isolation using their own internal assisted-service
// instance.
addHostConfig, err := currentHostHasMACAddress(addresses)
if err != nil {
return nil, err
}
if !addHostConfig {
continue
}
}
configs = append(configs, &hostConfig{
configDir: hostPath,
macAddresses: addresses,
})
}
return configs, nil
}
type hostConfig struct {
configDir string
macAddresses []string
hostID strfmt.UUID
}
// currentHostHasMACAddress returns true if this host has a MAC address in addresses string array.
func currentHostHasMACAddress(addresses []string) (bool, error) {
hostInterfaces, err := net.Interfaces()
if err != nil {
return false, fmt.Errorf("failed to get this host's interfaces: %w", err)
}
for _, iface := range hostInterfaces {
if iface.HardwareAddr == nil {
continue
}
for _, hostConfigMac := range addresses {
if iface.HardwareAddr.String() == hostConfigMac {
return true, nil
}
}
}
return false, nil
}
func (hc hostConfig) RootDeviceHints() (*bmh_v1alpha1.RootDeviceHints, error) {
hintData, err := os.ReadFile(path.Join(hc.configDir, "root-device-hints.yaml"))
if err != nil {
if os.IsNotExist(err) {
log.Info("No root device hints file found for host")
return nil, nil
}
return nil, fmt.Errorf("failed to read Root Device Hints file: %w", err)
}
rdh := &bmh_v1alpha1.RootDeviceHints{}
if err := yaml.UnmarshalStrict(hintData, rdh); err != nil {
return nil, fmt.Errorf("failed to parse Root Device Hints file: %w", err)
}
log.Info("Read root device hints file")
return rdh, nil
}
func (hc hostConfig) Role() (*string, error) {
roleData, err := os.ReadFile(path.Join(hc.configDir, "role"))
if err != nil {
if os.IsNotExist(err) {
log.Info("No role file found for host")
return nil, nil
}
return nil, fmt.Errorf("failed to read role file: %w", err)
}
role := strings.TrimSpace(string(roleData))
if len(role) == 0 {
log.Info("Empty role")
return nil, nil
}
log.Infof("Found role %s", role)
return &role, nil
}
type HostConfigs []*hostConfig
func (configs HostConfigs) findHostConfig(hostID strfmt.UUID, inventory *models.Inventory) *hostConfig {
log.Infof("Searching for config for host %s", hostID)
for _, hc := range configs {
for _, nic := range inventory.Interfaces {
if nic != nil {
for _, mac := range hc.macAddresses {
if nic.MacAddress == mac {
log.Infof("Found host config in %s", hc.configDir)
hc.hostID = hostID
return hc
}
}
}
}
}
log.Info("No config found for host")
return nil
}
func (configs HostConfigs) missing(log *log.Logger) []missingHost {
missing := []missingHost{}
for _, hc := range configs {
if hc.hostID == "" {
log.Infof("No agent found matching config at %s (%s)", hc.configDir, strings.Join(hc.macAddresses, ", "))
missing = append(missing, missingHost{config: hc})
}
}
return missing
}
type Failure interface {
Hostname() string
DescribeFailure() string
}
type UpdateFailure struct {
response errorutil.AssistedServiceErrorAPI
params *models.HostUpdateParams
config *hostConfig
host *models.Host
inventory *models.Inventory
}
func (uf *UpdateFailure) Error() string {
return fmt.Sprintf("Host %s update refused: %s", uf.Hostname(), errorutil.GetAssistedError(uf.response).Error())
}
func (uf *UpdateFailure) Unwrap() error {
return uf.response
}
func (uf *UpdateFailure) Hostname() string {
if uf.inventory != nil {
return uf.inventory.Hostname
}
return path.Base(uf.config.configDir)
}
func (uf *UpdateFailure) DescribeFailure() string {
changes := []string{}
if len(uf.params.DisksSelectedConfig) > 0 {
changes = append(changes, fmt.Sprintf(
"installation disk to %s (from %s)",
*uf.params.DisksSelectedConfig[0].ID,
uf.host.InstallationDiskID))
}
if uf.params.HostRole != nil {
changes = append(changes, fmt.Sprintf(
"role to %s (from %s)",
*uf.params.HostRole,
uf.host.SuggestedRole))
}
reason := "unknown reason"
if payload := uf.response.GetPayload(); payload != nil && payload.Reason != nil {
reason = *payload.Reason
}
return fmt.Sprintf("Failed to update host %s: %s",
strings.Join(changes, " and "),
reason)
}
type missingHost struct {
config *hostConfig
}
func (mh missingHost) Hostname() string {
return path.Base(mh.config.configDir)
}
func (mh missingHost) DescribeFailure() string {
return "Host not registered"
}