From e12731ddb33a2b69456779c76149246ea0d1cf84 Mon Sep 17 00:00:00 2001 From: oilbeater Date: Thu, 14 Nov 2024 02:58:12 +0000 Subject: [PATCH] fix build errors Signed-off-by: oilbeater --- pkg/controller/external_gw.go | 6 +++++- pkg/controller/service_lb.go | 6 +++++- pkg/controller/vpc.go | 6 +++++- pkg/daemon/handler.go | 10 +++++++++- pkg/daemon/init.go | 6 +++++- pkg/daemon/ovs_linux.go | 6 +++++- pkg/ovs/ovn-nb-logical_switch.go | 6 +++++- pkg/util/net.go | 28 ++++++++++++++++++++-------- pkg/util/net_test.go | 2 +- 9 files changed, 60 insertions(+), 16 deletions(-) diff --git a/pkg/controller/external_gw.go b/pkg/controller/external_gw.go index c6ca65b2456..352577b8ca0 100644 --- a/pkg/controller/external_gw.go +++ b/pkg/controller/external_gw.go @@ -192,7 +192,11 @@ func (c *Controller) createDefaultVpcLrpEip() (string, string, error) { 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 } diff --git a/pkg/controller/service_lb.go b/pkg/controller/service_lb.go index 6dc71d6d2c1..24f54ba81ab 100644 --- a/pkg/controller/service_lb.go +++ b/pkg/controller/service_lb.go @@ -305,7 +305,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])) diff --git a/pkg/controller/vpc.go b/pkg/controller/vpc.go index ce1945fff8b..05afcf30f94 100644 --- a/pkg/controller/vpc.go +++ b/pkg/controller/vpc.go @@ -1140,7 +1140,11 @@ func (c *Controller) handleAddVpcExternalSubnet(key, subnet string) error { return err } - v4ipCidr := util.GetIPAddrWithMask(v4ip, cachedSubnet.Spec.CIDRBlock) + v4ipCidr, err := util.GetIPAddrWithMask(v4ip, cachedSubnet.Spec.CIDRBlock) + if err != nil { + klog.Error(err) + return err + } lspName := fmt.Sprintf("%s-%s", subnet, key) lrpName := fmt.Sprintf("%s-%s", key, subnet) diff --git a/pkg/daemon/handler.go b/pkg/daemon/handler.go index 26fca359fde..d25978f11cf 100644 --- a/pkg/daemon/handler.go +++ b/pkg/daemon/handler.go @@ -130,7 +130,15 @@ func (csh cniServerHandler) handleAdd(req *restful.Request, resp *restful.Respon jitter = pod.Annotations[fmt.Sprintf(util.NetemQosJitterAnnotationTemplate, podRequest.Provider)] providerNetwork = pod.Annotations[fmt.Sprintf(util.ProviderNetworkTemplate, podRequest.Provider)] vmName = pod.Annotations[fmt.Sprintf(util.VMAnnotationTemplate, 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 s := pod.Annotations[fmt.Sprintf(util.RoutesAnnotationTemplate, podRequest.Provider)]; s != "" { if err = json.Unmarshal([]byte(s), &routes); err != nil { errMsg := fmt.Errorf("invalid routes for pod %s/%s: %v", pod.Namespace, pod.Name, err) diff --git a/pkg/daemon/init.go b/pkg/daemon/init.go index 17e58351dbf..9d35e6d867c 100644 --- a/pkg/daemon/init.go +++ b/pkg/daemon/init.go @@ -81,7 +81,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 addr with mask %s, %v", ip, err) + return err + } return configureNodeNic(portName, ipAddr, gw, cidr, mac, config.MTU) } diff --git a/pkg/daemon/ovs_linux.go b/pkg/daemon/ovs_linux.go index a04efd48e18..8d3d3525abc 100644 --- a/pkg/daemon/ovs_linux.go +++ b/pkg/daemon/ovs_linux.go @@ -757,7 +757,11 @@ func (c *Controller) loopOvnExt0Check() { } } nodeExtIP := cachedEip.Spec.V4Ip - ipAddr := util.GetIPAddrWithMask(ips, cachedSubnet.Spec.CIDRBlock) + ipAddr, err := util.GetIPAddrWithMask(ips, cachedSubnet.Spec.CIDRBlock) + if err != nil { + klog.Errorf("failed to get ip addr with mask %s, %v", ips, err) + return + } if err := c.checkNodeGwNicInNs(nodeExtIP, ipAddr, gw, gwNS); err == nil { // add all lrp ip in bfd listening list return diff --git a/pkg/ovs/ovn-nb-logical_switch.go b/pkg/ovs/ovn-nb-logical_switch.go index 066c1e873ed..7bbf28ebb5d 100644 --- a/pkg/ovs/ovn-nb-logical_switch.go +++ b/pkg/ovs/ovn-nb-logical_switch.go @@ -18,7 +18,11 @@ func (c *OVNNbClient) CreateLogicalSwitch(lsName, lrName, cidrBlock, gateway str lspName := fmt.Sprintf("%s-%s", lsName, lrName) lrpName := fmt.Sprintf("%s-%s", lrName, lsName) - networks := util.GetIPAddrWithMask(gateway, cidrBlock) + networks, err := util.GetIPAddrWithMask(gateway, cidrBlock) + if err != nil { + klog.Errorf("failed to get ip %s with mask %s, %v", gateway, cidrBlock, err) + return err + } exist, err := c.LogicalSwitchExists(lsName) if err != nil { diff --git a/pkg/util/net.go b/pkg/util/net.go index 6cea126758b..4c3455ed95e 100644 --- a/pkg/util/net.go +++ b/pkg/util/net.go @@ -300,20 +300,32 @@ func GetStringIP(v4IP, v6IP string) string { return ipStr } -func GetIPAddrWithMask(ip, cidr string) string { +func GetIPAddrWithMask(ip, cidr string) (string, error) { var ipAddr string + ips := strings.Split(ip, ",") 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]) + if len(ips) == 1 { + ipAddr = fmt.Sprintf("%s/%s", ip, strings.Split(cidr, "/")[1]) + } else { + err := fmt.Errorf("ip %s should be singlestack", ip) + klog.Error(err) + return ipAddr, err + } } - return ipAddr + return ipAddr, nil } func GetIPWithoutMask(ipStr string) string { diff --git a/pkg/util/net_test.go b/pkg/util/net_test.go index 9848ac66838..2f72a1ada03 100644 --- a/pkg/util/net_test.go +++ b/pkg/util/net_test.go @@ -774,7 +774,7 @@ func TestGetIPAddrWithMask(t *testing.T) { } for _, c := range tests { t.Run(c.name, func(t *testing.T) { - ans := GetIPAddrWithMask(c.ip, c.cidr) + ans, _ := GetIPAddrWithMask(c.ip, c.cidr) if c.want != ans { t.Errorf("%v, %v expected %v, but %v got", c.ip, c.cidr, c.want, ans)