Skip to content

Commit 93759b9

Browse files
authored
pod should use mac and ips provider by multus firstly (#4800)
* pod should use mac and ips provider by multus firstly --------- Signed-off-by: clyi <clyi@alauda.io>
1 parent 79acd89 commit 93759b9

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

pkg/controller/pod.go

+28-3
Original file line numberDiff line numberDiff line change
@@ -1135,11 +1135,21 @@ func (c *Controller) syncKubeOvnNet(pod *v1.Pod, podNets []*kubeovnNet) (*v1.Pod
11351135
targetPortNameList := strset.NewWithSize(len(podNets))
11361136
portsNeedToDel := []string{}
11371137
annotationsNeedToDel := []string{}
1138+
annotationsNeedToAdd := make(map[string]string)
11381139
subnetUsedByPort := make(map[string]string)
11391140

11401141
for _, podNet := range podNets {
11411142
portName := ovs.PodNameToPortName(podName, pod.Namespace, podNet.ProviderName)
11421143
targetPortNameList.Add(portName)
1144+
if podNet.IPRequest != "" {
1145+
klog.Infof("pod %s/%s use custom IP %s for provider %s", pod.Namespace, pod.Name, podNet.IPRequest, podNet.ProviderName)
1146+
annotationsNeedToAdd[fmt.Sprintf(util.IPAddressAnnotationTemplate, podNet.ProviderName)] = podNet.IPRequest
1147+
}
1148+
1149+
if podNet.MacRequest != "" {
1150+
klog.Infof("pod %s/%s use custom MAC %s for provider %s", pod.Namespace, pod.Name, podNet.MacRequest, podNet.ProviderName)
1151+
annotationsNeedToAdd[fmt.Sprintf(util.MacAddressAnnotationTemplate, podNet.ProviderName)] = podNet.MacRequest
1152+
}
11431153
}
11441154

11451155
ports, err := c.OVNNbClient.ListNormalLogicalSwitchPorts(true, map[string]string{"pod": key})
@@ -1161,7 +1171,7 @@ func (c *Controller) syncKubeOvnNet(pod *v1.Pod, podNets []*kubeovnNet) (*v1.Pod
11611171
}
11621172
}
11631173

1164-
if len(portsNeedToDel) == 0 {
1174+
if len(portsNeedToDel) == 0 && len(annotationsNeedToAdd) == 0 {
11651175
return pod, nil
11661176
}
11671177

@@ -1190,6 +1200,11 @@ func (c *Controller) syncKubeOvnNet(pod *v1.Pod, podNets []*kubeovnNet) (*v1.Pod
11901200
}
11911201
}
11921202
}
1203+
1204+
for key, value := range annotationsNeedToAdd {
1205+
patch[key] = value
1206+
}
1207+
11931208
if len(patch) == 0 {
11941209
return pod, nil
11951210
}
@@ -1445,6 +1460,8 @@ type kubeovnNet struct {
14451460
Subnet *kubeovnv1.Subnet
14461461
IsDefault bool
14471462
AllowLiveMigration bool
1463+
IPRequest string
1464+
MacRequest string
14481465
}
14491466

14501467
func (c *Controller) getPodAttachmentNet(pod *v1.Pod) ([]*kubeovnNet, error) {
@@ -1522,13 +1539,21 @@ func (c *Controller) getPodAttachmentNet(pod *v1.Pod) ([]*kubeovnNet, error) {
15221539
return nil, err
15231540
}
15241541
}
1525-
result = append(result, &kubeovnNet{
1542+
1543+
ret := &kubeovnNet{
15261544
Type: providerTypeOriginal,
15271545
ProviderName: providerName,
15281546
Subnet: subnet,
15291547
IsDefault: isDefault,
15301548
AllowLiveMigration: allowLiveMigration,
1531-
})
1549+
}
1550+
1551+
if len(attach.IPRequest) != 0 {
1552+
ret.IPRequest = strings.Join(attach.IPRequest, ",")
1553+
}
1554+
1555+
ret.MacRequest = attach.MacRequest
1556+
result = append(result, ret)
15321557
} else {
15331558
providerName = fmt.Sprintf("%s.%s", attach.Name, attach.Namespace)
15341559
for _, subnet := range subnets {

test/e2e/multus/e2e_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"flag"
66
"fmt"
7+
"strings"
78
"testing"
89

910
nadv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
@@ -498,4 +499,47 @@ var _ = framework.SerialDescribe("[group:multus]", func() {
498499
framework.ExpectContainElement(actualRoutes, request.Route{Destination: ipv6RouteDst, Gateway: ipv6RouteGw})
499500
}
500501
})
502+
503+
framework.ConformanceIt("should be able to use mac and ip provided by k8s.v1.cni.cncf.io/networks annotation", func() {
504+
provider := fmt.Sprintf("%s.%s.%s", nadName, namespaceName, util.OvnProvider)
505+
ginkgo.By("Creating network attachment definition " + nadName)
506+
nad := framework.MakeOVNNetworkAttachmentDefinition(nadName, namespaceName, provider, nil)
507+
nad = nadClient.Create(nad)
508+
framework.Logf("created network attachment definition config:\n%s", nad.Spec.Config)
509+
510+
ginkgo.By("Creating subnet " + subnetName)
511+
subnet = framework.MakeSubnet(subnetName, "", cidr, "", "", provider, nil, nil, nil)
512+
subnet = subnetClient.CreateSync(subnet)
513+
514+
ginkgo.By("Creating pod " + podName)
515+
mac := "00:00:00:11:22:33"
516+
randomIP := framework.RandomIPs(subnet.Spec.CIDRBlock, "", 1)
517+
518+
randomIPArray := strings.Split(randomIP, ",")
519+
var requestIPString string
520+
for i, ip := range randomIPArray {
521+
if i == len(randomIPArray)-1 {
522+
requestIPString += fmt.Sprintf(`"%s"`, ip)
523+
} else {
524+
requestIPString += fmt.Sprintf(`"%s",`, ip)
525+
}
526+
}
527+
528+
framework.Logf("requestIPString: %s", requestIPString)
529+
annotations := map[string]string{nadv1.NetworkAttachmentAnnot: fmt.Sprintf(`[{"name": "%s", "namespace": "%s", "mac": "%s", "ips": [%s]}]`, nad.Name, nad.Namespace, mac, requestIPString)}
530+
annotations[fmt.Sprintf(util.LogicalSwitchAnnotationTemplate, provider)] = subnetName
531+
532+
cmd := []string{"sh", "-c", "sleep infinity"}
533+
pod := framework.MakePrivilegedPod(namespaceName, podName, nil, annotations, f.KubeOVNImage, cmd, nil)
534+
pod = podClient.CreateSync(pod)
535+
536+
ginkgo.By("Validating pod annotations")
537+
framework.ExpectHaveKey(pod.Annotations, nadv1.NetworkStatusAnnot)
538+
framework.Logf("pod network status:\n%s", pod.Annotations[nadv1.NetworkStatusAnnot])
539+
retMac := pod.Annotations[fmt.Sprintf(util.MacAddressAnnotationTemplate, provider)]
540+
retIP := pod.Annotations[fmt.Sprintf(util.IPAddressAnnotationTemplate, provider)]
541+
542+
framework.ExpectEqual(mac, retMac)
543+
framework.ExpectEqual(strings.Join(randomIPArray, ","), retIP)
544+
})
501545
})

0 commit comments

Comments
 (0)