|
|
|
|
@@ -21,12 +21,10 @@ import (
|
|
|
|
|
"errors"
|
|
|
|
|
"os"
|
|
|
|
|
"regexp"
|
|
|
|
|
"runtime"
|
|
|
|
|
"strings"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/golang/glog"
|
|
|
|
|
"github.com/vmware/govmomi"
|
|
|
|
|
"github.com/vmware/govmomi/vim25"
|
|
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
|
@@ -34,7 +32,6 @@ import (
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
|
|
"github.com/vmware/govmomi/vim25/mo"
|
|
|
|
|
"k8s.io/api/core/v1"
|
|
|
|
|
k8stypes "k8s.io/apimachinery/pkg/types"
|
|
|
|
|
"k8s.io/kubernetes/pkg/cloudprovider/providers/vsphere/vclib"
|
|
|
|
|
"k8s.io/kubernetes/pkg/cloudprovider/providers/vsphere/vclib/diskmanagers"
|
|
|
|
|
@@ -46,63 +43,37 @@ const (
|
|
|
|
|
Folder = "Folder"
|
|
|
|
|
VirtualMachine = "VirtualMachine"
|
|
|
|
|
DummyDiskName = "kube-dummyDisk.vmdk"
|
|
|
|
|
vSphereConfFileEnvVar = "VSPHERE_CONF_FILE"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// GetVSphere reads vSphere configuration from system environment and construct vSphere object
|
|
|
|
|
func GetVSphere() (*VSphere, error) {
|
|
|
|
|
cfg := getVSphereConfig()
|
|
|
|
|
vSphereConn := getVSphereConn(cfg)
|
|
|
|
|
client, err := GetgovmomiClient(vSphereConn)
|
|
|
|
|
cfg, err := getVSphereConfig()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
vSphereConn.GoVmomiClient = client
|
|
|
|
|
vsphereIns := &VSphereInstance{
|
|
|
|
|
conn: vSphereConn,
|
|
|
|
|
cfg: &VirtualCenterConfig{
|
|
|
|
|
User: cfg.Global.User,
|
|
|
|
|
Password: cfg.Global.Password,
|
|
|
|
|
VCenterPort: cfg.Global.VCenterPort,
|
|
|
|
|
Datacenters: cfg.Global.Datacenters,
|
|
|
|
|
RoundTripperCount: cfg.Global.RoundTripperCount,
|
|
|
|
|
},
|
|
|
|
|
vs, err := newControllerNode(*cfg)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
vsphereInsMap := make(map[string]*VSphereInstance)
|
|
|
|
|
vsphereInsMap[cfg.Global.VCenterIP] = vsphereIns
|
|
|
|
|
// TODO: Initialize nodeManager and set it in VSphere.
|
|
|
|
|
vs := &VSphere{
|
|
|
|
|
vsphereInstanceMap: vsphereInsMap,
|
|
|
|
|
hostName: "",
|
|
|
|
|
cfg: cfg,
|
|
|
|
|
nodeManager: &NodeManager{
|
|
|
|
|
vsphereInstanceMap: vsphereInsMap,
|
|
|
|
|
nodeInfoMap: make(map[string]*NodeInfo),
|
|
|
|
|
registeredNodes: make(map[string]*v1.Node),
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
runtime.SetFinalizer(vs, logout)
|
|
|
|
|
return vs, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getVSphereConfig() *VSphereConfig {
|
|
|
|
|
var cfg VSphereConfig
|
|
|
|
|
cfg.Global.VCenterIP = os.Getenv("VSPHERE_VCENTER")
|
|
|
|
|
cfg.Global.VCenterPort = os.Getenv("VSPHERE_VCENTER_PORT")
|
|
|
|
|
cfg.Global.User = os.Getenv("VSPHERE_USER")
|
|
|
|
|
cfg.Global.Password = os.Getenv("VSPHERE_PASSWORD")
|
|
|
|
|
cfg.Global.Datacenters = os.Getenv("VSPHERE_DATACENTER")
|
|
|
|
|
cfg.Global.DefaultDatastore = os.Getenv("VSPHERE_DATASTORE")
|
|
|
|
|
cfg.Global.WorkingDir = os.Getenv("VSPHERE_WORKING_DIR")
|
|
|
|
|
cfg.Global.VMName = os.Getenv("VSPHERE_VM_NAME")
|
|
|
|
|
cfg.Global.InsecureFlag = false
|
|
|
|
|
if strings.ToLower(os.Getenv("VSPHERE_INSECURE")) == "true" {
|
|
|
|
|
cfg.Global.InsecureFlag = true
|
|
|
|
|
func getVSphereConfig() (*VSphereConfig, error) {
|
|
|
|
|
confFileLocation := os.Getenv(vSphereConfFileEnvVar)
|
|
|
|
|
if confFileLocation == "" {
|
|
|
|
|
return nil, fmt.Errorf("Env variable 'VSPHERE_CONF_FILE' is not set.")
|
|
|
|
|
}
|
|
|
|
|
cfg.Workspace.VCenterIP = cfg.Global.VCenterIP
|
|
|
|
|
cfg.Workspace.Datacenter = cfg.Global.Datacenters
|
|
|
|
|
cfg.Workspace.DefaultDatastore = cfg.Global.DefaultDatastore
|
|
|
|
|
cfg.Workspace.Folder = cfg.Global.WorkingDir
|
|
|
|
|
return &cfg
|
|
|
|
|
confFile, err := os.Open(confFileLocation)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
defer confFile.Close()
|
|
|
|
|
cfg, err := readConfig(confFile)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &cfg, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getVSphereConn(cfg *VSphereConfig) *vclib.VSphereConnection {
|
|
|
|
|
@@ -117,16 +88,6 @@ func getVSphereConn(cfg *VSphereConfig) *vclib.VSphereConnection {
|
|
|
|
|
return vSphereConn
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetgovmomiClient gets the goVMOMI client for the vsphere connection object
|
|
|
|
|
func GetgovmomiClient(conn *vclib.VSphereConnection) (*govmomi.Client, error) {
|
|
|
|
|
if conn == nil {
|
|
|
|
|
cfg := getVSphereConfig()
|
|
|
|
|
conn = getVSphereConn(cfg)
|
|
|
|
|
}
|
|
|
|
|
client, err := conn.NewClient(context.TODO())
|
|
|
|
|
return client, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Returns the accessible datastores for the given node VM.
|
|
|
|
|
func getAccessibleDatastores(ctx context.Context, nodeVmDetail *NodeDetails, nodeManager *NodeManager) ([]*vclib.DatastoreInfo, error) {
|
|
|
|
|
accessibleDatastores, err := nodeVmDetail.vm.GetAllAccessibleDatastores(ctx)
|
|
|
|
|
@@ -512,3 +473,40 @@ func (vs *VSphere) checkDiskAttached(ctx context.Context, nodes []k8stypes.NodeN
|
|
|
|
|
}
|
|
|
|
|
return nodesToRetry, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (vs *VSphere) IsDummyVMPresent(vmName string) (bool, error) {
|
|
|
|
|
isDummyVMPresent := false
|
|
|
|
|
|
|
|
|
|
// Create context
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
vsi, err := vs.getVSphereInstanceForServer(vs.cfg.Workspace.VCenterIP, ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return isDummyVMPresent, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dc, err := vclib.GetDatacenter(ctx, vsi.conn, vs.cfg.Workspace.Datacenter)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return isDummyVMPresent, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vmFolder, err := dc.GetFolderByPath(ctx, vs.cfg.Workspace.Folder)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return isDummyVMPresent, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vms, err := vmFolder.GetVirtualMachines(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return isDummyVMPresent, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, vm := range vms {
|
|
|
|
|
if vm.Name() == vmName {
|
|
|
|
|
isDummyVMPresent = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return isDummyVMPresent, nil
|
|
|
|
|
}
|
|
|
|
|
|