Skip to content

Commit d21595e

Browse files
committed
pod should use mac and ips provider by multus firstly
Signed-off-by: clyi <clyi@alauda.io>
1 parent e04c248 commit d21595e

File tree

2 files changed

+64
-3
lines changed

2 files changed

+64
-3
lines changed

pkg/controller/pod.go

+32-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,15 @@ func (c *Controller) syncKubeOvnNet(pod *v1.Pod, podNets []*kubeovnNet) (*v1.Pod
11901200
}
11911201
}
11921202
}
1203+
1204+
for _, providerName := range annotationsNeedToAdd {
1205+
for key, value := range pod.Annotations {
1206+
if strings.HasPrefix(key, providerName) {
1207+
patch[key] = value
1208+
}
1209+
}
1210+
}
1211+
11931212
if len(patch) == 0 {
11941213
return pod, nil
11951214
}
@@ -1445,6 +1464,8 @@ type kubeovnNet struct {
14451464
Subnet *kubeovnv1.Subnet
14461465
IsDefault bool
14471466
AllowLiveMigration bool
1467+
IPRequest string
1468+
MacRequest string
14481469
}
14491470

14501471
func (c *Controller) getPodAttachmentNet(pod *v1.Pod) ([]*kubeovnNet, error) {
@@ -1522,13 +1543,21 @@ func (c *Controller) getPodAttachmentNet(pod *v1.Pod) ([]*kubeovnNet, error) {
15221543
return nil, err
15231544
}
15241545
}
1525-
result = append(result, &kubeovnNet{
1546+
1547+
ret := &kubeovnNet{
15261548
Type: providerTypeOriginal,
15271549
ProviderName: providerName,
15281550
Subnet: subnet,
15291551
IsDefault: isDefault,
15301552
AllowLiveMigration: allowLiveMigration,
1531-
})
1553+
}
1554+
1555+
if len(attach.IPRequest) != 0 {
1556+
ret.IPRequest = attach.IPRequest[0]
1557+
}
1558+
1559+
ret.MacRequest = attach.MacRequest
1560+
result = append(result, ret)
15321561
} else {
15331562
providerName = fmt.Sprintf("%s.%s", attach.Name, attach.Namespace)
15341563
for _, subnet := range subnets {

test/e2e/multus/e2e_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -498,4 +498,36 @@ var _ = framework.SerialDescribe("[group:multus]", func() {
498498
framework.ExpectContainElement(actualRoutes, request.Route{Destination: ipv6RouteDst, Gateway: ipv6RouteGw})
499499
}
500500
})
501+
502+
framework.ConformanceIt("should be able to use mac and ip provided by k8s.v1.cni.cncf.io/networks annotation", func() {
503+
provider := fmt.Sprintf("%s.%s.%s", nadName, namespaceName, util.OvnProvider)
504+
ginkgo.By("Creating network attachment definition " + nadName)
505+
nad := framework.MakeOVNNetworkAttachmentDefinition(nadName, namespaceName, provider, nil)
506+
nad = nadClient.Create(nad)
507+
framework.Logf("created network attachment definition config:\n%s", nad.Spec.Config)
508+
509+
ginkgo.By("Creating subnet " + subnetName)
510+
subnet = framework.MakeSubnet(subnetName, "", cidr, "", "", provider, nil, nil, nil)
511+
subnet = subnetClient.CreateSync(subnet)
512+
513+
ginkgo.By("Creating pod " + podName)
514+
mac := "00:00:00:11:22:33"
515+
randomIP := framework.RandomIPs(subnet.Spec.CIDRBlock, "", 1)
516+
517+
annotations := map[string]string{nadv1.NetworkAttachmentAnnot: fmt.Sprintf(`[{"name": "%s", "namespace": "%s", "mac": "%s", "ips": ["%s"]}]`, nad.Name, nad.Namespace, mac, randomIP)}
518+
annotations[fmt.Sprintf(util.LogicalSwitchAnnotationTemplate, provider)] = subnetName
519+
520+
cmd := []string{"sh", "-c", "sleep infinity"}
521+
pod := framework.MakePrivilegedPod(namespaceName, podName, nil, annotations, f.KubeOVNImage, cmd, nil)
522+
pod = podClient.CreateSync(pod)
523+
524+
ginkgo.By("Validating pod annotations")
525+
framework.ExpectHaveKey(pod.Annotations, nadv1.NetworkStatusAnnot)
526+
framework.Logf("pod network status:\n%s", pod.Annotations[nadv1.NetworkStatusAnnot])
527+
retMac := pod.Annotations[fmt.Sprintf(util.MacAddressAnnotationTemplate, provider)]
528+
retIP := pod.Annotations[fmt.Sprintf(util.IPAddressAnnotationTemplate, provider)]
529+
530+
framework.ExpectEqual(mac, retMac)
531+
framework.ExpectEqual(randomIP, retIP)
532+
})
501533
})

0 commit comments

Comments
 (0)