Skip to content

Commit

Permalink
pod should use mac and ips provider by multus firstly (#4800)
Browse files Browse the repository at this point in the history
* pod should use mac and ips provider by multus firstly

---------

Signed-off-by: clyi <[email protected]>
  • Loading branch information
changluyi authored Dec 27, 2024
1 parent 79acd89 commit 93759b9
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
31 changes: 28 additions & 3 deletions pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -1135,11 +1135,21 @@ func (c *Controller) syncKubeOvnNet(pod *v1.Pod, podNets []*kubeovnNet) (*v1.Pod
targetPortNameList := strset.NewWithSize(len(podNets))
portsNeedToDel := []string{}
annotationsNeedToDel := []string{}
annotationsNeedToAdd := make(map[string]string)
subnetUsedByPort := make(map[string]string)

for _, podNet := range podNets {
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
}
}

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

if len(portsNeedToDel) == 0 {
if len(portsNeedToDel) == 0 && len(annotationsNeedToAdd) == 0 {
return pod, nil
}

Expand Down Expand Up @@ -1190,6 +1200,11 @@ func (c *Controller) syncKubeOvnNet(pod *v1.Pod, podNets []*kubeovnNet) (*v1.Pod
}
}
}

for key, value := range annotationsNeedToAdd {
patch[key] = value
}

if len(patch) == 0 {
return pod, nil
}
Expand Down Expand Up @@ -1445,6 +1460,8 @@ type kubeovnNet struct {
Subnet *kubeovnv1.Subnet
IsDefault bool
AllowLiveMigration bool
IPRequest string
MacRequest string
}

func (c *Controller) getPodAttachmentNet(pod *v1.Pod) ([]*kubeovnNet, error) {
Expand Down Expand Up @@ -1522,13 +1539,21 @@ func (c *Controller) getPodAttachmentNet(pod *v1.Pod) ([]*kubeovnNet, error) {
return nil, err
}
}
result = append(result, &kubeovnNet{

ret := &kubeovnNet{
Type: providerTypeOriginal,
ProviderName: providerName,
Subnet: subnet,
IsDefault: isDefault,
AllowLiveMigration: allowLiveMigration,
})
}

if len(attach.IPRequest) != 0 {
ret.IPRequest = strings.Join(attach.IPRequest, ",")
}

ret.MacRequest = attach.MacRequest
result = append(result, ret)
} else {
providerName = fmt.Sprintf("%s.%s", attach.Name, attach.Namespace)
for _, subnet := range subnets {
Expand Down
44 changes: 44 additions & 0 deletions test/e2e/multus/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"flag"
"fmt"
"strings"
"testing"

nadv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
Expand Down Expand Up @@ -498,4 +499,47 @@ 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)

randomIPArray := strings.Split(randomIP, ",")
var requestIPString string
for i, ip := range randomIPArray {
if i == len(randomIPArray)-1 {
requestIPString += fmt.Sprintf(`"%s"`, ip)
} else {
requestIPString += fmt.Sprintf(`"%s",`, ip)
}
}

framework.Logf("requestIPString: %s", requestIPString)
annotations := map[string]string{nadv1.NetworkAttachmentAnnot: fmt.Sprintf(`[{"name": "%s", "namespace": "%s", "mac": "%s", "ips": [%s]}]`, nad.Name, nad.Namespace, mac, requestIPString)}
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)

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(strings.Join(randomIPArray, ","), retIP)
})
})

0 comments on commit 93759b9

Please sign in to comment.