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

Updates based on the Travis' feedback

Updated test

Update validate func

Remove because no longer needed

Made global lock a member variable

Remove service account logic

Update based on AK feedback

Update based on AK feedback

Update based on feedback

Fix go test

Update go test for TestConnectionLabels
  • Loading branch information
davidvonthenen committed Aug 30, 2019
1 parent 22afbeb commit f98e4bc
Show file tree
Hide file tree
Showing 29 changed files with 692 additions and 397 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, client)
vs.connectionManager = connMgr
vs.nodeManager.connectionManager = connMgr

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

vs.informMgr.Listen()

//if running secrets, init them
connMgr.InitializeSecretLister()

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
9 changes: 5 additions & 4 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 @@ -73,7 +75,7 @@ func TestInstance(t *testing.T) {
/*
* Setup
*/
connMgr := cm.NewConnectionManager(cfg, nil)
connMgr := cm.NewConnectionManager(cfg, nil, nil)
nm := newMyNodeManager(connMgr, nil)
instances := newInstances(&nm.NodeManager)

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 All @@ -153,7 +154,7 @@ func TestInvalidInstance(t *testing.T) {
/*
* Setup
*/
connMgr := cm.NewConnectionManager(cfg, nil)
connMgr := cm.NewConnectionManager(cfg, nil, nil)
nm := newMyNodeManager(connMgr, nil)
instances := newInstances(&nm.NodeManager)

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
8 changes: 4 additions & 4 deletions pkg/cloudprovider/vsphere/nodemanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestRegUnregNode(t *testing.T) {
cfg, ok := configFromEnvOrSim(true)
defer ok()

connMgr := cm.NewConnectionManager(cfg, nil)
connMgr := cm.NewConnectionManager(cfg, nil, nil)
defer connMgr.Logout()

nm := newNodeManager(connMgr, nil)
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestDiscoverNodeByName(t *testing.T) {
cfg, ok := configFromEnvOrSim(true)
defer ok()

connMgr := cm.NewConnectionManager(cfg, nil)
connMgr := cm.NewConnectionManager(cfg, nil, nil)
defer connMgr.Logout()

nm := newNodeManager(connMgr, nil)
Expand All @@ -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 All @@ -116,7 +116,7 @@ func TestExport(t *testing.T) {
cfg, ok := configFromEnvOrSim(true)
defer ok()

connMgr := cm.NewConnectionManager(cfg, nil)
connMgr := cm.NewConnectionManager(cfg, nil, nil)
defer connMgr.Logout()

nm := newNodeManager(connMgr, nil)
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
24 changes: 13 additions & 11 deletions pkg/cloudprovider/vsphere/vsphere_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,21 @@ 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 {
cfg.Global.Datacenters = vclib.TestDefaultDatacenter
}
cfg.VirtualCenter = make(map[string]*vcfg.VirtualCenterConfig)
cfg.VirtualCenter[s.URL.Hostname()] = &vcfg.VirtualCenterConfig{
User: cfg.Global.User,
Password: cfg.Global.Password,
VCenterPort: cfg.Global.VCenterPort,
InsecureFlag: cfg.Global.InsecureFlag,
Datacenters: cfg.Global.Datacenters,
User: cfg.Global.User,
Password: cfg.Global.Password,
ConnectionLabel: cfg.Global.VCenterIP,
VCenterIP: cfg.Global.VCenterIP,
VCenterPort: cfg.Global.VCenterPort,
InsecureFlag: cfg.Global.InsecureFlag,
Datacenters: cfg.Global.Datacenters,
}

// Configure region and zone categories
Expand All @@ -132,15 +134,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 All @@ -159,7 +161,7 @@ func TestVSphereLogin(t *testing.T) {
if err != nil {
t.Fatalf("Failed to construct/authenticate vSphere: %s", err)
}
vs.connectionManager = cm.NewConnectionManager(cfg, nil)
vs.connectionManager = cm.NewConnectionManager(cfg, nil, nil)
defer vs.connectionManager.Logout()

// Create context
Expand Down Expand Up @@ -192,7 +194,7 @@ func TestVSphereLoginByToken(t *testing.T) {
if err != nil {
t.Fatalf("Failed to construct/authenticate vSphere: %s", err)
}
vs.connectionManager = cm.NewConnectionManager(cfg, nil)
vs.connectionManager = cm.NewConnectionManager(cfg, nil, nil)

ctx := context.Background()

Expand Down Expand Up @@ -492,7 +494,7 @@ func TestSecretVSphereConfig(t *testing.T) {
if err != nil { // testcase.expectedError {
t.Fatalf("buildVSphereFromConfig: Should succeed when a valid config is provided: %v", err)
}
vs.connectionManager = cm.NewConnectionManager(cfg, nil)
vs.connectionManager = cm.NewConnectionManager(cfg, nil, nil)

if testcase.expectedIsSecretProvided && (vs.cfg.Global.SecretNamespace == "" || vs.cfg.Global.SecretName == "") {
t.Fatalf("SecretName and SecretNamespace was expected in config %s. error: %s",
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
4 changes: 2 additions & 2 deletions pkg/cloudprovider/vsphere/zones_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ func TestZones(t *testing.T) {
cfg.Global.Password = localhostKey

// Create configuration object
connMgr := cm.NewConnectionManager(cfg, nil)
connMgr := cm.NewConnectionManager(cfg, nil, nil)
defer connMgr.Logout()

nm := newNodeManager(connMgr, nil)
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 f98e4bc

Please sign in to comment.