Skip to content

Commit 951f89c

Browse files
committed
adapt ippool annotation (#2678)
* adapt ippool annotation * append e2e for ippool separated by comma
1 parent 96e8be6 commit 951f89c

File tree

2 files changed

+104
-31
lines changed

2 files changed

+104
-31
lines changed

pkg/controller/pod.go

+36-31
Original file line numberDiff line numberDiff line change
@@ -1438,49 +1438,54 @@ func (c *Controller) acquireAddress(pod *v1.Pod, podNet *kubeovnNet) (string, st
14381438
}
14391439

14401440
// IPPool allocate
1441-
ipPool := strings.Split(pod.Annotations[fmt.Sprintf(util.IpPoolAnnotationTemplate, podNet.ProviderName)], ";")
1442-
for i, ip := range ipPool {
1443-
ipPool[i] = strings.TrimSpace(ip)
1444-
}
1441+
if pod.Annotations[fmt.Sprintf(util.IpPoolAnnotationTemplate, podNet.ProviderName)] != "" {
1442+
var ipPool []string
1443+
if strings.Contains(pod.Annotations[fmt.Sprintf(util.IpPoolAnnotationTemplate, podNet.ProviderName)], ";") {
1444+
ipPool = strings.Split(pod.Annotations[fmt.Sprintf(util.IpPoolAnnotationTemplate, podNet.ProviderName)], ";")
1445+
} else {
1446+
ipPool = strings.Split(pod.Annotations[fmt.Sprintf(util.IpPoolAnnotationTemplate, podNet.ProviderName)], ",")
1447+
}
1448+
for i, ip := range ipPool {
1449+
ipPool[i] = strings.TrimSpace(ip)
1450+
}
14451451

1446-
if !isStsPod {
1447-
for _, net := range nsNets {
1448-
for _, staticIPs := range ipPool {
1449-
ipProtocol := util.CheckProtocol(staticIPs)
1450-
for _, staticIP := range strings.Split(staticIPs, ",") {
1451-
if assignedPod, ok := c.ipam.IsIPAssignedToOtherPod(staticIP, net.Subnet.Name, key); ok {
1452+
if !isStsPod {
1453+
for _, net := range nsNets {
1454+
for _, staticIP := range ipPool {
1455+
var checkIP string
1456+
ipProtocol := util.CheckProtocol(staticIP)
1457+
if ipProtocol == kubeovnv1.ProtocolDual {
1458+
checkIP = strings.Split(staticIP, ",")[0]
1459+
} else {
1460+
checkIP = staticIP
1461+
}
1462+
1463+
if assignedPod, ok := c.ipam.IsIPAssignedToOtherPod(checkIP, net.Subnet.Name, key); ok {
14521464
klog.Errorf("static address %s for %s has been assigned to %s", staticIP, key, assignedPod)
14531465
continue
14541466
}
14551467

1456-
if ipProtocol != kubeovnv1.ProtocolDual {
1457-
v4IP, v6IP, mac, err = c.acquireStaticAddress(key, portName, staticIP, macStr, net.Subnet.Name, net.AllowLiveMigration)
1458-
if err == nil {
1459-
return v4IP, v6IP, mac, net.Subnet, nil
1460-
}
1461-
}
1462-
}
1463-
if ipProtocol == kubeovnv1.ProtocolDual {
1464-
v4IP, v6IP, mac, err = c.acquireStaticAddress(key, portName, staticIPs, macStr, net.Subnet.Name, net.AllowLiveMigration)
1468+
v4IP, v6IP, mac, err = c.acquireStaticAddress(key, portName, staticIP, macStr, net.Subnet.Name, net.AllowLiveMigration)
14651469
if err == nil {
14661470
return v4IP, v6IP, mac, net.Subnet, nil
14671471
}
14681472
}
14691473
}
1470-
}
1471-
klog.Errorf("acquire address %s for %s failed, %v", pod.Annotations[fmt.Sprintf(util.IpPoolAnnotationTemplate, podNet.ProviderName)], key, err)
1472-
} else {
1473-
tempStrs := strings.Split(pod.Name, "-")
1474-
numStr := tempStrs[len(tempStrs)-1]
1475-
index, _ := strconv.Atoi(numStr)
1476-
if index < len(ipPool) {
1477-
for _, net := range nsNets {
1478-
v4IP, v6IP, mac, err = c.acquireStaticAddress(key, portName, ipPool[index], macStr, net.Subnet.Name, net.AllowLiveMigration)
1479-
if err == nil {
1480-
return v4IP, v6IP, mac, net.Subnet, nil
1474+
klog.Errorf("acquire address %s for %s failed, %v", pod.Annotations[fmt.Sprintf(util.IpPoolAnnotationTemplate, podNet.ProviderName)], key, err)
1475+
} else {
1476+
tempStrs := strings.Split(pod.Name, "-")
1477+
numStr := tempStrs[len(tempStrs)-1]
1478+
index, _ := strconv.Atoi(numStr)
1479+
1480+
if index < len(ipPool) {
1481+
for _, net := range nsNets {
1482+
v4IP, v6IP, mac, err = c.acquireStaticAddress(key, portName, ipPool[index], macStr, net.Subnet.Name, net.AllowLiveMigration)
1483+
if err == nil {
1484+
return v4IP, v6IP, mac, net.Subnet, nil
1485+
}
14811486
}
1487+
klog.Errorf("acquire address %s for %s failed, %v", ipPool[index], key, err)
14821488
}
1483-
klog.Errorf("acquire address %s for %s failed, %v", ipPool[index], key, err)
14841489
}
14851490
}
14861491
klog.Errorf("alloc address for %s failed, return NoAvailableAddress", key)

test/e2e/kube-ovn/ipam/ipam.go

+68
Original file line numberDiff line numberDiff line change
@@ -327,4 +327,72 @@ var _ = framework.Describe("[group:ipam]", func() {
327327
err = cs.AppsV1().StatefulSets(namespaceName).Delete(context.TODO(), name, metav1.DeleteOptions{})
328328
framework.ExpectNoError(err, "failed to delete statefulset "+name)
329329
})
330+
331+
// separate ippool annotation by comma
332+
framework.ConformanceIt("should allocate static ip for statefulset with ippool separated by comma", func() {
333+
if f.ClusterIpFamily == "dual" {
334+
ginkgo.Skip("Comma separated ippool is not supported for dual stack")
335+
}
336+
337+
ippoolSep := ","
338+
replicas := 3
339+
ippool := framework.RandomIPPool(cidr, ippoolSep, replicas)
340+
labels := map[string]string{"app": stsName}
341+
342+
ginkgo.By("Creating statefulset " + stsName + " with ippool " + ippool)
343+
sts := framework.MakeStatefulSet(stsName, stsName, int32(replicas), labels, framework.PauseImage)
344+
sts.Spec.Template.Annotations = map[string]string{util.IpPoolAnnotation: ippool}
345+
sts = stsClient.CreateSync(sts)
346+
347+
ginkgo.By("Getting pods for statefulset " + stsName)
348+
pods := stsClient.GetPods(sts)
349+
framework.ExpectHaveLen(pods.Items, replicas)
350+
351+
ips := make([]string, 0, replicas)
352+
for _, pod := range pods.Items {
353+
framework.ExpectHaveKeyWithValue(pod.Annotations, util.AllocatedAnnotation, "true")
354+
framework.ExpectHaveKeyWithValue(pod.Annotations, util.CidrAnnotation, subnet.Spec.CIDRBlock)
355+
framework.ExpectHaveKeyWithValue(pod.Annotations, util.GatewayAnnotation, subnet.Spec.Gateway)
356+
framework.ExpectHaveKeyWithValue(pod.Annotations, util.IpPoolAnnotation, ippool)
357+
framework.ExpectHaveKeyWithValue(pod.Annotations, util.LogicalSwitchAnnotation, subnet.Name)
358+
framework.ExpectMAC(pod.Annotations[util.MacAddressAnnotation])
359+
framework.ExpectHaveKeyWithValue(pod.Annotations, util.RoutedAnnotation, "true")
360+
361+
podIPs := make([]string, 0, len(pod.Status.PodIPs))
362+
for _, podIP := range pod.Status.PodIPs {
363+
podIPs = append(podIPs, podIP.IP)
364+
}
365+
framework.ExpectConsistOf(podIPs, strings.Split(pod.Annotations[util.IpAddressAnnotation], ","))
366+
ips = append(ips, pod.Annotations[util.IpAddressAnnotation])
367+
}
368+
framework.ExpectConsistOf(ips, strings.Split(ippool, ippoolSep))
369+
370+
ginkgo.By("Deleting pods for statefulset " + stsName)
371+
for _, pod := range pods.Items {
372+
err := podClient.Delete(context.TODO(), pod.Name, metav1.DeleteOptions{})
373+
framework.ExpectNoError(err, "failed to delete pod "+pod.Name)
374+
}
375+
stsClient.WaitForRunningAndReady(sts)
376+
377+
ginkgo.By("Getting pods for statefulset " + stsName)
378+
pods = stsClient.GetPods(sts)
379+
framework.ExpectHaveLen(pods.Items, replicas)
380+
381+
for i, pod := range pods.Items {
382+
framework.ExpectHaveKeyWithValue(pod.Annotations, util.AllocatedAnnotation, "true")
383+
framework.ExpectHaveKeyWithValue(pod.Annotations, util.CidrAnnotation, subnet.Spec.CIDRBlock)
384+
framework.ExpectHaveKeyWithValue(pod.Annotations, util.GatewayAnnotation, subnet.Spec.Gateway)
385+
framework.ExpectHaveKeyWithValue(pod.Annotations, util.IpPoolAnnotation, ippool)
386+
framework.ExpectHaveKeyWithValue(pod.Annotations, util.IpAddressAnnotation, ips[i])
387+
framework.ExpectHaveKeyWithValue(pod.Annotations, util.LogicalSwitchAnnotation, subnet.Name)
388+
framework.ExpectMAC(pod.Annotations[util.MacAddressAnnotation])
389+
framework.ExpectHaveKeyWithValue(pod.Annotations, util.RoutedAnnotation, "true")
390+
391+
podIPs := make([]string, 0, len(pod.Status.PodIPs))
392+
for _, podIP := range pod.Status.PodIPs {
393+
podIPs = append(podIPs, podIP.IP)
394+
}
395+
framework.ExpectConsistOf(podIPs, strings.Split(pod.Annotations[util.IpAddressAnnotation], ","))
396+
}
397+
})
330398
})

0 commit comments

Comments
 (0)