-
Notifications
You must be signed in to change notification settings - Fork 784
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
bridge: Extract some netlink operations code to file #998
base: main
Are you sure you want to change the base?
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2024 CNI authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package link | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/vishvananda/netlink" | ||
) | ||
|
||
func WaitForOperStateUp(linkName string) (netlink.Link, error) { | ||
var link netlink.Link | ||
var err error | ||
retries := []int{0, 50, 500, 1000, 1000} | ||
for idx, sleep := range retries { | ||
time.Sleep(time.Duration(sleep) * time.Millisecond) | ||
|
||
link, err = netlink.LinkByName(linkName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
linkOpState := link.Attrs().OperState | ||
if linkOpState == netlink.OperUp { | ||
break | ||
} | ||
|
||
if idx == len(retries)-1 { | ||
return nil, fmt.Errorf("timeout waiting for %q state %q to be up", linkName, linkOpState) | ||
} | ||
} | ||
return link, nil | ||
} | ||
|
||
func SetUp(linkName string) error { | ||
link, err := netlink.LinkByName(linkName) | ||
if err != nil { | ||
return fmt.Errorf("failed to retrieve link: %w", err) | ||
} | ||
if err = netlink.LinkSetUp(link); err != nil { | ||
return fmt.Errorf("failed to set %q up: %w", linkName, err) | ||
} | ||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,6 @@ import ( | |
"runtime" | ||
"sort" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/vishvananda/netlink" | ||
|
||
|
@@ -677,39 +676,18 @@ func cmdAdd(args *skel.CmdArgs) error { | |
} | ||
} | ||
} else { | ||
// If layer 2 we still need to set the container veth to up | ||
if err := netns.Do(func(_ ns.NetNS) error { | ||
link, err := netlink.LinkByName(args.IfName) | ||
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. Why do you want a function for this? I understand the one for retries which includes some logic with the for loop but this is simple and not used anywhere else. 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. My intent was to improve readability, even though there is no much of logic its less code to read, and may reduce some cognitive load (I dont need to know we need to fetch the link in order to bring it down). Also, with #997 changes, it makes the code read better:
WDYT? 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. no, I think this is simple enough. If you want to improve readability, then abstract all the logic inside if layer3 branch, then in your other pr, you can do
|
||
if err != nil { | ||
return fmt.Errorf("failed to retrieve link: %v", err) | ||
} | ||
// If layer 2 we still need to set the container veth to up | ||
if err = netlink.LinkSetUp(link); err != nil { | ||
return fmt.Errorf("failed to set %q up: %v", args.IfName, err) | ||
} | ||
return nil | ||
return link.SetUp(args.IfName) | ||
}); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
var hostVeth netlink.Link | ||
|
||
// check bridge port state | ||
retries := []int{0, 50, 500, 1000, 1000} | ||
for idx, sleep := range retries { | ||
time.Sleep(time.Duration(sleep) * time.Millisecond) | ||
|
||
hostVeth, err = netlink.LinkByName(hostInterface.Name) | ||
if err != nil { | ||
return err | ||
} | ||
if hostVeth.Attrs().OperState == netlink.OperUp { | ||
break | ||
} | ||
|
||
if idx == len(retries)-1 { | ||
return fmt.Errorf("bridge port in error state: %s", hostVeth.Attrs().OperState) | ||
} | ||
hostVeth, err := link.WaitForOperStateUp(hostInterface.Name) | ||
if err != nil { | ||
return fmt.Errorf("bridge port in error state %q: %v", hostVeth.Attrs().OperState, err) | ||
} | ||
|
||
// In certain circumstances, the host-side of the veth may change addrs | ||
|
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.
let's be consistent and return here fmt.Errorf("failed to retrieve link: %v", err) as in SetUp