Properly sanitize extra system data (#307)
Fixes https://github.com/rancher/elemental-operator/issues/306
This commit is contained in:
parent
95bb57dc4e
commit
4658997ab3
|
|
@ -454,14 +454,14 @@ func updateInventoryFromSystemData(data []byte, inv *elementalv1.MachineInventor
|
|||
// Model still looks weird, maybe there is a way of getting it differently as we need to sanitize a lot of data in there?
|
||||
// Currently, something like "Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz" ends up being:
|
||||
// "Intel-R-Core-TM-i7-7700K-CPU-4-20GHz"
|
||||
inv.Labels["elemental.cattle.io/CpuModel"] = doubleDash.ReplaceAllString(sanitize.ReplaceAllString(systemData.CPU.Processors[0].Model, "-"), "-")
|
||||
inv.Labels["elemental.cattle.io/CpuVendor"] = doubleDash.ReplaceAllString(sanitize.ReplaceAllString(systemData.CPU.Processors[0].Vendor, "-"), "-")
|
||||
inv.Labels["elemental.cattle.io/CpuModel"] = sanitizeString(systemData.CPU.Processors[0].Model)
|
||||
inv.Labels["elemental.cattle.io/CpuVendor"] = sanitizeString(systemData.CPU.Processors[0].Vendor)
|
||||
// Capabilities available here at systemData.CPU.Processors[X].Capabilities
|
||||
}
|
||||
// This could happen so always check.
|
||||
if systemData.GPU != nil && len(systemData.GPU.GraphicsCards) > 0 && systemData.GPU.GraphicsCards[0].DeviceInfo != nil {
|
||||
inv.Labels["elemental.cattle.io/GpuModel"] = sanitize.ReplaceAllString(systemData.GPU.GraphicsCards[0].DeviceInfo.Product.Name, "-")
|
||||
inv.Labels["elemental.cattle.io/GpuVendor"] = sanitize.ReplaceAllString(systemData.GPU.GraphicsCards[0].DeviceInfo.Vendor.Name, "-")
|
||||
inv.Labels["elemental.cattle.io/GpuModel"] = sanitizeString(systemData.GPU.GraphicsCards[0].DeviceInfo.Product.Name)
|
||||
inv.Labels["elemental.cattle.io/GpuVendor"] = sanitizeString(systemData.GPU.GraphicsCards[0].DeviceInfo.Vendor.Name)
|
||||
}
|
||||
inv.Labels["elemental.cattle.io/NetNumberInterfaces"] = strconv.Itoa(len(systemData.Network.NICs))
|
||||
|
||||
|
|
@ -492,6 +492,16 @@ func updateInventoryFromSystemData(data []byte, inv *elementalv1.MachineInventor
|
|||
return nil
|
||||
}
|
||||
|
||||
// sanitizeString will sanitize a given string by:
|
||||
// replacing all invalid chars as set on the sanitize regex by dashes
|
||||
// removing any double dashes resulted from the above method
|
||||
// removing prefix+suffix if they are a dash
|
||||
func sanitizeString(s string) string {
|
||||
s1 := sanitize.ReplaceAllString(s, "-")
|
||||
s2 := doubleDash.ReplaceAllString(s1, "-")
|
||||
return strings.TrimSuffix(strings.TrimPrefix(s2, "-"), "-")
|
||||
}
|
||||
|
||||
func mergeInventoryLabels(inventory *elementalv1.MachineInventory, data []byte) error {
|
||||
labels := map[string]string{}
|
||||
if err := json.Unmarshal(data, &labels); err != nil {
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ import (
|
|||
"github.com/jaypipes/ghw/pkg/memory"
|
||||
"github.com/jaypipes/ghw/pkg/net"
|
||||
"github.com/rancher/elemental-operator/pkg/hostinfo"
|
||||
"k8s.io/apimachinery/pkg/util/validation"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
|
|
@ -294,24 +295,23 @@ func TestMergeInventoryLabels(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
func TestUpdateInventoryFromSystemData(t *testing.T) {
|
||||
inventory := &elementalv1.MachineInventory{}
|
||||
data := hostinfo.HostInfo{
|
||||
Block: &block.Info{
|
||||
Disks: []*block.Disk{
|
||||
Disks: []*block.Disk{
|
||||
{
|
||||
Name: "testdisk1",
|
||||
SizeBytes: 300,
|
||||
IsRemovable: true,
|
||||
Name: "testdisk1",
|
||||
SizeBytes: 300,
|
||||
IsRemovable: true,
|
||||
},
|
||||
{
|
||||
Name: "testdisk2",
|
||||
SizeBytes: 600,
|
||||
IsRemovable: false,
|
||||
Name: "testdisk2",
|
||||
SizeBytes: 600,
|
||||
IsRemovable: false,
|
||||
},
|
||||
},
|
||||
Partitions: nil,
|
||||
Partitions: nil,
|
||||
},
|
||||
Memory: &memory.Info{
|
||||
Area: memory.Area{
|
||||
|
|
@ -320,7 +320,7 @@ func TestUpdateInventoryFromSystemData(t *testing.T) {
|
|||
},
|
||||
CPU: &cpu.Info{
|
||||
TotalCores: 300,
|
||||
TotalThreads: 300,
|
||||
TotalThreads: 300,
|
||||
},
|
||||
Network: &net.Info{
|
||||
NICs: []*net.NIC{
|
||||
|
|
@ -338,16 +338,82 @@ func TestUpdateInventoryFromSystemData(t *testing.T) {
|
|||
err = updateInventoryFromSystemData(encodedData, inventory)
|
||||
assert.NilError(t, err)
|
||||
// Check that the labels we properly added to the inventory
|
||||
assert.Equal(t,inventory.Labels["elemental.cattle.io/TotalMemory"], "100" )
|
||||
assert.Equal(t,inventory.Labels["elemental.cattle.io/CpuTotalCores"], "300" )
|
||||
assert.Equal(t,inventory.Labels["elemental.cattle.io/CpuTotalThreads"], "300" )
|
||||
assert.Equal(t,inventory.Labels["elemental.cattle.io/NetNumberInterfaces"], "2" )
|
||||
assert.Equal(t,inventory.Labels["elemental.cattle.io/NetIface0-Name"], "myNic1" )
|
||||
assert.Equal(t,inventory.Labels["elemental.cattle.io/NetIface1-Name"], "myNic2" )
|
||||
assert.Equal(t,inventory.Labels["elemental.cattle.io/BlockDevice0-Name"], "testdisk1" )
|
||||
assert.Equal(t,inventory.Labels["elemental.cattle.io/BlockDevice1-Name"], "testdisk2" )
|
||||
assert.Equal(t,inventory.Labels["elemental.cattle.io/BlockDevice0-Size"], "300" )
|
||||
assert.Equal(t,inventory.Labels["elemental.cattle.io/BlockDevice1-Size"], "600" )
|
||||
assert.Equal(t,inventory.Labels["elemental.cattle.io/BlockDevice0-Removable"], "true" )
|
||||
assert.Equal(t,inventory.Labels["elemental.cattle.io/BlockDevice1-Removable"], "false" )
|
||||
}
|
||||
assert.Equal(t, inventory.Labels["elemental.cattle.io/TotalMemory"], "100")
|
||||
assert.Equal(t, inventory.Labels["elemental.cattle.io/CpuTotalCores"], "300")
|
||||
assert.Equal(t, inventory.Labels["elemental.cattle.io/CpuTotalThreads"], "300")
|
||||
assert.Equal(t, inventory.Labels["elemental.cattle.io/NetNumberInterfaces"], "2")
|
||||
assert.Equal(t, inventory.Labels["elemental.cattle.io/NetIface0-Name"], "myNic1")
|
||||
assert.Equal(t, inventory.Labels["elemental.cattle.io/NetIface1-Name"], "myNic2")
|
||||
assert.Equal(t, inventory.Labels["elemental.cattle.io/BlockDevice0-Name"], "testdisk1")
|
||||
assert.Equal(t, inventory.Labels["elemental.cattle.io/BlockDevice1-Name"], "testdisk2")
|
||||
assert.Equal(t, inventory.Labels["elemental.cattle.io/BlockDevice0-Size"], "300")
|
||||
assert.Equal(t, inventory.Labels["elemental.cattle.io/BlockDevice1-Size"], "600")
|
||||
assert.Equal(t, inventory.Labels["elemental.cattle.io/BlockDevice0-Removable"], "true")
|
||||
assert.Equal(t, inventory.Labels["elemental.cattle.io/BlockDevice1-Removable"], "false")
|
||||
}
|
||||
|
||||
func TestUpdateInventoryFromSystemDataSanitized(t *testing.T) {
|
||||
inventory := &elementalv1.MachineInventory{}
|
||||
data := hostinfo.HostInfo{
|
||||
Block: &block.Info{
|
||||
Disks: []*block.Disk{
|
||||
{
|
||||
Name: "testdisk1",
|
||||
SizeBytes: 300,
|
||||
IsRemovable: true,
|
||||
},
|
||||
{
|
||||
Name: "testdisk2",
|
||||
SizeBytes: 600,
|
||||
IsRemovable: false,
|
||||
},
|
||||
},
|
||||
Partitions: nil,
|
||||
},
|
||||
Memory: &memory.Info{
|
||||
Area: memory.Area{
|
||||
TotalPhysicalBytes: 100,
|
||||
},
|
||||
},
|
||||
CPU: &cpu.Info{
|
||||
TotalCores: 300,
|
||||
TotalThreads: 300,
|
||||
Processors: []*cpu.Processor{
|
||||
{
|
||||
Vendor: "-this_is@broken?TM-][{¬{$h4yh46Ŋ£$⅝ŋg46¬~{~←ħ¬",
|
||||
Model: "-this_is@broken?TM-][{¬{$h4yh46Ŋ£$⅝ŋg46¬~{~←ħ¬",
|
||||
},
|
||||
},
|
||||
},
|
||||
Network: &net.Info{
|
||||
NICs: []*net.NIC{
|
||||
{
|
||||
Name: "myNic1",
|
||||
},
|
||||
{
|
||||
Name: "myNic2",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
encodedData, err := json.Marshal(data)
|
||||
assert.NilError(t, err)
|
||||
err = updateInventoryFromSystemData(encodedData, inventory)
|
||||
assert.NilError(t, err)
|
||||
// Check that the labels we properly added to the inventory
|
||||
assert.Equal(t, inventory.Labels["elemental.cattle.io/TotalMemory"], "100")
|
||||
assert.Equal(t, inventory.Labels["elemental.cattle.io/CpuTotalCores"], "300")
|
||||
assert.Equal(t, inventory.Labels["elemental.cattle.io/CpuTotalThreads"], "300")
|
||||
assert.Equal(t, inventory.Labels["elemental.cattle.io/NetNumberInterfaces"], "2")
|
||||
assert.Equal(t, inventory.Labels["elemental.cattle.io/NetIface0-Name"], "myNic1")
|
||||
assert.Equal(t, inventory.Labels["elemental.cattle.io/NetIface1-Name"], "myNic2")
|
||||
assert.Equal(t, inventory.Labels["elemental.cattle.io/BlockDevice0-Name"], "testdisk1")
|
||||
assert.Equal(t, inventory.Labels["elemental.cattle.io/BlockDevice1-Name"], "testdisk2")
|
||||
assert.Equal(t, inventory.Labels["elemental.cattle.io/BlockDevice0-Size"], "300")
|
||||
assert.Equal(t, inventory.Labels["elemental.cattle.io/BlockDevice1-Size"], "600")
|
||||
assert.Equal(t, inventory.Labels["elemental.cattle.io/BlockDevice0-Removable"], "true")
|
||||
assert.Equal(t, inventory.Labels["elemental.cattle.io/BlockDevice1-Removable"], "false")
|
||||
// Check values were sanitized
|
||||
assert.Equal(t, len(validation.IsValidLabelValue(inventory.Labels["elemental.cattle.io/CpuModel"])), 0)
|
||||
assert.Equal(t, len(validation.IsValidLabelValue(inventory.Labels["elemental.cattle.io/CpuVendor"])), 0)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue