mirror of https://github.com/k3s-io/k3s
Merge pull request #71147 from pivotal-k8s/vsphere-windows-uuid
Support retrieving the VM UUID on Windowspull/564/head
commit
e4db1471f1
|
@ -13,6 +13,9 @@ go_library(
|
||||||
"nodemanager.go",
|
"nodemanager.go",
|
||||||
"vsphere.go",
|
"vsphere.go",
|
||||||
"vsphere_util.go",
|
"vsphere_util.go",
|
||||||
|
"vsphere_util_linux.go",
|
||||||
|
"vsphere_util_unsupported.go",
|
||||||
|
"vsphere_util_windows.go",
|
||||||
],
|
],
|
||||||
importpath = "k8s.io/kubernetes/pkg/cloudprovider/providers/vsphere",
|
importpath = "k8s.io/kubernetes/pkg/cloudprovider/providers/vsphere",
|
||||||
deps = [
|
deps = [
|
||||||
|
|
|
@ -20,7 +20,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -45,8 +44,6 @@ const (
|
||||||
Folder = "Folder"
|
Folder = "Folder"
|
||||||
VirtualMachine = "VirtualMachine"
|
VirtualMachine = "VirtualMachine"
|
||||||
DummyDiskName = "kube-dummyDisk.vmdk"
|
DummyDiskName = "kube-dummyDisk.vmdk"
|
||||||
UUIDPath = "/sys/class/dmi/id/product_serial"
|
|
||||||
UUIDPrefix = "VMware-"
|
|
||||||
ProviderPrefix = "vsphere://"
|
ProviderPrefix = "vsphere://"
|
||||||
vSphereConfFileEnvVar = "VSPHERE_CONF_FILE"
|
vSphereConfFileEnvVar = "VSPHERE_CONF_FILE"
|
||||||
)
|
)
|
||||||
|
@ -552,29 +549,6 @@ func (vs *VSphere) GetNodeNameFromProviderID(providerID string) (string, error)
|
||||||
return nodeName, nil
|
return nodeName, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetVMUUID() (string, error) {
|
|
||||||
id, err := ioutil.ReadFile(UUIDPath)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("error retrieving vm uuid: %s", err)
|
|
||||||
}
|
|
||||||
uuidFromFile := string(id[:])
|
|
||||||
//strip leading and trailing white space and new line char
|
|
||||||
uuid := strings.TrimSpace(uuidFromFile)
|
|
||||||
// check the uuid starts with "VMware-"
|
|
||||||
if !strings.HasPrefix(uuid, UUIDPrefix) {
|
|
||||||
return "", fmt.Errorf("Failed to match Prefix, UUID read from the file is %v", uuidFromFile)
|
|
||||||
}
|
|
||||||
// Strip the prefix and white spaces and -
|
|
||||||
uuid = strings.Replace(uuid[len(UUIDPrefix):(len(uuid))], " ", "", -1)
|
|
||||||
uuid = strings.Replace(uuid, "-", "", -1)
|
|
||||||
if len(uuid) != 32 {
|
|
||||||
return "", fmt.Errorf("Length check failed, UUID read from the file is %v", uuidFromFile)
|
|
||||||
}
|
|
||||||
// need to add dashes, e.g. "564d395e-d807-e18a-cb25-b79f65eb2b9f"
|
|
||||||
uuid = fmt.Sprintf("%s-%s-%s-%s-%s", uuid[0:8], uuid[8:12], uuid[12:16], uuid[16:20], uuid[20:32])
|
|
||||||
return uuid, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetUUIDFromProviderID(providerID string) string {
|
func GetUUIDFromProviderID(providerID string) string {
|
||||||
return strings.TrimPrefix(providerID, ProviderPrefix)
|
return strings.TrimPrefix(providerID, ProviderPrefix)
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,53 @@
|
||||||
|
// +build linux
|
||||||
|
|
||||||
|
/*
|
||||||
|
Copyright 2018 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package vsphere
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
UUIDPath = "/sys/class/dmi/id/product_serial"
|
||||||
|
UUIDPrefix = "VMware-"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetVMUUID() (string, error) {
|
||||||
|
id, err := ioutil.ReadFile(UUIDPath)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("error retrieving vm uuid: %s", err)
|
||||||
|
}
|
||||||
|
uuidFromFile := string(id[:])
|
||||||
|
//strip leading and trailing white space and new line char
|
||||||
|
uuid := strings.TrimSpace(uuidFromFile)
|
||||||
|
// check the uuid starts with "VMware-"
|
||||||
|
if !strings.HasPrefix(uuid, UUIDPrefix) {
|
||||||
|
return "", fmt.Errorf("Failed to match Prefix, UUID read from the file is %v", uuidFromFile)
|
||||||
|
}
|
||||||
|
// Strip the prefix and white spaces and -
|
||||||
|
uuid = strings.Replace(uuid[len(UUIDPrefix):(len(uuid))], " ", "", -1)
|
||||||
|
uuid = strings.Replace(uuid, "-", "", -1)
|
||||||
|
if len(uuid) != 32 {
|
||||||
|
return "", fmt.Errorf("Length check failed, UUID read from the file is %v", uuidFromFile)
|
||||||
|
}
|
||||||
|
// need to add dashes, e.g. "564d395e-d807-e18a-cb25-b79f65eb2b9f"
|
||||||
|
uuid = fmt.Sprintf("%s-%s-%s-%s-%s", uuid[0:8], uuid[8:12], uuid[12:16], uuid[16:20], uuid[20:32])
|
||||||
|
return uuid, nil
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
// +build !windows,!linux
|
||||||
|
|
||||||
|
/*
|
||||||
|
Copyright 2018 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package vsphere
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func GetVMUUID() (string, error) {
|
||||||
|
return "", fmt.Errorf("Retrieving VM UUID on this build is not implemented.")
|
||||||
|
}
|
|
@ -0,0 +1,37 @@
|
||||||
|
// +build windows
|
||||||
|
|
||||||
|
/*
|
||||||
|
Copyright 2018 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package vsphere
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os/exec"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetVMUUID() (string, error) {
|
||||||
|
result, err := exec.Command("wmic", "csproduct", "get", "UUID").Output()
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("error retrieving vm uuid: %s", err)
|
||||||
|
}
|
||||||
|
fields := strings.Fields(string(result))
|
||||||
|
if len(fields) != 2 {
|
||||||
|
return "", fmt.Errorf("received unexpected value retrieving vm uuid: %q", string(result))
|
||||||
|
}
|
||||||
|
return fields[1], nil
|
||||||
|
}
|
Loading…
Reference in New Issue