Skip to content

Commit

Permalink
Merge pull request #12939 from tomponline/tp-network-attach
Browse files Browse the repository at this point in the history
lxd: Fix incorrect network device attach warnings
  • Loading branch information
tomponline authored Feb 22, 2024
2 parents 3fa8bcf + 7974515 commit 1a07184
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lxd/db/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ func (c *ClusterTx) GetNetworkWithInterface(ctx context.Context, devName string)
}

if id == -1 {
return -1, nil, fmt.Errorf("No network found for interface: %s", devName)
return -1, nil, api.StatusErrorf(http.StatusNotFound, "No network found for interface: %s", devName)
}

network := api.Network{
Expand Down
2 changes: 1 addition & 1 deletion lxd/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ func deviceEventListener(stateFunc func() *state.State) {
logger.Debugf("Scheduler: network: %s has been added: updating network priorities", e[0])
err = networkAutoAttach(s.DB.Cluster, e[0])
if err != nil {
logger.Warn("Failed to auto-attach network", logger.Ctx{"err": err})
logger.Warn("Failed to auto-attach network", logger.Ctx{"err": err, "dev": e[0]})
}

case e := <-chUSB:
Expand Down
16 changes: 12 additions & 4 deletions lxd/networks_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package main

import (
"context"
"fmt"
"net/http"

"github.com/canonical/lxd/lxd/cluster"
"github.com/canonical/lxd/lxd/db"
Expand All @@ -18,16 +20,22 @@ func networkAutoAttach(cluster *db.Cluster, devName string) error {
_ = cluster.Transaction(context.TODO(), func(ctx context.Context, c *db.ClusterTx) error {
_, dbInfo, err := c.GetNetworkWithInterface(ctx, devName)
if err != nil {
// No match found, move on
logger.Warnf("Failed to find network matching interface %v", devName)
return nil
if !api.StatusErrorCheck(err, http.StatusNotFound) {
return fmt.Errorf("Failed finding network matching interface %q: %w", devName, err)
}

return nil // No match found, move on.
}

networkName = dbInfo.Name
return nil
})

return network.AttachInterface(networkName, devName)
if networkName != "" {
return network.AttachInterface(networkName, devName)
}

return nil
}

// networkUpdateForkdnsServersTask runs every 30s and refreshes the forkdns servers list.
Expand Down

0 comments on commit 1a07184

Please sign in to comment.