Skip to content

Commit a455906

Browse files
authored
Merge pull request kubernetes-sigs#668 from akutz/feature/rm-moref
Replace managed object reference with provider ID
2 parents 1765fab + 9ca6819 commit a455906

15 files changed

+416
-311
lines changed

api/v1alpha2/types.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,6 @@ type VirtualMachine struct {
210210
// BiosUUID is the VM's BIOS UUID.
211211
BiosUUID string `json:"biosUUID"`
212212

213-
// InstanceUUID is the VM's instance UUID.
214-
InstanceUUID string `json:"instanceUUID"`
215-
216213
// State is the VM's state.
217214
State VirtualMachineState `json:"state"`
218215

api/v1alpha2/vspheremachine_types.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@ type VSphereMachineSpec struct {
3636
// +optional
3737
ProviderID *string `json:"providerID,omitempty"`
3838

39-
// This value is set automatically at runtime and should not be set or
40-
// modified by users.
41-
// MachineRef is used to lookup the VM.
42-
// +optional
43-
MachineRef string `json:"machineRef,omitempty"`
44-
4539
// Template is the name, inventory path, or instance UUID of the template
4640
// used to clone new machines.
4741
Template string `json:"template"`

config/crd/bases/infrastructure.cluster.x-k8s.io_vspheremachines.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,6 @@ spec:
4444
this machine is cloned.
4545
format: int32
4646
type: integer
47-
machineRef:
48-
description: This value is set automatically at runtime and should not
49-
be set or modified by users. MachineRef is used to lookup the VM.
50-
type: string
5147
memoryMiB:
5248
description: MemoryMiB is the size of a virtual machine's memory, in
5349
MiB. Defaults to the analogue property value in the template from

config/crd/bases/infrastructure.cluster.x-k8s.io_vspheremachinetemplates.yaml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,6 @@ spec:
159159
from which this machine is cloned.
160160
format: int32
161161
type: integer
162-
machineRef:
163-
description: This value is set automatically at runtime and
164-
should not be set or modified by users. MachineRef is used
165-
to lookup the VM.
166-
type: string
167162
memoryMiB:
168163
description: MemoryMiB is the size of a virtual machine's memory,
169164
in MiB. Defaults to the analogue property value in the template

controllers/vspheremachine_controller.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/context"
4040
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/services"
4141
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/services/govmomi"
42+
infrautilv1 "sigs.k8s.io/cluster-api-provider-vsphere/pkg/util"
4243
)
4344

4445
// VSphereMachineReconciler reconciles a VSphereMachine object
@@ -270,7 +271,10 @@ func (r *VSphereMachineReconciler) reconcileNetwork(ctx *context.MachineContext,
270271
}
271272

272273
func (r *VSphereMachineReconciler) reconcileProviderID(ctx *context.MachineContext, vm infrav1.VirtualMachine, vmService services.VirtualMachineService) error {
273-
providerID := fmt.Sprintf("vsphere://%s", vm.BiosUUID)
274+
providerID := infrautilv1.ConvertUUIDToProviderID(vm.BiosUUID)
275+
if providerID == "" {
276+
return errors.Errorf("invalid BIOS UUID %s for %s", vm.BiosUUID, ctx)
277+
}
274278
if ctx.VSphereMachine.Spec.ProviderID == nil || *ctx.VSphereMachine.Spec.ProviderID != providerID {
275279
ctx.VSphereMachine.Spec.ProviderID = &providerID
276280
ctx.Logger.V(6).Info("updated provider ID", "provider-id", providerID)

docs/design/machine-controller-reconcile.puml

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,31 @@ start;
77
repeat
88
:Reconcile;
99

10-
if (Machine has a TaskRef) then (yes)
11-
if (Task exists) then (yes)
12-
if (Task is running/pending) then (yes)
13-
#LightGreen:Requeue;
14-
elseif (Task is success/failed) then (yes)
15-
#edf7de:Remove TaskRef from Machine;
16-
endif
17-
else (no)
18-
#edf7de:Remove TaskRef from Machine;
10+
if (Machine has DeletionTimestamp) then (yes)
11+
if (Machine has in-flight task) then (yes)
1912
#LightGreen:Requeue;
20-
endif
21-
endif
22-
if (Is requeued or in error) then (no)
23-
if (Machine has a MachineRef) then (no)
24-
if (Can find VM by InstanceUUID) then (yes)
25-
#edf7de:Assign MachineRef to Machine;
26-
endif
27-
endif
28-
if (Machine has a MachineRef) then (no)
29-
if (Can find VM by MachineRef) then (no)
30-
#edf7de:Remove MachineRef from Machine;
13+
else (no)
14+
if (Machine has a ProviderID) then (yes)
15+
#edf7de:Find VM by BIOS UUID;
16+
else (no)
17+
#edf7de:Find VM by instance UUID;
3118
endif
32-
endif
33-
if (Machine has DeletionTimestamp) then (yes)
34-
if (Machine has a MachineRef) then (yes)
19+
if (VM exists) then (yes)
3520
#edf7de:Delete VM;
3621
#edf7de:Assign delete TaskRef to Machine;
3722
#LightGreen:Requeue;
3823
endif
24+
endif
25+
else (no)
26+
if (Machine has in-flight task) then (yes)
27+
#LightGreen:Requeue;
3928
else (no)
40-
if (Machine has a MachineRef) then (yes)
29+
if (Machine has a ProviderID) then (yes)
30+
#edf7de:Find VM by BIOS UUID;
31+
else (no)
32+
#edf7de:Find VM by instance UUID;
33+
endif
34+
if (VM exists) then (yes)
4135
if (VM metadata matches calculated metadata) then (no)
4236
#edf7de:Reconfigure VM with calculated metadata;
4337
#edf7de:Assign reconfigure TaskRef to Machine;

docs/design/machine-controller-reconcile.svg

Lines changed: 21 additions & 27 deletions
Loading

hack/release.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ function build_images() {
103103
MANAGER_IMAGE_NAME=${CAPV_MANAGER_IMAGE_RELEASE}
104104
MANIFESTS_IMAGE_NAME=${CAPV_MANIFESTS_IMAGE_RELEASE}
105105
;;
106+
*)
107+
# A local dev build
108+
MANAGER_IMAGE_NAME="${BUILD_RELEASE_TYPE}-manager"
109+
MANIFESTS_IMAGE_NAME="${BUILD_RELEASE_TYPE}-manifests"
106110
esac
107111

108112
# Manager image
@@ -212,7 +216,7 @@ case "${BUILD_RELEASE_TYPE}" in
212216
# do nothing
213217
;;
214218
*)
215-
fatal "invalid BUILD_RELEASE_TYPE: ${BUILD_RELEASE_TYPE}"
219+
echo "custom BUILD_RELEASE_TYPE: ${BUILD_RELEASE_TYPE}"
216220
;;
217221
esac
218222

pkg/context/session.go

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,28 +93,26 @@ func getOrCreateCachedSession(ctx *MachineContext) (*Session, error) {
9393
return &session, nil
9494
}
9595

96+
// FindByBIOSUUID finds an object by its BIOS UUID.
97+
//
98+
// To avoid comments about this function's name, please see the Golang
99+
// WIKI https://github.com/golang/go/wiki/CodeReviewComments#initialisms.
100+
// This function is named in accordance with the example "XMLHTTP".
101+
func (s *Session) FindByBIOSUUID(ctx context.Context, uuid string) (object.Reference, error) {
102+
return s.findByUUID(ctx, uuid, false)
103+
}
104+
96105
// FindByInstanceUUID finds an object by its instance UUID.
97106
func (s *Session) FindByInstanceUUID(ctx context.Context, uuid string) (object.Reference, error) {
98-
if s.Client == nil {
99-
return nil, errors.New("vSphere client is not initialized")
100-
}
101-
si := object.NewSearchIndex(s.Client.Client)
102-
findFlag := true
103-
ref, err := si.FindByUuid(ctx, s.datacenter, uuid, true, &findFlag)
104-
if err != nil {
105-
return nil, errors.Wrapf(err, "error finding object by instance uuid %q", uuid)
106-
}
107-
return ref, nil
107+
return s.findByUUID(ctx, uuid, true)
108108
}
109109

110-
// FindByUUID finds an object by its UUID.
111-
func (s *Session) FindByUUID(ctx context.Context, uuid string) (object.Reference, error) {
110+
func (s *Session) findByUUID(ctx context.Context, uuid string, findByInstanceUUID bool) (object.Reference, error) {
112111
if s.Client == nil {
113112
return nil, errors.New("vSphere client is not initialized")
114113
}
115114
si := object.NewSearchIndex(s.Client.Client)
116-
findFlag := false
117-
ref, err := si.FindByUuid(ctx, s.datacenter, uuid, true, &findFlag)
115+
ref, err := si.FindByUuid(ctx, s.datacenter, uuid, true, &findByInstanceUUID)
118116
if err != nil {
119117
return nil, errors.Wrapf(err, "error finding object by uuid %q", uuid)
120118
}

pkg/services/govmomi/context.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package govmomi
18+
19+
import (
20+
"github.com/vmware/govmomi/object"
21+
"github.com/vmware/govmomi/vim25/types"
22+
23+
infrav1 "sigs.k8s.io/cluster-api-provider-vsphere/api/v1alpha2"
24+
"sigs.k8s.io/cluster-api-provider-vsphere/pkg/context"
25+
)
26+
27+
type virtualMachineContext struct {
28+
context.MachineContext
29+
Ref types.ManagedObjectReference
30+
Obj *object.VirtualMachine
31+
State *infrav1.VirtualMachine
32+
}

pkg/services/govmomi/errors.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package govmomi
18+
19+
import "fmt"
20+
21+
// errNotFound is returned by the findVM function when a VM is not found.
22+
type errNotFound struct {
23+
instanceUUID bool
24+
uuid string
25+
}
26+
27+
func (e errNotFound) Error() string {
28+
if e.instanceUUID {
29+
return fmt.Sprintf("vm with instance uuid %s not found", e.uuid)
30+
}
31+
return fmt.Sprintf("vm with bios uuid %s not found", e.uuid)
32+
}
33+
34+
func isNotFound(err error) bool {
35+
switch err.(type) {
36+
case errNotFound, *errNotFound:
37+
return true
38+
default:
39+
return false
40+
}
41+
}

0 commit comments

Comments
 (0)