mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-29 14:37:00 +00:00
Allow using netns path instead of container PID to change hairpin mode
This commit is contained in:
parent
952e8302fb
commit
55e6eb2ce3
@ -1957,7 +1957,7 @@ func (dm *DockerManager) SyncPod(pod *api.Pod, _ api.PodStatus, podStatus *kubec
|
|||||||
}
|
}
|
||||||
|
|
||||||
if dm.configureHairpinMode {
|
if dm.configureHairpinMode {
|
||||||
if err = hairpin.SetUpContainer(podInfraContainer.State.Pid, network.DefaultInterfaceName); err != nil {
|
if err = hairpin.SetUpContainerPid(podInfraContainer.State.Pid, network.DefaultInterfaceName); err != nil {
|
||||||
glog.Warningf("Hairpin setup failed for pod %q: %v", format.Pod(pod), err)
|
glog.Warningf("Hairpin setup failed for pod %q: %v", format.Pod(pod), err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -40,13 +40,23 @@ var (
|
|||||||
ethtoolOutputRegex = regexp.MustCompile("peer_ifindex: (\\d+)")
|
ethtoolOutputRegex = regexp.MustCompile("peer_ifindex: (\\d+)")
|
||||||
)
|
)
|
||||||
|
|
||||||
func SetUpContainer(containerPid int, containerInterfaceName string) error {
|
func SetUpContainerPid(containerPid int, containerInterfaceName string) error {
|
||||||
e := exec.New()
|
pidStr := fmt.Sprintf("%d", containerPid)
|
||||||
return setUpContainerInternal(e, containerPid, containerInterfaceName)
|
nsenterArgs := []string{"-t", pidStr, "-n"}
|
||||||
|
return setUpContainerInternal(containerInterfaceName, pidStr, nsenterArgs)
|
||||||
}
|
}
|
||||||
|
|
||||||
func setUpContainerInternal(e exec.Interface, containerPid int, containerInterfaceName string) error {
|
func SetUpContainerPath(netnsPath string, containerInterfaceName string) error {
|
||||||
hostIfName, err := findPairInterfaceOfContainerInterface(e, containerPid, containerInterfaceName)
|
if netnsPath[0] != '/' {
|
||||||
|
return fmt.Errorf("netnsPath path '%s' was invalid", netnsPath)
|
||||||
|
}
|
||||||
|
nsenterArgs := []string{"-n", netnsPath}
|
||||||
|
return setUpContainerInternal(containerInterfaceName, netnsPath, nsenterArgs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func setUpContainerInternal(containerInterfaceName, containerDesc string, nsenterArgs []string) error {
|
||||||
|
e := exec.New()
|
||||||
|
hostIfName, err := findPairInterfaceOfContainerInterface(e, containerInterfaceName, containerDesc, nsenterArgs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Infof("Unable to find pair interface, setting up all interfaces: %v", err)
|
glog.Infof("Unable to find pair interface, setting up all interfaces: %v", err)
|
||||||
return setUpAllInterfaces()
|
return setUpAllInterfaces()
|
||||||
@ -54,7 +64,7 @@ func setUpContainerInternal(e exec.Interface, containerPid int, containerInterfa
|
|||||||
return setUpInterface(hostIfName)
|
return setUpInterface(hostIfName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func findPairInterfaceOfContainerInterface(e exec.Interface, containerPid int, containerInterfaceName string) (string, error) {
|
func findPairInterfaceOfContainerInterface(e exec.Interface, containerInterfaceName, containerDesc string, nsenterArgs []string) (string, error) {
|
||||||
nsenterPath, err := e.LookPath("nsenter")
|
nsenterPath, err := e.LookPath("nsenter")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@ -63,15 +73,16 @@ func findPairInterfaceOfContainerInterface(e exec.Interface, containerPid int, c
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
// Get container's interface index
|
|
||||||
output, err := e.Command(nsenterPath, "-t", fmt.Sprintf("%d", containerPid), "-n", "-F", "--", ethtoolPath, "--statistics", containerInterfaceName).CombinedOutput()
|
nsenterArgs = append(nsenterArgs, "-F", "--", ethtoolPath, "--statistics", containerInterfaceName)
|
||||||
|
output, err := e.Command(nsenterPath, nsenterArgs...).CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("Unable to query interface %s of container %d: %v: %s", containerInterfaceName, containerPid, err, string(output))
|
return "", fmt.Errorf("Unable to query interface %s of container %s: %v: %s", containerInterfaceName, containerDesc, err, string(output))
|
||||||
}
|
}
|
||||||
// look for peer_ifindex
|
// look for peer_ifindex
|
||||||
match := ethtoolOutputRegex.FindSubmatch(output)
|
match := ethtoolOutputRegex.FindSubmatch(output)
|
||||||
if match == nil {
|
if match == nil {
|
||||||
return "", fmt.Errorf("No peer_ifindex in interface statistics for %s of container %d", containerInterfaceName, containerPid)
|
return "", fmt.Errorf("No peer_ifindex in interface statistics for %s of container %s", containerInterfaceName, containerDesc)
|
||||||
}
|
}
|
||||||
peerIfIndex, err := strconv.Atoi(string(match[1]))
|
peerIfIndex, err := strconv.Atoi(string(match[1]))
|
||||||
if err != nil { // seems impossible (\d+ not numeric)
|
if err != nil { // seems impossible (\d+ not numeric)
|
||||||
|
@ -69,7 +69,8 @@ func TestFindPairInterfaceOfContainerInterface(t *testing.T) {
|
|||||||
return fmt.Sprintf("/fake-bin/%s", file), nil
|
return fmt.Sprintf("/fake-bin/%s", file), nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
name, err := findPairInterfaceOfContainerInterface(&fexec, 123, "eth0")
|
nsenterArgs := []string{"-t", "123", "-n"}
|
||||||
|
name, err := findPairInterfaceOfContainerInterface(&fexec, "eth0", "123", nsenterArgs)
|
||||||
if test.expectErr {
|
if test.expectErr {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("unexpected non-error")
|
t.Errorf("unexpected non-error")
|
||||||
|
Loading…
Reference in New Issue
Block a user