Skip to content

Conversation

@MusicDin
Copy link
Member

@MusicDin MusicDin commented Jun 5, 2025

This PR changes the devLXD response functions in a way that it determines the response format based on the vsock usage. If devLXD is being interacted over vsock (from LXD agent), the responses are returned as api.Response. Otherwise, the response are formatted as api.DevLXDResponse. This information is stored in the request context, and is retrieved from it during the response rendering.

Additionally, the LXD agent now uses the devLXD client to interact with the devLXD. This means that the devLXD client is adjusted to expect api.Response when used from LXD agent.


"github.com/canonical/lxd/client"
"github.com/canonical/lxd/lxd/metrics"
"github.com/canonical/lxd/lxd/request"
Copy link
Member Author

@MusicDin MusicDin Jun 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to draw some attention here.
Since the information whether the vsock is being used is required during response rendering, the response package is now depending on request package (to reference a context key - request.DevLXDOverVsock).

If this is not desired, we can use plain string to reference the key. Or maybe there is even better approach that I am not seeing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not certain about the side effects of this but one option would be to define the context key elsewhere. The context key should be of a particular type and not just a plain string (you'll get a linter complaint otherwise, I think this is required for key uniqueness or something)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, so if there are any undesired side effects then we should move the key in some package that can be referenced from lxd and lxd/response package. We can use lxd/response package itself as this would remove the need for a new dependency and the key is mainly used to determine the response format.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be an issue for MicroCluster, but I think @roosterfish is in the process of untangling that stuff anyway.

@tomponline
Copy link
Member

Please rebase

@markylaing
Copy link
Contributor

Initial review - all looking very neat and tidy! Especially in the lxd-agent 🚀

@MusicDin MusicDin force-pushed the feat/devlxd-cleanup branch from 57b1812 to 3f266b5 Compare June 5, 2025 18:32
@MusicDin MusicDin marked this pull request as ready for review June 6, 2025 09:00
@markylaing
Copy link
Contributor

	trnsprt, ok := r.http.Transport.(*http.Transport)
	if !ok {
		panic("error")
	}
	
	if trnsprt.TLSClientConfig != nil {
		// When communicating with devLXD over vsock, the response is
		// expected to be in devLXD format.
		resp, etag, err := lxdParseResponse(resp)
		if err != nil {
			return nil, "", err
		}

		// Wrap into devLXD response.
		return &api.DevLXDResponse{
			Content:    resp.Metadata,
			StatusCode: resp.StatusCode,
		}, etag, nil
	}

Suggested in 1:1 using the above instead of having an isVSock field :)

@MusicDin
Copy link
Member Author

MusicDin commented Jun 6, 2025

Just for context, @markylaing's suggestion is a way to get rid of setting/hardcoding isDevLXDOverVsock=true when ConnectDevLXDHTTP is called.

@MusicDin MusicDin force-pushed the feat/devlxd-cleanup branch from 3f266b5 to 303ef87 Compare June 9, 2025 09:27
@MusicDin MusicDin force-pushed the feat/devlxd-cleanup branch from 303ef87 to e7c8405 Compare June 9, 2025 09:30
}, nil
}

// ConnectDevLXDHTTPWithContext lets you connect to devLXD over a VM socket.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

VM socket? You mean Instance socket - is this for container or VMs?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For containers we have just ConnectDevLXD[WithContext](), which connects to the provided unix path (e.g. /dev/lxd/sock).

This is for VMs, and it was meant to be ... over a vsock.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see the important difference was "HTTP".

Copy link
Member

@tomponline tomponline left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks!

@tomponline tomponline merged commit 00d17d1 into canonical:main Jun 9, 2025
30 checks passed
@MusicDin MusicDin deleted the feat/devlxd-cleanup branch June 9, 2025 15:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants