Skip to content

Commit

Permalink
fix build errors
Browse files Browse the repository at this point in the history
Signed-off-by: oilbeater <[email protected]>
  • Loading branch information
oilbeater committed Nov 14, 2024
1 parent c3e8fb4 commit e12731d
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 16 deletions.
6 changes: 5 additions & 1 deletion pkg/controller/external_gw.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

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 @@ -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]))
Expand Down
6 changes: 5 additions & 1 deletion pkg/controller/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
10 changes: 9 additions & 1 deletion pkg/daemon/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion pkg/daemon/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/daemon/ovs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion pkg/ovs/ovn-nb-logical_switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
28 changes: 20 additions & 8 deletions pkg/util/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e12731d

Please sign in to comment.