Skip to content

Commit deb3f78

Browse files
authored
fix eniInfo from annotation (kubernetes-sigs#1529)
1 parent 4506ade commit deb3f78

File tree

3 files changed

+142
-29
lines changed

3 files changed

+142
-29
lines changed

pkg/k8s/pod_info.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@ type PodInfo struct {
2323
Conditions []corev1.PodCondition
2424
PodIP string
2525

26-
ENIInfo *PodENIInfo
26+
ENIInfos []PodENIInfo
2727
}
2828

2929
// PodENIInfo is a json convertible structure that stores the Branch ENI details that can be
3030
// used by the CNI plugin or the component consuming the resource
3131
type PodENIInfo struct {
3232
// ENIID is the network interface id of the branch interface
3333
ENIID string `json:"eniId"`
34+
35+
// PrivateIP is the primary IP of the branch Network interface
36+
PrivateIP string `json:"privateIp"`
3437
}
3538

3639
// HasAnyOfReadinessGates returns whether podInfo has any of these readinessGates
@@ -81,10 +84,10 @@ func (i *PodInfo) LookupContainerPort(port intstr.IntOrString) (int64, error) {
8184
func buildPodInfo(pod *corev1.Pod) PodInfo {
8285
podKey := NamespacedName(pod)
8386

84-
var podENIInfo *PodENIInfo
87+
var podENIInfos []PodENIInfo
8588
// we kept podENIInfo as nil if the eniInfo via annotation is malformed.
86-
if eniInfo, err := buildPodENIInfo(pod); err == nil {
87-
podENIInfo = eniInfo
89+
if eniInfo, err := buildPodENIInfos(pod); err == nil {
90+
podENIInfos = eniInfo
8891
}
8992

9093
var containerPorts []corev1.ContainerPort
@@ -100,21 +103,21 @@ func buildPodInfo(pod *corev1.Pod) PodInfo {
100103
Conditions: pod.Status.Conditions,
101104
PodIP: pod.Status.PodIP,
102105

103-
ENIInfo: podENIInfo,
106+
ENIInfos: podENIInfos,
104107
}
105108
}
106109

107110
// buildPodENIInfo will construct PodENIInfo for given pod if any.
108-
func buildPodENIInfo(pod *corev1.Pod) (*PodENIInfo, error) {
111+
func buildPodENIInfos(pod *corev1.Pod) ([]PodENIInfo, error) {
109112
rawAnnotation, ok := pod.Annotations[annotationKeyPodENIInfo]
110113
if !ok {
111114
return nil, nil
112115
}
113116

114-
var podENIInfo PodENIInfo
115-
if err := json.Unmarshal([]byte(rawAnnotation), &podENIInfo); err != nil {
117+
var podENIInfos []PodENIInfo
118+
if err := json.Unmarshal([]byte(rawAnnotation), &podENIInfos); err != nil {
116119
return nil, err
117120
}
118121

119-
return &podENIInfo, nil
122+
return podENIInfos, nil
120123
}

pkg/k8s/pod_info_test.go

Lines changed: 123 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,109 @@ func Test_buildPodInfo(t *testing.T) {
411411
PodIP: "192.168.1.1",
412412
},
413413
},
414+
{
415+
name: "standard case - with ENIInfo",
416+
args: args{
417+
pod: &corev1.Pod{
418+
ObjectMeta: metav1.ObjectMeta{
419+
Namespace: "my-ns",
420+
Name: "pod-1",
421+
UID: "pod-uuid",
422+
Annotations: map[string]string{
423+
"vpc.amazonaws.com/pod-eni": `[{"eniId":"eni-06a712e1622fda4a0","ifAddress":"02:34:a5:25:0b:63","privateIp":"192.168.219.103","vlanId":3,"subnetCidr":"192.168.192.0/19"}]`,
424+
},
425+
},
426+
Spec: corev1.PodSpec{
427+
Containers: []corev1.Container{
428+
{
429+
Ports: []corev1.ContainerPort{
430+
{
431+
Name: "ssh",
432+
ContainerPort: 22,
433+
},
434+
{
435+
Name: "http",
436+
ContainerPort: 8080,
437+
},
438+
},
439+
},
440+
{
441+
Ports: []corev1.ContainerPort{
442+
{
443+
Name: "https",
444+
ContainerPort: 8443,
445+
},
446+
},
447+
},
448+
},
449+
ReadinessGates: []corev1.PodReadinessGate{
450+
{
451+
ConditionType: "ingress.k8s.aws/cond-1",
452+
},
453+
{
454+
ConditionType: "ingress.k8s.aws/cond-2",
455+
},
456+
},
457+
},
458+
Status: corev1.PodStatus{
459+
Conditions: []corev1.PodCondition{
460+
{
461+
Type: corev1.ContainersReady,
462+
Status: corev1.ConditionFalse,
463+
},
464+
{
465+
Type: "ingress.k8s.aws/cond-2",
466+
Status: corev1.ConditionTrue,
467+
},
468+
},
469+
PodIP: "192.168.1.1",
470+
},
471+
},
472+
},
473+
want: PodInfo{
474+
Key: types.NamespacedName{Namespace: "my-ns", Name: "pod-1"},
475+
UID: "pod-uuid",
476+
ContainerPorts: []corev1.ContainerPort{
477+
{
478+
Name: "ssh",
479+
ContainerPort: 22,
480+
},
481+
{
482+
Name: "http",
483+
ContainerPort: 8080,
484+
},
485+
{
486+
Name: "https",
487+
ContainerPort: 8443,
488+
},
489+
},
490+
ReadinessGates: []corev1.PodReadinessGate{
491+
{
492+
ConditionType: "ingress.k8s.aws/cond-1",
493+
},
494+
{
495+
ConditionType: "ingress.k8s.aws/cond-2",
496+
},
497+
},
498+
Conditions: []corev1.PodCondition{
499+
{
500+
Type: corev1.ContainersReady,
501+
Status: corev1.ConditionFalse,
502+
},
503+
{
504+
Type: "ingress.k8s.aws/cond-2",
505+
Status: corev1.ConditionTrue,
506+
},
507+
},
508+
PodIP: "192.168.1.1",
509+
ENIInfos: []PodENIInfo{
510+
{
511+
ENIID: "eni-06a712e1622fda4a0",
512+
PrivateIP: "192.168.219.103",
513+
},
514+
},
515+
},
516+
},
414517
}
415518
for _, tt := range tests {
416519
t.Run(tt.name, func(t *testing.T) {
@@ -427,24 +530,27 @@ func Test_buildPodENIInfo(t *testing.T) {
427530
tests := []struct {
428531
name string
429532
args args
430-
want *PodENIInfo
533+
want []PodENIInfo
431534
wantErr error
432535
}{
433-
//{
434-
// name: "pod-eni annotation exists and valid",
435-
// args: args{
436-
// pod: &corev1.Pod{
437-
// ObjectMeta: metav1.ObjectMeta{
438-
// Annotations: map[string]string{
439-
// "vpc.amazonaws.com/pod-eni": `[{"eniId":"eni-04df112df841b9465","ifAddress":"02:2d:47:ec:a5:e3","privateIp":"192.168.129.175","vlanId":1,"subnetCidr":"192.168.128.0/19"}]`,
440-
// },
441-
// },
442-
// },
443-
// },
444-
// want: &PodENIInfo{
445-
// ENIID: "eni-04df112df841b9465",
446-
// },
447-
//},
536+
{
537+
name: "pod-eni annotation exists and valid",
538+
args: args{
539+
pod: &corev1.Pod{
540+
ObjectMeta: metav1.ObjectMeta{
541+
Annotations: map[string]string{
542+
"vpc.amazonaws.com/pod-eni": `[{"eniId":"eni-06a712e1622fda4a0","ifAddress":"02:34:a5:25:0b:63","privateIp":"192.168.219.103","vlanId":3,"subnetCidr":"192.168.192.0/19"}]`,
543+
},
544+
},
545+
},
546+
},
547+
want: []PodENIInfo{
548+
{
549+
ENIID: "eni-06a712e1622fda4a0",
550+
PrivateIP: "192.168.219.103",
551+
},
552+
},
553+
},
448554
{
449555
name: "pod-eni annotation didn't exist",
450556
args: args{
@@ -472,7 +578,7 @@ func Test_buildPodENIInfo(t *testing.T) {
472578
}
473579
for _, tt := range tests {
474580
t.Run(tt.name, func(t *testing.T) {
475-
got, err := buildPodENIInfo(tt.args.pod)
581+
got, err := buildPodENIInfos(tt.args.pod)
476582
if tt.wantErr != nil {
477583
assert.EqualError(t, err, tt.wantErr.Error())
478584
} else {

pkg/networking/pod_eni_info_resolver.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,17 @@ func (r *defaultPodENIInfoResolver) resolveViaCascadedLookup(ctx context.Context
155155
func (r *defaultPodENIInfoResolver) resolveViaPodENIAnnotation(ctx context.Context, pods []k8s.PodInfo) (map[types.NamespacedName]ENIInfo, error) {
156156
podKeysByENIID := make(map[string][]types.NamespacedName)
157157
for _, pod := range pods {
158-
podENIInfo := pod.ENIInfo
159-
if podENIInfo == nil {
158+
var eniID string
159+
for _, podENIInfo := range pod.ENIInfos {
160+
if podENIInfo.PrivateIP == pod.PodIP {
161+
eniID = podENIInfo.ENIID
162+
}
163+
}
164+
if len(eniID) == 0 {
160165
continue
161166
}
162167

163168
podKey := pod.Key
164-
eniID := podENIInfo.ENIID
165169
podKeysByENIID[eniID] = append(podKeysByENIID[eniID], podKey)
166170
}
167171
if len(podKeysByENIID) == 0 {

0 commit comments

Comments
 (0)