Skip to content

Commit

Permalink
Add UT
Browse files Browse the repository at this point in the history
  • Loading branch information
caseydavenport committed Oct 13, 2021
1 parent ab0b452 commit 9f18417
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/backend/k8s/conversion/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (c converter) IsHostNetworked(pod *kapiv1.Pod) bool {
}

func (c converter) HasIPAddress(pod *kapiv1.Pod) bool {
return pod.Status.PodIP != "" || pod.Annotations[AnnotationPodIP] != ""
return pod.Status.PodIP != "" || pod.Annotations[AnnotationPodIP] != "" || pod.Annotations[AnnotationAWSPodIPs] != ""
// Note: we don't need to check PodIPs and AnnotationPodIPs here, because those cannot be
// non-empty if the corresponding singular field is empty.
}
Expand All @@ -210,8 +210,8 @@ func getPodIPs(pod *kapiv1.Pod) ([]*cnet.IPNet, error) {
log.WithField("ip", ip).Debug("No PodStatus IPs, use Calico singular annotation")
podIPs = append(podIPs, ip)
} else if ips := pod.Annotations[AnnotationAWSPodIPs]; ips != "" {
log.WithField("ip", ip).Debug("No PodStatus IPs, use AWS VPC annotation")
podIPs = append(podIPs, ip)
log.WithField("ips", ips).Debug("No PodStatus IPs, use AWS VPC annotation")
podIPs = append(podIPs, strings.Split(ips, ",")...)
} else {
log.Debug("Pod has no IP")
return nil, nil
Expand Down
27 changes: 27 additions & 0 deletions lib/backend/k8s/conversion/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,33 @@ var _ = Describe("Test Pod conversion", func() {
Expect(wep.Value.(*libapiv3.WorkloadEndpoint).Spec.IPNetworks).To(ConsistOf("192.168.0.1/32", "fd5f:8067::1/128"))
})

It("should look for the AWS VPC CNI annotation for pod IPs", func() {
pod := kapiv1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "podA",
Namespace: "default",
Annotations: map[string]string{
"arbitrary": "annotation",
"vpc.amazonaws.com/pod-ips": "192.168.0.1",
},
Labels: map[string]string{
"labelA": "valueA",
"labelB": "valueB",
},
ResourceVersion: "1234",
},
Spec: kapiv1.PodSpec{
NodeName: "nodeA",
Containers: []kapiv1.Container{},
},
}

wep, err := podToWorkloadEndpoint(c, &pod)
Expect(err).NotTo(HaveOccurred())
Expect(c.HasIPAddress(&pod)).To(BeTrue())
Expect(wep.Value.(*libapiv3.WorkloadEndpoint).Spec.IPNetworks).To(ConsistOf("192.168.0.1/32"))
})

It("should handle IP annotations with /32 and /128", func() {
// In fact we create annotations with /32 and /128 suffixes, so check that we can
// also parse those.
Expand Down

0 comments on commit 9f18417

Please sign in to comment.