Skip to content
Merged
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
4 changes: 3 additions & 1 deletion pkg/controller/vip.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ func (c *Controller) handleAddVirtualIP(key string) error {
var macPointer *string
ipStr := util.GetStringIP(sourceV4Ip, sourceV6Ip)
if ipStr != "" || vip.Spec.MacAddress != "" {
macPointer = &vip.Spec.MacAddress
if vip.Spec.MacAddress != "" {
macPointer = &vip.Spec.MacAddress
}
v4ip, v6ip, mac, err = c.acquireStaticIPAddress(subnet.Name, vip.Name, portName, ipStr, macPointer)
Comment on lines 105 to 109
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

此处的逻辑是正确的,成功修复了当 vip.Spec.MacAddress 为空字符串时 macPointer 不为 nil 的问题。

不过,为了提高代码的可读性,可以考虑重构这部分逻辑,避免使用嵌套的 if 语句。可以将 macPointer 的赋值操作提前,然后使用一个新的条件来判断是进行静态分配还是随机分配。这样可以让代码结构更清晰。

例如,可以这样修改:

if vip.Spec.MacAddress != "" {
    macPointer = &vip.Spec.MacAddress
}

if ipStr != "" || macPointer != nil {
    v4ip, v6ip, mac, err = c.acquireStaticIPAddress(subnet.Name, vip.Name, portName, ipStr, macPointer)
} else {
    // Random allocate
    v4ip, v6ip, mac, err = c.acquireIPAddress(subnet.Name, vip.Name, portName)
}

当然,这只是一个优化建议,当前的代码已经可以正常工作。

Comment on lines 105 to 109
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

这部分代码的逻辑是正确的,成功修复了 bug。不过,嵌套的 if 语句可以简化,以提高代码的可读性。可以将内部的 if 条件合并到外部,使代码更简洁。

if vip.Spec.MacAddress != "" {
	macPointer = &vip.Spec.MacAddress
}
v4ip, v6ip, mac, err = c.acquireStaticIPAddress(subnet.Name, vip.Name, portName, ipStr, macPointer)

Comment on lines 105 to 109
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

这部分代码的逻辑是正确的,成功修复了问题。不过,通过一个小重构可以使代码更简洁易读。当前的嵌套 if 语句有些冗余。我们可以先处理 macPointer 的赋值,然后再用一个 if-else 块来决定是调用 acquireStaticIPAddress 还是 acquireIPAddress。这样可以使逻辑分离更清晰。

 	if vip.Spec.MacAddress != "" {
 		macPointer = &vip.Spec.MacAddress
 	}

	if ipStr != "" || macPointer != nil {
		v4ip, v6ip, mac, err = c.acquireStaticIPAddress(subnet.Name, vip.Name, portName, ipStr, macPointer)
	} else {
		// Random allocate
		v4ip, v6ip, mac, err = c.acquireIPAddress(subnet.Name, vip.Name, portName)
	}

} else {
// Random allocate
Expand Down