Skip to content

Commit

Permalink
auto detect kubevirt install (#4791)
Browse files Browse the repository at this point in the history
* auto detect kubevirt install

---------

Signed-off-by: clyi <[email protected]>
  • Loading branch information
changluyi authored Dec 6, 2024
1 parent 6b6bf2d commit 322fb8e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
15 changes: 3 additions & 12 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ type Controller struct {
vmiMigrationSynced cache.InformerSynced
addOrUpdateVMIMigrationQueue workqueue.TypedRateLimitingInterface[string]
kubevirtInformerFactory kubevirtController.KubeInformerFactory
hasKubevirtVMIMigration bool

recorder record.EventRecorder
informerFactory kubeinformers.SharedInformerFactory
Expand Down Expand Up @@ -638,11 +637,6 @@ func Run(ctx context.Context, config *Configuration) {
controller.kubeovnInformerFactory.Start(ctx.Done())
controller.anpInformerFactory.Start(ctx.Done())

controller.hasKubevirtVMIMigration = controller.isVMIMigrationCRDInstalled()
if controller.config.EnableLiveMigrationOptimize && controller.hasKubevirtVMIMigration {
kubevirtInformerFactory.Start(ctx.Done())
}

klog.Info("Waiting for informer caches to sync")
cacheSyncs := []cache.InformerSynced{
controller.vpcNatGatewaySynced, controller.vpcEgressGatewaySynced,
Expand All @@ -664,10 +658,6 @@ func Run(ctx context.Context, config *Configuration) {
cacheSyncs = append(cacheSyncs, controller.anpsSynced, controller.banpsSynced)
}

if controller.config.EnableLiveMigrationOptimize && controller.hasKubevirtVMIMigration {
cacheSyncs = append(cacheSyncs, controller.vmiMigrationSynced)
}

if !cache.WaitForCacheSync(ctx.Done(), cacheSyncs...) {
util.LogFatalAndExit(nil, "failed to wait for caches to sync")
}
Expand Down Expand Up @@ -921,13 +911,14 @@ func Run(ctx context.Context, config *Configuration) {
}
}

if config.EnableLiveMigrationOptimize && controller.hasKubevirtVMIMigration {
if config.EnableLiveMigrationOptimize {
if _, err = vmiMigrationInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: controller.enqueueAddVMIMigration,
UpdateFunc: controller.enqueueUpdateVMIMigration,
}); err != nil {
util.LogFatalAndExit(err, "failed to add VMI Migration event handler")
}
controller.StartMigrationInformerFactory(ctx, kubevirtInformerFactory)
}

controller.Run(ctx)
Expand Down Expand Up @@ -1335,7 +1326,7 @@ func (c *Controller) startWorkers(ctx context.Context) {
go wait.Until(runWorker("delete base admin network policy", c.deleteBanpQueue, c.handleDeleteBanp), time.Second, ctx.Done())
}

if c.config.EnableLiveMigrationOptimize && c.hasKubevirtVMIMigration {
if c.config.EnableLiveMigrationOptimize {
go wait.Until(runWorker("add/update vmiMigration ", c.addOrUpdateVMIMigrationQueue, c.handleAddOrUpdateVMIMigration), 50*time.Millisecond, ctx.Done())
}
}
Expand Down
27 changes: 26 additions & 1 deletion pkg/controller/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import (
"context"
"fmt"
"reflect"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
kubevirtv1 "kubevirt.io/api/core/v1"
kubevirtController "kubevirt.io/kubevirt/pkg/controller"

"github.com/kubeovn/kube-ovn/pkg/ovs"
"github.com/kubeovn/kube-ovn/pkg/util"
)

func (c *Controller) enqueueAddVMIMigration(obj interface{}) {
Expand Down Expand Up @@ -138,6 +141,28 @@ func (c *Controller) isVMIMigrationCRDInstalled() bool {
if err != nil {
return false
}
klog.Info("Detect VMI Migration CRD")
klog.Info("Found KubeVirt VMI Migration CRD")
return true
}

func (c *Controller) StartMigrationInformerFactory(ctx context.Context, kubevirtInformerFactory kubevirtController.KubeInformerFactory) {
ticker := time.NewTicker(10 * time.Second)
go func() {
defer ticker.Stop()
for {
select {
case <-ticker.C:
if c.isVMIMigrationCRDInstalled() {
klog.Info("Start VMI migration informer")
kubevirtInformerFactory.Start(ctx.Done())
if !cache.WaitForCacheSync(ctx.Done(), c.vmiMigrationSynced) {
util.LogFatalAndExit(nil, "failed to wait for vmi migration caches to sync")
}
return
}
case <-ctx.Done():
return
}
}
}()
}
2 changes: 0 additions & 2 deletions pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,6 @@ func (c *Controller) reconcileAllocateSubnets(cachedPod, pod *v1.Pod, needAlloca

var err error
var vmKey string
// var isMigrate, migrated, migratedFail bool
// var vmKey, srcNodeName, targetNodeName string
if isVMPod && c.config.EnableKeepVMIP {
vmKey = fmt.Sprintf("%s/%s", namespace, vmName)
}
Expand Down

0 comments on commit 322fb8e

Please sign in to comment.