Skip to content

Commit 33bfe74

Browse files
committed
Fix Windows to read VM UUIDs from serial numbers
Certain versions of vSphere do not have the same value for product_uuid and product_serial. This mimics the change in kubernetes#59519. Fixes kubernetes#74888
1 parent 79e8a29 commit 33bfe74

File tree

4 files changed

+41
-31
lines changed

4 files changed

+41
-31
lines changed

pkg/cloudprovider/providers/vsphere/vsphere_util.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ const (
4848
DummyDiskName = "kube-dummyDisk.vmdk"
4949
ProviderPrefix = "vsphere://"
5050
vSphereConfFileEnvVar = "VSPHERE_CONF_FILE"
51+
UUIDPrefix = "VMware-"
5152
)
5253

5354
// GetVSphere reads vSphere configuration from system environment and construct vSphere object
@@ -675,3 +676,25 @@ func GetNodeUUID(node *v1.Node) (string, error) {
675676
}
676677
return GetUUIDFromProviderID(node.Spec.ProviderID), nil
677678
}
679+
680+
func GetVMUUID() (string, error) {
681+
uuidFromFile, err := getRawUUID()
682+
if err != nil {
683+
return "", fmt.Errorf("error retrieving vm uuid: %s", err)
684+
}
685+
//strip leading and trailing white space and new line char
686+
uuid := strings.TrimSpace(uuidFromFile)
687+
// check the uuid starts with "VMware-"
688+
if !strings.HasPrefix(uuid, UUIDPrefix) {
689+
return "", fmt.Errorf("Failed to match Prefix, UUID read from the file is %v", uuidFromFile)
690+
}
691+
// Strip the prefix and white spaces and -
692+
uuid = strings.Replace(uuid[len(UUIDPrefix):(len(uuid))], " ", "", -1)
693+
uuid = strings.Replace(uuid, "-", "", -1)
694+
if len(uuid) != 32 {
695+
return "", fmt.Errorf("Length check failed, UUID read from the file is %v", uuidFromFile)
696+
}
697+
// need to add dashes, e.g. "564d395e-d807-e18a-cb25-b79f65eb2b9f"
698+
uuid = fmt.Sprintf("%s-%s-%s-%s-%s", uuid[0:8], uuid[8:12], uuid[12:16], uuid[16:20], uuid[20:32])
699+
return uuid, nil
700+
}

pkg/cloudprovider/providers/vsphere/vsphere_util_linux.go

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,15 @@ limitations under the License.
1919
package vsphere
2020

2121
import (
22-
"fmt"
2322
"io/ioutil"
24-
"strings"
2523
)
2624

27-
const (
28-
UUIDPath = "/sys/class/dmi/id/product_serial"
29-
UUIDPrefix = "VMware-"
30-
)
25+
const UUIDPath = "/sys/class/dmi/id/product_serial"
3126

32-
func GetVMUUID() (string, error) {
27+
func getRawUUID() (string, error) {
3328
id, err := ioutil.ReadFile(UUIDPath)
3429
if err != nil {
35-
return "", fmt.Errorf("error retrieving vm uuid: %s", err)
36-
}
37-
uuidFromFile := string(id[:])
38-
//strip leading and trailing white space and new line char
39-
uuid := strings.TrimSpace(uuidFromFile)
40-
// check the uuid starts with "VMware-"
41-
if !strings.HasPrefix(uuid, UUIDPrefix) {
42-
return "", fmt.Errorf("Failed to match Prefix, UUID read from the file is %v", uuidFromFile)
43-
}
44-
// Strip the prefix and white spaces and -
45-
uuid = strings.Replace(uuid[len(UUIDPrefix):(len(uuid))], " ", "", -1)
46-
uuid = strings.Replace(uuid, "-", "", -1)
47-
if len(uuid) != 32 {
48-
return "", fmt.Errorf("Length check failed, UUID read from the file is %v", uuidFromFile)
30+
return "", err
4931
}
50-
// need to add dashes, e.g. "564d395e-d807-e18a-cb25-b79f65eb2b9f"
51-
uuid = fmt.Sprintf("%s-%s-%s-%s-%s", uuid[0:8], uuid[8:12], uuid[12:16], uuid[16:20], uuid[20:32])
52-
return uuid, nil
32+
return string(id), nil
5333
}

pkg/cloudprovider/providers/vsphere/vsphere_util_unsupported.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ package vsphere
2020

2121
import "fmt"
2222

23-
func GetVMUUID() (string, error) {
23+
func getRawUUID() (string, error) {
2424
return "", fmt.Errorf("Retrieving VM UUID on this build is not implemented.")
2525
}

pkg/cloudprovider/providers/vsphere/vsphere_util_windows.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,21 @@ import (
2424
"strings"
2525
)
2626

27-
func GetVMUUID() (string, error) {
28-
result, err := exec.Command("wmic", "csproduct", "get", "UUID").Output()
27+
func getRawUUID() (string, error) {
28+
result, err := exec.Command("wmic", "bios", "get", "serialnumber").Output()
2929
if err != nil {
30-
return "", fmt.Errorf("error retrieving vm uuid: %s", err)
30+
return "", err
3131
}
32-
fields := strings.Fields(string(result))
33-
if len(fields) != 2 {
32+
lines := strings.FieldsFunc(string(result), func(r rune) bool {
33+
switch r {
34+
case '\n', '\r':
35+
return true
36+
default:
37+
return false
38+
}
39+
})
40+
if len(lines) != 2 {
3441
return "", fmt.Errorf("received unexpected value retrieving vm uuid: %q", string(result))
3542
}
36-
return fields[1], nil
43+
return lines[1], nil
3744
}

0 commit comments

Comments
 (0)