Skip to content

Commit 9903901

Browse files
feat: Allow usage of namedPorts for sidecar containers (#4237)
Signed-off-by: heshamelsherif97 <[email protected]>
1 parent 31ec9f0 commit 9903901

File tree

4 files changed

+133
-3
lines changed

4 files changed

+133
-3
lines changed

pkg/k8s/pod_info.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package k8s
22

33
import (
44
"encoding/json"
5+
56
"github.com/pkg/errors"
67
corev1 "k8s.io/api/core/v1"
78
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -97,6 +98,12 @@ func buildPodInfo(pod *corev1.Pod) PodInfo {
9798
for _, podContainer := range pod.Spec.Containers {
9899
containerPorts = append(containerPorts, podContainer.Ports...)
99100
}
101+
// also support sidecar container (initContainer with restartPolicy=Always)
102+
for _, podContainer := range pod.Spec.InitContainers {
103+
if podContainer.RestartPolicy != nil && *podContainer.RestartPolicy == corev1.ContainerRestartPolicyAlways {
104+
containerPorts = append(containerPorts, podContainer.Ports...)
105+
}
106+
}
100107
return PodInfo{
101108
Key: podKey,
102109
UID: pod.UID,

pkg/k8s/pod_info_test.go

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package k8s
22

33
import (
44
"errors"
5+
"testing"
6+
"time"
7+
58
"github.com/stretchr/testify/assert"
69
corev1 "k8s.io/api/core/v1"
710
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
811
"k8s.io/apimachinery/pkg/types"
912
"k8s.io/apimachinery/pkg/util/intstr"
10-
"testing"
11-
"time"
1213
)
1314

1415
func TestPodInfo_HasAnyOfReadinessGates(t *testing.T) {
@@ -317,6 +318,8 @@ func Test_buildPodInfo(t *testing.T) {
317318

318319
timeNow := time.Now()
319320

321+
initContainerRestartPolicyAlways := corev1.ContainerRestartPolicyAlways
322+
320323
tests := []struct {
321324
name string
322325
args args
@@ -358,6 +361,29 @@ func Test_buildPodInfo(t *testing.T) {
358361
},
359362
},
360363
},
364+
InitContainers: []corev1.Container{
365+
{
366+
RestartPolicy: &initContainerRestartPolicyAlways,
367+
Ports: []corev1.ContainerPort{
368+
{
369+
Name: "dns",
370+
ContainerPort: 53,
371+
},
372+
{
373+
Name: "ftp",
374+
ContainerPort: 21,
375+
},
376+
},
377+
},
378+
{
379+
Ports: []corev1.ContainerPort{
380+
{
381+
Name: "smtp",
382+
ContainerPort: 25,
383+
},
384+
},
385+
},
386+
},
361387
ReadinessGates: []corev1.PodReadinessGate{
362388
{
363389
ConditionType: "ingress.k8s.aws/cond-1",
@@ -398,6 +424,14 @@ func Test_buildPodInfo(t *testing.T) {
398424
Name: "https",
399425
ContainerPort: 8443,
400426
},
427+
{
428+
Name: "dns",
429+
ContainerPort: 53,
430+
},
431+
{
432+
Name: "ftp",
433+
ContainerPort: 21,
434+
},
401435
},
402436
ReadinessGates: []corev1.PodReadinessGate{
403437
{
@@ -461,6 +495,29 @@ func Test_buildPodInfo(t *testing.T) {
461495
},
462496
},
463497
},
498+
InitContainers: []corev1.Container{
499+
{
500+
RestartPolicy: &initContainerRestartPolicyAlways,
501+
Ports: []corev1.ContainerPort{
502+
{
503+
Name: "dns",
504+
ContainerPort: 53,
505+
},
506+
{
507+
Name: "ftp",
508+
ContainerPort: 21,
509+
},
510+
},
511+
},
512+
{
513+
Ports: []corev1.ContainerPort{
514+
{
515+
Name: "smtp",
516+
ContainerPort: 25,
517+
},
518+
},
519+
},
520+
},
464521
ReadinessGates: []corev1.PodReadinessGate{
465522
{
466523
ConditionType: "ingress.k8s.aws/cond-1",
@@ -501,6 +558,14 @@ func Test_buildPodInfo(t *testing.T) {
501558
Name: "https",
502559
ContainerPort: 8443,
503560
},
561+
{
562+
Name: "dns",
563+
ContainerPort: 53,
564+
},
565+
{
566+
Name: "ftp",
567+
ContainerPort: 21,
568+
},
504569
},
505570
ReadinessGates: []corev1.PodReadinessGate{
506571
{

pkg/k8s/pod_utils.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ func LookupContainerPort(pod *corev1.Pod, port intstr.IntOrString) (int64, error
5353
}
5454
}
5555
}
56+
// also support sidecar container (initContainer with restartPolicy=Always)
57+
for _, podContainer := range pod.Spec.InitContainers {
58+
if podContainer.RestartPolicy != nil && *podContainer.RestartPolicy == corev1.ContainerRestartPolicyAlways {
59+
for _, podPort := range podContainer.Ports {
60+
if podPort.Name == port.StrVal {
61+
return int64(podPort.ContainerPort), nil
62+
}
63+
}
64+
}
65+
}
5666
case intstr.Int:
5767
return int64(port.IntVal), nil
5868
}

pkg/k8s/pod_utils_test.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package k8s
22

33
import (
4+
"testing"
5+
46
"github.com/pkg/errors"
57
"github.com/stretchr/testify/assert"
68
corev1 "k8s.io/api/core/v1"
79
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
810
"k8s.io/apimachinery/pkg/util/intstr"
9-
"testing"
1011
)
1112

1213
func TestIsPodHasReadinessGate(t *testing.T) {
@@ -178,6 +179,7 @@ func TestGetPodCondition(t *testing.T) {
178179
}
179180

180181
func TestLookupContainerPort(t *testing.T) {
182+
initContainerRestartPolicyAlways := corev1.ContainerRestartPolicyAlways
181183
pod := &corev1.Pod{
182184
ObjectMeta: metav1.ObjectMeta{
183185
Namespace: "ns",
@@ -205,6 +207,28 @@ func TestLookupContainerPort(t *testing.T) {
205207
},
206208
},
207209
},
210+
InitContainers: []corev1.Container{
211+
{
212+
RestartPolicy: &initContainerRestartPolicyAlways,
213+
Ports: []corev1.ContainerPort{
214+
{
215+
ContainerPort: 53,
216+
},
217+
{
218+
Name: "ftp",
219+
ContainerPort: 21,
220+
},
221+
},
222+
},
223+
{
224+
Ports: []corev1.ContainerPort{
225+
{
226+
Name: "smtp",
227+
ContainerPort: 25,
228+
},
229+
},
230+
},
231+
},
208232
},
209233
}
210234
type args struct {
@@ -233,6 +257,14 @@ func TestLookupContainerPort(t *testing.T) {
233257
},
234258
want: 80,
235259
},
260+
{
261+
name: "named pod within pod spec (as an initContainer (sidecar)) can be found",
262+
args: args{
263+
pod: pod,
264+
port: intstr.FromString("ftp"),
265+
},
266+
want: 21,
267+
},
236268
{
237269
name: "named pod within pod spec cannot be found",
238270
args: args{
@@ -241,6 +273,14 @@ func TestLookupContainerPort(t *testing.T) {
241273
},
242274
wantErr: errors.New("unable to find port https on pod ns/default"),
243275
},
276+
{
277+
name: "named pod within initContainer that is not a sidecar cannot be found",
278+
args: args{
279+
pod: pod,
280+
port: intstr.FromString("smtp"),
281+
},
282+
wantErr: errors.New("unable to find port smtp on pod ns/default"),
283+
},
244284
{
245285
name: "numerical pod within pod spec can be found",
246286
args: args{
@@ -249,6 +289,14 @@ func TestLookupContainerPort(t *testing.T) {
249289
},
250290
want: 9999,
251291
},
292+
{
293+
name: "numerical pod within pod spec (as an initContainer (sidecar)) can be found",
294+
args: args{
295+
pod: pod,
296+
port: intstr.FromInt(53),
297+
},
298+
want: 53,
299+
},
252300
{
253301
name: "numerical pod not within pod spec should still be found",
254302
args: args{

0 commit comments

Comments
 (0)