Skip to content

Commit

Permalink
add e2e test script
Browse files Browse the repository at this point in the history
Signed-off-by: clyi <[email protected]>
  • Loading branch information
changluyi committed Dec 11, 2024
1 parent d8198f3 commit 92158c6
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 27 deletions.
50 changes: 23 additions & 27 deletions pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -1163,10 +1163,12 @@ func (c *Controller) syncKubeOvnNet(cachedPod, pod *v1.Pod, podNets []*kubeovnNe
portName := ovs.PodNameToPortName(podName, pod.Namespace, podNet.ProviderName)
targetPortNameList.Add(portName)
if podNet.IPRequest != "" {
klog.Infof("pod %s/%s use custom IP %s for provider %s", pod.Namespace, pod.Name, podNet.IPRequest, podNet.ProviderName)
annotationsNeedToAdd[fmt.Sprintf(util.IPAddressAnnotationTemplate, podNet.ProviderName)] = podNet.IPRequest
}

if podNet.MacRequest != "" {
klog.Infof("pod %s/%s use custom MAC %s for provider %s", pod.Namespace, pod.Name, podNet.MacRequest, podNet.ProviderName)
annotationsNeedToAdd[fmt.Sprintf(util.MacAddressAnnotationTemplate, podNet.ProviderName)] = podNet.MacRequest
}
}
Expand Down Expand Up @@ -1194,42 +1196,39 @@ func (c *Controller) syncKubeOvnNet(cachedPod, pod *v1.Pod, podNets []*kubeovnNe
return pod, nil
}

if len(portsNeedToDel) > 0 {
for _, portNeedDel := range portsNeedToDel {
klog.Infof("release port %s for pod %s", portNeedDel, podName)
if subnet, ok := c.ipam.Subnets[subnetUsedByPort[portNeedDel]]; ok {
subnet.ReleaseAddressWithNicName(podName, portNeedDel)
}
if err := c.OVNNbClient.DeleteLogicalSwitchPort(portNeedDel); err != nil {
klog.Errorf("failed to delete lsp %s, %v", portNeedDel, err)
for _, portNeedDel := range portsNeedToDel {
klog.Infof("release port %s for pod %s", portNeedDel, podName)
if subnet, ok := c.ipam.Subnets[subnetUsedByPort[portNeedDel]]; ok {
subnet.ReleaseAddressWithNicName(podName, portNeedDel)
}
if err := c.OVNNbClient.DeleteLogicalSwitchPort(portNeedDel); err != nil {
klog.Errorf("failed to delete lsp %s, %v", portNeedDel, err)
return nil, err
}
if err := c.config.KubeOvnClient.KubeovnV1().IPs().Delete(context.Background(), portNeedDel, metav1.DeleteOptions{}); err != nil {
if !k8serrors.IsNotFound(err) {
klog.Errorf("failed to delete ip %s, %v", portNeedDel, err)
return nil, err
}
if err := c.config.KubeOvnClient.KubeovnV1().IPs().Delete(context.Background(), portNeedDel, metav1.DeleteOptions{}); err != nil {
if !k8serrors.IsNotFound(err) {
klog.Errorf("failed to delete ip %s, %v", portNeedDel, err)
return nil, err
}
}
}
}

for _, providerName := range annotationsNeedToDel {
for annotationKey := range pod.Annotations {
if strings.HasPrefix(annotationKey, providerName) {
delete(pod.Annotations, annotationKey)
}
for _, providerName := range annotationsNeedToDel {
for annotationKey := range pod.Annotations {
if strings.HasPrefix(annotationKey, providerName) {
delete(pod.Annotations, annotationKey)
}
}
}

if len(annotationsNeedToAdd) > 0 {
for annotationKey, annotationValue := range annotationsNeedToAdd {
pod.Annotations[annotationKey] = annotationValue
}
for annotationKey, annotationValue := range annotationsNeedToAdd {
pod.Annotations[annotationKey] = annotationValue
}

if len(cachedPod.Annotations) == len(pod.Annotations) {
return pod, nil
}

patch, err := util.GenerateMergePatchPayload(cachedPod, pod)
if err != nil {
klog.Errorf("failed to generate patch payload for pod '%s', %v", pod.Name, err)
Expand Down Expand Up @@ -1572,10 +1571,7 @@ func (c *Controller) getPodAttachmentNet(pod *v1.Pod) ([]*kubeovnNet, error) {
ret.IPRequest = attach.IPRequest[0]
}

if attach.MacRequest != "" {
ret.MacRequest = attach.MacRequest
}

ret.MacRequest = attach.MacRequest
result = append(result, ret)
} else {
providerName = fmt.Sprintf("%s.%s", attach.Name, attach.Namespace)
Expand Down
32 changes: 32 additions & 0 deletions test/e2e/multus/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,4 +431,36 @@ var _ = framework.SerialDescribe("[group:multus]", func() {
framework.ExpectContainElement(actualRoutes, request.Route{Destination: ipv6RouteDst, Gateway: ipv6RouteGw})
}
})

framework.ConformanceIt("should be able to use mac and ip provided by k8s.v1.cni.cncf.io/networks annotation", func() {
provider := fmt.Sprintf("%s.%s.%s", nadName, namespaceName, util.OvnProvider)
ginkgo.By("Creating network attachment definition " + nadName)
nad := framework.MakeOVNNetworkAttachmentDefinition(nadName, namespaceName, provider, nil)
nad = nadClient.Create(nad)
framework.Logf("created network attachment definition config:\n%s", nad.Spec.Config)

ginkgo.By("Creating subnet " + subnetName)
subnet = framework.MakeSubnet(subnetName, "", cidr, "", "", provider, nil, nil, nil)
subnet = subnetClient.CreateSync(subnet)

ginkgo.By("Creating pod " + podName)
mac := "00:00:00:11:22:33"
randomIP := framework.RandomIPs(subnet.Spec.CIDRBlock, "", 1)

annotations := map[string]string{nadv1.NetworkAttachmentAnnot: fmt.Sprintf(`[{"name": "%s", "namespace": "%s", "mac": "%s", "ips": ["%s"]}]`, nad.Name, nad.Namespace, mac, randomIP)}
annotations[fmt.Sprintf(util.LogicalSwitchAnnotationTemplate, provider)] = subnetName

cmd := []string{"sh", "-c", "sleep infinity"}
pod := framework.MakePrivilegedPod(namespaceName, podName, nil, annotations, f.KubeOVNImage, cmd, nil)
pod = podClient.CreateSync(pod)

Check failure on line 455 in test/e2e/multus/e2e_test.go

View workflow job for this annotation

GitHub Actions / Multus Conformance E2E (dual)

It 12/11/24 11:22:01.55

ginkgo.By("Validating pod annotations")
framework.ExpectHaveKey(pod.Annotations, nadv1.NetworkStatusAnnot)
framework.Logf("pod network status:\n%s", pod.Annotations[nadv1.NetworkStatusAnnot])
retMac := pod.Annotations[fmt.Sprintf(util.MacAddressAnnotationTemplate, provider)]
retIP := pod.Annotations[fmt.Sprintf(util.IPAddressAnnotationTemplate, provider)]

framework.ExpectEqual(mac, retMac)
framework.ExpectEqual(randomIP, retIP)
})
})

0 comments on commit 92158c6

Please sign in to comment.