mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 22:17:14 +00:00
kubelet/cni/kubenet: use common container IP address functions
This commit is contained in:
parent
9865ac325c
commit
db078dbea4
@ -18,9 +18,7 @@ package cni
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/appc/cni/libcni"
|
"github.com/appc/cni/libcni"
|
||||||
cnitypes "github.com/appc/cni/pkg/types"
|
cnitypes "github.com/appc/cni/pkg/types"
|
||||||
@ -138,30 +136,6 @@ func (plugin *cniNetworkPlugin) TearDownPod(namespace string, name string, id ku
|
|||||||
return plugin.defaultNetwork.deleteFromNetwork(name, namespace, id, netnsPath)
|
return plugin.defaultNetwork.deleteFromNetwork(name, namespace, id, netnsPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (plugin *cniNetworkPlugin) getContainerIPAddress(netnsPath, addrType string) (net.IP, error) {
|
|
||||||
// Try to retrieve ip inside container network namespace
|
|
||||||
output, err := plugin.execer.Command(plugin.nsenterPath, fmt.Sprintf("--net=%s", netnsPath), "-F", "--",
|
|
||||||
"ip", "-o", addrType, "addr", "show", "dev", network.DefaultInterfaceName, "scope", "global").CombinedOutput()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("Unexpected command output %s with error: %v", output, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
lines := strings.Split(string(output), "\n")
|
|
||||||
if len(lines) < 1 {
|
|
||||||
return nil, fmt.Errorf("Unexpected command output %s", output)
|
|
||||||
}
|
|
||||||
fields := strings.Fields(lines[0])
|
|
||||||
if len(fields) < 4 {
|
|
||||||
return nil, fmt.Errorf("Unexpected address output %s ", lines[0])
|
|
||||||
}
|
|
||||||
ip, _, err := net.ParseCIDR(fields[3])
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("CNI failed to parse ip from output %s due to %v", output, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return ip, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Use the addToNetwork function to obtain the IP of the Pod. That will assume idempotent ADD call to the plugin.
|
// 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
|
// 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) {
|
func (plugin *cniNetworkPlugin) GetPodNetworkStatus(namespace string, name string, id kubecontainer.ContainerID) (*network.PodNetworkStatus, error) {
|
||||||
@ -170,11 +144,7 @@ func (plugin *cniNetworkPlugin) GetPodNetworkStatus(namespace string, name strin
|
|||||||
return nil, fmt.Errorf("CNI failed to retrieve network namespace path: %v", err)
|
return nil, fmt.Errorf("CNI failed to retrieve network namespace path: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ip, err := plugin.getContainerIPAddress(netnsPath, "-4")
|
ip, err := network.GetPodIP(plugin.execer, plugin.nsenterPath, netnsPath, network.DefaultInterfaceName)
|
||||||
if err != nil {
|
|
||||||
// Fall back to IPv6 address if no IPv4 address is present
|
|
||||||
ip, err = plugin.getContainerIPAddress(netnsPath, "-6")
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -468,20 +468,11 @@ func (plugin *kubenetNetworkPlugin) GetPodNetworkStatus(namespace string, name s
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
// Try to retrieve ip inside container network namespace
|
ip, err := network.GetPodIP(plugin.execer, nsenterPath, netnsPath, network.DefaultInterfaceName)
|
||||||
output, err := plugin.execer.Command(nsenterPath, fmt.Sprintf("--net=%s", netnsPath), "-F", "--",
|
|
||||||
"ip", "-o", "-4", "addr", "show", "dev", network.DefaultInterfaceName).CombinedOutput()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Unexpected command output %s with error: %v", output, err)
|
return nil, err
|
||||||
}
|
|
||||||
fields := strings.Fields(string(output))
|
|
||||||
if len(fields) < 4 {
|
|
||||||
return nil, fmt.Errorf("Unexpected command output %s ", output)
|
|
||||||
}
|
|
||||||
ip, _, err := net.ParseCIDR(fields[3])
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("Kubenet failed to parse ip from output %s due to %v", output, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
plugin.podIPs[id] = ip.String()
|
plugin.podIPs[id] = ip.String()
|
||||||
return &network.PodNetworkStatus{IP: ip}, nil
|
return &network.PodNetworkStatus{IP: ip}, nil
|
||||||
}
|
}
|
||||||
|
@ -199,3 +199,41 @@ func (plugin *NoopNetworkPlugin) GetPodNetworkStatus(namespace string, name stri
|
|||||||
func (plugin *NoopNetworkPlugin) Status() error {
|
func (plugin *NoopNetworkPlugin) Status() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getOnePodIP(execer utilexec.Interface, nsenterPath, netnsPath, interfaceName, addrType string) (net.IP, error) {
|
||||||
|
// Try to retrieve ip inside container network namespace
|
||||||
|
output, err := execer.Command(nsenterPath, fmt.Sprintf("--net=%s", netnsPath), "-F", "--",
|
||||||
|
"ip", "-o", addrType, "addr", "show", "dev", interfaceName, "scope", "global").CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Unexpected command output %s with error: %v", output, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
lines := strings.Split(string(output), "\n")
|
||||||
|
if len(lines) < 1 {
|
||||||
|
return nil, fmt.Errorf("Unexpected command output %s", output)
|
||||||
|
}
|
||||||
|
fields := strings.Fields(lines[0])
|
||||||
|
if len(fields) < 4 {
|
||||||
|
return nil, fmt.Errorf("Unexpected address output %s ", lines[0])
|
||||||
|
}
|
||||||
|
ip, _, err := net.ParseCIDR(fields[3])
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("CNI failed to parse ip from output %s due to %v", output, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ip, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetPodIP gets the IP of the pod by inspecting the network info inside the pod's network namespace.
|
||||||
|
func GetPodIP(execer utilexec.Interface, nsenterPath, netnsPath, interfaceName string) (net.IP, error) {
|
||||||
|
ip, err := getOnePodIP(execer, nsenterPath, netnsPath, interfaceName, "-4")
|
||||||
|
if err != nil {
|
||||||
|
// Fall back to IPv6 address if no IPv4 address is present
|
||||||
|
ip, err = getOnePodIP(execer, nsenterPath, netnsPath, interfaceName, "-6")
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ip, nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user