Skip to content

Commit

Permalink
split ippoolname and old ippool
Browse files Browse the repository at this point in the history
  • Loading branch information
bobz965 committed Jul 19, 2023
1 parent ed58b21 commit 1674ca4
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pkg/controller/ippool.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (c *Controller) handleDeleteIPPool(ippool *kubeovnv1.IPPool) error {
if len(ns.Annotations) == 0 {
continue
}
if ns.Annotations[util.IpPoolAnnotation] == ippool.Name {
if ns.Annotations[util.IpPoolNameAnnotation] == ippool.Name {
c.enqueueAddNamespace(ns)
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (c *Controller) handleAddNamespace(key string) error {
if namespace.Annotations[util.LogicalSwitchAnnotation] == strings.Join(lss, ",") &&
namespace.Annotations[util.CidrAnnotation] == strings.Join(cidrs, ";") &&
namespace.Annotations[util.ExcludeIpsAnnotation] == strings.Join(excludeIps, ";") &&
namespace.Annotations[util.IpPoolAnnotation] == ippool {
namespace.Annotations[util.IpPoolNameAnnotation] == ippool {
return nil
}
}
Expand All @@ -201,9 +201,9 @@ func (c *Controller) handleAddNamespace(key string) error {
namespace.Annotations[util.ExcludeIpsAnnotation] = strings.Join(excludeIps, ";")

if ippool == "" {
delete(namespace.Annotations, util.IpPoolAnnotation)
delete(namespace.Annotations, util.IpPoolNameAnnotation)
} else {
namespace.Annotations[util.IpPoolAnnotation] = ippool
namespace.Annotations[util.IpPoolNameAnnotation] = ippool
}

patch, err := util.GenerateStrategicMergePatchPayload(cachedNs, namespace)
Expand Down
24 changes: 18 additions & 6 deletions pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,7 @@ func (c *Controller) acquireAddress(pod *v1.Pod, podNet *kubeovnNet) (string, st
*macStr = ""
}

ipPoolNameStr := pod.Annotations[fmt.Sprintf(util.IpPoolNameAnnotationTemplate, podNet.ProviderName)]
ippoolStr := pod.Annotations[fmt.Sprintf(util.IpPoolAnnotationTemplate, podNet.ProviderName)]
if ippoolStr == "" {
ns, err := c.namespacesLister.Get(pod.Namespace)
Expand All @@ -1490,7 +1491,7 @@ func (c *Controller) acquireAddress(pod *v1.Pod, podNet *kubeovnNet) (string, st
return "", "", "", podNet.Subnet, err
}
if len(ns.Annotations) != 0 {
ippoolStr = ns.Annotations[util.IpPoolAnnotation]
ippoolStr = ns.Annotations[util.IpPoolNameAnnotation]
}
}

Expand Down Expand Up @@ -1544,21 +1545,32 @@ func (c *Controller) acquireAddress(pod *v1.Pod, podNet *kubeovnNet) (string, st

// IPPool allocate
if ippoolStr != "" {
var ipPool []string
var ipPool, ipPoolNames []string
if strings.ContainsRune(ippoolStr, ';') {
ipPool = strings.Split(ippoolStr, ";")
} else {
ipPool = strings.Split(ippoolStr, ",")
if podNet.Subnet.Spec.Protocol == kubeovnv1.ProtocolDual && isStsPod {
klog.Infof("sts pod in dual stack subnet use ip pool: %s", ippoolStr)
ipPool = strings.Split(ippoolStr, ";")
} else {
ipPool = strings.Split(ippoolStr, ",")
}
}
if ipPoolNameStr != "" {
ipPoolNames = strings.Split(ipPoolNameStr, ",")
}
for i, ip := range ipPool {
ipPool[i] = strings.TrimSpace(ip)
}

if len(ipPool) == 1 && net.ParseIP(ipPool[0]) == nil {
for i, name := range ipPoolNames {
ipPoolNames[i] = strings.TrimSpace(name)
}
if len(ipPoolNames) == 1 && ipPoolNames[0] != "" {
klog.Infof("random allocate ip by ip pool %s from subnet %s", ipPoolNames[0], podNet.Subnet.Name)
var skippedAddrs []string
for {
portName := ovs.PodNameToPortName(podName, pod.Namespace, podNet.ProviderName)
ipv4, ipv6, mac, err := c.ipam.GetRandomAddress(key, portName, macStr, podNet.Subnet.Name, ipPool[0], skippedAddrs, !podNet.AllowLiveMigration)
ipv4, ipv6, mac, err := c.ipam.GetRandomAddress(key, portName, macStr, podNet.Subnet.Name, ipPoolNames[0], skippedAddrs, !podNet.AllowLiveMigration)
if err != nil {
return "", "", "", podNet.Subnet, err
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/util/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
CidrAnnotation = "ovn.kubernetes.io/cidr"
GatewayAnnotation = "ovn.kubernetes.io/gateway"
IpPoolAnnotation = "ovn.kubernetes.io/ip_pool"
IpPoolNameAnnotation = "ovn.kubernetes.io/ip_pool_name"
BgpAnnotation = "ovn.kubernetes.io/bgp"
SnatAnnotation = "ovn.kubernetes.io/snat"
EipAnnotation = "ovn.kubernetes.io/eip"
Expand Down Expand Up @@ -73,6 +74,7 @@ const (
CidrAnnotationTemplate = "%s.kubernetes.io/cidr"
GatewayAnnotationTemplate = "%s.kubernetes.io/gateway"
IpPoolAnnotationTemplate = "%s.kubernetes.io/ip_pool"
IpPoolNameAnnotationTemplate = "%s.kubernetes.io/ip_pool_name"
LogicalSwitchAnnotationTemplate = "%s.kubernetes.io/logical_switch"
LogicalRouterAnnotationTemplate = "%s.kubernetes.io/logical_router"
VlanIdAnnotationTemplate = "%s.kubernetes.io/vlan_id"
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/kube-ovn/ipam/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ var _ = framework.Describe("[group:ipam]", func() {
ginkgo.By("Creating deployment " + deployName + " within ippool " + ippoolName)
replicas := 3
labels := map[string]string{"app": deployName}
annotations := map[string]string{util.IpPoolAnnotation: ippoolName}
annotations := map[string]string{util.IpPoolNameAnnotation: ippoolName}
deploy := framework.MakeDeployment(deployName, int32(replicas), labels, annotations, "pause", framework.PauseImage, "")
deploy = deployClient.CreateSync(deploy)

Expand Down Expand Up @@ -530,7 +530,7 @@ var _ = framework.Describe("[group:ipam]", func() {
ginkgo.By("Validating namespace annotations")
framework.WaitUntil(2*time.Second, 30*time.Second, func(_ context.Context) (bool, error) {
ns := nsClient.Get(namespaceName)
return len(ns.Annotations) != 0 && ns.Annotations[util.IpPoolAnnotation] == ippoolName, nil
return len(ns.Annotations) != 0 && ns.Annotations[util.IpPoolNameAnnotation] == ippoolName, nil
}, "")

ginkgo.By("Patching deployment " + deployName)
Expand Down

0 comments on commit 1674ca4

Please sign in to comment.