From b973b6da2d32de0a1894b732b93d3027bd78eb7c Mon Sep 17 00:00:00 2001 From: clyi Date: Thu, 28 Nov 2024 13:44:51 +0800 Subject: [PATCH] fix code format Signed-off-by: clyi --- pkg/controller/kubevirt.go | 15 +++++--- pkg/controller/pod.go | 79 -------------------------------------- 2 files changed, 10 insertions(+), 84 deletions(-) diff --git a/pkg/controller/kubevirt.go b/pkg/controller/kubevirt.go index ab4d2d73b11..faac39946ca 100644 --- a/pkg/controller/kubevirt.go +++ b/pkg/controller/kubevirt.go @@ -5,13 +5,13 @@ import ( "fmt" "reflect" + 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" "github.com/kubeovn/kube-ovn/pkg/ovs" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - utilruntime "k8s.io/apimachinery/pkg/util/runtime" ) func (c *Controller) enqueueAddVMIMigration(obj interface{}) { @@ -54,7 +54,7 @@ func (c *Controller) handleAddOrUpdateVMIMigration(key string) error { vmiMigration, err := c.config.KubevirtClient.VirtualMachineInstanceMigration(namespace).Get(context.TODO(), name, metav1.GetOptions{}) if err != nil { - utilruntime.HandleError(fmt.Errorf("failed to get VMI migration by key %s: %v", key, err)) + utilruntime.HandleError(fmt.Errorf("failed to get VMI migration by key %s: %w", key, err)) return err } if vmiMigration.Status.MigrationState == nil { @@ -64,7 +64,7 @@ func (c *Controller) handleAddOrUpdateVMIMigration(key string) error { vmi, err := c.config.KubevirtClient.VirtualMachineInstance(namespace).Get(context.TODO(), vmiMigration.Spec.VMIName, metav1.GetOptions{}) if err != nil { - utilruntime.HandleError(fmt.Errorf("failed to get VMI by name %s: %v", vmiMigration.Spec.VMIName, err)) + utilruntime.HandleError(fmt.Errorf("failed to get VMI by name %s: %w", vmiMigration.Spec.VMIName, err)) return err } @@ -88,9 +88,14 @@ func (c *Controller) handleAddOrUpdateVMIMigration(key string) error { sourcePodName := vmi.Status.MigrationState.SourcePod sourcePod, err := c.config.KubeClient.CoreV1().Pods(namespace).Get(context.TODO(), sourcePodName, metav1.GetOptions{}) + if err != nil { + klog.Errorf("failed to get source pod %s, %w", sourcePodName, err) + return err + } + podNets, err := c.getPodKubeovnNets(sourcePod) if err != nil { - klog.Errorf("failed to get pod nets %v", err) + klog.Errorf("failed to get pod nets %w", err) return err } diff --git a/pkg/controller/pod.go b/pkg/controller/pod.go index 8571b03b494..2234b2f7be7 100644 --- a/pkg/controller/pod.go +++ b/pkg/controller/pod.go @@ -518,10 +518,6 @@ func (c *Controller) reconcileAllocateSubnets(cachedPod, pod *v1.Pod, needAlloca // var vmKey, srcNodeName, targetNodeName string if isVMPod && c.config.EnableKeepVMIP { vmKey = fmt.Sprintf("%s/%s", namespace, vmName) - // if isMigrate, migrated, migratedFail, srcNodeName, targetNodeName, err = c.migrateVM(pod, vmKey); err != nil { - // klog.Error(err) - // return nil, err - // } } // Avoid create lsp for already running pod in ovn-nb when controller restart for _, podNet := range needAllocatePodNets { @@ -605,24 +601,6 @@ func (c *Controller) reconcileAllocateSubnets(cachedPod, pod *v1.Pod, needAlloca return nil, err } - // if isMigrate { - // if migrated { - // klog.Infof("migrate end reset options for lsp %s from %s to %s, migrated fail: %t", portName, srcNodeName, targetNodeName, migratedFail) - // if err := c.OVNNbClient.ResetLogicalSwitchPortMigrateOptions(portName, srcNodeName, targetNodeName, migratedFail); err != nil { - // err = fmt.Errorf("failed to clean migrate options for lsp %s, %w", portName, err) - // klog.Error(err) - // return nil, err - // } - // } else { - // klog.Infof("migrate start set options for lsp %s from %s to %s", portName, srcNodeName, targetNodeName) - // if err := c.OVNNbClient.SetLogicalSwitchPortMigrateOptions(portName, srcNodeName, targetNodeName); err != nil { - // err = fmt.Errorf("failed to set migrate options for lsp %s, %w", portName, err) - // klog.Error(err) - // return nil, err - // } - // } - // } - if pod.Annotations[fmt.Sprintf(util.Layer2ForwardAnnotationTemplate, podNet.ProviderName)] == "true" { if err := c.OVNNbClient.EnablePortLayer2forward(portName); err != nil { c.recorder.Eventf(pod, v1.EventTypeWarning, "SetOVNPortL2ForwardFailed", err.Error()) @@ -2098,63 +2076,6 @@ func (c *Controller) getVirtualIPs(pod *v1.Pod, podNets []*kubeovnNet) map[strin return vipsMap } -// migrate vm return migrate, migrated, fail, src node, target node, err -func (c *Controller) migrateVM(pod *v1.Pod, vmKey string) (bool, bool, bool, string, string, error) { - // try optimize vm migration, no need return error - // migrate true means need ovn set migrate options - // migrated ok means need set migrate options to target node - // migrated failed means need set migrate options to source node - if _, ok := pod.Annotations[util.MigrationJobAnnotation]; !ok { - return false, false, false, "", "", nil - } - if _, ok := pod.Annotations[util.MigrationSourceAnnotation]; ok { - klog.Infof("will migrate out vm %s pod %s from source node %s", vmKey, pod.Name, pod.Spec.NodeName) - return false, false, false, "", "", nil - } - // ovn set migrator only in the process of target vm pod - if _, ok := pod.Annotations[util.MigrationTargetAnnotation]; !ok { - return false, false, false, "", "", nil - } - srcNode, ok := pod.Annotations[util.MigrationSourceNodeAnnotation] - if !ok || srcNode == "" { - err := fmt.Errorf("vm %s migration source node is not set", vmKey) - klog.Warning(err) - return false, false, false, "", "", nil - } - targetNode := pod.Spec.NodeName - if targetNode == "" { - err := fmt.Errorf("vm %s migration target node is not set", vmKey) - klog.Warning(err) - return false, false, false, "", "", nil - } - migratePhase, ok := pod.Annotations[util.MigrationPhaseAnnotation] - if !ok { - err := fmt.Errorf("vm %s migration phase is not set", vmKey) - klog.Warning(err) - return false, false, false, "", "", nil - } - // check migrate phase - if migratePhase == "" { - err := fmt.Errorf("vm %s migration phase is empty", vmKey) - klog.Warning(err) - return false, false, false, "", "", nil - } - if migratePhase == util.MigrationPhaseStarted { - klog.Infof("start to migrate src vm %s from %s to %s", vmKey, srcNode, targetNode) - return true, false, false, srcNode, targetNode, nil - } - if migratePhase == util.MigrationPhaseSucceeded { - klog.Infof("succeed to migrate src vm %s from %s to %s", vmKey, srcNode, targetNode) - return true, true, false, srcNode, targetNode, nil - } - if migratePhase == util.MigrationPhaseFailed { - klog.Infof("failed to migrate src vm %s from %s to %s", vmKey, srcNode, targetNode) - return true, true, true, srcNode, targetNode, nil - } - - return false, false, false, "", "", nil -} - func setPodRoutesAnnotation(annotations map[string]string, provider string, routes []request.Route) error { key := fmt.Sprintf(util.RoutesAnnotationTemplate, provider) if len(routes) == 0 {