Skip to content
Permalink
Browse files

Remove nsenter approach and replace it with cni cached result

This commit takes into account ipv6 dual stack feature.

Signed-off-by: Alexey Perevalov <a.perevalov@samsung.com>
  • Loading branch information
AlexeyPerevalov committed Apr 26, 2019
1 parent 2e135b8 commit f0a04a836e8c06c3b3caba0d5ac7be40fbc38f6a
@@ -18,8 +18,10 @@ go_library(
"//pkg/kubelet/apis/config:go_default_library",
"//pkg/kubelet/container:go_default_library",
"//pkg/kubelet/dockershim/network:go_default_library",
"//pkg/kubelet/util/format:go_default_library",
"//pkg/util/bandwidth:go_default_library",
"//pkg/util/slice:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
"//staging/src/k8s.io/cri-api/pkg/apis/runtime/v1alpha2:go_default_library",
"//vendor/github.com/containernetworking/cni/libcni:go_default_library",
@@ -30,12 +30,14 @@ import (
"github.com/containernetworking/cni/libcni"
cnitypes "github.com/containernetworking/cni/pkg/types"
cnicurrent "github.com/containernetworking/cni/pkg/types/current"
machinerytypes "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
"k8s.io/klog"
kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
"k8s.io/kubernetes/pkg/kubelet/dockershim/network"
"k8s.io/kubernetes/pkg/kubelet/util/format"
"k8s.io/kubernetes/pkg/util/bandwidth"
utilslice "k8s.io/kubernetes/pkg/util/slice"
utilexec "k8s.io/utils/exec"
@@ -67,7 +69,6 @@ type cniNetworkPlugin struct {

host network.Host
execer utilexec.Interface
nsenterPath string
confDir string
binDirs []string
cacheDir string
@@ -220,11 +221,6 @@ func getDefaultCNINetwork(confDir string, binDirs []string) (*cniNetwork, error)
}

func (plugin *cniNetworkPlugin) Init(host network.Host, hairpinMode kubeletconfig.HairpinMode, nonMasqueradeCIDR string, mtu int) error {
err := plugin.platformInit()
if err != nil {
return err
}

plugin.host = host

plugin.syncNetworkConfig()
@@ -349,10 +345,6 @@ func (plugin *cniNetworkPlugin) TearDownPod(namespace string, name string, id ku
return plugin.deleteFromNetwork(cniTimeoutCtx, plugin.getDefaultNetwork(), name, namespace, id, netnsPath, nil)
}

func podDesc(namespace, name string, id kubecontainer.ContainerID) string {
return fmt.Sprintf("%s_%s/%s", namespace, name, id.ID)
}

func getIpsFromResult(res *cnicurrent.Result, ifaceName string) []net.IP {
if res == nil {
return nil
@@ -386,7 +378,7 @@ func (plugin *cniNetworkPlugin) addToNetwork(ctx context.Context, network *cniNe
return nil, err
}

pdesc := podDesc(podNamespace, podName, podSandboxID)
pdesc := format.PodDesc(podName, podNamespace, machinerytypes.UID(podSandboxID.ID))
netConf, cniNet := network.NetworkConfig, network.CNIConfig
klog.V(4).Infof("Adding %s to network %s/%s netns %q", pdesc, netConf.Plugins[0].Network.Type, netConf.Name, podNetnsPath)
res, err := cniNet.AddNetworkList(ctx, netConf, rt)
@@ -414,7 +406,7 @@ func (plugin *cniNetworkPlugin) deleteFromNetwork(ctx context.Context, network *
return err
}

pdesc := podDesc(podNamespace, podName, podSandboxID)
pdesc := format.PodDesc(podName, podNamespace, machinerytypes.UID(podSandboxID.ID))
netConf, cniNet := network.NetworkConfig, network.CNIConfig
klog.V(4).Infof("Deleting %s from network %s/%s netns %q", pdesc, netConf.Plugins[0].Network.Type, netConf.Name, podNetnsPath)
err = cniNet.DelNetworkList(ctx, netConf, rt)
@@ -22,10 +22,13 @@ import (
"fmt"

"github.com/containernetworking/cni/libcni"
cnicurrent "github.com/containernetworking/cni/pkg/types/current"
"k8s.io/apimachinery/pkg/types"
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
"k8s.io/klog"
kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
"k8s.io/kubernetes/pkg/kubelet/dockershim/network"
"k8s.io/kubernetes/pkg/kubelet/util/format"
)

func getLoNetwork(binDirs []string) *cniNetwork {
@@ -50,15 +53,6 @@ func getLoNetwork(binDirs []string) *cniNetwork {
return loNetwork
}

func (plugin *cniNetworkPlugin) platformInit() error {
var err error
plugin.nsenterPath, err = plugin.execer.LookPath("nsenter")
if err != nil {
return err
}
return nil
}

// TODO: Use the addToNetwork function to obtain the IP of the Pod. That will assume idempotent ADD call to the plugin.
// Also fix the runtime's call to Status function to be done only in the case that the IP is lost, no need to do periodic calls
func (plugin *cniNetworkPlugin) GetPodNetworkStatus(namespace string, name string, id kubecontainer.ContainerID) (*network.PodNetworkStatus, error) {
@@ -67,23 +61,47 @@ func (plugin *cniNetworkPlugin) GetPodNetworkStatus(namespace string, name strin
klog.V(3).Infof("get pod ip %v from plugin", podIPs)
return &network.PodNetworkStatus{IP: podIPs[0], IPs: podIPs}, nil
}
cninetwork := plugin.getDefaultNetwork()
cniNet := cninetwork.CNIConfig
netConfList := cninetwork.NetworkConfig
netnsPath, err := plugin.host.GetNetNS(id.ID)
if err != nil {
return nil, fmt.Errorf("CNI failed to retrieve network namespace path: %v", err)
}
if netnsPath == "" {
return nil, fmt.Errorf("cannot find the network namespace, skipping pod network status for container %q", id)
}
rt, err := plugin.buildCNIRuntimeConf(name, namespace, id, netnsPath, nil, nil)
if err != nil {
klog.Errorf("Error get pod network status when building cni runtime conf: %v", err)
return nil, err
}
res, err := cniNet.GetNetworkListCachedResult(netConfList, rt)

ips, err := network.GetPodIPs(plugin.execer, plugin.nsenterPath, netnsPath, network.DefaultInterfaceName)
pdesc := format.PodDesc(name, namespace, types.UID(id.ID))
if res == nil {
klog.V(3).Infof("Cached result doesn't exists for %s", pdesc)
return nil, nil
}
if err != nil {
klog.Errorf("Error get cached result %s for network %s: %v", pdesc, netConfList.Name, err)
return nil, err
}

klog.V(4).Infof("Get cached result for %s in the network %s: %v", pdesc, netConfList.Name, res)

curRes, err := cnicurrent.NewResultFromResult(res)
if curRes == nil || len(curRes.IPs) == 0 {
klog.Errorf("CNI result conversion failed: %v", err)
return nil, err
}

ips := getIpsFromResult(curRes, rt.IfName)
if len(ips) == 0 {
return nil, fmt.Errorf("cannot find pod IPs in the network namespace, skipping pod network status for container %q", id)
}

klog.V(3).Infof("get pod ip %v from cached result", ips)
plugin.setPodCNIResult(id, curRes)
return &network.PodNetworkStatus{
IP: ips[0],
IPs: ips,

0 comments on commit f0a04a8

Please sign in to comment.
You can’t perform that action at this time.