/
recommend.go
382 lines (345 loc) · 12.3 KB
/
recommend.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
/*
*
* MIT License
*
* (C) Copyright 2023 Hewlett Packard Enterprise Development LP
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
*/
package csm
import (
"errors"
"fmt"
"sort"
"github.com/Cray-HPE/cani/internal/inventory"
"github.com/Cray-HPE/cani/internal/provider"
"github.com/Cray-HPE/cani/pkg/hardwaretypes"
"github.com/google/uuid"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)
func (csm *CSM) RecommendHardware(inv inventory.Inventory, cmd *cobra.Command, args []string, auto bool) (recommended provider.HardwareRecommendations, err error) {
var deviceTypeSlug string
if cmd.Parent().Name() == "add" {
deviceTypeSlug = args[0]
}
// loop through the existing inventory to check for vlans
log.Debug().Msg("Checking existing hardware to find recommendations")
deviceType, exists := csm.hardwareLibrary.DeviceTypes[deviceTypeSlug]
if !exists {
return recommended, err
}
log.Debug().Msgf("Recommending a %v", deviceType.HardwareType)
switch deviceType.HardwareType {
case hardwaretypes.Cabinet:
r, err := csm.recommendCabinet(inv, cmd, args, auto)
if err != nil {
return recommended, err
}
if !auto {
r.CabinetOrdinal, _ = cmd.Flags().GetInt("cabinet")
r.ProviderMetadata["HMNVlan"], _ = cmd.Flags().GetInt("vlan-id")
}
recommended = r
case hardwaretypes.NodeBlade:
// Get the cabinet recommendations
r, err := csm.recommendBlade(inv, deviceTypeSlug)
if err != nil {
return recommended, err
}
recommended = r
case hardwaretypes.Node:
r, err := csm.recommendNode(inv, cmd, args, auto)
if err != nil {
return recommended, err
}
recommended = r
default:
// This function only handles cabinets and blades
}
// return the recommendations
return recommended, nil
}
func (csm *CSM) recommendNode(inv inventory.Inventory, cmd *cobra.Command, args []string, auto bool) (recommended provider.HardwareRecommendations, err error) {
return recommended, nil
}
func (csm *CSM) recommendCabinet(inv inventory.Inventory, cmd *cobra.Command, args []string, auto bool) (recommended provider.HardwareRecommendations, err error) {
deviceTypeSlug := args[0]
// slice to track existing vlans
var existingVlans = []int{}
// slice to track existing cabinets
var existingCabinets = []int{}
// loop through the existing inventory to check for vlans
log.Debug().Msg("Checking existing hardware to find recommendations")
for _, cHardware := range inv.Hardware {
switch cHardware.Type {
case hardwaretypes.Cabinet:
log.Debug().Msgf("Checking %s (%s)", cHardware.Type, cHardware.ID.String())
log.Debug().Msgf("Decoding csm properties %+v", cHardware.ProviderMetadata)
metadata, err := DecodeProviderMetadata(cHardware)
if err != nil {
return provider.HardwareRecommendations{}, errors.Join(fmt.Errorf("failed to decode CSM metadata from cabinet (%v)", cHardware.ID), err)
}
if metadata.Cabinet != nil && metadata.Cabinet.HMNVlan != nil {
// add it to the slice that tracks existing vlans
existingVlans = append(existingVlans, *metadata.Cabinet.HMNVlan)
}
// add the ordinal to the existing cabinets slice for choosing a new one later
existingCabinets = append(existingCabinets, *cHardware.LocationOrdinal)
default:
// This function only handles cabinets
continue
}
}
var chosenOrdinal int
chosenOrdinal, err = DetermineStartingOrdinalFromSlug(deviceTypeSlug, *csm.hardwareLibrary)
if err != nil {
return recommended, err
}
log.Debug().Msgf("chosenOrdinal %d (%s)", chosenOrdinal, deviceTypeSlug)
// Set the cabinet location
if len(existingCabinets) == 0 {
// there are no cabinets yet, so set it to the provider default
recommended.CabinetOrdinal = chosenOrdinal
log.Debug().Msgf("No cabinets found, using %d", recommended.CabinetOrdinal)
} else {
// set the recommended cabinet number
recommended.CabinetOrdinal = nextAvailableInt(existingCabinets, chosenOrdinal)
log.Debug().Msgf("Existing cabinets found (%v), using %d", existingCabinets, recommended.CabinetOrdinal)
}
// Determine the hardware class based off the slug
// This is needed to assign an approriate VLAN from the ranges defined above
class, err := DetermineHardwareClassFromSlug(deviceTypeSlug, *csm.hardwareLibrary)
if err != nil {
return recommended, err
}
// Set the metadata vlan
var startingVlan, chosenVlan int
startingVlan, err = DetermineStartingVlanFromSlug(deviceTypeSlug, *csm.hardwareLibrary)
if err != nil {
return recommended, err
}
if len(existingCabinets) == 0 {
// choose a starting vlan based on the class
chosenVlan = startingVlan
log.Debug().Msgf("No cabinet VLANs found, using %d for %s %s", chosenVlan, class, hardwaretypes.Cabinet)
} else {
// set the recommended vlan by finding an available one from the existing
chosenVlan = nextAvailableInt(existingVlans, startingVlan)
}
// set the provider metadata
recommended.ProviderMetadata = map[string]interface{}{
// there are no vlans yet, and presumably no cabinets, so set it to 1
ProviderMetadataVlanId: chosenVlan,
}
// return the recommendations
return recommended, nil
}
// recommendBlade recommends values for cabinet, chassis, and blade ordinals
// by collecting all of the possible ordinals for a given cabinet/chassis
// then checking to see if a slot is populated or not
func (csm *CSM) recommendBlade(inv inventory.Inventory, deviceTypeSlug string) (recommended provider.HardwareRecommendations, err error) {
// struct for sorting a map by their ordinal location
type ordinalmap struct {
key uuid.UUID
ordinals []int
}
// a slot will be a nodebay and can be populated or not
type slot struct {
ordinals []int
populated bool
id uuid.UUID
}
// get all cabinets, chassis, and blades
allCabinets := inv.FilterHardwareByType(hardwaretypes.Cabinet)
if err != nil {
return recommended, err
}
allChassis := inv.FilterHardwareByType(hardwaretypes.Chassis)
if err != nil {
return recommended, err
}
allBlades := inv.FilterHardwareByType(hardwaretypes.NodeBlade)
if err != nil {
return recommended, err
}
// map uuid of the hw types to their location path so they can be sorted
cabinetMap := getOrdinals(allChassis)
// sort the cabinets by their ordinals
var sortedCabinetSlice []ordinalmap
for i, cabinetLocations := range cabinetMap {
l := ordinalmap{
key: i,
ordinals: cabinetLocations,
}
sortedCabinetSlice = append(sortedCabinetSlice, l)
}
sort.Slice(sortedCabinetSlice, func(i, j int) bool {
iSlice := sortedCabinetSlice[i].ordinals
jSlice := sortedCabinetSlice[j].ordinals
for x := 0; x < len(iSlice) && x < len(jSlice); x++ {
if iSlice[x] < jSlice[x] {
return true
} else if iSlice[x] > jSlice[x] {
return false
}
}
return false // equal slices
})
// map uuid of the hw types to their location path so they can be sorted
bladeMap := getOrdinals(allBlades)
// create a sorted list of what exists and is or is not populated with a blade
existing := []slot{}
// for each chassis (now in order)
for _, chass := range sortedCabinetSlice {
// get the chassis and cabinet Hardware
chassis := allChassis[chass.key]
cabinet := allCabinets[chassis.Parent]
// add each blade to a slice for sorting
sortedBladeSlice := []ordinalmap{}
for i, bladeLocations := range bladeMap {
blade := allBlades[i]
if blade.Parent == chassis.ID {
l := ordinalmap{
key: i,
ordinals: bladeLocations,
}
sortedBladeSlice = append(sortedBladeSlice, l)
}
}
// compare slices element-by-element
// move the the next element only if the current elements are equal
sort.Slice(sortedBladeSlice, func(i, j int) bool {
iSlice := sortedBladeSlice[i].ordinals
jSlice := sortedBladeSlice[j].ordinals
for x := 0; x < len(iSlice) && x < len(jSlice); x++ {
if iSlice[x] < jSlice[x] {
return true
} else if iSlice[x] > jSlice[x] {
return false
}
}
return false // equal slices
})
// check the chassis specification to determine how many slots it should have
chassisSpecs, exists := csm.hardwareLibrary.DeviceTypes[chassis.DeviceTypeSlug]
if !exists {
return recommended, err
}
// check to ensure the bay supports a blade
var bladeSupported bool
var bay hardwaretypes.DeviceBay
// loop through all of the devices bays
for _, b := range chassisSpecs.DeviceBays {
bay = b
// bays can have different things than just blades in them
for _, t := range b.Allowed.Types {
// so check if the bay supports a NodeBlade
if t == hardwaretypes.NodeBlade {
bladeSupported = true
break
} else {
// ignore non-blade bays
bladeSupported = false
continue
}
}
// if a blade is supported
if bladeSupported {
baySlot := slot{}
baySlot.populated = false
baySlot.ordinals = []int{0, *cabinet.LocationOrdinal, *chassis.LocationOrdinal, bay.Ordinal}
for _, bld := range sortedBladeSlice {
blade := allBlades[bld.key]
// if the blade matches the bay, a blade is there
if *blade.LocationOrdinal == bay.Ordinal {
if blade.Status != inventory.HardwareStatusEmpty {
baySlot.populated = true
}
baySlot.ordinals = blade.LocationPath.GetOrdinalPath()
baySlot.id = blade.ID
}
}
// append it to what exists in the chassis
existing = append(existing, baySlot)
}
}
}
// loop throuigh what exists
var nextAvailable *slot
for e := range existing {
log.Debug().Msgf("%s %d -> %s %d -> %s %s %d -> %s %s",
hardwaretypes.Cabinet, existing[e].ordinals[1],
hardwaretypes.Chassis, existing[e].ordinals[2],
hardwaretypes.NodeBlade, "Bay", existing[e].ordinals[3],
hardwaretypes.NodeBlade, existing[e].id.String())
// if it is not populated, that is the next available
if !existing[e].populated {
nextAvailable = &existing[e]
break
}
}
if nextAvailable != nil {
log.Debug().Msgf("Next available: %v", nextAvailable.ordinals)
recommended.CabinetOrdinal = nextAvailable.ordinals[1]
recommended.ChassisOrdinal = nextAvailable.ordinals[2]
recommended.BladeOrdinal = nextAvailable.ordinals[3]
} else {
return recommended, fmt.Errorf("no available %s slots", string(hardwaretypes.NodeBlade))
}
// return the recommendations
return recommended, nil
}
// nextAvailableInt finds the next available integer accounting for gaps
func nextAvailableInt(s []int, offset int) int {
// slice must be sorted in order to work properly
sort.Ints(s)
// If the slice is empty, return the offset
if len(s) == 0 || offset < s[0] {
return offset
}
// Check if the offset is in the slice
i := sort.Search(len(s), func(i int) bool { return s[i] >= offset })
if i < len(s) && s[i] == offset {
for ; i < len(s); i++ {
// if it is the last element or there is a gap to the next one
if i == len(s)-1 || s[i+1]-s[i] > 1 {
return s[i] + 1
}
}
}
// the offset is not in the slice, so return it
return offset
}
// getOrdinals returns a map of uuids to a slice of a hardware's ordinal path
func getOrdinals(hwMap map[uuid.UUID]inventory.Hardware) (ordinals map[uuid.UUID][]int) {
// make a map of existing hw and the ordinals they contain
ordinals = make(map[uuid.UUID][]int, 0)
// for each harware in the given map
for _, hw := range hwMap {
// check if the hw is already in the map
_, exists := ordinals[hw.ID]
// if not in the map, add it
if !exists {
ordinals[hw.ID] = hw.LocationPath.GetOrdinalPath()
}
}
return ordinals
}