Skip to content

Commit b8ba5d8

Browse files
committed
ipam: check subnet's available ipv6 address count (#4903)
Signed-off-by: zhangzujian <zhangzujian.7@gmail.com>
1 parent 26224c5 commit b8ba5d8

File tree

1 file changed

+42
-40
lines changed

1 file changed

+42
-40
lines changed

pkg/controller/pod.go

+42-40
Original file line numberDiff line numberDiff line change
@@ -1381,59 +1381,61 @@ func needRouteSubnets(pod *v1.Pod, nets []*kubeovnNet) []*kubeovnNet {
13811381
}
13821382

13831383
func (c *Controller) getPodDefaultSubnet(pod *v1.Pod) (*kubeovnv1.Subnet, error) {
1384-
var subnet *kubeovnv1.Subnet
1385-
var err error
1386-
// 1. check annotation subnet
1387-
lsName, lsExist := pod.Annotations[util.LogicalSwitchAnnotation]
1388-
if lsExist {
1389-
subnet, err = c.subnetsLister.Get(lsName)
1384+
// check pod annotations
1385+
if lsName := pod.Annotations[util.LogicalSwitchAnnotation]; lsName != "" {
1386+
subnet, err := c.subnetsLister.Get(lsName)
13901387
if err != nil {
1391-
klog.Errorf("failed to get subnet %v", err)
1388+
klog.Errorf("failed to get subnet %s: %v", lsName, err)
13921389
return nil, err
13931390
}
1394-
} else {
1395-
ns, err := c.namespacesLister.Get(pod.Namespace)
1396-
if err != nil {
1397-
klog.Errorf("failed to get namespace %s, %v", pod.Namespace, err)
1391+
return subnet, nil
1392+
}
1393+
1394+
ns, err := c.namespacesLister.Get(pod.Namespace)
1395+
if err != nil {
1396+
klog.Errorf("failed to get namespace %s: %v", pod.Namespace, err)
1397+
return nil, err
1398+
}
1399+
if len(ns.Annotations) == 0 {
1400+
err = fmt.Errorf("namespace %s network annotations is empty", ns.Name)
1401+
klog.Error(err)
1402+
return nil, err
1403+
}
1404+
1405+
subnetNames := ns.Annotations[util.LogicalSwitchAnnotation]
1406+
for _, subnetName := range strings.Split(subnetNames, ",") {
1407+
if subnetName == "" {
1408+
err = fmt.Errorf("namespace %s default logical switch is not found", ns.Name)
1409+
klog.Error(err)
13981410
return nil, err
13991411
}
1400-
if ns.Annotations == nil {
1401-
err = fmt.Errorf("namespace %s network annotations is nil", pod.Namespace)
1402-
klog.Error(err)
1412+
subnet, err := c.subnetsLister.Get(subnetName)
1413+
if err != nil {
1414+
klog.Errorf("failed to get subnet %s: %v", subnetName, err)
14031415
return nil, err
14041416
}
14051417

1406-
subnetNames := ns.Annotations[util.LogicalSwitchAnnotation]
1407-
for _, subnetName := range strings.Split(subnetNames, ",") {
1408-
if subnetName == "" {
1409-
err = fmt.Errorf("namespace %s default logical switch is not found", pod.Namespace)
1410-
klog.Error(err)
1411-
return nil, err
1418+
switch subnet.Spec.Protocol {
1419+
case kubeovnv1.ProtocolDual:
1420+
if subnet.Status.V6AvailableIPs == 0 {
1421+
klog.Infof("there's no available ipv6 address in subnet %s, try next one", subnet.Name)
1422+
continue
14121423
}
1413-
subnet, err = c.subnetsLister.Get(subnetName)
1414-
if err != nil {
1415-
klog.Errorf("failed to get subnet %v", err)
1416-
return nil, err
1424+
fallthrough
1425+
case kubeovnv1.ProtocolIPv4:
1426+
if subnet.Status.V4AvailableIPs == 0 {
1427+
klog.Infof("there's no available ipv4 address in subnet %s, try next one", subnet.Name)
1428+
continue
14171429
}
1418-
1419-
switch subnet.Spec.Protocol {
1420-
case kubeovnv1.ProtocolIPv4:
1421-
fallthrough
1422-
case kubeovnv1.ProtocolDual:
1423-
if subnet.Status.V4AvailableIPs == 0 {
1424-
klog.V(3).Infof("there's no available ips for subnet %v, try next subnet", subnet.Name)
1425-
continue
1426-
}
1427-
case kubeovnv1.ProtocolIPv6:
1428-
if subnet.Status.V6AvailableIPs == 0 {
1429-
klog.Infof("there's no available ips for subnet %v, try next subnet", subnet.Name)
1430-
continue
1431-
}
1430+
case kubeovnv1.ProtocolIPv6:
1431+
if subnet.Status.V6AvailableIPs == 0 {
1432+
klog.Infof("there's no available ipv6 address in subnet %s, try next one", subnet.Name)
1433+
continue
14321434
}
1433-
break
14341435
}
1436+
return subnet, nil
14351437
}
1436-
return subnet, nil
1438+
return nil, ipam.ErrNoAvailable
14371439
}
14381440

14391441
func loadNetConf(bytes []byte) (*multustypes.DelegateNetConf, error) {

0 commit comments

Comments
 (0)