Skip to content

Commit

Permalink
fix: remove mask field from ip annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
oilbeater committed Jun 5, 2019
1 parent b398412 commit f8d8e18
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
6 changes: 3 additions & 3 deletions pkg/controller/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,9 +648,9 @@ func getNodeTunlIP(node *v1.Node) (net.IP, error) {
if nodeTunlIP == "" {
return nil, errors.New("node has no tunl ip annotation")
}
nodeTunlIPAddr, _, err := net.ParseCIDR(nodeTunlIP)
if err != nil {
return nil, errors.Annotatef(err, "parse node tunl ip %s faield", nodeTunlIP)
nodeTunlIPAddr := net.ParseIP(nodeTunlIP)
if nodeTunlIPAddr == nil {
return nil, fmt.Errorf("failed to parse node tunl ip %s", nodeTunlIP)
}
return nodeTunlIPAddr, nil
}
19 changes: 10 additions & 9 deletions pkg/daemon/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (csh cniServerHandler) handleAdd(req *restful.Request, resp *restful.Respon
return
}
klog.Infof("add port request %v", podRequest)
var macAddr, ipAddr, cidr, gw, ingress, egress string
var macAddr, ip, ipAddr, cidr, gw, ingress, egress string
for i := 0; i < 10; i++ {
pod, err := csh.KubeClient.CoreV1().Pods(podRequest.PodNamespace).Get(podRequest.PodName, v1.GetOptions{})
if err != nil {
Expand All @@ -43,28 +43,29 @@ func (csh cniServerHandler) handleAdd(req *restful.Request, resp *restful.Respon
resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.PodResponse{Err: errMsg.Error()})
return
}
if err := util.ValidatePodNetwork(pod.Annotations); err != nil {
klog.Errorf("validate pod %s/%s failed, %v", podRequest.PodNamespace, podRequest.PodName, err)
// wait controller assign an address
time.Sleep(2 * time.Second)
continue
}
macAddr = pod.Annotations[util.MacAddressAnnotation]
ipAddr = pod.Annotations[util.IpAddressAnnotation]
ip = pod.Annotations[util.IpAddressAnnotation]
cidr = pod.Annotations[util.CidrAnnotation]
gw = pod.Annotations[util.GatewayAnnotation]
ingress = pod.Annotations[util.IngressRateAnnotation]
egress = pod.Annotations[util.EgressRateAnnotation]

if macAddr == "" || ipAddr == "" || cidr == "" || gw == "" {
// wait controller assign an address
time.Sleep(2 * time.Second)
continue
}
break
}

if macAddr == "" || ipAddr == "" || cidr == "" || gw == "" {
if macAddr == "" || ip == "" || cidr == "" || gw == "" {
errMsg := fmt.Errorf("no available ip for pod %s/%s", podRequest.PodNamespace, podRequest.PodName)
klog.Error(errMsg)
resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.PodResponse{Err: errMsg.Error()})
return
}

ipAddr = fmt.Sprintf("%s/%s", ip, strings.Split(cidr, "/")[1])
klog.Infof("create container mac %s, ip %s, cidr %s, gw %s", macAddr, ipAddr, cidr, gw)
err = csh.configureNic(podRequest.PodName, podRequest.PodNamespace, podRequest.NetNs, podRequest.ContainerID, macAddr, ipAddr, gw, ingress, egress)
if err != nil {
Expand Down
19 changes: 12 additions & 7 deletions pkg/daemon/init.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
package daemon

import (
"fmt"
"github.com/alauda/kube-ovn/pkg/util"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog"
"strings"
"time"
)

// InitNodeGateway init ovn0
func InitNodeGateway(config *Configuration) error {
var portName, ipAddr, macAddr, gw string
var portName, ip, cidr, macAddr, gw, ipAddr string
for {
nodeName := config.NodeName
node, err := config.KubeClient.CoreV1().Nodes().Get(nodeName, v1.GetOptions{})
if err != nil {
klog.Errorf("failed to get node %s info %v", nodeName, err)
return err
}
macAddr = node.Annotations[util.MacAddressAnnotation]
ipAddr = node.Annotations[util.IpAddressAnnotation]
portName = node.Annotations[util.PortNameAnnotation]
gw = node.Annotations[util.GatewayAnnotation]
if macAddr == "" || ipAddr == "" || portName == "" || gw == "" {
klog.Errorf("can not find macAddr, ipAddr, portName and gw, wait 3 seconds...")
if err := util.ValidatePodNetwork(node.Annotations); err != nil {
klog.Errorf("validate node %s failed, %v", nodeName, err)
time.Sleep(3 * time.Second)
continue
} else {
macAddr = node.Annotations[util.MacAddressAnnotation]
ip = node.Annotations[util.IpAddressAnnotation]
cidr = node.Annotations[util.CidrAnnotation]
portName = node.Annotations[util.PortNameAnnotation]
gw = node.Annotations[util.GatewayAnnotation]
ipAddr = fmt.Sprintf("%s/%s", ip, strings.Split(cidr, "/")[1])
break
}
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/ovs/ovn-nbctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ func (c Client) CreatePort(ls, port, ip, mac string) (*nic, error) {
return nil, err
}
subnet, gw := strings.Trim(strings.Split(output, "\n")[0], "\""), strings.Trim(strings.Split(output, "\n")[1], "\"")
mask := strings.Split(subnet, "/")[1]
return &nic{IpAddress: fmt.Sprintf("%s/%s", ip, mask), MacAddress: mac, CIDR: subnet, Gateway: gw}, nil
return &nic{IpAddress: ip, MacAddress: mac, CIDR: subnet, Gateway: gw}, nil
}

type nic struct {
Expand Down

0 comments on commit f8d8e18

Please sign in to comment.