Skip to content

Commit

Permalink
upgrade for subnet from single protocol to dual-stack
Browse files Browse the repository at this point in the history
  • Loading branch information
hongzhen-ma committed Dec 31, 2020
1 parent bbc6857 commit 171dcff
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
28 changes: 26 additions & 2 deletions pkg/controller/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,20 @@ func (c *Controller) InitDefaultVpc() error {

// InitDefaultLogicalSwitch init the default logical switch for ovn network
func (c *Controller) initDefaultLogicalSwitch() error {
_, err := c.config.KubeOvnClient.KubeovnV1().Subnets().Get(context.Background(), c.config.DefaultLogicalSwitch, v1.GetOptions{})
subnet, err := c.config.KubeOvnClient.KubeovnV1().Subnets().Get(context.Background(), c.config.DefaultLogicalSwitch, v1.GetOptions{})
if err == nil {
if subnet != nil && subnet.Spec.CIDRBlock != c.config.DefaultCIDR {
// upgrade to dual-stack
if util.CheckProtocol(c.config.DefaultCIDR) == kubeovnv1.ProtocolDual {
subnet.Spec.CIDRBlock = c.config.DefaultCIDR
if err := formatSubnet(subnet, c); err != nil {
klog.Errorf("init format subnet %s failed %v", c.config.DefaultLogicalSwitch, err)
return err
}
} else {
return fmt.Errorf("can not modify subnet to single protocol")
}
}
return nil
}

Expand Down Expand Up @@ -118,8 +130,20 @@ func (c *Controller) initDefaultLogicalSwitch() error {

// InitNodeSwitch init node switch to connect host and pod
func (c *Controller) initNodeSwitch() error {
_, err := c.config.KubeOvnClient.KubeovnV1().Subnets().Get(context.Background(), c.config.NodeSwitch, v1.GetOptions{})
subnet, err := c.config.KubeOvnClient.KubeovnV1().Subnets().Get(context.Background(), c.config.NodeSwitch, v1.GetOptions{})
if err == nil {
if subnet != nil && subnet.Spec.CIDRBlock != c.config.NodeSwitchCIDR {
// upgrade to dual-stack
if util.CheckProtocol(c.config.NodeSwitchCIDR) == kubeovnv1.ProtocolDual {
subnet.Spec.CIDRBlock = c.config.NodeSwitchCIDR
if err := formatSubnet(subnet, c); err != nil {
klog.Errorf("init format subnet %s failed %v", c.config.NodeSwitch, err)
return err
}
} else {
return fmt.Errorf("can not modify subnet to single protocol")
}
}
return nil
}

Expand Down
9 changes: 6 additions & 3 deletions pkg/controller/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ func formatSubnet(subnet *kubeovnv1.Subnet, c *Controller) error {
return err
}
changed = checkAndUpdateExcludeIps(subnet)

klog.Infof("format subnet %v, changed %v", subnet.Name, changed)
if changed {
_, err = c.config.KubeOvnClient.KubeovnV1().Subnets().Update(context.Background(), subnet, metav1.UpdateOptions{})
if err != nil {
Expand Down Expand Up @@ -319,7 +319,7 @@ func checkAndUpdateCIDR(subnet *kubeovnv1.Subnet) (bool, error) {

func checkAndUpdateGateway(subnet *kubeovnv1.Subnet) (bool, error) {
changed := false
if subnet.Spec.Gateway == "" {
if subnet.Spec.Gateway == "" || util.CheckProtocol(subnet.Spec.Gateway) != util.CheckProtocol(subnet.Spec.CIDRBlock) {
gw, err := util.GetGwByCidr(subnet.Spec.CIDRBlock)
if err != nil {
klog.Error(err)
Expand All @@ -343,9 +343,12 @@ func checkAndUpdateExcludeIps(subnet *kubeovnv1.Subnet) bool {
subnet.Spec.ExcludeIps = excludeIps
changed = true
} else {
gwExists := false
for _, gw := range excludeIps {
gwExists := false
for _, ip := range ovs.ExpandExcludeIPs(subnet.Spec.ExcludeIps, subnet.Spec.CIDRBlock) {
if util.CheckProtocol(gw) != util.CheckProtocol(ip) {
continue
}
if ip == gw {
gwExists = true
break
Expand Down
4 changes: 4 additions & 0 deletions pkg/ipam/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ func NewSubnet(name, cidrStr string, excludeIps []string) (*Subnet, error) {
V4ReservedIPList: convertExcludeIps(v4ExcludeIps),
V4PodToIP: map[string]IP{},
V4IPToPod: map[IP]string{},
V6PodToIP: map[string]IP{},
V6IPToPod: map[IP]string{},
MacToPod: map[string]string{},
PodToMac: map[string]string{},
}
Expand All @@ -75,6 +77,8 @@ func NewSubnet(name, cidrStr string, excludeIps []string) (*Subnet, error) {
V6FreeIPList: IPRangeList{&IPRange{Start: IP(firstIP), End: IP(lastIP)}},
V6ReleasedIPList: IPRangeList{},
V6ReservedIPList: convertExcludeIps(v6ExcludeIps),
V4PodToIP: map[string]IP{},
V4IPToPod: map[IP]string{},
V6PodToIP: map[string]IP{},
V6IPToPod: map[IP]string{},
MacToPod: map[string]string{},
Expand Down
14 changes: 7 additions & 7 deletions pkg/ovs/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ func trimCommandOutput(raw []byte) string {
// ExpandExcludeIPs parse ovn exclude_ips to ip slice
func ExpandExcludeIPs(excludeIPs []string, cidr string) []string {
rv := []string{}
for _, cidrBlock := range strings.Split(cidr, ",") {
subnetNum := util.SubnetNumber(cidrBlock)
broadcast := util.SubnetBroadCast(cidrBlock)
for _, excludeIP := range excludeIPs {
if strings.Index(excludeIP, "..") != -1 {
for _, excludeIP := range excludeIPs {
if strings.Index(excludeIP, "..") != -1 {
for _, cidrBlock := range strings.Split(cidr, ",") {
subnetNum := util.SubnetNumber(cidrBlock)
broadcast := util.SubnetBroadCast(cidrBlock)
parts := strings.Split(excludeIP, "..")
s := util.Ip2BigInt(parts[0])
e := util.Ip2BigInt(parts[1])
Expand All @@ -40,9 +40,9 @@ func ExpandExcludeIPs(excludeIPs []string, cidr string) []string {
}
s.Add(s, big.NewInt(1))
}
} else {
rv = append(rv, excludeIP)
}
} else {
rv = append(rv, excludeIP)
}
}
return rv
Expand Down

0 comments on commit 171dcff

Please sign in to comment.