Skip to content

Commit

Permalink
Network: add target parameter for GET 1.0/networks and network_get_ta…
Browse files Browse the repository at this point in the history
…rget api extension (#14419)

# Done

* add target parameter for GET 1.0/networks 
* add network_get_target api extension

fixes #14406 

~~The UI can fetch local networks for each member, and we don't need to
populate the `locations` field for non managed networks in the response
of `GET 1.0/netwroks`. Populating that field is expensive, because LXD
would have to fan out the request to all members to collect the network
interface lists for all of them.~~ Edit: We still need to add the
`locations` field in a follow-up, because it is the right thing to do
for the CLI.

This unblocks the UI work on Network management for cluster
environments. Especially to fetch networks per cluster member in order
to provide the right list of available uplinks/parents per member.
  • Loading branch information
tomponline authored Dec 12, 2024
2 parents b99d691 + b774b67 commit 510c011
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 1 deletion.
4 changes: 4 additions & 0 deletions doc/api-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2538,3 +2538,7 @@ Adds a new {config:option}`device-unix-hotplug-device-conf:subsystem` configurat

## `storage_ceph_osd_pool_size`
This introduces the configuration keys {config:option}`storage-ceph-pool-conf:ceph.osd.pool_size`, and {config:option}`storage-cephfs-pool-conf:cephfs.osd_pool_size` to be used when adding or updating a `ceph` or `cephfs` storage pool to instruct LXD to create set the replication size for the underlying OSD pools.

## `network_get_target`

Adds optional `target` parameter to `GET /1.0/network`. When target is set, forward the request to the specified cluster member and return the non-managed interfaces from that member.
5 changes: 5 additions & 0 deletions doc/rest-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13884,6 +13884,11 @@ paths:
in: query
name: project
type: string
- description: Cluster member name
example: lxd01
in: query
name: target
type: string
produces:
- application/json
responses:
Expand Down
11 changes: 10 additions & 1 deletion lxc/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,7 @@ type cmdNetworkList struct {
network *cmdNetwork

flagFormat string
flagTarget string
}

func (c *cmdNetworkList) command() *cobra.Command {
Expand All @@ -1007,6 +1008,7 @@ func (c *cmdNetworkList) command() *cobra.Command {

cmd.RunE = c.run
cmd.Flags().StringVarP(&c.flagFormat, "format", "f", "table", i18n.G("Format (csv|json|table|yaml|compact)")+"``")
cmd.Flags().StringVar(&c.flagTarget, "target", "", i18n.G("Cluster member name")+"``")

cmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
Expand Down Expand Up @@ -1044,7 +1046,14 @@ func (c *cmdNetworkList) run(cmd *cobra.Command, args []string) error {
return errors.New(i18n.G("Filtering isn't supported yet"))
}

networks, err := resource.server.GetNetworks()
client := resource.server

// Targeting.
if c.flagTarget != "" {
client = client.UseTarget(c.flagTarget)
}

networks, err := client.GetNetworks()
if err != nil {
return err
}
Expand Down
11 changes: 11 additions & 0 deletions lxd/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ func networkAccessHandler(entitlement auth.Entitlement) func(d *Daemon, r *http.
// description: Project name
// type: string
// example: default
// - in: query
// name: target
// description: Cluster member name
// type: string
// example: lxd01
// responses:
// "200":
// description: API endpoints
Expand Down Expand Up @@ -231,6 +236,12 @@ func networkAccessHandler(entitlement auth.Entitlement) func(d *Daemon, r *http.
func networksGet(d *Daemon, r *http.Request) response.Response {
s := d.State()

// If a target was specified, forward the request to the relevant node.
resp := forwardedResponseIfTargetIsRemote(s, r)
if resp != nil {
return resp
}

requestProjectName := request.ProjectParam(r)
effectiveProjectName, reqProject, err := project.NetworkProject(s.DB.Cluster, requestProjectName)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions shared/version/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ var APIExtensions = []string{
"unix_device_hotplug_ownership_inherit",
"unix_device_hotplug_subsystem_device_option",
"storage_ceph_osd_pool_size",
"network_get_target",
}

// APIExtensionsCount returns the number of available API extensions.
Expand Down
1 change: 1 addition & 0 deletions test/includes/clustering.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ ip link set dev "${veth2}" name eth0
ip link set eth0 up
ip addr add "10.1.1.10${id}/16" dev eth0
ip route add default via 10.1.1.1
ip link add localBridge${id} type bridge
EOF
}

Expand Down
7 changes: 7 additions & 0 deletions test/suites/clustering.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1217,6 +1217,13 @@ test_clustering_network() {
LXD_DIR="${LXD_ONE_DIR}" lxc config device remove c3 eth0
LXD_DIR="${LXD_ONE_DIR}" lxc config device add c3 eth0 nic hwaddr="${c1MAC}" nictype=bridged parent="${net}"

# Check networks local to a cluster member show up when targeting that member
# and hidden when targeting other cluster members. Setup is in includes/clustering.sh
LXD_DIR="${LXD_ONE_DIR}" lxc network list --target=node1 | grep localBridge1
! LXD_DIR="${LXD_ONE_DIR}" lxc network list --target=node1 | grep localBridge2 || false
! LXD_DIR="${LXD_ONE_DIR}" lxc network list --target=node2 | grep localBridge1 || false
LXD_DIR="${LXD_ONE_DIR}" lxc network list --target=node2 | grep localBridge2

# Cleanup instances and image.
LXD_DIR="${LXD_ONE_DIR}" lxc delete -f c1 c2 c3
LXD_DIR="${LXD_ONE_DIR}" lxc image delete testimage
Expand Down

0 comments on commit 510c011

Please sign in to comment.