diff --git a/pkg/api/endpoints/util_test.go b/pkg/api/endpoints/util_test.go index a2f960317df..7e003e66dd7 100644 --- a/pkg/api/endpoints/util_test.go +++ b/pkg/api/endpoints/util_test.go @@ -66,6 +66,16 @@ func TestPackSubsets(t *testing.T) { Addresses: []api.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []api.EndpointPort{{Port: 111}}, }}, + }, { + name: "one set, one ip, one port (IPv6)", + given: []api.EndpointSubset{{ + Addresses: []api.EndpointAddress{{IP: "beef::1:2:3:4"}}, + Ports: []api.EndpointPort{{Port: 111}}, + }}, + expect: []api.EndpointSubset{{ + Addresses: []api.EndpointAddress{{IP: "beef::1:2:3:4"}}, + Ports: []api.EndpointPort{{Port: 111}}, + }}, }, { name: "one set, one notReady ip, one port", given: []api.EndpointSubset{{ @@ -169,6 +179,16 @@ func TestPackSubsets(t *testing.T) { Addresses: []api.EndpointAddress{{IP: "1.2.3.4"}}, Ports: []api.EndpointPort{{Port: 111}}, }}, + }, { + name: "one set, dup ips, one port (IPv6)", + given: []api.EndpointSubset{{ + Addresses: []api.EndpointAddress{{IP: "beef::1"}, {IP: "beef::1"}}, + Ports: []api.EndpointPort{{Port: 111}}, + }}, + expect: []api.EndpointSubset{{ + Addresses: []api.EndpointAddress{{IP: "beef::1"}}, + Ports: []api.EndpointPort{{Port: 111}}, + }}, }, { name: "one set, dup ips with target-refs, one port", given: []api.EndpointSubset{{ diff --git a/pkg/kubelet/dockertools/manager.go b/pkg/kubelet/dockertools/manager.go index 30557e14044..3a87bfd7a73 100644 --- a/pkg/kubelet/dockertools/manager.go +++ b/pkg/kubelet/dockertools/manager.go @@ -324,6 +324,11 @@ func (dm *DockerManager) determineContainerIP(podNamespace, podName string, cont if container.NetworkSettings != nil { result = container.NetworkSettings.IPAddress + + // Fall back to IPv6 address if no IPv4 address is present + if result == "" { + result = container.NetworkSettings.GlobalIPv6Address + } } if dm.networkPlugin.Name() != network.DefaultPluginName { @@ -1171,6 +1176,15 @@ func (dm *DockerManager) GetContainerIP(containerID, interfaceName string) (stri args := []string{"-t", fmt.Sprintf("%d", containerPid), "-n", "--", "bash", "-c", extractIPCmd} command := exec.Command("nsenter", args...) out, err := command.CombinedOutput() + + // Fall back to IPv6 address if no IPv4 address is present + if err == nil && string(out) == "" { + extractIPCmd = fmt.Sprintf("ip -6 addr show %s scope global | grep inet6 | awk -F\" \" '{print $2}'", interfaceName) + args = []string{"-t", fmt.Sprintf("%d", containerPid), "-n", "--", "bash", "-c", extractIPCmd} + command = exec.Command("nsenter", args...) + out, err = command.CombinedOutput() + } + if err != nil { return "", err }