Skip to content

Commit

Permalink
fix: static spectify one ip (#3614)
Browse files Browse the repository at this point in the history
* fix: static spectify one ip

---------

Signed-off-by: bobz965 <zhangbingbing2_yewu@cmss.chinamobile.com>
  • Loading branch information
bobz965 committed Jan 11, 2024
1 parent 6e80f35 commit 3fec12e
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ test/e2e/ovnnb_db.*
test/e2e/ovnsb_db.*
kube-ovn.yaml
kube-ovn-crd.yaml
kube-ovn-app-sa.yaml
kube-ovn-cni-sa.yaml
kube-ovn-sa.yaml
ovn.yaml
ovn-ic-0.yaml
ovn-ic-1.yaml
ovn-ovs-sa.yaml
ovs-ovn-ds.yaml
kind.yaml
kube-ovn.tar
vpc-nat-gateway.tar
Expand Down
6 changes: 5 additions & 1 deletion pkg/controller/external-gw.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,11 @@ func (c *Controller) createDefaultVpcLrpEip(config map[string]string) (string, s
klog.Error(err)
return "", "", err
}
v4ipCidr := util.GetIpAddrWithMask(v4ip, cachedSubnet.Spec.CIDRBlock)
v4ipCidr, err := util.GetIpAddrWithMask(v4ip, cachedSubnet.Spec.CIDRBlock)
if err != nil {
klog.Errorf("failed to get ip %s with mask %s, %v", v4ip, cachedSubnet.Spec.CIDRBlock, err)
return "", "", err
}
return v4ipCidr, mac, nil
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/controller/service_lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,11 @@ func (c *Controller) updatePodAttachNets(pod *corev1.Pod, svc *corev1.Service) e
}

loadBalancerIP := pod.Annotations[attachIpAnnotation]
ipAddr := util.GetIpAddrWithMask(loadBalancerIP, pod.Annotations[attachCidrAnnotation])
ipAddr, err := util.GetIpAddrWithMask(loadBalancerIP, pod.Annotations[attachCidrAnnotation])
if err != nil {
klog.Errorf("failed to get ip addr with mask, err: %v", err)
return err
}

var addRules []string
addRules = append(addRules, fmt.Sprintf("%s,%s", ipAddr, pod.Annotations[attachGatewayAnnotation]))
Expand Down
22 changes: 18 additions & 4 deletions pkg/controller/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,11 @@ func (c *Controller) reconcileRouterPorts(vpc *kubeovnv1.Vpc) error {
if subnet.Spec.U2OInterconnection && subnet.Status.U2OInterconnectionIP != "" {
gateway = subnet.Status.U2OInterconnectionIP
}
networks := util.GetIpAddrWithMask(gateway, subnet.Spec.CIDRBlock)
networks, err := util.GetIpAddrWithMask(gateway, subnet.Spec.CIDRBlock)
if err != nil {
klog.Error(err)
return err
}
klog.Infof("add vpc lrp %s, networks %s", routerPortName, networks)
if err := c.ovnClient.AddLogicalRouterPort(router, routerPortName, "", networks); err != nil {
klog.ErrorS(err, "unable to create router port", "vpc", vpc.Name, "subnet", subnetName)
Expand Down Expand Up @@ -229,7 +233,11 @@ func (c *Controller) reconcileRouterPortBySubnet(vpc *kubeovnv1.Vpc, subnet *kub
if subnet.Spec.U2OInterconnection && subnet.Status.U2OInterconnectionIP != "" {
gateway = subnet.Status.U2OInterconnectionIP
}
networks := util.GetIpAddrWithMask(gateway, subnet.Spec.CIDRBlock)
networks, err := util.GetIpAddrWithMask(gateway, subnet.Spec.CIDRBlock)
if err != nil {
klog.Errorf("failed to get ip addr with mask, gateway %s, cidr %s", gateway, subnet.Spec.CIDRBlock)
return err
}
klog.Infof("router port does not exist, trying to create %s with ip %s", routerPortName, networks)

if err := c.ovnClient.AddLogicalRouterPort(router, routerPortName, "", networks); err != nil {
Expand Down Expand Up @@ -311,11 +319,12 @@ func (c *Controller) handleAddOrUpdateVpc(key string) error {
return err
}
if err = c.createVpcRouter(key); err != nil {
klog.Errorf("failed to create vpc router: %v", err)
return err
}

if err := c.reconcileRouterPorts(vpc); err != nil {
klog.ErrorS(err, "unable to reconcileRouterPorts")
klog.Errorf("failed to reconcileRouterPorts: %v", err)
return err
}

Expand Down Expand Up @@ -753,6 +762,7 @@ func (c *Controller) getVpcSubnets(vpc *kubeovnv1.Vpc) (subnets []string, defaul
subnets = []string{}
allSubnets, err := c.subnetsLister.List(labels.Everything())
if err != nil {
klog.Error(err)
return nil, "", err
}

Expand Down Expand Up @@ -835,7 +845,11 @@ func (c *Controller) handleAddVpcExternal(key string) error {
klog.Errorf("failed to get gateway chassis, %v", err)
return err
}
v4ipCidr := util.GetIpAddrWithMask(v4ip, cachedSubnet.Spec.CIDRBlock)
v4ipCidr, err := util.GetIpAddrWithMask(v4ip, cachedSubnet.Spec.CIDRBlock)
if err != nil {
klog.Errorf("failed to get ip addr with mask, ip %s, cidr %s", v4ip, cachedSubnet.Spec.CIDRBlock)
return err
}
if err := c.ovnLegacyClient.ConnectRouterToExternal(c.config.ExternalGatewaySwitch, key, v4ipCidr, mac, chassises); err != nil {
klog.Errorf("failed to connect router '%s' to external, %v", key, err)
return err
Expand Down
10 changes: 9 additions & 1 deletion pkg/daemon/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,15 @@ func (csh cniServerHandler) handleAdd(req *restful.Request, resp *restful.Respon
loss = pod.Annotations[fmt.Sprintf(util.NetemQosLossAnnotationTemplate, podRequest.Provider)]
providerNetwork = pod.Annotations[fmt.Sprintf(util.ProviderNetworkTemplate, podRequest.Provider)]
vmName = pod.Annotations[fmt.Sprintf(util.VmTemplate, podRequest.Provider)]
ipAddr = util.GetIpAddrWithMask(ip, cidr)
ipAddr, err = util.GetIpAddrWithMask(ip, cidr)
if err != nil {
errMsg := fmt.Errorf("failed to get ip address with mask, %v", err)
klog.Error(errMsg)
if err := resp.WriteHeaderAndEntity(http.StatusInternalServerError, request.CniResponse{Err: errMsg.Error()}); err != nil {
klog.Errorf("failed to write response, %v", err)
}
return
}
if ifName = podRequest.IfName; ifName == "" {
ifName = "eth0"
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/daemon/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,11 @@ func InitNodeGateway(config *Configuration) error {
return fmt.Errorf("failed to parse mac %s %v", mac, err)
}

ipAddr = util.GetIpAddrWithMask(ip, cidr)
ipAddr, err = util.GetIpAddrWithMask(ip, cidr)
if err != nil {
klog.Errorf("failed to get ip %s with mask %s, %v", ip, cidr, err)
return err
}
return configureNodeNic(portName, ipAddr, gw, mac, config.MTU)
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/ovs/ovn-nbctl-legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2958,7 +2958,11 @@ func (c *LegacyClient) DeleteDHCPOptions(ls string, protocol string) error {
func (c *LegacyClient) UpdateRouterPortIPv6RA(ls, lr, cidrBlock, gateway, ipv6RAConfigsStr string, enableIPv6RA bool) error {
var err error
lrTols := fmt.Sprintf("%s-%s", lr, ls)
ip := util.GetIpAddrWithMask(gateway, cidrBlock)
ip, err := util.GetIpAddrWithMask(gateway, cidrBlock)
if err != nil {
klog.Errorf("failed to get ip addr with mask, %v", err)
return err
}
ipStr := strings.Split(ip, ",")
if enableIPv6RA {
var ipv6Prefix string
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/arp.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func ArpDetectIPConflict(nic, ip string, mac net.HardwareAddr) (net.HardwareAddr
}

func AnnounceArpAddress(nic, ip string, mac net.HardwareAddr, announceNum int, announceInterval time.Duration) error {
klog.Infof("announce arp address nic %s , ip %s, with mac %v ", nic, ip, mac)
klog.Infof("announce arp address nic %s, ip %s, with mac %v", nic, ip, mac)
netInterface, err := net.InterfaceByName(nic)
if err != nil {
return err
Expand Down
18 changes: 12 additions & 6 deletions pkg/util/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,20 +278,26 @@ func GetStringIP(v4IP, v6IP string) string {
return ipStr
}

func GetIpAddrWithMask(ip, cidr string) string {
func GetIpAddrWithMask(ip, cidr string) (string, error) {
var ipAddr string
if CheckProtocol(cidr) == kubeovnv1.ProtocolDual {
cidrBlocks := strings.Split(cidr, ",")
ips := strings.Split(ip, ",")
if len(cidrBlocks) == 2 && len(ips) == 2 {
v4IP := fmt.Sprintf("%s/%s", ips[0], strings.Split(cidrBlocks[0], "/")[1])
v6IP := fmt.Sprintf("%s/%s", ips[1], strings.Split(cidrBlocks[1], "/")[1])
ipAddr = v4IP + "," + v6IP
if len(cidrBlocks) == 2 {
if len(ips) == 2 {
v4IP := fmt.Sprintf("%s/%s", ips[0], strings.Split(cidrBlocks[0], "/")[1])
v6IP := fmt.Sprintf("%s/%s", ips[1], strings.Split(cidrBlocks[1], "/")[1])
ipAddr = v4IP + "," + v6IP
} else {
err := fmt.Errorf("ip %s should be dualstack", ip)
klog.Error(err)
return "", err
}
}
} else {
ipAddr = fmt.Sprintf("%s/%s", ip, strings.Split(cidr, "/")[1])
}
return ipAddr
return ipAddr, nil
}

func GetIpWithoutMask(ipStr string) string {
Expand Down
4 changes: 2 additions & 2 deletions pkg/util/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,8 +727,8 @@ func TestGetIpAddrWithMask(t *testing.T) {
}
for _, c := range tests {
t.Run(c.name, func(t *testing.T) {
ans := GetIpAddrWithMask(c.ip, c.cidr)
if c.want != ans {
ans, err := GetIpAddrWithMask(c.ip, c.cidr)
if err != nil || c.want != ans {
t.Errorf("%v, %v expected %v, but %v got",
c.ip, c.cidr, c.want, ans)
}
Expand Down

0 comments on commit 3fec12e

Please sign in to comment.