Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright 2025 Flant JSC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package watcher

import (
"fmt"

"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/source"

virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
)

func NewClusterVirtualImageWatcher() *CLusterVirtualImageWatcher {
return &CLusterVirtualImageWatcher{}
}

type CLusterVirtualImageWatcher struct{}

func (w *CLusterVirtualImageWatcher) Watch(mgr manager.Manager, ctr controller.Controller) error {
if err := ctr.Watch(
source.Kind(mgr.GetCache(), &virtv2.ClusterVirtualImage{}),
handler.EnqueueRequestsFromMapFunc(enqueueRequestsBlockDevice(mgr.GetClient(), virtv2.ClusterImageDevice)),
predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool { return true },
DeleteFunc: func(e event.DeleteEvent) bool { return true },
UpdateFunc: func(e event.UpdateEvent) bool {
oldCvi, oldOk := e.ObjectOld.(*virtv2.ClusterVirtualImage)
newCvi, newOk := e.ObjectNew.(*virtv2.ClusterVirtualImage)
if !oldOk || !newOk {
return false
}
return oldCvi.Status.Phase != newCvi.Status.Phase
},
},
); err != nil {
return fmt.Errorf("error setting watch on ClusterVirtualImage: %w", err)
}
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright 2025 Flant JSC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package watcher

import (
"fmt"

virtv1 "kubevirt.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/source"

"github.com/deckhouse/virtualization-controller/pkg/common/annotations"
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
)

func NewKVVMWatcher() *KVVMWatcher {
return &KVVMWatcher{}
}

type KVVMWatcher struct{}

func (w *KVVMWatcher) Watch(mgr manager.Manager, ctr controller.Controller) error {
if err := ctr.Watch(
source.Kind(mgr.GetCache(), &virtv1.VirtualMachine{}),
handler.EnqueueRequestForOwner(
mgr.GetScheme(),
mgr.GetRESTMapper(),
&virtv2.VirtualMachine{},
handler.OnlyControllerOwner(),
),
predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool { return true },
DeleteFunc: func(e event.DeleteEvent) bool { return true },
UpdateFunc: func(e event.UpdateEvent) bool {
oldVM := e.ObjectOld.(*virtv1.VirtualMachine)
newVM := e.ObjectNew.(*virtv1.VirtualMachine)
return oldVM.Status.PrintableStatus != newVM.Status.PrintableStatus ||
oldVM.Status.Ready != newVM.Status.Ready ||
oldVM.Annotations[annotations.AnnVmStartRequested] != newVM.Annotations[annotations.AnnVmStartRequested] ||
oldVM.Annotations[annotations.AnnVmRestartRequested] != newVM.Annotations[annotations.AnnVmRestartRequested]
},
},
); err != nil {
return fmt.Errorf("error setting watch on VirtualMachine: %w", err)
}
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
Copyright 2025 Flant JSC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package watcher

import (
"context"
"fmt"
"reflect"

"k8s.io/apimachinery/pkg/types"
virtv1 "kubevirt.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)

func NewKVVMIWatcher() *KVVMIWatcher {
return &KVVMIWatcher{}
}

type KVVMIWatcher struct{}

func (w *KVVMIWatcher) Watch(mgr manager.Manager, ctr controller.Controller) error {
// Subscribe on Kubevirt VirtualMachineInstances to update our VM status.
if err := ctr.Watch(
source.Kind(mgr.GetCache(), &virtv1.VirtualMachineInstance{}),
handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, vmi client.Object) []reconcile.Request {
return []reconcile.Request{
{
NamespacedName: types.NamespacedName{
Name: vmi.GetName(),
Namespace: vmi.GetNamespace(),
},
},
}
}),
predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool { return true },
DeleteFunc: func(e event.DeleteEvent) bool { return true },
UpdateFunc: func(e event.UpdateEvent) bool {
oldVM := e.ObjectOld.(*virtv1.VirtualMachineInstance)
newVM := e.ObjectNew.(*virtv1.VirtualMachineInstance)
return !reflect.DeepEqual(oldVM.Status, newVM.Status)
},
},
); err != nil {
return fmt.Errorf("error setting watch on VirtualMachine: %w", err)
}
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright 2025 Flant JSC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package watcher

import (
"context"
"fmt"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)

func NewPodWatcher() *PodWatcher {
return &PodWatcher{}
}

type PodWatcher struct{}

func (w *PodWatcher) Watch(mgr manager.Manager, ctr controller.Controller) error {
// Watch for Pods created on behalf of VMs. Handle only changes in status.phase.
// Pod tracking is required to detect when Pod becomes Completed after guest initiated reset or shutdown.
if err := ctr.Watch(
source.Kind(mgr.GetCache(), &corev1.Pod{}),
handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, pod client.Object) []reconcile.Request {
vmName, hasLabel := pod.GetLabels()["vm.kubevirt.io/name"]
if !hasLabel {
return nil
}

return []reconcile.Request{
{
NamespacedName: types.NamespacedName{
Name: vmName,
Namespace: pod.GetNamespace(),
},
},
}
}),
predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool { return true },
DeleteFunc: func(e event.DeleteEvent) bool { return true },
UpdateFunc: func(e event.UpdateEvent) bool {
oldPod := e.ObjectOld.(*corev1.Pod)
newPod := e.ObjectNew.(*corev1.Pod)
return oldPod.Status.Phase != newPod.Status.Phase
},
},
); err != nil {
return fmt.Errorf("error setting watch on Pod: %w", err)
}
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2025 Flant JSC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package watcher

import (
"fmt"

"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/source"

"github.com/deckhouse/virtualization-controller/pkg/controller/conditions"
virtv2 "github.com/deckhouse/virtualization/api/core/v1alpha2"
"github.com/deckhouse/virtualization/api/core/v1alpha2/vdcondition"
)

func NewVirtualDiskWatcher() *VirtualDiskWatcher {
return &VirtualDiskWatcher{}
}

type VirtualDiskWatcher struct{}

func (w *VirtualDiskWatcher) Watch(mgr manager.Manager, ctr controller.Controller) error {
if err := ctr.Watch(
source.Kind(mgr.GetCache(), &virtv2.VirtualDisk{}),
handler.EnqueueRequestsFromMapFunc(enqueueRequestsBlockDevice(mgr.GetClient(), virtv2.DiskDevice)),
predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool { return true },
DeleteFunc: func(e event.DeleteEvent) bool { return true },
UpdateFunc: func(e event.UpdateEvent) bool {
oldVd, oldOk := e.ObjectOld.(*virtv2.VirtualDisk)
newVd, newOk := e.ObjectNew.(*virtv2.VirtualDisk)
if !oldOk || !newOk {
return false
}

oldInUseCondition, _ := conditions.GetCondition(vdcondition.InUseType, oldVd.Status.Conditions)
newInUseCondition, _ := conditions.GetCondition(vdcondition.InUseType, newVd.Status.Conditions)

if oldVd.Status.Phase != newVd.Status.Phase || oldInUseCondition != newInUseCondition {
return true
}

return false
},
},
); err != nil {
return fmt.Errorf("error setting watch on VirtualDisk: %w", err)
}
return nil
}
Loading