-
Notifications
You must be signed in to change notification settings - Fork 523
修复vip无法正常固定ip #5865
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
修复vip无法正常固定ip #5865
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Comment on lines
105
to
109
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这部分代码的逻辑是正确的,成功修复了问题。不过,通过一个小重构可以使代码更简洁易读。当前的嵌套 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 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
此处的逻辑是正确的,成功修复了当
vip.Spec.MacAddress为空字符串时macPointer不为nil的问题。不过,为了提高代码的可读性,可以考虑重构这部分逻辑,避免使用嵌套的
if语句。可以将macPointer的赋值操作提前,然后使用一个新的条件来判断是进行静态分配还是随机分配。这样可以让代码结构更清晰。例如,可以这样修改:
当然,这只是一个优化建议,当前的代码已经可以正常工作。