Skip to content

Commit

Permalink
delete the NIC regardless of whether the Pod was found or not.
Browse files Browse the repository at this point in the history
  • Loading branch information
liyh-yusur committed Sep 10, 2024
1 parent 2020fd9 commit 77cfca9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 62 deletions.
123 changes: 67 additions & 56 deletions pkg/daemon/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/emicklei/go-restful/v3"
"k8s.io/klog/v2"
"net"
"net/http"
"strconv"
"strings"
"time"

"github.com/emicklei/go-restful/v3"
v1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"

kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
clientset "github.com/kubeovn/kube-ovn/pkg/client/clientset/versioned"
"github.com/kubeovn/kube-ovn/pkg/ovs"
"github.com/kubeovn/kube-ovn/pkg/request"
"github.com/kubeovn/kube-ovn/pkg/util"
v1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes"
)

const (
Expand Down Expand Up @@ -426,14 +425,10 @@ func (csh cniServerHandler) handleDel(req *restful.Request, resp *restful.Respon
return
}

// Try to get the Pod, but if it fails due to not being found, log a warning and continue.
pod, err := csh.Controller.podsLister.Pods(podRequest.PodNamespace).Get(podRequest.PodName)
if err != nil {
if k8serrors.IsNotFound(err) {
resp.WriteHeader(http.StatusNoContent)
return
}

errMsg := fmt.Errorf("parse del request failed %w", err)
if err != nil && !k8serrors.IsNotFound(err) {
errMsg := fmt.Errorf("failed to retrieve Pod %s/%s: %v", podRequest.PodNamespace, podRequest.PodName, err)
klog.Error(errMsg)
if err := resp.WriteHeaderAndEntity(http.StatusBadRequest, request.CniResponse{Err: errMsg.Error()}); err != nil {
klog.Errorf("failed to write response, %v", err)
Expand All @@ -456,56 +451,72 @@ func (csh cniServerHandler) handleDel(req *restful.Request, resp *restful.Respon
return
}

if pod.Annotations != nil && (util.IsOvnProvider(podRequest.Provider) || podRequest.CniType == util.CniTypeName) {
subnet := pod.Annotations[fmt.Sprintf(util.LogicalSwitchAnnotationTemplate, podRequest.Provider)]
if subnet != "" {
ip := pod.Annotations[fmt.Sprintf(util.IPAddressAnnotationTemplate, podRequest.Provider)]
if err = csh.Controller.removeEgressConfig(subnet, ip); err != nil {
errMsg := fmt.Errorf("failed to remove egress configuration: %w", err)
klog.Error(errMsg)
if err = resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.CniResponse{Err: errMsg.Error()}); err != nil {
klog.Errorf("failed to write response, %v", err)
var nicType string
var vmName string

// If the Pod was found, process its annotations and labels.
if err == nil {
if pod.Annotations != nil && (util.IsOvnProvider(podRequest.Provider) || podRequest.CniType == util.CniTypeName) {
subnet := pod.Annotations[fmt.Sprintf(util.LogicalSwitchAnnotationTemplate, podRequest.Provider)]
if subnet != "" {
ip := pod.Annotations[fmt.Sprintf(util.IPAddressAnnotationTemplate, podRequest.Provider)]
if err = csh.Controller.removeEgressConfig(subnet, ip); err != nil {
errMsg := fmt.Errorf("failed to remove egress configuration: %w", err)
klog.Error(errMsg)
if err = resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.CniResponse{Err: errMsg.Error()}); err != nil {
klog.Errorf("failed to write response, %v", err)
}
return
}
return
}
}

// For Support kubevirt hotplug dpdk nic, forbidden set the volume name
if podRequest.VhostUserSocketConsumption == util.ConsumptionKubevirt {
podRequest.VhostUserSocketVolumeName = util.VhostUserSocketVolumeName
}

var nicType string
switch {
case podRequest.DeviceID != "":
nicType = util.OffloadType
case podRequest.VhostUserSocketVolumeName != "":
nicType = util.DpdkType
if err = removeShortSharedDir(pod, podRequest.VhostUserSocketVolumeName, podRequest.VhostUserSocketConsumption); err != nil {
klog.Error(err.Error())
if err = resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.CniResponse{Err: err.Error()}); err != nil {
klog.Errorf("failed to write response: %v", err)
switch {
case podRequest.DeviceID != "":
nicType = util.OffloadType
case podRequest.VhostUserSocketVolumeName != "":
nicType = util.DpdkType
if err = removeShortSharedDir(pod, podRequest.VhostUserSocketVolumeName, podRequest.VhostUserSocketConsumption); err != nil {
klog.Error(err.Error())
if err = resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.CniResponse{Err: err.Error()}); err != nil {
klog.Errorf("failed to write response: %v", err)
}
return
}
return
default:
nicType = pod.Annotations[fmt.Sprintf(util.PodNicAnnotationTemplate, podRequest.Provider)]
}
default:
nicType = pod.Annotations[fmt.Sprintf(util.PodNicAnnotationTemplate, podRequest.Provider)]
}
vmName := pod.Annotations[fmt.Sprintf(util.VMAnnotationTemplate, podRequest.Provider)]
if vmName != "" {
podRequest.PodName = vmName
}

err = csh.deleteNic(podRequest.PodName, podRequest.PodNamespace, podRequest.ContainerID, podRequest.NetNs, podRequest.DeviceID, podRequest.IfName, nicType, podRequest.Provider)
if err != nil {
errMsg := fmt.Errorf("del nic failed %w", err)
klog.Error(errMsg)
if err := resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.CniResponse{Err: errMsg.Error()}); err != nil {
klog.Errorf("failed to write response, %v", err)
vmName = pod.Annotations[fmt.Sprintf(util.VMAnnotationTemplate, podRequest.Provider)]
if vmName != "" {
podRequest.PodName = vmName
}
return
}
} else {
// If the Pod is not found, assign a default value.
klog.Warningf("Pod %s not found, proceeding with NIC deletion using ContainerID and NetNs", podRequest.PodName)
if podRequest.DeviceID != "" {
nicType = util.OffloadType
} else if podRequest.VhostUserSocketVolumeName != "" {
nicType = util.DpdkType
} else {
nicType = "veth-pair"
}
}

// For Support kubevirt hotplug dpdk nic, forbidden set the volume name
if podRequest.VhostUserSocketConsumption == util.ConsumptionKubevirt {
podRequest.VhostUserSocketVolumeName = util.VhostUserSocketVolumeName
}

// Proceed to delete the NIC regardless of whether the Pod was found or not.
err = csh.deleteNic(podRequest.PodName, podRequest.PodNamespace, podRequest.ContainerID, podRequest.NetNs, podRequest.DeviceID, podRequest.IfName, nicType, podRequest.Provider)
if err != nil {
errMsg := fmt.Errorf("del nic failed %w", err)
klog.Error(errMsg)
if err := resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.CniResponse{Err: errMsg.Error()}); err != nil {
klog.Errorf("failed to write response, %v", err)
}
return
}
resp.WriteHeader(http.StatusNoContent)
}
7 changes: 1 addition & 6 deletions pkg/daemon/ovs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,7 @@ func (csh cniServerHandler) releaseVf(podName, podNamespace, podNetns, ifName, n
return fmt.Errorf("failed to bring down container interface %s %s: %w", ifName, podDesc, err)
}
// rename VF device back to its original name in the host namespace:
pod, err := csh.Controller.podsLister.Pods(podNamespace).Get(podName)
if err != nil {
klog.Errorf("failed to get pod %s/%s: %v", podName, podNamespace, err)
return err
}
vfName := pod.Annotations[fmt.Sprintf(util.VfNameTemplate, provider)]
vfName := link.Attrs().Alias
if err = netlink.LinkSetName(link, vfName); err != nil {
return fmt.Errorf("failed to rename container interface %s to %s %s: %w",
ifName, vfName, podDesc, err)
Expand Down

0 comments on commit 77cfca9

Please sign in to comment.