Skip to content

Commit

Permalink
use status updater to abstract enable/disable of VM status subresource
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Henriksen <[email protected]>
  • Loading branch information
mhenriks committed Aug 17, 2020
1 parent 77d5257 commit fd7f96d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
24 changes: 13 additions & 11 deletions pkg/virt-controller/watch/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"kubevirt.io/client-go/kubecli"
"kubevirt.io/client-go/log"
"kubevirt.io/kubevirt/pkg/controller"
"kubevirt.io/kubevirt/pkg/util/status"
)

const (
Expand Down Expand Up @@ -62,9 +63,10 @@ type snapshotSource interface {
}

type vmSnapshotSource struct {
client kubecli.KubevirtClient
vm *kubevirtv1.VirtualMachine
snapshot *snapshotv1.VirtualMachineSnapshot
client kubecli.KubevirtClient
vmStatusUpdater *status.VMStatusUpdater
vm *kubevirtv1.VirtualMachine
snapshot *snapshotv1.VirtualMachineSnapshot
}

func cacheKeyFunc(namespace, name string) string {
Expand Down Expand Up @@ -362,9 +364,10 @@ func (ctrl *SnapshotController) getSnapshotSource(vmSnapshot *snapshotv1.Virtual
}

return &vmSnapshotSource{
client: ctrl.client,
vm: vm,
snapshot: vmSnapshot,
client: ctrl.client,
vmStatusUpdater: ctrl.vmStatusUpdater,
vm: vm,
snapshot: vmSnapshot,
}, nil
}

Expand Down Expand Up @@ -691,10 +694,9 @@ func (s *vmSnapshotSource) Lock() (bool, error) {

if vmCopy.Status.SnapshotInProgress == nil {
vmCopy.Status.SnapshotInProgress = &s.snapshot.Name
vmCopy, err = s.client.VirtualMachine(vmCopy.Namespace).UpdateStatus(vmCopy)
if err != nil {
return false, err
}
// unfortunately, status updater does not return the updated resource
// but the controller is watching VMs so will get notified
return false, s.vmStatusUpdater.UpdateStatus(vmCopy)
}

if !controller.HasFinalizer(vmCopy, sourceFinalizer) {
Expand Down Expand Up @@ -725,7 +727,7 @@ func (s *vmSnapshotSource) Unlock() error {
}

vmCopy.Status.SnapshotInProgress = nil
_, err = s.client.VirtualMachine(vmCopy.Namespace).UpdateStatus(vmCopy)
err = s.vmStatusUpdater.UpdateStatus(vmCopy)
if err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/virt-controller/watch/snapshot_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"kubevirt.io/client-go/kubecli"
"kubevirt.io/client-go/log"
"kubevirt.io/kubevirt/pkg/controller"
"kubevirt.io/kubevirt/pkg/util/status"
)

const (
Expand Down Expand Up @@ -78,6 +79,8 @@ type SnapshotController struct {
recorder record.EventRecorder

resyncPeriod time.Duration

vmStatusUpdater *status.VMStatusUpdater
}

// NewSnapshotController creates a new SnapshotController
Expand Down Expand Up @@ -106,6 +109,7 @@ func NewSnapshotController(
crdInformer: crdInformer,
recorder: recorder,
resyncPeriod: resyncPeriod,
vmStatusUpdater: status.NewVMStatusUpdater(client),
}

ctrl.dynamicInformerMap = map[string]*dynamicInformer{
Expand Down

0 comments on commit fd7f96d

Please sign in to comment.