Skip to content

Commit ef39312

Browse files
committed
Fixed an issue where duplicate containerPorts where not allowed across different address families
1 parent 84fe3db commit ef39312

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

pkg/kubelet/container/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ go_library(
3939
"//staging/src/k8s.io/cri-api/pkg/apis/runtime/v1alpha2:go_default_library",
4040
"//third_party/forked/golang/expansion:go_default_library",
4141
"//vendor/k8s.io/klog:go_default_library",
42+
"//vendor/k8s.io/utils/net:go_default_library",
4243
],
4344
)
4445

pkg/kubelet/container/helpers.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"k8s.io/kubernetes/pkg/kubelet/util/format"
3636
hashutil "k8s.io/kubernetes/pkg/util/hash"
3737
"k8s.io/kubernetes/third_party/forked/golang/expansion"
38+
utilsnet "k8s.io/utils/net"
3839
)
3940

4041
// HandlerRunner runs a lifecycle handler for a container.
@@ -319,16 +320,28 @@ func MakePortMappings(container *v1.Container) (ports []PortMapping) {
319320
HostIP: p.HostIP,
320321
}
321322

323+
// We need to determine the address family this entry applies to. We do this to ensure
324+
// duplicate containerPort / protocol rules work across different address families.
325+
// https://github.com/kubernetes/kubernetes/issues/82373
326+
family := "any"
327+
if p.HostIP != "" {
328+
if utilsnet.IsIPv6String(p.HostIP) {
329+
family = "v6"
330+
} else {
331+
family = "v4"
332+
}
333+
}
334+
322335
// We need to create some default port name if it's not specified, since
323-
// this is necessary for rkt.
324-
// http://issue.k8s.io/7710
336+
// this is necessary for the dockershim CNI driver.
337+
// https://github.com/kubernetes/kubernetes/pull/82374#issuecomment-529496888
325338
if p.Name == "" {
326-
pm.Name = fmt.Sprintf("%s-%s:%d", container.Name, p.Protocol, p.ContainerPort)
339+
pm.Name = fmt.Sprintf("%s-%s-%s:%d", container.Name, family, p.Protocol, p.ContainerPort)
327340
} else {
328341
pm.Name = fmt.Sprintf("%s-%s", container.Name, p.Name)
329342
}
330343

331-
// Protect against exposing the same protocol-port more than once in a container.
344+
// Protect against a port name being used more than once in a container.
332345
if _, ok := names[pm.Name]; ok {
333346
klog.Warningf("Port name conflicted, %q is defined more than once", pm.Name)
334347
continue

pkg/kubelet/container/helpers_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -558,13 +558,19 @@ func TestMakePortMappings(t *testing.T) {
558558
// Duplicated, should be ignored.
559559
port("foo", v1.ProtocolUDP, 888, 8888, ""),
560560
// Duplicated, should be ignored.
561-
port("", v1.ProtocolTCP, 80, 8888, ""),
561+
port("", v1.ProtocolTCP, 80, 8888, "127.0.0.1"),
562+
// Duplicated with different address family, shouldn't be ignored
563+
port("", v1.ProtocolTCP, 80, 8080, "::"),
564+
// No address family specified
565+
port("", v1.ProtocolTCP, 1234, 5678, ""),
562566
},
563567
},
564568
[]PortMapping{
565-
portMapping("fooContainer-TCP:80", v1.ProtocolTCP, 80, 8080, "127.0.0.1"),
566-
portMapping("fooContainer-TCP:443", v1.ProtocolTCP, 443, 4343, "192.168.0.1"),
569+
portMapping("fooContainer-v4-TCP:80", v1.ProtocolTCP, 80, 8080, "127.0.0.1"),
570+
portMapping("fooContainer-v4-TCP:443", v1.ProtocolTCP, 443, 4343, "192.168.0.1"),
567571
portMapping("fooContainer-foo", v1.ProtocolUDP, 555, 5555, ""),
572+
portMapping("fooContainer-v6-TCP:80", v1.ProtocolTCP, 80, 8080, "::"),
573+
portMapping("fooContainer-any-TCP:1234", v1.ProtocolTCP, 1234, 5678, ""),
568574
},
569575
},
570576
}

0 commit comments

Comments
 (0)