Skip to content

Commit

Permalink
update VM phase when domain is shutoff
Browse files Browse the repository at this point in the history
  • Loading branch information
mpolednik committed Mar 28, 2017
1 parent 5b92716 commit 01b7cfb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cmd/virt-handler/virt-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func main() {
if err != nil {
panic(err)
}
domainStore, domainController := virthandler.NewDomainController(vmQueue, vmStore, domainSharedInformer)
domainStore, domainController := virthandler.NewDomainController(vmQueue, vmStore, domainSharedInformer, *restClient)

if err != nil {
panic(err)
Expand Down
41 changes: 36 additions & 5 deletions pkg/virt-handler/domain.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package virthandler

import (
kubeapi "k8s.io/client-go/pkg/api"
"k8s.io/client-go/pkg/util/workqueue"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"

"kubevirt.io/kubevirt/pkg/api/v1"
Expand All @@ -15,20 +17,22 @@ TODO: Define the exact scope of this controller.
For now it looks like we should use domain events to detect unexpected domain changes like crashes or vms going
into pause mode because of resource shortage or cut off connections to storage.
*/
func NewDomainController(vmQueue workqueue.RateLimitingInterface, vmStore cache.Store, informer cache.SharedInformer) (cache.Store, *kubecli.Controller) {
func NewDomainController(vmQueue workqueue.RateLimitingInterface, vmStore cache.Store, informer cache.SharedInformer, restClient rest.RESTClient) (cache.Store, *kubecli.Controller) {
queue := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
informer.AddEventHandler(kubecli.NewResourceEventHandlerFuncsForQorkqueue(queue))

dispatch := DomainDispatch{
vmQueue: vmQueue,
vmStore: vmStore,
vmQueue: vmQueue,
vmStore: vmStore,
restClient: restClient,
}
return kubecli.NewControllerFromInformer(informer.GetStore(), informer, queue, &dispatch)
}

type DomainDispatch struct {
vmQueue workqueue.RateLimitingInterface
vmStore cache.Store
vmQueue workqueue.RateLimitingInterface
vmStore cache.Store
restClient rest.RESTClient
}

func (d *DomainDispatch) Execute(indexer cache.Store, queue workqueue.RateLimitingInterface, key interface{}) {
Expand Down Expand Up @@ -56,6 +60,33 @@ func (d *DomainDispatch) Execute(indexer cache.Store, queue workqueue.RateLimiti
} else if !vmExists || obj.(*v1.VM).GetObjectMeta().GetUID() != domain.GetObjectMeta().GetUID() {
// The VM is not in the vm cache, or is a VM with a differend uuid, tell the VM controller to investigate it
d.vmQueue.Add(key)
} else {
err = setVmPhaseForStatusReason(domain, obj.(*v1.VM), d.restClient)
if err != nil {
queue.AddRateLimited(key)
}
}

return
}

func setVmPhaseForStatusReason(domain *virtwrap.Domain, vm *v1.VM, restClient rest.RESTClient) error {
flag := false
if domain.Status.Status == virtwrap.Shutoff {
switch domain.Status.Reason {
case virtwrap.ReasonFailed, virtwrap.ReasonCrashed:
vm.Status.Phase = v1.Failed
flag = true
case virtwrap.ReasonShutdown, virtwrap.ReasonDestroyed, virtwrap.ReasonMigrated, virtwrap.ReasonSaved, virtwrap.ReasonFromSnapshot:
vm.Status.Phase = v1.Succeeded
flag = true
}
}

if flag {
logging.DefaultLogger().Info().Object(vm).Msgf("Changing VM phase to %s", vm.Status.Phase)
return restClient.Put().Resource("vms").Body(vm).Name(vm.ObjectMeta.Name).Namespace(kubeapi.NamespaceDefault).Do().Error()
}

return nil
}
4 changes: 3 additions & 1 deletion pkg/virt-handler/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
. "github.com/onsi/gomega"
"k8s.io/client-go/pkg/types"
"k8s.io/client-go/pkg/util/workqueue"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/cache/testing"
"kubevirt.io/kubevirt/pkg/api/v1"
Expand All @@ -18,6 +19,7 @@ var _ = Describe("Domain", func() {
var fakeWatcher *framework.FakeControllerSource
var vmStore cache.Store
var vmQueue workqueue.RateLimitingInterface
var restClient rest.RESTClient

logging.DefaultLogger().SetIOWriter(GinkgoWriter)

Expand All @@ -28,7 +30,7 @@ var _ = Describe("Domain", func() {
vmStore = cache.NewStore(cache.DeletionHandlingMetaNamespaceKeyFunc)
vmQueue = workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
informer := cache.NewSharedInformer(fakeWatcher, &virtwrap.Domain{}, 0)
_, controller := NewDomainController(vmQueue, vmStore, informer)
_, controller := NewDomainController(vmQueue, vmStore, informer, restClient)
controller.StartInformer(stopChan)
controller.WaitForSync(stopChan)
go controller.Run(1, stopChan)
Expand Down

0 comments on commit 01b7cfb

Please sign in to comment.