Skip to content

Commit 3344ea6

Browse files
frelondavidcassany
authored andcommitted
Add hostname to system-data
Add new ${System Data/Runtime/Hostname} key to the registration data sent from the elemental-register command. Signed-off-by: Fredrik Lönnegren <[email protected]> (cherry picked from commit 0a4fe2e)
1 parent 054e003 commit 3344ea6

File tree

4 files changed

+80
-6
lines changed

4 files changed

+80
-6
lines changed

pkg/hostinfo/hostinfo.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright © 2022 - 2023 SUSE LLC
2+
Copyright © 2022 - 2024 SUSE LLC
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -32,6 +32,8 @@ import (
3232
"github.com/jaypipes/ghw/pkg/net"
3333
"github.com/jaypipes/ghw/pkg/product"
3434
"github.com/jaypipes/ghw/pkg/topology"
35+
36+
"github.com/rancher/elemental-operator/pkg/runtime"
3537
)
3638

3739
// HostInfo represents all the host info minus the PCI devices
@@ -51,6 +53,7 @@ type HostInfo struct {
5153
BIOS *bios.Info `json:"bios"`
5254
Baseboard *baseboard.Info `json:"baseboard"`
5355
Product *product.Info `json:"product"`
56+
Runtime *runtime.Info `json:"runtime"`
5457
}
5558

5659
// Host returns a pointer to a HostInfo struct that contains fields with
@@ -102,6 +105,11 @@ func host(opts ...*ghw.WithOption) (*HostInfo, error) {
102105
if err != nil {
103106
return nil, err
104107
}
108+
runtimeInfo, err := runtime.New()
109+
if err != nil {
110+
return nil, err
111+
}
112+
105113
return &HostInfo{
106114
ctx: ctx,
107115
CPU: cpuInfo,
@@ -114,6 +122,7 @@ func host(opts ...*ghw.WithOption) (*HostInfo, error) {
114122
BIOS: biosInfo,
115123
Baseboard: baseboardInfo,
116124
Product: productInfo,
125+
Runtime: runtimeInfo,
117126
}, nil
118127
}
119128

@@ -187,13 +196,19 @@ func FillData(data []byte) (map[string]interface{}, error) {
187196
}
188197
}
189198

199+
runtime := map[string]interface{}{}
200+
if systemData.Runtime != nil {
201+
runtime["Hostname"] = systemData.Runtime.Hostname
202+
}
203+
190204
labels := map[string]interface{}{}
191205
labels["System Data"] = map[string]interface{}{
192206
"Memory": memory,
193207
"CPU": cpu,
194208
"GPU": gpu,
195209
"Network": network,
196210
"Block Devices": block,
211+
"Runtime": runtime,
197212
}
198213

199214
// Also available but not used:

pkg/runtime/info.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Copyright © 2022 - 2024 SUSE LLC
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package runtime
18+
19+
import "os"
20+
21+
type Info struct {
22+
Hostname string `json:"hostname"`
23+
}
24+
25+
func New() (*Info, error) {
26+
name, err := os.Hostname()
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
return &Info{
32+
Hostname: name,
33+
}, nil
34+
}

pkg/server/api_registration.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright © 2022 - 2023 SUSE LLC
2+
Copyright © 2022 - 2024 SUSE LLC
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -441,16 +441,17 @@ func updateInventoryFromSMBIOSData(data []byte, mInventory *elementalv1.MachineI
441441
// Sanitize any lower dashes into dashes as hostnames cannot have lower dashes, and we use the inventory name
442442
// to set the machine hostname. Also set it to lowercase
443443
name, err := replaceStringData(smbiosData, mInventory.Name)
444-
if err != nil {
444+
if err == nil {
445+
mInventory.Name = strings.ToLower(sanitizeHostname.ReplaceAllString(name, "-"))
446+
} else {
445447
if errors.Is(err, errValueNotFound) {
446-
log.Warningf("Value not found: %v", mInventory.Name)
447-
name = "m"
448+
// value not found, will be set in updateInventoryFromSystemData
449+
log.Warningf("SMBIOS Value not found: %v", mInventory.Name)
448450
} else {
449451
return err
450452
}
451453
}
452454

453-
mInventory.Name = strings.ToLower(sanitizeHostname.ReplaceAllString(name, "-"))
454455
log.Debugf("Adding labels from registration")
455456
// Add extra label info from data coming from smbios and based on the registration data
456457
if mInventory.Labels == nil {
@@ -489,6 +490,18 @@ func updateInventoryFromSystemData(data []byte, inv *elementalv1.MachineInventor
489490
// systemData.Chassis -> asset, serial, vendor,version,product, type. Maybe be useful depending on the provider.
490491
// systemData.Topology -> CPU/memory and cache topology. No idea if useful.
491492

493+
name, err := replaceStringData(labels, inv.Name)
494+
if err != nil {
495+
if errors.Is(err, errValueNotFound) {
496+
log.Warningf("System data value not found: %v", inv.Name)
497+
name = "m"
498+
} else {
499+
return err
500+
}
501+
}
502+
503+
inv.Name = strings.ToLower(sanitizeHostname.ReplaceAllString(name, "-"))
504+
492505
log.Debugf("Parsing labels from System Data")
493506

494507
if inv.Labels == nil {

pkg/server/api_registration_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import (
4747
elementalv1 "github.com/rancher/elemental-operator/api/v1beta1"
4848
"github.com/rancher/elemental-operator/pkg/hostinfo"
4949
"github.com/rancher/elemental-operator/pkg/register"
50+
elementalruntime "github.com/rancher/elemental-operator/pkg/runtime"
5051
)
5152

5253
func TestUnauthenticatedResponse(t *testing.T) {
@@ -264,6 +265,7 @@ func TestUpdateInventoryFromSystemData(t *testing.T) {
264265
registration := &elementalv1.MachineRegistration{
265266
Spec: elementalv1.MachineRegistrationSpec{
266267
MachineInventoryLabels: map[string]string{
268+
"elemental.cattle.io/Hostname": "${System Data/Runtime/Hostname}",
267269
"elemental.cattle.io/TotalMemory": "${System Data/Memory/Total Physical Bytes}",
268270
"elemental.cattle.io/AvailableMemory": "${System Data/Memory/Total Usable Bytes}",
269271
"elemental.cattle.io/CpuTotalCores": "${System Data/CPU/Total Cores}",
@@ -321,12 +323,16 @@ func TestUpdateInventoryFromSystemData(t *testing.T) {
321323
},
322324
},
323325
},
326+
Runtime: &elementalruntime.Info{
327+
Hostname: "machine-1",
328+
},
324329
}
325330
encodedData, err := json.Marshal(data)
326331
assert.NilError(t, err)
327332
err = updateInventoryFromSystemData(encodedData, inventory, registration)
328333
assert.NilError(t, err)
329334
// Check that the labels we properly added to the inventory
335+
assert.Equal(t, inventory.Labels["elemental.cattle.io/Hostname"], "machine-1")
330336
assert.Equal(t, inventory.Labels["elemental.cattle.io/TotalMemory"], "100")
331337
assert.Equal(t, inventory.Labels["elemental.cattle.io/TotalMemory"], "100")
332338
assert.Equal(t, inventory.Labels["elemental.cattle.io/CpuTotalCores"], "300")
@@ -347,6 +353,8 @@ func TestUpdateInventoryFromSystemData(t *testing.T) {
347353

348354
func TestUpdateInventoryFromSystemDataSanitized(t *testing.T) {
349355
inventory := &elementalv1.MachineInventory{}
356+
inventory.Name = "${System Data/Runtime/Hostname}"
357+
350358
registration := &elementalv1.MachineRegistration{
351359
Spec: elementalv1.MachineRegistrationSpec{
352360
MachineInventoryLabels: map[string]string{
@@ -408,12 +416,16 @@ func TestUpdateInventoryFromSystemDataSanitized(t *testing.T) {
408416
},
409417
},
410418
},
419+
Runtime: &elementalruntime.Info{
420+
Hostname: "machine-1",
421+
},
411422
}
412423
encodedData, err := json.Marshal(data)
413424
assert.NilError(t, err)
414425
err = updateInventoryFromSystemData(encodedData, inventory, registration)
415426
assert.NilError(t, err)
416427
// Check that the labels we properly added to the inventory
428+
assert.Equal(t, inventory.Name, "machine-1")
417429
assert.Equal(t, inventory.Labels["elemental.cattle.io/TotalMemory"], "100")
418430
assert.Equal(t, inventory.Labels["elemental.cattle.io/CpuTotalCores"], "300")
419431
assert.Equal(t, inventory.Labels["elemental.cattle.io/CpuTotalThreads"], "300")

0 commit comments

Comments
 (0)