Skip to content

Commit

Permalink
filter repeat exclude ips
Browse files Browse the repository at this point in the history
  • Loading branch information
hongzhen-ma committed Feb 20, 2021
1 parent 3021743 commit 5ead6b1
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
4 changes: 4 additions & 0 deletions pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"time"

"k8s.io/client-go/kubernetes"

"github.com/alauda/kube-ovn/pkg/ipam"

v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -724,6 +726,7 @@ func (c *Controller) acquireAddress(pod *v1.Pod, subnet *kubeovnv1.Subnet) (stri
if ok, _ := isStatefulSetPod(pod); !ok {
for _, staticIP := range ipPool {
if c.ipam.IsIPAssignedToPod(staticIP, subnet.Name) {
klog.Errorf("static address %s for %s has been assigned", staticIP, key)
continue
}
if v4IP, v6IP, mac, err := c.acquireStaticAddress(key, staticIP, macStr, subnet.Name); err == nil {
Expand All @@ -738,6 +741,7 @@ func (c *Controller) acquireAddress(pod *v1.Pod, subnet *kubeovnv1.Subnet) (stri
return c.acquireStaticAddress(key, ipPool[index], macStr, subnet.Name)
}
}
klog.Errorf("alloc address for %s failed, return NoAvailableAddress", key)
return "", "", "", ipam.NoAvailableError
}

Expand Down
73 changes: 73 additions & 0 deletions pkg/controller/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

kubeovnv1 "github.com/alauda/kube-ovn/pkg/apis/kubeovn/v1"
"github.com/alauda/kube-ovn/pkg/ipam"
"github.com/alauda/kube-ovn/pkg/ovs"
"github.com/alauda/kube-ovn/pkg/util"

Expand Down Expand Up @@ -374,6 +375,7 @@ func checkAndUpdateExcludeIps(subnet *kubeovnv1.Subnet) bool {
subnet.Spec.ExcludeIps = excludeIps
changed = true
} else {
checkAndFormatsExcludeIps(subnet)
for _, gw := range excludeIps {
gwExists := false
for _, excludeIP := range subnet.Spec.ExcludeIps {
Expand Down Expand Up @@ -1114,3 +1116,74 @@ func (c *Controller) getSubnetVlanTag(subnet *kubeovnv1.Subnet) (string, error)
}
return tag, nil
}

func checkAndFormatsExcludeIps(subnet *kubeovnv1.Subnet) {
var excludeIps []string
mapIps := make(map[string]ipam.IPRange, len(subnet.Spec.ExcludeIps))

for _, excludeIP := range subnet.Spec.ExcludeIps {
ips := strings.Split(excludeIP, "..")
if len(ips) == 1 {
if _, ok := mapIps[excludeIP]; !ok {
ipr := ipam.IPRange{Start: ipam.IP(ips[0]), End: ipam.IP(ips[0])}
mapIps[excludeIP] = ipr
}
} else {
if _, ok := mapIps[excludeIP]; !ok {
ipr := ipam.IPRange{Start: ipam.IP(ips[0]), End: ipam.IP(ips[1])}
mapIps[excludeIP] = ipr
}
}
}
newMap := filterRepeatIPRange(mapIps)
for _, v := range newMap {
if v.Start == v.End {
excludeIps = append(excludeIps, string(v.Start))
} else {
excludeIps = append(excludeIps, string(v.Start)+".."+string(v.End))
}
}
klog.V(3).Infof("excludeips before format is %v, after format is %v", subnet.Spec.ExcludeIps, excludeIps)
subnet.Spec.ExcludeIps = excludeIps
}

func filterRepeatIPRange(mapIps map[string]ipam.IPRange) map[string]ipam.IPRange {
for ka, a := range mapIps {
for kb, b := range mapIps {
if ka == kb && a == b {
continue
}

if b.End.LessThan(a.Start) || b.Start.GreaterThan(a.End) {
continue
}

if (a.Start.Equal(b.Start) || a.Start.GreaterThan(b.Start)) &&
(a.End.Equal(b.End) || a.End.LessThan(b.End)) {
delete(mapIps, ka)
continue
}

if (a.Start.Equal(b.Start) || a.Start.GreaterThan(b.Start)) &&
a.End.GreaterThan(b.End) {
ipr := ipam.IPRange{Start: b.Start, End: a.End}
delete(mapIps, ka)
mapIps[kb] = ipr
continue
}

if (a.End.Equal(b.End) || a.End.LessThan(b.End)) &&
a.Start.LessThan(b.Start) {
ipr := ipam.IPRange{Start: a.Start, End: b.End}
delete(mapIps, ka)
mapIps[kb] = ipr
continue
}

// a contains b
mapIps[kb] = a
delete(mapIps, ka)
}
}
return mapIps
}
2 changes: 1 addition & 1 deletion pkg/util/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func SplitStringIP(ipStr string) (string, string) {
return v4IP, v6IP
}

// How to distinguish repeat values
// ExpandExcludeIPs used to get exclude ips in range of subnet cidr, excludes cidr addr and broadcast addr
func ExpandExcludeIPs(excludeIPs []string, cidr string) []string {
rv := []string{}
for _, excludeIP := range excludeIPs {
Expand Down

0 comments on commit 5ead6b1

Please sign in to comment.