From 82c09a98b728697b333a2c150bb3ea0492b22cc0 Mon Sep 17 00:00:00 2001 From: Tomofumi Hayashi Date: Fri, 11 Nov 2022 00:22:15 +0900 Subject: [PATCH] Change conditional flow to use cache file in CmdDel This fix changes conditional flow to use net-attach-def if cache file is collapsed in CmdDel. Fix #948 --- pkg/multus/multus.go | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/pkg/multus/multus.go b/pkg/multus/multus.go index 8968e6fc6..4389049ed 100644 --- a/pkg/multus/multus.go +++ b/pkg/multus/multus.go @@ -825,8 +825,25 @@ func CmdDel(args *skel.CmdArgs, exec invoke.Exec, kubeClient *k8s.ClientInfo) er // Read the cache to get delegates json for the pod netconfBytes, path, err := consumeScratchNetConf(args.ContainerID, in.CNIDir) - removeCacheConf := false - if err != nil { + useCacheConf := false + if err == nil { + in.Delegates = []*types.DelegateNetConf{} + if err := json.Unmarshal(netconfBytes, &in.Delegates); err != nil { + logging.Errorf("Multus: failed to load netconf: %v", err) + } else { + useCacheConf = true + // check plugins field and enable ConfListPlugin if there is + for _, v := range in.Delegates { + if len(v.ConfList.Plugins) != 0 { + v.ConfListPlugin = true + } + } + // First delegate is always the master plugin + in.Delegates[0].MasterPlugin = true + } + } + + if !useCacheConf { // Fetch delegates again if cache is not exist and pod info can be read if os.IsNotExist(err) && pod != nil { if in.ClusterNetwork != "" { @@ -854,20 +871,6 @@ func CmdDel(args *skel.CmdArgs, exec invoke.Exec, kubeClient *k8s.ClientInfo) er logging.Errorf("Multus: failed to get the cached delegates file: %v, cannot properly delete", err) return nil } - } else { - removeCacheConf = true - in.Delegates = []*types.DelegateNetConf{} - if err := json.Unmarshal(netconfBytes, &in.Delegates); err != nil { - return cmdErr(k8sArgs, "failed to load netconf: %v", err) - } - // check plugins field and enable ConfListPlugin if there is - for _, v := range in.Delegates { - if len(v.ConfList.Plugins) != 0 { - v.ConfListPlugin = true - } - } - // First delegate is always the master plugin - in.Delegates[0].MasterPlugin = true } // set CNIVersion in delegate CNI config if there is no CNIVersion and multus conf have CNIVersion. @@ -905,7 +908,7 @@ func CmdDel(args *skel.CmdArgs, exec invoke.Exec, kubeClient *k8s.ClientInfo) er // put "delete cache file" off later ensure have enough info delegate DEL message when Pod has been fully // deleted from ETCD before sandbox cleanup success.. if in.RetryDeleteOnError { - if removeCacheConf { + if useCacheConf { // Kubelet though this error as has been cleanup success and never retry, clean cache also // Block sandbox cleanup error message can not contain "no such file or directory", CNI Runtime maybe should adaptor it ! if e == nil || strings.Contains(e.Error(), "no such file or directory") { @@ -913,7 +916,8 @@ func CmdDel(args *skel.CmdArgs, exec invoke.Exec, kubeClient *k8s.ClientInfo) er } } } else { - if removeCacheConf { + if useCacheConf { + // remove used cache file _ = os.Remove(path) // lgtm[go/path-injection] } }