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

bridge: Extract some netlink operations code to file #998

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
56 changes: 56 additions & 0 deletions pkg/link/opstate.go
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
Copy link
Member

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

}
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
}
32 changes: 5 additions & 27 deletions plugins/main/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"runtime"
"sort"
"syscall"
"time"

"github.com/vishvananda/netlink"

Expand Down Expand Up @@ -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)
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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:

...
else if !disableContIface {
        link.SetUp(ifaceName)
}
...

WDYT?

Copy link
Member

@mlguerrero12 mlguerrero12 Jan 16, 2024

Choose a reason for hiding this comment

The 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 !disableContIface {
	if layer {
		layer3func()
	} else {
		thiscode
	}
	WaitOperStateUp()
}

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
Expand Down