Skip to content

Commit

Permalink
Merge pull request #1365 from srm09/backport/datacenter-defaulting-0.7
Browse files Browse the repository at this point in the history
Backport: move datacenter defaulting to resource defaults
  • Loading branch information
k8s-ci-robot authored Jan 27, 2022
2 parents acb1c4f + 530e08b commit 7e0499b
Show file tree
Hide file tree
Showing 15 changed files with 76 additions and 17 deletions.
1 change: 1 addition & 0 deletions api/v1alpha3/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type VirtualMachineCloneSpec struct {

// Datacenter is the name or inventory path of the datacenter in which the
// virtual machine is created/located.
// Defaults to * which selects the default datacenter.
// +optional
Datacenter string `json:"datacenter,omitempty"`

Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha3/vspherefailuredomain_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ type FailureDomain struct {
}

type Topology struct {
// The underlying infrastructure for this failure domain
// Datacenter as the failure domain
Datacenter string `json:"datacenter"`

Expand Down Expand Up @@ -90,6 +89,7 @@ type FailureDomainHosts struct {
// +kubebuilder:object:root=true
// +kubebuilder:resource:path=vspherefailuredomains,scope=Cluster,categories=cluster-api
// +kubebuilder:storageversion

// VSphereFailureDomain is the Schema for the vspherefailuredomains API
type VSphereFailureDomain struct {
metav1.TypeMeta `json:",inline"`
Expand Down
12 changes: 12 additions & 0 deletions api/v1alpha3/vspherefailuredomain_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ import (
"k8s.io/utils/pointer"
)

//nolint
func TestVsphereFailureDomain_Default(t *testing.T) {
g := NewWithT(t)
m := &VSphereFailureDomain{
Spec: VSphereFailureDomainSpec{},
}
m.Default()

g.Expect(*m.Spec.Zone.AutoConfigure).To(BeFalse())
g.Expect(*m.Spec.Region.AutoConfigure).To(BeFalse())
}

func TestVSphereFailureDomain_ValidateCreate(t *testing.T) {
g := NewWithT(t)

Expand Down
11 changes: 11 additions & 0 deletions api/v1alpha3/vspheremachine_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)

func (r *VSphereMachine) SetupWebhookWithManager(mgr ctrl.Manager) error {
Expand All @@ -35,6 +36,16 @@ func (r *VSphereMachine) SetupWebhookWithManager(mgr ctrl.Manager) error {
}

// +kubebuilder:webhook:verbs=create;update,path=/validate-infrastructure-cluster-x-k8s-io-v1alpha3-vspheremachine,mutating=false,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=vspheremachines,versions=v1alpha3,name=validation.vspheremachine.infrastructure.x-k8s.io,sideEffects=None
// +kubebuilder:webhook:verbs=create;update,path=/mutate-infrastructure-cluster-x-k8s-io-v1alpha3-vspheremachine,mutating=true,failurePolicy=fail,matchPolicy=Equivalent,groups=infrastructure.cluster.x-k8s.io,resources=vspheremachines,versions=v1alpha3,name=default.vspheremachine.infrastructure.cluster.x-k8s.io,sideEffects=None

var _ webhook.Validator = &VSphereMachine{}
var _ webhook.Defaulter = &VSphereMachine{}

func (r *VSphereMachine) Default() {
if r.Spec.Datacenter == "" {
r.Spec.Datacenter = "*"
}
}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *VSphereMachine) ValidateCreate() error {
Expand Down
11 changes: 11 additions & 0 deletions api/v1alpha3/vspheremachine_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ var (
someProviderID = "vsphere://42305f0b-dad7-1d3d-5727-0eaffffffffc"
)

//nolint
func TestVsphereMachine_Default(t *testing.T) {
g := NewWithT(t)
m := &VSphereMachine{
Spec: VSphereMachineSpec{},
}
m.Default()

g.Expect(m.Spec.Datacenter).To(Equal("*"))
}

//nolint
func TestVSphereMachine_ValidateCreate(t *testing.T) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ spec:
type: object
datacenter:
description: Datacenter is the name or inventory path of the datacenter
in which the virtual machine is created/located.
in which the virtual machine is created/located. Defaults to
* which selects the default datacenter.
type: string
datastore:
description: Datastore is the name or inventory path of the datastore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ spec:
description: ComputeCluster as the failure domain
type: string
datacenter:
description: The underlying infrastructure for this failure domain
Datacenter as the failure domain
description: Datacenter as the failure domain
type: string
datastore:
description: Datastore is the name or inventory path of the datastore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ spec:
type: object
datacenter:
description: Datacenter is the name or inventory path of the datacenter
in which the virtual machine is created/located.
in which the virtual machine is created/located. Defaults to * which
selects the default datacenter.
type: string
datastore:
description: Datastore is the name or inventory path of the datastore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ spec:
datacenter:
description: Datacenter is the name or inventory path of the
datacenter in which the virtual machine is created/located.
Defaults to * which selects the default datacenter.
type: string
datastore:
description: Datastore is the name or inventory path of the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ spec:
type: object
datacenter:
description: Datacenter is the name or inventory path of the datacenter
in which the virtual machine is created/located.
in which the virtual machine is created/located. Defaults to * which
selects the default datacenter.
type: string
datastore:
description: Datastore is the name or inventory path of the datastore
Expand Down
20 changes: 20 additions & 0 deletions config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ webhooks:
resources:
- vspherefailuredomains
sideEffects: None
- clientConfig:
caBundle: Cg==
service:
name: webhook-service
namespace: system
path: /mutate-infrastructure-cluster-x-k8s-io-v1alpha3-vspheremachine
failurePolicy: Fail
matchPolicy: Equivalent
name: default.vspheremachine.infrastructure.cluster.x-k8s.io
rules:
- apiGroups:
- infrastructure.cluster.x-k8s.io
apiVersions:
- v1alpha3
operations:
- CREATE
- UPDATE
resources:
- vspheremachines
sideEffects: None

---
apiVersion: admissionregistration.k8s.io/v1beta1
Expand Down
6 changes: 3 additions & 3 deletions controllers/vspheredeploymentzone_controller_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func ForComputeClusterZone(t *testing.T) {

controllerCtx := fake.NewControllerContext(mgmtContext)

authSession, err := session.GetOrCreate(controllerCtx, logr.DiscardLogger{}, simr.ServerURL().Host, "",
authSession, err := session.GetOrCreate(controllerCtx, logr.DiscardLogger{}, simr.ServerURL().Host, "*",
simr.Username(), simr.Password(), "")
g.Expect(err).NotTo(HaveOccurred())

Expand Down Expand Up @@ -142,7 +142,7 @@ func ForHostGroupZone(t *testing.T) {

controllerCtx := fake.NewControllerContext(mgmtContext)

authSession, err := session.GetOrCreate(controllerCtx, logr.DiscardLogger{}, simr.ServerURL().Host, "",
authSession, err := session.GetOrCreate(controllerCtx, logr.DiscardLogger{}, simr.ServerURL().Host, "*",
simr.Username(), simr.Password(), "")
g.Expect(err).NotTo(HaveOccurred())

Expand Down Expand Up @@ -211,7 +211,7 @@ func TestVsphereDeploymentZoneReconciler_Reconcile_CreateAndAttachMetadata(t *te
mgmtContext.Password = simr.Password()

controllerCtx := fake.NewControllerContext(mgmtContext)
authSession, err := session.GetOrCreate(controllerCtx, logr.DiscardLogger{}, simr.ServerURL().Host, "",
authSession, err := session.GetOrCreate(controllerCtx, logr.DiscardLogger{}, simr.ServerURL().Host, "*",
simr.Username(), simr.Password(), "")
NewWithT(t).Expect(err).NotTo(HaveOccurred())

Expand Down
2 changes: 1 addition & 1 deletion pkg/services/govmomi/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestCreate(t *testing.T) {
authSession, err := session.GetOrCreate(
vmContext,
logr.DiscardLogger{},
vmContext.VSphereVM.Spec.Server, "",
vmContext.VSphereVM.Spec.Server, "*",
simr.Username(), simr.Password(), "")
if err != nil {
t.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/govmomi/vcenter/clone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func initSimulator(t *testing.T) (*simulator.Model, *session.Session, *simulator
authSession, err := session.GetOrCreate(
ctx.TODO(),
logr.DiscardLogger{},
server.URL.Host, "",
server.URL.Host, "*",
server.URL.User.Username(), pass, "")
if err != nil {
t.Fatal(err)
Expand Down
13 changes: 7 additions & 6 deletions pkg/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,14 @@ func GetOrCreate(
session.TagManager = manager

// Assign the datacenter if one was specified.
dc, err := session.Finder.DatacenterOrDefault(ctx, datacenter)
if err != nil {
return nil, errors.Wrapf(err, "unable to find datacenter %q", datacenter)
if datacenter != "" {
dc, err := session.Finder.Datacenter(ctx, datacenter)
if err != nil {
return nil, errors.Wrapf(err, "unable to find datacenter %q", 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 7e0499b

Please sign in to comment.