mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-23 19:56:01 +00:00
Merge pull request #27331 from hpcloud/hpe/vsphere-get-id
Automatic merge from submit-queue vSphere provider - Getting node data by ip instead of uuid To get the uuid we need the service to be running as root. This change allows us to run the controller-manager and api server as non-root.
This commit is contained in:
commit
56db230455
@ -17,12 +17,11 @@ limitations under the License.
|
|||||||
package vsphere
|
package vsphere
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os/exec"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/vmware/govmomi"
|
"github.com/vmware/govmomi"
|
||||||
@ -104,14 +103,12 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func readInstanceID(cfg *VSphereConfig) (string, error) {
|
func readInstanceID(cfg *VSphereConfig) (string, error) {
|
||||||
cmd := exec.Command("bash", "-c", `dmidecode -t 1 | grep UUID | tr -d ' ' | cut -f 2 -d ':'`)
|
addrs, err := net.InterfaceAddrs()
|
||||||
var out bytes.Buffer
|
|
||||||
cmd.Stdout = &out
|
|
||||||
err := cmd.Run()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if out.Len() == 0 {
|
|
||||||
|
if len(addrs) == 0 {
|
||||||
return "", fmt.Errorf("unable to retrieve Instance ID")
|
return "", fmt.Errorf("unable to retrieve Instance ID")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,7 +135,22 @@ func readInstanceID(cfg *VSphereConfig) (string, error) {
|
|||||||
|
|
||||||
s := object.NewSearchIndex(c.Client)
|
s := object.NewSearchIndex(c.Client)
|
||||||
|
|
||||||
svm, err := s.FindByUuid(ctx, dc, strings.ToLower(strings.TrimSpace(out.String())), true, nil)
|
var svm object.Reference
|
||||||
|
for _, v := range addrs {
|
||||||
|
ip, _, err := net.ParseCIDR(v.String())
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("unable to parse cidr from ip")
|
||||||
|
}
|
||||||
|
|
||||||
|
svm, err = s.FindByIp(ctx, dc, ip.String(), true)
|
||||||
|
if err == nil && svm != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if svm == nil {
|
||||||
|
return "", fmt.Errorf("unable to retrieve vm reference from vSphere")
|
||||||
|
}
|
||||||
|
|
||||||
var vm mo.VirtualMachine
|
var vm mo.VirtualMachine
|
||||||
err = s.Properties(ctx, svm.Reference(), []string{"name"}, &vm)
|
err = s.Properties(ctx, svm.Reference(), []string{"name"}, &vm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user