Skip to content

Commit

Permalink
Merge pull request #90 from nauti-io/fix/deal-cnf
Browse files Browse the repository at this point in the history
fix: cnf pod  without dedinic
  • Loading branch information
lmxia authored Aug 1, 2024
2 parents f9d8761 + 3d4ca42 commit 633619b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ image-crossdns:
image-cnf:
docker build $(DOCKERARGS) -f ./build/cnf.Dockerfile ./ -t ${REGISTRY}/${REGISTRY_NAMESPACE}/cnf:${IMAGE_TAG}
docker push ${REGISTRY}/${REGISTRY_NAMESPACE}/cnf:${IMAGE_TAG}
docker tag ${REGISTRY}/${REGISTRY_NAMESPACE}/cnf:${IMAGE_TAG} ${REGISTRY}/${REGISTRY_NAMESPACE}/controller:latest
docker tag ${REGISTRY}/${REGISTRY_NAMESPACE}/cnf:${IMAGE_TAG} ${REGISTRY}/${REGISTRY_NAMESPACE}/cnf:latest
docker push ${REGISTRY}/${REGISTRY_NAMESPACE}/cnf:latest

image-ep-controller:
Expand Down
26 changes: 15 additions & 11 deletions pkg/dedinic/cni_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ func (ch cniHandler) handleAdd(rq *CniRequest) error {
}

func isCNFSelf(podNamespace, podName string) bool {
if podName == CNFPodName && podNamespace == CNFPodNamespace {
if (podName == CNFPodName && podNamespace == CNFPodNamespace) ||
(podNamespace == CNFPodNamespace && strings.Contains(podName, "cnf")) {
return true
}
return false
Expand Down Expand Up @@ -134,10 +135,10 @@ func (ch cniHandler) handleDel(rq *CniRequest) error {
return ch.deleteNic(rq.NetNs, rq.IfName)
}

func (ch cniHandler) deleteNic(nsPath, ifName string) error {
klog.V(6).Infof("deleteNic nsPath: %s, ifName: %s", nsPath, ifName)
func (ch cniHandler) deleteNic(podPath, ifName string) error {
klog.V(6).Infof("deleteNic podPath: %s, ifName: %s", podPath, ifName)

podNs, err := netns.GetFromPath(nsPath)
podNs, err := netns.GetFromPath(podPath)
if err != nil {
klog.Errorf("error get podNs: %v", err)
return err
Expand All @@ -149,7 +150,7 @@ func (ch cniHandler) deleteNic(nsPath, ifName string) error {
}
}(&podNs)

origns, err := netns.Get()
currentNs, err := netns.Get()
if err != nil {
klog.Errorf("Failed to get current namespace: %v", err)
return err
Expand All @@ -159,7 +160,7 @@ func (ch cniHandler) deleteNic(nsPath, ifName string) error {
if err != nil {
klog.Errorf("close cnf netns failed: %v", err)
}
}(&origns)
}(&currentNs)

if err = netns.Set(podNs); err != nil {
klog.Errorf("Failed to set namespace: %v", err)
Expand All @@ -170,15 +171,19 @@ func (ch cniHandler) deleteNic(nsPath, ifName string) error {
if err != nil {
klog.Errorf("change namespace to cnf pod failed: %v", err)
}
}(origns)
}(currentNs)
links, err := netlink.LinkList()
if err != nil {
klog.Errorf("Failed to list links: %v", err)
return err
}

for _, link := range links {
klog.V(6).Infof("links: %v/%v/%v", link.Attrs().NetNsID, link.Attrs().Name, link.Type())
if len(links) > 0 {
for _, link := range links {
klog.V(6).Infof("links: %v/%v/%v", link.Attrs().NetNsID, link.Attrs().Name, link.Type())
}
} else {
klog.Error("get links for exist pod failed!!!")
}

var iface netlink.Link
Expand All @@ -201,12 +206,11 @@ func (ch cniHandler) deleteNic(nsPath, ifName string) error {
peer, err := netlink.LinkByName(veth.PeerName)
if err != nil {
klog.Errorf("Failed to get peer link: %v", err)
return err
}
klog.Infof("the peer name is %v", peer)
if err := netlink.LinkDel(iface); err != nil {
klog.Errorf("Failed to delete link %s: %v", ifName, err)
return err
return nil
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/dedinic/nri.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ func (p *CNIPlugin) Configure(config, runtime, version string) (stub.EventMask,

func (p *CNIPlugin) Synchronize(pods []*api.PodSandbox, containers []*api.Container) ([]*api.ContainerUpdate, error) {
for _, pod := range pods {
klog.Infof("[Synchronize]: %v/%v", pod.Namespace, pod.Name)
if isCNFSelf(pod.Namespace, pod.Name) {
klog.Infof("skip the cnf releated pod: %v/%v", pod.Namespace, pod.Name)
continue
}
nsPath, err := GetNSPathFromPod(pod)
Expand All @@ -100,11 +102,9 @@ func (p *CNIPlugin) Synchronize(pods []*api.PodSandbox, containers []*api.Contai

if err := csh.handleDel(podRequest); err != nil {
klog.Errorf("delete exist network failed: %v", err)
continue
}
if err := csh.handleAdd(podRequest); err != nil {
klog.Errorf("add network failed: %v", err)
continue
}
}
return nil, nil
Expand Down

0 comments on commit 633619b

Please sign in to comment.