Skip to content

Commit

Permalink
lxd: Provide state for physical managed networks (#13965)
Browse files Browse the repository at this point in the history
This PR adds support for providing state for physical managed networks.
When the parent of a physical managed network is available, we provide
this parent state. When the parent is not available, we provide a
response indicating that the state is `unavailable` and the type is
`unknown`.

Closes #13553.
  • Loading branch information
tomponline authored Aug 23, 2024
2 parents 3c1805d + a581ba6 commit 68e1b1b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lxd/network/driver_physical.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/canonical/lxd/lxd/cluster/request"
"github.com/canonical/lxd/lxd/db"
"github.com/canonical/lxd/lxd/ip"
"github.com/canonical/lxd/lxd/resources"
"github.com/canonical/lxd/shared"
"github.com/canonical/lxd/shared/api"
"github.com/canonical/lxd/shared/logger"
Expand Down Expand Up @@ -504,3 +505,26 @@ func (n *physical) DHCPv6Subnet() *net.IPNet {

return subnet
}

// State returns the api.NetworkState for the network.
func (n *physical) State() (*api.NetworkState, error) {
if !n.IsManaged() {
return resources.GetNetworkState(n.name)
}

state, err := resources.GetNetworkState(GetHostDevice(n.config["parent"], n.config["vlan"]))
if err != nil {
// If the parent is not found, return a response indicating the network is unavailable.
if api.StatusErrorCheck(err, 404) {
return &api.NetworkState{
State: "unavailable",
Type: "unknown",
}, nil
}

// In all other cases, return the original error.
return nil, err
}

return state, nil
}
16 changes: 16 additions & 0 deletions test/suites/network.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ test_network() {
lxc network create lxdt$$ ipv4.address=none ipv6.address=none
lxc network delete lxdt$$

# Check that we can return state for physical networks
ip link add dummy0 type dummy
lxc network create lxdt$$ --type=physical parent=dummy0

expected_state=$(lxc network info dummy0 | grep -F "State:")
expected_type=$(lxc network info dummy0 | grep -F "Type:")
lxc network info lxdt$$ | grep -qF "${expected_state}"
lxc network info lxdt$$ | grep -qF "${expected_type}"

# Delete physical network and check for expected response
ip link delete dummy0
lxc network info lxdt$$ | grep -qF "State: unavailable"
lxc network info lxdt$$ | grep -qF "Type: unknown"

lxc network delete lxdt$$

# Configured bridge with static assignment
lxc network create lxdt$$ dns.domain=test dns.mode=managed ipv6.dhcp.stateful=true
lxc network attach lxdt$$ nettest eth0
Expand Down

0 comments on commit 68e1b1b

Please sign in to comment.