From 1657bbbe18a27842558b12b2028e1b80efe88c74 Mon Sep 17 00:00:00 2001 From: Gyuho Lee Date: Tue, 31 Dec 2024 00:47:50 +0800 Subject: [PATCH] fix(os): fetch system manufacturer once for linux Signed-off-by: Gyuho Lee --- components/os/component_output.go | 27 ++++++++++++++++++++------- pkg/host/machine_id.go | 2 +- pkg/host/virt.go | 2 +- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/components/os/component_output.go b/components/os/component_output.go index e23380eb..22373a74 100644 --- a/components/os/component_output.go +++ b/components/os/component_output.go @@ -360,7 +360,10 @@ type MachineMetadata struct { OSMachineID string `json:"os_machine_id"` } -var currentMachineMetadata MachineMetadata +var ( + currentMachineMetadata MachineMetadata + currentSystemManufacturer string +) func init() { // Linux-specific operations @@ -395,6 +398,13 @@ func init() { if err != nil { log.Logger.Warnw("failed to get os machine id", "error", err) } + + cctx, ccancel := context.WithTimeout(context.Background(), 20*time.Second) + currentSystemManufacturer, err = pkg_host.SystemManufacturer(cctx) + ccancel() + if err != nil { + log.Logger.Warnw("failed to get system manufacturer", "error", err) + } } var ( @@ -438,13 +448,16 @@ func CreateGet(cfg Config) func(ctx context.Context) (_ any, e error) { } o.VirtualizationEnvironment = virtEnv - cctx, ccancel = context.WithTimeout(ctx, 10*time.Second) - manufacturer, err := pkg_host.SystemManufacturer(cctx) - ccancel() - if err != nil { - return nil, err + // for some reason, init failed + if currentSystemManufacturer == "" && runtime.GOOS == "linux" { + cctx, ccancel = context.WithTimeout(ctx, 20*time.Second) + currentSystemManufacturer, err = pkg_host.SystemManufacturer(cctx) + ccancel() + if err != nil { + log.Logger.Warnw("failed to get system manufacturer", "error", err) + } } - o.SystemManufacturer = manufacturer + o.SystemManufacturer = currentSystemManufacturer o.MachineMetadata = currentMachineMetadata diff --git a/pkg/host/machine_id.go b/pkg/host/machine_id.go index 35f3c168..b46e0fad 100644 --- a/pkg/host/machine_id.go +++ b/pkg/host/machine_id.go @@ -64,7 +64,7 @@ func DmidecodeUUID(ctx context.Context) (string, error) { } }), ); err != nil { - return "", fmt.Errorf("failed to read dmidecode output: %w\n\noutput:\n%s", err, strings.Join(lines, "\n")) + return "", fmt.Errorf("failed to read dmidecode for uuid: %w\n\noutput:\n%s", err, strings.Join(lines, "\n")) } return uuid, nil diff --git a/pkg/host/virt.go b/pkg/host/virt.go index b88179cd..4dc4eec2 100644 --- a/pkg/host/virt.go +++ b/pkg/host/virt.go @@ -128,7 +128,7 @@ func SystemManufacturer(ctx context.Context) (string, error) { }), process.WithWaitForCmd(), ); err != nil { - return "", fmt.Errorf("failed to read dmidecode output: %w\n\noutput:\n%s", err, strings.Join(lines, "\n")) + return "", fmt.Errorf("failed to read dmidecode for system manufacturer: %w\n\noutput:\n%s", err, strings.Join(lines, "\n")) } out := strings.TrimSpace(strings.Join(lines, "\n"))