Skip to content

Commit

Permalink
Assign datacenter only if specified
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Jansen <jan.jansen@gdata.de>
  • Loading branch information
farodin91 committed Oct 21, 2021
1 parent 381fc8a commit 9673515
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 20 deletions.
23 changes: 15 additions & 8 deletions controllers/vspheredeploymentzone_controller_domain_test.go
Expand Up @@ -33,6 +33,10 @@ import (
"sigs.k8s.io/cluster-api-provider-vsphere/test/helpers"
)

const (
defaultDatacenter string = "DC0"
)

func TestVsphereDeploymentZoneReconciler_Reconcile_VerifyFailureDomain(t *testing.T) {
t.Run("for Compute Cluster Zone Failure Domain", ForComputeClusterZone)
t.Run("for Host Group Zone Failure Domain", ForHostGroupZone)
Expand Down Expand Up @@ -66,7 +70,8 @@ func ForComputeClusterZone(t *testing.T) {

params := session.NewParams().
WithServer(simr.ServerURL().Host).
WithUserInfo(simr.Username(), simr.Password())
WithUserInfo(simr.Username(), simr.Password()).
WithDatacenter(defaultDatacenter)
authSession, err := session.GetOrCreate(controllerCtx, params)
g.Expect(err).NotTo(HaveOccurred())

Expand All @@ -85,7 +90,7 @@ func ForComputeClusterZone(t *testing.T) {
AutoConfigure: nil,
},
Topology: infrav1.Topology{
Datacenter: "DC0",
Datacenter: defaultDatacenter,
ComputeCluster: pointer.String("DC0_C0"),
},
},
Expand Down Expand Up @@ -146,7 +151,8 @@ func ForHostGroupZone(t *testing.T) {

params := session.NewParams().
WithServer(simr.ServerURL().Host).
WithUserInfo(simr.Username(), simr.Password())
WithUserInfo(simr.Username(), simr.Password()).
WithDatacenter(defaultDatacenter)
authSession, err := session.GetOrCreate(controllerCtx, params)
g.Expect(err).NotTo(HaveOccurred())

Expand All @@ -165,7 +171,7 @@ func ForHostGroupZone(t *testing.T) {
AutoConfigure: nil,
},
Topology: infrav1.Topology{
Datacenter: "DC0",
Datacenter: defaultDatacenter,
ComputeCluster: pointer.String("DC0_C0"),
Hosts: &infrav1.FailureDomainHosts{
HostGroupName: "test_grp_1",
Expand Down Expand Up @@ -217,7 +223,8 @@ func TestVsphereDeploymentZoneReconciler_Reconcile_CreateAndAttachMetadata(t *te
controllerCtx := fake.NewControllerContext(mgmtContext)
params := session.NewParams().
WithServer(simr.ServerURL().Host).
WithUserInfo(simr.Username(), simr.Password())
WithUserInfo(simr.Username(), simr.Password()).
WithDatacenter(defaultDatacenter)
authSession, err := session.GetOrCreate(controllerCtx, params)
NewWithT(t).Expect(err).NotTo(HaveOccurred())

Expand All @@ -237,7 +244,7 @@ func TestVsphereDeploymentZoneReconciler_Reconcile_CreateAndAttachMetadata(t *te
AutoConfigure: nil,
},
Topology: infrav1.Topology{
Datacenter: "DC0",
Datacenter: defaultDatacenter,
ComputeCluster: nil,
},
},
Expand All @@ -252,7 +259,7 @@ func TestVsphereDeploymentZoneReconciler_Reconcile_CreateAndAttachMetadata(t *te
AutoConfigure: nil,
},
Topology: infrav1.Topology{
Datacenter: "DC0",
Datacenter: defaultDatacenter,
ComputeCluster: pointer.String("DC0_C0"),
},
},
Expand All @@ -267,7 +274,7 @@ func TestVsphereDeploymentZoneReconciler_Reconcile_CreateAndAttachMetadata(t *te
AutoConfigure: pointer.Bool(true),
},
Topology: infrav1.Topology{
Datacenter: "DC0",
Datacenter: defaultDatacenter,
ComputeCluster: pointer.String("DC0_C0"),
Hosts: &infrav1.FailureDomainHosts{
HostGroupName: "group-one",
Expand Down
3 changes: 2 additions & 1 deletion pkg/services/govmomi/create_test.go
Expand Up @@ -45,7 +45,8 @@ func TestCreate(t *testing.T) {
vmContext.Context,
session.NewParams().
WithServer(vmContext.VSphereVM.Spec.Server).
WithUserInfo(simr.Username(), simr.Password()))
WithUserInfo(simr.Username(), simr.Password()).
WithDefaultDatacenter())
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/services/govmomi/vcenter/clone_test.go
Expand Up @@ -149,7 +149,8 @@ func initSimulator(t *testing.T) (*simulator.Model, *session.Session, *simulator
ctx.TODO(),
session.NewParams().
WithServer(server.URL.Host).
WithUserInfo(server.URL.User.Username(), pass))
WithUserInfo(server.URL.User.Username(), pass).
WithDefaultDatacenter())
if err != nil {
t.Fatal(err)
}
Expand Down
35 changes: 25 additions & 10 deletions pkg/session/session.go
Expand Up @@ -62,11 +62,12 @@ func DefaultFeature() Feature {
}

type Params struct {
server string
datacenter string
userinfo *url.Userinfo
thumbprint string
feature Feature
server string
datacenter string
tryDefaultDatacenter bool
userinfo *url.Userinfo
thumbprint string
feature Feature
}

func NewParams() *Params {
Expand All @@ -85,6 +86,11 @@ func (p *Params) WithDatacenter(datacenter string) *Params {
return p
}

func (p *Params) WithDefaultDatacenter() *Params {
p.tryDefaultDatacenter = true
return p
}

func (p *Params) WithUserInfo(username, password string) *Params {
p.userinfo = url.UserPassword(username, password)
return p
Expand Down Expand Up @@ -150,12 +156,21 @@ func GetOrCreate(ctx context.Context, params *Params) (*Session, error) {
session.TagManager = manager

// Assign the datacenter if one was specified.
dc, err := session.Finder.DatacenterOrDefault(ctx, params.datacenter)
if err != nil {
return nil, errors.Wrapf(err, "unable to find datacenter %q", params.datacenter)
if params.datacenter != "" {
dc, err := session.Finder.Datacenter(ctx, params.datacenter)
if err != nil {
return nil, errors.Wrapf(err, "unable to find datacenter %q", params.datacenter)
}
session.datacenter = dc
session.Finder.SetDatacenter(dc)
} else if params.tryDefaultDatacenter {
dc, err := session.Finder.DefaultDatacenter(ctx)
if err != nil {
return nil, errors.Wrapf(err, "unable to find default datacenter %q", params.datacenter)
}
session.datacenter = dc
session.Finder.SetDatacenter(dc)
}
session.datacenter = dc
session.Finder.SetDatacenter(dc)

// Cache the session.
sessionCache[sessionKey] = session
Expand Down

0 comments on commit 9673515

Please sign in to comment.