Skip to content

Commit

Permalink
Initial commit for handling multiple service accounts
Browse files Browse the repository at this point in the history
Improvement to use ConnectionLabels

Update with bug fixes

Fix bug with global lister

Fix for ConnectionLabel in config

Fix lint, vet, and etc

Fix the rename of the variable

Fix go test

Remove klog InitFlags from go test

Removed commented out code

Revert YAML to remove arg0
  • Loading branch information
davidvonthenen committed Aug 27, 2019
1 parent 22afbeb commit 44b759e
Show file tree
Hide file tree
Showing 28 changed files with 625 additions and 365 deletions.
6 changes: 4 additions & 2 deletions pkg/cloudprovider/vsphere/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,17 @@ func (vs *VSphere) Initialize(clientBuilder cloudprovider.ControllerClientBuilde

vs.informMgr = k8s.NewInformer(client)

connMgr := cm.NewConnectionManager(vs.cfg, vs.informMgr.GetSecretListener())
connMgr := cm.NewConnectionManager(vs.cfg, vs.informMgr)
vs.connectionManager = connMgr
vs.nodeManager.connectionManager = connMgr

vs.informMgr.AddNodeListener(vs.nodeAdded, vs.nodeDeleted, nil)

vs.informMgr.Listen()

//if running multiple service accounts, init them
connMgr.InitializeServiceAccounts()

if !vs.cfg.Global.APIDisable {
klog.V(1).Info("Starting the API Server")
vs.server.Start()
Expand All @@ -85,7 +88,6 @@ func (vs *VSphere) Initialize(clientBuilder cloudprovider.ControllerClientBuilde
} else {
klog.Errorf("Kubernetes Client Init Failed: %v", err)
}

}

// LoadBalancer returns a balancer interface. Also returns true if the
Expand Down
5 changes: 3 additions & 2 deletions pkg/cloudprovider/vsphere/instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import (
"testing"

"github.com/vmware/govmomi/simulator"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
clientv1 "k8s.io/client-go/listers/core/v1"
cm "k8s.io/cloud-provider-vsphere/pkg/common/connectionmanager"
v1helper "k8s.io/cloud-provider/node/helpers"

cm "k8s.io/cloud-provider-vsphere/pkg/common/connectionmanager"
)

type MyNodeManager struct {
Expand Down Expand Up @@ -140,7 +142,6 @@ func TestInstance(t *testing.T) {
if ishut {
t.Error("InstanceShutdownByProviderID is shutdown")
}

}

func TestInvalidInstance(t *testing.T) {
Expand Down
10 changes: 7 additions & 3 deletions pkg/cloudprovider/vsphere/nodemanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,17 @@ func (nm *NodeManager) DiscoverNode(nodeID string, searchBy cm.FindVM) error {
return err
}

vcInstance := nm.connectionManager.VsphereInstanceMap[vmDI.VcServer]
connectionLabel := vmDI.VcServer
if vmDI.ConnectionLabel != "" {
connectionLabel = vmDI.ConnectionLabel
}
vcInstance := nm.connectionManager.VsphereInstanceMap[connectionLabel]

ipFamily := []string{vcfg.DefaultIPFamily}
if vcInstance != nil {
ipFamily = vcInstance.Cfg.IPFamilyPriority
} else {
klog.Warningf("Unable to find vcInstance for %s. Defaulting to ipv4.", vmDI.VcServer)
klog.Warningf("Unable to find vcInstance for %s. Defaulting to ipv4.", connectionLabel)
}

found := false
Expand Down Expand Up @@ -245,7 +249,7 @@ func (nm *NodeManager) DiscoverNode(nodeID string, searchBy cm.FindVM) error {
os,
)

nodeInfo := &NodeInfo{dataCenter: vmDI.DataCenter, vm: vmDI.VM, vcServer: vmDI.VcServer,
nodeInfo := &NodeInfo{connectionLabel: connectionLabel, dataCenter: vmDI.DataCenter, vm: vmDI.VM, vcServer: vmDI.VcServer,
UUID: vmDI.UUID, NodeName: vmDI.NodeName, NodeType: instanceType, NodeAddresses: addrs}
nm.addNodeInfo(nodeInfo)

Expand Down
2 changes: 1 addition & 1 deletion pkg/cloudprovider/vsphere/nodemanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func TestDiscoverNodeByName(t *testing.T) {
vm.Guest.HostName = strings.ToLower(vm.Name) // simulator.SearchIndex.FindByDnsName matches against the guest.hostName property
name := vm.Name

err := connMgr.Connect(context.Background(), cfg.Global.VCenterIP)
err := connMgr.Connect(context.Background(), connMgr.VsphereInstanceMap[cfg.Global.VCenterIP])
if err != nil {
t.Errorf("Failed to Connect to vSphere: %s", err)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/cloudprovider/vsphere/proto/cloudprovidervsphere.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 8 additions & 7 deletions pkg/cloudprovider/vsphere/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ type VSphere struct {

// NodeInfo is information about a Kubernetes node.
type NodeInfo struct {
dataCenter *vclib.Datacenter
vm *vclib.VirtualMachine
vcServer string
UUID string
NodeName string
NodeType string
NodeAddresses []v1.NodeAddress
connectionLabel string
dataCenter *vclib.Datacenter
vm *vclib.VirtualMachine
vcServer string
UUID string
NodeName string
NodeType string
NodeAddresses []v1.NodeAddress
}

// DatacenterInfo is information about a vCenter datascenter.
Expand Down
7 changes: 4 additions & 3 deletions pkg/cloudprovider/vsphere/vsphere_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func configFromSimWithTLS(tlsConfig *tls.Config, insecureAllowed bool, multiDc b
cfg.Global.VCenterPort = s.URL.Port()
cfg.Global.User = s.URL.User.Username()
cfg.Global.Password, _ = s.URL.User.Password()
// Configure region and zone categories

if multiDc {
cfg.Global.Datacenters = "DC0,DC1"
} else {
Expand All @@ -114,6 +114,7 @@ func configFromSimWithTLS(tlsConfig *tls.Config, insecureAllowed bool, multiDc b
cfg.VirtualCenter[s.URL.Hostname()] = &vcfg.VirtualCenterConfig{
User: cfg.Global.User,
Password: cfg.Global.Password,
VCenterIP: cfg.Global.VCenterIP,
VCenterPort: cfg.Global.VCenterPort,
InsecureFlag: cfg.Global.InsecureFlag,
Datacenters: cfg.Global.Datacenters,
Expand All @@ -132,15 +133,15 @@ func configFromSimWithTLS(tlsConfig *tls.Config, insecureAllowed bool, multiDc b
// configFromEnvOrSim returns config from configFromEnv if set, otherwise returns configFromSim.
func configFromEnvOrSim(multiDc bool) (*vcfg.Config, func()) {
cfg := &vcfg.Config{}
if err := vcfg.FromEnv(cfg); err != nil {
if err := cfg.FromEnv(); err != nil {
return configFromSim(multiDc)
}
return cfg, func() {}
}

func TestNewVSphere(t *testing.T) {
cfg := &vcfg.Config{}
if err := vcfg.FromEnv(cfg); err != nil {
if err := cfg.FromEnv(); err != nil {
t.Skipf("No config found in environment")
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/cloudprovider/vsphere/zones.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (z *zones) GetZone(ctx context.Context) (cloudprovider.Zone, error) {
klog.V(4).Infof("Host owning VM is %s", oHost.Summary.Config.Name)

zoneResult, err := z.nodeManager.connectionManager.LookupZoneByMoref(
ctx, node.dataCenter, vmHost.Reference(), z.zone, z.region)
ctx, node.connectionLabel, vmHost.Reference(), z.zone, z.region)
if err != nil {
klog.Errorf("Failed to get host system properties. err: %+v", err)
return zone, err
Expand Down Expand Up @@ -111,7 +111,7 @@ func (z *zones) GetZoneByNodeName(ctx context.Context, nodeName k8stypes.NodeNam
klog.V(4).Infof("Host owning VM is %s", oHost.Summary.Config.Name)

zoneResult, err := z.nodeManager.connectionManager.LookupZoneByMoref(
ctx, node.dataCenter, vmHost.Reference(), z.zone, z.region)
ctx, node.connectionLabel, vmHost.Reference(), z.zone, z.region)
if err != nil {
klog.Errorf("Failed to get host system properties. err: %+v", err)
return zone, err
Expand Down Expand Up @@ -151,7 +151,7 @@ func (z *zones) GetZoneByProviderID(ctx context.Context, providerID string) (clo
klog.V(4).Infof("Host owning VM is %s", oHost.Summary.Config.Name)

zoneResult, err := z.nodeManager.connectionManager.LookupZoneByMoref(
ctx, node.dataCenter, vmHost.Reference(), z.zone, z.region)
ctx, node.connectionLabel, vmHost.Reference(), z.zone, z.region)
if err != nil {
klog.Errorf("Failed to get host system properties. err: %+v", err)
return zone, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloudprovider/vsphere/zones_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestZones(t *testing.T) {
zones := newZones(nm, cfg.Labels.Zone, cfg.Labels.Region)

// Create vSphere client
err := connMgr.Connect(ctx, cfg.Global.VCenterIP)
err := connMgr.Connect(ctx, connMgr.VsphereInstanceMap[cfg.Global.VCenterIP])
if err != nil {
t.Errorf("Failed to connect to vSphere: %s", err)
}
Expand Down
Loading

0 comments on commit 44b759e

Please sign in to comment.