diff --git a/pkg/kubelet/winstats/perfcounter_nodestats.go b/pkg/kubelet/winstats/perfcounter_nodestats.go index dc4ddf6fe24..9a22b047b32 100644 --- a/pkg/kubelet/winstats/perfcounter_nodestats.go +++ b/pkg/kubelet/winstats/perfcounter_nodestats.go @@ -21,7 +21,9 @@ package winstats import ( "errors" "os" + "os/exec" "runtime" + "strings" "sync" "time" "unsafe" @@ -136,10 +138,16 @@ func (p *perfCounterNodeStatsClient) getMachineInfo() (*cadvisorapi.MachineInfo, return nil, err } + systemUUID, err := getSystemUUID() + if err != nil { + return nil, err + } + return &cadvisorapi.MachineInfo{ NumCores: runtime.NumCPU(), MemoryCapacity: p.nodeInfo.memoryPhysicalCapacityBytes, MachineID: hostname, + SystemUUID: systemUUID, }, nil } @@ -212,6 +220,16 @@ func (p *perfCounterNodeStatsClient) getCPUUsageNanoCores() uint64 { return cpuUsageNanoCores } +func getSystemUUID() (string, error) { + cmd := exec.Command("powershell.exe", "-Command", "(Get-CimInstance -Class Win32_ComputerSystemProduct).UUID") + out, err := cmd.CombinedOutput() + if err != nil { + return "", err + } + systemUUID := strings.TrimRight(string(out), "\r\n") + return systemUUID, nil +} + func getPhysicallyInstalledSystemMemoryBytes() (uint64, error) { // We use GlobalMemoryStatusEx instead of GetPhysicallyInstalledSystemMemory // on Windows node for the following reasons: diff --git a/pkg/kubelet/winstats/winstats.go b/pkg/kubelet/winstats/winstats.go index 2b34bfd8ae4..2b42cd850d7 100644 --- a/pkg/kubelet/winstats/winstats.go +++ b/pkg/kubelet/winstats/winstats.go @@ -20,8 +20,6 @@ limitations under the License. package winstats import ( - "os/exec" - "strings" "syscall" "time" "unsafe" @@ -105,24 +103,9 @@ func (c *StatsClient) WinContainerInfos() (map[string]cadvisorapiv2.ContainerInf } // WinMachineInfo returns a cadvisorapi.MachineInfo with details about the -// node machine. Run the powershell command to get the SystemUUID for Windows node -// in here if it isn't provided by cadvisor. +// node machine. func (c *StatsClient) WinMachineInfo() (*cadvisorapi.MachineInfo, error) { - infos, err := c.client.getMachineInfo() - if err != nil { - return nil, err - } - - if infos.SystemUUID == "" { - cmd := exec.Command("powershell.exe", "-Command", "(Get-CimInstance -Class Win32_ComputerSystemProduct).UUID") - out, err := cmd.CombinedOutput() - if err != nil { - return infos, err - } - infos.SystemUUID = strings.TrimRight(string(out), "\r\n") - } - - return infos, nil + return c.client.getMachineInfo() } // WinVersionInfo returns a cadvisorapi.VersionInfo with version info of diff --git a/pkg/kubelet/winstats/winstats_test.go b/pkg/kubelet/winstats/winstats_test.go index 130c60f1709..aa0fbefc616 100644 --- a/pkg/kubelet/winstats/winstats_test.go +++ b/pkg/kubelet/winstats/winstats_test.go @@ -121,7 +121,7 @@ func TestWinMachineInfo(t *testing.T) { NumCores: 4, MemoryCapacity: 1.6e+10, MachineID: "somehostname", - SystemUUID: machineInfo.SystemUUID,}) + SystemUUID: machineInfo.SystemUUID}) } func TestWinVersionInfo(t *testing.T) {