Merge pull request #125444 from babugeet/babugeet-patch-1

Added logic to check portnumber by name in sidecar container (init)
This commit is contained in:
Kubernetes Prow Robot 2024-06-12 07:44:42 -07:00 committed by GitHub
commit cf0b457690
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 68 additions and 1 deletions

View File

@ -31,6 +31,12 @@ func LookupContainerPortNumberByName(pod v1.Pod, name string) (int32, error) {
}
}
}
for _, ctr := range pod.Spec.InitContainers {
for _, ctrportspec := range ctr.Ports {
if ctrportspec.Name == name {
return ctrportspec.ContainerPort, nil
}
}
}
return int32(-1), fmt.Errorf("Pod '%s' does not have a named port '%s'", pod.Name, name)
}

View File

@ -71,6 +71,67 @@ func TestLookupContainerPortNumberByName(t *testing.T) {
portnum: int32(0),
err: true,
},
{
name: "test success 2",
pod: v1.Pod{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Ports: []v1.ContainerPort{
{
Name: "https",
ContainerPort: int32(443)},
{
Name: "http",
ContainerPort: int32(80)},
},
},
},
InitContainers: []v1.Container{
{
Ports: []v1.ContainerPort{
{
Name: "sql",
ContainerPort: int32(3306)},
},
},
},
},
},
portname: "sql",
portnum: int32(3306),
err: false,
}, {
name: "test failure 2",
pod: v1.Pod{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Ports: []v1.ContainerPort{
{
Name: "https",
ContainerPort: int32(443)},
{
Name: "http",
ContainerPort: int32(80)},
},
},
},
InitContainers: []v1.Container{
{
Ports: []v1.ContainerPort{
{
Name: "sql",
ContainerPort: int32(3306)},
},
},
},
},
},
portname: "metrics",
portnum: int32(0),
err: true,
},
}
for _, tt := range tests {