kubectl: add port names to describe pod output

Kubernetes-commit: 1d0fd5928818036289a76358503778eff26e53ef
This commit is contained in:
arush sharma 2025-07-16 22:47:06 -07:00 committed by Kubernetes Publisher
parent 29d75ee358
commit 8067f3a1a6
2 changed files with 78 additions and 2 deletions

View File

@ -1865,7 +1865,11 @@ func describeContainerBasicInfo(container corev1.Container, status corev1.Contai
func describeContainerPorts(cPorts []corev1.ContainerPort) string {
ports := make([]string, 0, len(cPorts))
for _, cPort := range cPorts {
ports = append(ports, fmt.Sprintf("%d/%s", cPort.ContainerPort, cPort.Protocol))
portStr := fmt.Sprintf("%d/%s", cPort.ContainerPort, cPort.Protocol)
if cPort.Name != "" {
portStr = fmt.Sprintf("%s (%s)", portStr, cPort.Name)
}
ports = append(ports, portStr)
}
return strings.Join(ports, ", ")
}
@ -1873,7 +1877,11 @@ func describeContainerPorts(cPorts []corev1.ContainerPort) string {
func describeContainerHostPorts(cPorts []corev1.ContainerPort) string {
ports := make([]string, 0, len(cPorts))
for _, cPort := range cPorts {
ports = append(ports, fmt.Sprintf("%d/%s", cPort.HostPort, cPort.Protocol))
portStr := fmt.Sprintf("%d/%s", cPort.HostPort, cPort.Protocol)
if cPort.Name != "" {
portStr = fmt.Sprintf("%s (%s)", portStr, cPort.Name)
}
ports = append(ports, portStr)
}
return strings.Join(ports, ", ")
}

View File

@ -1372,6 +1372,74 @@ func TestDescribeResources(t *testing.T) {
}
}
func TestDescribeContainerPorts(t *testing.T) {
testCases := []struct {
name string
ports []corev1.ContainerPort
expectedContainer string
expectedHost string
}{
{
name: "no ports",
ports: []corev1.ContainerPort{},
expectedContainer: "",
expectedHost: "",
},
{
name: "container and host port, with name",
ports: []corev1.ContainerPort{
{Name: "web", ContainerPort: 8080, HostPort: 8080, Protocol: corev1.ProtocolTCP},
},
expectedContainer: "8080/TCP (web)",
expectedHost: "8080/TCP (web)",
},
{
name: "container and host port, no name",
ports: []corev1.ContainerPort{
{ContainerPort: 8080, HostPort: 8080, Protocol: corev1.ProtocolTCP},
},
expectedContainer: "8080/TCP",
expectedHost: "8080/TCP",
},
{
name: "multiple ports with mixed configuration",
ports: []corev1.ContainerPort{
{Name: "controller", ContainerPort: 9093, HostPort: 9093, Protocol: corev1.ProtocolTCP},
{ContainerPort: 9092, Protocol: corev1.ProtocolTCP},
{Name: "interbroker", ContainerPort: 9094, HostPort: 9094, Protocol: corev1.ProtocolTCP},
},
expectedContainer: "9093/TCP (controller), 9092/TCP, 9094/TCP (interbroker)",
expectedHost: "9093/TCP (controller), 0/TCP, 9094/TCP (interbroker)",
},
{
name: "all ports with mixed configuration",
ports: []corev1.ContainerPort{
{Name: "controller", ContainerPort: 9093, HostPort: 9093, Protocol: corev1.ProtocolTCP},
{Name: "client", ContainerPort: 9092, HostPort: 9092, Protocol: corev1.ProtocolTCP},
{Name: "interbroker", ContainerPort: 9094, HostPort: 9094, Protocol: corev1.ProtocolTCP},
},
expectedContainer: "9093/TCP (controller), 9092/TCP (client), 9094/TCP (interbroker)",
expectedHost: "9093/TCP (controller), 9092/TCP (client), 9094/TCP (interbroker)",
},
}
for _, tc := range testCases {
t.Run(tc.name+" - container ports", func(t *testing.T) {
result := describeContainerPorts(tc.ports)
if result != tc.expectedContainer {
t.Errorf("describeContainerPorts: expected %q, got %q", tc.expectedContainer, result)
}
})
t.Run(tc.name+" - host ports", func(t *testing.T) {
result := describeContainerHostPorts(tc.ports)
if result != tc.expectedHost {
t.Errorf("describeContainerHostPorts: expected %q, got %q", tc.expectedHost, result)
}
})
}
}
func TestDescribeContainers(t *testing.T) {
trueVal := true
testCases := []struct {