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

Allow changing the parent value on physical networks #1317

Merged
merged 4 commits into from
Oct 18, 2024
Merged
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
4 changes: 2 additions & 2 deletions cmd/incusd/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ func networkDelete(d *Daemon, r *http.Request) response.Response {
clusterNotification := isClusterNotification(r)
if !clusterNotification {
// Quick checks.
inUse, err := n.IsUsed()
inUse, err := n.IsUsed(false)
if err != nil {
return response.SmartError(err)
}
Expand Down Expand Up @@ -1208,7 +1208,7 @@ func networkPost(d *Daemon, r *http.Request) response.Response {
}

// Check network isn't in use.
inUse, err := n.IsUsed()
inUse, err := n.IsUsed(false)
if err != nil {
return response.InternalError(fmt.Errorf("Failed checking network in use: %w", err))
}
Expand Down
17 changes: 15 additions & 2 deletions internal/server/network/driver_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,21 @@ func (n *common) Locations() []string {
return locations
}

// IsUsed returns whether the network is used by any instances or profiles.
func (n *common) IsUsed() (bool, error) {
// IsUsed returns whether the network is in use by instances or by downstream networks.
func (n *common) IsUsed(instanceOnly bool) (bool, error) {
if instanceOnly {
usedBy := 0
err := UsedByInstanceDevices(n.state, n.project, n.name, n.netType, func(inst db.InstanceArgs, nicName string, nicConfig map[string]string) error {
usedBy++
return nil
})
if err != nil {
return false, err
}

return usedBy > 0, nil
}

usedBy, err := UsedBy(n.state, n.project, n.id, n.name, n.netType, true)
if err != nil {
return false, err
Expand Down
23 changes: 21 additions & 2 deletions internal/server/network/driver_physical.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/lxc/incus/v6/internal/server/cluster/request"
"github.com/lxc/incus/v6/internal/server/db"
"github.com/lxc/incus/v6/internal/server/ip"
"github.com/lxc/incus/v6/internal/server/network/ovs"
"github.com/lxc/incus/v6/shared/api"
"github.com/lxc/incus/v6/shared/logger"
"github.com/lxc/incus/v6/shared/util"
Expand Down Expand Up @@ -297,7 +298,7 @@ func (n *physical) Update(newNetwork api.NetworkPut, targetNode string, clientTy
// We only need to check in the database once, not on every clustered node.
if clientType == request.ClientTypeNormal {
if hostNameChanged {
isUsed, err := n.IsUsed()
isUsed, err := n.IsUsed(true)
if isUsed || err != nil {
return fmt.Errorf("Cannot update network parent interface when in use")
}
Expand Down Expand Up @@ -329,7 +330,7 @@ func (n *physical) Update(newNetwork api.NetworkPut, targetNode string, clientTy
_ = n.common.update(oldNetwork, targetNode, clientType)
})

// Apply changes to all nodes and databse.
// Apply changes to all nodes and database.
err = n.common.update(newNetwork, targetNode, clientType)
if err != nil {
return err
Expand All @@ -340,6 +341,24 @@ func (n *physical) Update(newNetwork api.NetworkPut, targetNode string, clientTy
return err
}

// Update OVS bridge entries (for dependent OVN networks).
if hostNameChanged {
vswitch, err := n.state.OVS()
if err == nil {
ovsBridge := fmt.Sprintf("incusovn%d", n.id)

err := vswitch.DeleteBridgePort(context.TODO(), ovsBridge, oldNetwork.Config["parent"])
if err != nil && err != ovs.ErrNotFound {
return err
}

err = vswitch.CreateBridgePort(context.TODO(), ovsBridge, newNetwork.Config["parent"], true)
if err != nil && err != ovs.ErrNotFound {
return err
}
}
}

revert.Success()

// Notify dependent networks (those using this network as their uplink) of the changes.
Expand Down
2 changes: 1 addition & 1 deletion internal/server/network/network_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type Network interface {
LocalStatus() string
Config() map[string]string
Locations() []string
IsUsed() (bool, error)
IsUsed(instanceOnly bool) (bool, error)
IsManaged() bool
DHCPv4Subnet() *net.IPNet
DHCPv6Subnet() *net.IPNet
Expand Down
Loading