Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: static one ip in dualstack subnet #3625

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pkg/controller/external_gw.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,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
7 changes: 5 additions & 2 deletions pkg/controller/service_lb.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +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]))
klog.Infof("add eip rules for lb svc pod, %v", addRules)
Expand Down
7 changes: 6 additions & 1 deletion pkg/controller/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ func (c *Controller) handleAddOrUpdateVpc(key string) error {
return err
}
if err = c.createVpcRouter(key); err != nil {
klog.Errorf("failed to create vpc router for vpc %s: %v", key, err)
return err
}

Expand Down Expand Up @@ -1157,7 +1158,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)
zbb88888 marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -129,7 +129,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.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
}
oldPodName = podRequest.PodName
if s := pod.Annotations[fmt.Sprintf(util.RoutesAnnotationTemplate, podRequest.Provider)]; s != "" {
if err = json.Unmarshal([]byte(s), &routes); err != nil {
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 %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/daemon/ovs_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,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
7 changes: 6 additions & 1 deletion pkg/ovs/ovn-nb-logical_switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ 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 {
klog.Error(err)
return err
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/util/arp.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,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 {
klog.Error(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 @@ -298,20 +298,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
6 changes: 5 additions & 1 deletion pkg/util/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,11 @@ func TestGetIPAddrWithMask(t *testing.T) {
}
for _, c := range tests {
t.Run(c.name, func(t *testing.T) {
ans := GetIPAddrWithMask(c.ip, c.cidr)
ans, err := GetIPAddrWithMask(c.ip, c.cidr)
if err != nil {
t.Errorf("%v, %v expected %v, but %v got",
c.ip, c.cidr, c.want, err)
}
if c.want != ans {
t.Errorf("%v, %v expected %v, but %v got",
c.ip, c.cidr, c.want, ans)
Expand Down
4 changes: 3 additions & 1 deletion test/e2e/kube-ovn/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ var _ = framework.OrderedDescribe("[group:node]", func() {
framework.ExpectNoError(err)
framework.ExpectHaveLen(links, 1)
framework.Logf(util.GetIPAddrWithMask(node.Annotations[util.IPAddressAnnotation], join.Spec.CIDRBlock))
ips := strings.Split(util.GetIPAddrWithMask(node.Annotations[util.IPAddressAnnotation], join.Spec.CIDRBlock), ",")
ipCIDRs, err := util.GetIPAddrWithMask(node.Annotations[util.IPAddressAnnotation], join.Spec.CIDRBlock)
framework.ExpectNoError(err)
ips := strings.Split(ipCIDRs, ",")
framework.ExpectConsistOf(links[0].NonLinkLocalAddresses(), ips)

err = podClient.Delete(podName)
Expand Down
Loading