canonical / lxd

Powerful system container and virtual machine manager
https://canonical.com/lxd
GNU Affero General Public License v3.0
4.28k stars 914 forks source link

Fix image copy between two remotes when source is not in default project #9234

Closed stgraber closed 2 years ago

stgraber commented 2 years ago

There is an issue when doing: lxc image copy local:some-image remote: --project foo

Where the target server isn't told what project to fetch from resulting in an error if the default project on the source doesn't happen to contain the image too.

presztak commented 2 years ago

@stgraber Can I take this one as well?

stgraber commented 2 years ago

Absolutely!

presztak commented 2 years ago

@stgraber Should we assume that destination server also have foo project or should we copy to default project?

stgraber commented 2 years ago

No, we know the source project from the remote that's currently being used and we know the target project from the --project foo being passed here.

So if I do:

This will copy from the local default project over to the foo project on dest.

If I do:

This will copy from the local blah project over to whatever the project is for the dest remote (usually default).

Then you get:

This will copy from blah on local over to foo on dest.

presztak commented 2 years ago

@stgraber,

thanks for the explanation. I thought that --project flag is responsible for setting source project. From help of lxc tool: --project Override the source project

So, how should we handle this kind of situation:

We currently use remote1 and we do:

lxc image copy remote2:alpine/3.12 remote1: --project p1

how can we know source project if we are on remote1?

Anyway I see two problems:

  1. --project flag sets project for both, sourceServer and destinationServer: https://github.com/lxc/lxd/blob/2a036b5dcae85022ac43171aa0cd604421d7703f/lxc/image.go#L180 and https://github.com/lxc/lxd/blob/2a036b5dcae85022ac43171aa0cd604421d7703f/lxc/image.go#L186 in GetImageServer and ParseServers. So both servers have set the same project (from --project flag) but information about source project isn't passed anywhere (see pt 2)

  2. In ImageDownload function the source project isn't passed to remote: https://github.com/lxc/lxd/blob/2a036b5dcae85022ac43171aa0cd604421d7703f/lxd/daemon_images.go#L89 So remote doesn't know from which project get the image. Question is: Should we scan all projects? If no, we don't have information here about source project. In instance copy this kind information is passed by Source field in InstanceSource struct.

stgraber commented 2 years ago
stgraber@dakara:~/data/code/lxc/lxd (stgraber/master)$ lxc image copy local:test --project test s-sateda: --debug
DBUG[09-26|21:32:13] Connecting to a local LXD over a Unix socket 
DBUG[09-26|21:32:13] Sending request to LXD                   method=GET url=http://unix.socket/1.0 etag=
DBUG[09-26|21:32:13] Got response struct from LXD 
DBUG[09-26|21:32:13] Connecting to a remote LXD over HTTPs 
DBUG[09-26|21:32:13] Sending request to LXD                   method=GET url=https://sateda.hosts.mtl.stgraber.net:8443/1.0 etag=
DBUG[09-26|21:32:13] Got response struct from LXD 
DBUG[09-26|21:32:13] Sending request to LXD                   method=GET url="http://unix.socket/1.0/images/aliases/test?project=test" etag=
DBUG[09-26|21:32:13] Got response struct from LXD 
DBUG[09-26|21:32:13] 
    {
        "description": "",
        "target": "0f6f7fb06662d5922ce2b1a431f7f7ac95726b09b43009909a09d07c3611bb81",
        "name": "test",
        "type": "container"
    } 
DBUG[09-26|21:32:13] Sending request to LXD                   method=GET url="http://unix.socket/1.0/images/0f6f7fb06662d5922ce2b1a431f7f7ac95726b09b43009909a09d07c3611bb81?project=test" etag=
DBUG[09-26|21:32:13] Got response struct from LXD 
DBUG[09-26|21:32:13] 
    {
        "auto_update": false,
        "properties": {
            "architecture": "amd64",
            "description": "Alpine edge amd64 (20210926_13:26)",
            "os": "Alpine",
            "release": "edge",
            "serial": "20210926_13:26",
            "type": "squashfs",
            "variant": "default"
        },
        "public": false,
        "expires_at": "1969-12-31T19:00:00-05:00",
        "profiles": [
            "default"
        ],
        "aliases": [
            {
                "name": "test",
                "description": ""
            }
        ],
        "architecture": "x86_64",
        "cached": false,
        "filename": "lxd.tar.xz",
        "fingerprint": "0f6f7fb06662d5922ce2b1a431f7f7ac95726b09b43009909a09d07c3611bb81",
        "size": 5260132,
        "update_source": {
            "alias": "alpine/edge",
            "certificate": "",
            "protocol": "simplestreams",
            "server": "https://images.linuxcontainers.org",
            "image_type": ""
        },
        "type": "container",
        "created_at": "2021-09-25T20:00:00-04:00",
        "last_used_at": "0001-01-01T00:00:00Z",
        "uploaded_at": "2021-09-26T21:27:45.308045248-04:00"
    } 
DBUG[09-26|21:32:13] Connected to the websocket: ws://unix.socket/1.0/events?project=test 
DBUG[09-26|21:32:13] Sending request to LXD                   method=POST url="http://unix.socket/1.0/images/0f6f7fb06662d5922ce2b1a431f7f7ac95726b09b43009909a09d07c3611bb81/secret?project=test" etag=
DBUG[09-26|21:32:13] Got operation from LXD 
DBUG[09-26|21:32:13] 
    {
        "id": "71ceb927-f687-4381-8ff3-d97352fe81cf",
        "class": "token",
        "description": "Image download token",
        "created_at": "2021-09-26T21:32:13.39984512-04:00",
        "updated_at": "2021-09-26T21:32:13.39984512-04:00",
        "status": "Running",
        "status_code": 103,
        "resources": {
            "images": [
                "/1.0/images/0f6f7fb06662d5922ce2b1a431f7f7ac95726b09b43009909a09d07c3611bb81"
            ]
        },
        "metadata": {
            "secret": "39c56f90fad5a4d10b96e0723035396d52f51e061cea7bf572bd69689fffa623"
        },
        "may_cancel": true,
        "err": "",
        "location": "none"
    } 
DBUG[09-26|21:32:13] Connected to the websocket: wss://sateda.hosts.mtl.stgraber.net:8443/1.0/events?project=test 
DBUG[09-26|21:32:13] Sending request to LXD                   method=POST url="https://sateda.hosts.mtl.stgraber.net:8443/1.0/images?project=test" etag=
DBUG[09-26|21:32:13] 
    {
        "auto_update": false,
        "properties": null,
        "public": false,
        "expires_at": "0001-01-01T00:00:00Z",
        "profiles": null,
        "filename": "",
        "source": {
            "alias": "",
            "certificate": "-----BEGIN CERTIFICATE-----\nMIICAjCCAYmgAwIBAgIQa/k9an3krBObfO6DNtW/lTAKBggqhkjOPQQDAzA0MRww\nGgYDVQQKExNsaW51eGNvbnRhaW5lcnMub3JnMRQwEgYDVQQDDAtyb290QGRha2Fy\nYTAeFw0yMTA4MjAwMjQzMTFaFw0zMTA4MTgwMjQzMTFaMDQxHDAaBgNVBAoTE2xp\nbnV4Y29udGFpbmVycy5vcmcxFDASBgNVBAMMC3Jvb3RAZGFrYXJhMHYwEAYHKoZI\nzj0CAQYFK4EEACIDYgAEOAqDZ43q3Vc16fHY7T+isfcMQf4F64k/crvEp+tQriH9\nYBjU0gG/Iz9/dJKN0LHW8OWKbu8wdNzleVA3Xjapc/LOhqJu+nMobCNRB8EHlvNd\n7kn84febwMwxumOlvqtao2AwXjAOBgNVHQ8BAf8EBAMCBaAwEwYDVR0lBAwwCgYI\nKwYBBQUHAwEwDAYDVR0TAQH/BAIwADApBgNVHREEIjAgggZkYWthcmGHBH8AAAGH\nEAAAAAAAAAAAAAAAAAAAAAEwCgYIKoZIzj0EAwMDZwAwZAIwB24TGLrC74OTAe9h\nnVHFRdA80EaKHOg/p/wX0P4cCtllQxs7VrfY/8ryp6OKTluvAjBYlBcDJQrab9lk\nsCtiXBUPbYR7/u6Wfe7nL8NT+NsFKXcLLy5SMBiD0CDrnQdUyXM=\n-----END CERTIFICATE-----\n",
            "protocol": "lxd",
            "server": "https://172.17.250.1:8443",
            "image_type": "",
            "mode": "pull",
            "type": "image",
            "url": "",
            "name": "",
            "fingerprint": "0f6f7fb06662d5922ce2b1a431f7f7ac95726b09b43009909a09d07c3611bb81",
            "secret": "39c56f90fad5a4d10b96e0723035396d52f51e061cea7bf572bd69689fffa623"
        },
        "compression_algorithm": "",
        "aliases": null
    } 
DBUG[09-26|21:32:13] Sending request to LXD                   method=POST url="https://sateda.hosts.mtl.stgraber.net:8443/1.0/images?project=test" etag=
DBUG[09-26|21:32:13] 
    {
        "auto_update": false,
        "properties": null,
        "public": false,
        "expires_at": "0001-01-01T00:00:00Z",
        "profiles": null,
        "filename": "",
        "source": {
            "alias": "",
            "certificate": "-----BEGIN CERTIFICATE-----\nMIICAjCCAYmgAwIBAgIQa/k9an3krBObfO6DNtW/lTAKBggqhkjOPQQDAzA0MRww\nGgYDVQQKExNsaW51eGNvbnRhaW5lcnMub3JnMRQwEgYDVQQDDAtyb290QGRha2Fy\nYTAeFw0yMTA4MjAwMjQzMTFaFw0zMTA4MTgwMjQzMTFaMDQxHDAaBgNVBAoTE2xp\nbnV4Y29udGFpbmVycy5vcmcxFDASBgNVBAMMC3Jvb3RAZGFrYXJhMHYwEAYHKoZI\nzj0CAQYFK4EEACIDYgAEOAqDZ43q3Vc16fHY7T+isfcMQf4F64k/crvEp+tQriH9\nYBjU0gG/Iz9/dJKN0LHW8OWKbu8wdNzleVA3Xjapc/LOhqJu+nMobCNRB8EHlvNd\n7kn84febwMwxumOlvqtao2AwXjAOBgNVHQ8BAf8EBAMCBaAwEwYDVR0lBAwwCgYI\nKwYBBQUHAwEwDAYDVR0TAQH/BAIwADApBgNVHREEIjAgggZkYWthcmGHBH8AAAGH\nEAAAAAAAAAAAAAAAAAAAAAEwCgYIKoZIzj0EAwMDZwAwZAIwB24TGLrC74OTAe9h\nnVHFRdA80EaKHOg/p/wX0P4cCtllQxs7VrfY/8ryp6OKTluvAjBYlBcDJQrab9lk\nsCtiXBUPbYR7/u6Wfe7nL8NT+NsFKXcLLy5SMBiD0CDrnQdUyXM=\n-----END CERTIFICATE-----\n",
            "protocol": "lxd",
            "server": "https://[2602:fc62:b:250::1]:8443",
            "image_type": "",
            "mode": "pull",
            "type": "image",
            "url": "",
            "name": "",
            "fingerprint": "0f6f7fb06662d5922ce2b1a431f7f7ac95726b09b43009909a09d07c3611bb81",
            "secret": "39c56f90fad5a4d10b96e0723035396d52f51e061cea7bf572bd69689fffa623"
        },
        "compression_algorithm": "",
        "aliases": null
    } 
DBUG[09-26|21:32:13] Sending request to LXD                   method=POST url="https://sateda.hosts.mtl.stgraber.net:8443/1.0/images?project=test" etag=
DBUG[09-26|21:32:13] 
    {
        "auto_update": false,
        "properties": null,
        "public": false,
        "expires_at": "0001-01-01T00:00:00Z",
        "profiles": null,
        "filename": "",
        "source": {
            "alias": "",
            "certificate": "-----BEGIN CERTIFICATE-----\nMIICAjCCAYmgAwIBAgIQa/k9an3krBObfO6DNtW/lTAKBggqhkjOPQQDAzA0MRww\nGgYDVQQKExNsaW51eGNvbnRhaW5lcnMub3JnMRQwEgYDVQQDDAtyb290QGRha2Fy\nYTAeFw0yMTA4MjAwMjQzMTFaFw0zMTA4MTgwMjQzMTFaMDQxHDAaBgNVBAoTE2xp\nbnV4Y29udGFpbmVycy5vcmcxFDASBgNVBAMMC3Jvb3RAZGFrYXJhMHYwEAYHKoZI\nzj0CAQYFK4EEACIDYgAEOAqDZ43q3Vc16fHY7T+isfcMQf4F64k/crvEp+tQriH9\nYBjU0gG/Iz9/dJKN0LHW8OWKbu8wdNzleVA3Xjapc/LOhqJu+nMobCNRB8EHlvNd\n7kn84febwMwxumOlvqtao2AwXjAOBgNVHQ8BAf8EBAMCBaAwEwYDVR0lBAwwCgYI\nKwYBBQUHAwEwDAYDVR0TAQH/BAIwADApBgNVHREEIjAgggZkYWthcmGHBH8AAAGH\nEAAAAAAAAAAAAAAAAAAAAAEwCgYIKoZIzj0EAwMDZwAwZAIwB24TGLrC74OTAe9h\nnVHFRdA80EaKHOg/p/wX0P4cCtllQxs7VrfY/8ryp6OKTluvAjBYlBcDJQrab9lk\nsCtiXBUPbYR7/u6Wfe7nL8NT+NsFKXcLLy5SMBiD0CDrnQdUyXM=\n-----END CERTIFICATE-----\n",
            "protocol": "lxd",
            "server": "https://172.17.0.232:8443",
            "image_type": "",
            "mode": "pull",
            "type": "image",
            "url": "",
            "name": "",
            "fingerprint": "0f6f7fb06662d5922ce2b1a431f7f7ac95726b09b43009909a09d07c3611bb81",
            "secret": "39c56f90fad5a4d10b96e0723035396d52f51e061cea7bf572bd69689fffa623"
        },
        "compression_algorithm": "",
        "aliases": null
    } 
DBUG[09-26|21:32:13] Sending request to LXD                   method=POST url="https://sateda.hosts.mtl.stgraber.net:8443/1.0/images?project=test" etag=
DBUG[09-26|21:32:13] 
    {
        "auto_update": false,
        "properties": null,
        "public": false,
        "expires_at": "0001-01-01T00:00:00Z",
        "profiles": null,
        "filename": "",
        "source": {
            "alias": "",
            "certificate": "-----BEGIN CERTIFICATE-----\nMIICAjCCAYmgAwIBAgIQa/k9an3krBObfO6DNtW/lTAKBggqhkjOPQQDAzA0MRww\nGgYDVQQKExNsaW51eGNvbnRhaW5lcnMub3JnMRQwEgYDVQQDDAtyb290QGRha2Fy\nYTAeFw0yMTA4MjAwMjQzMTFaFw0zMTA4MTgwMjQzMTFaMDQxHDAaBgNVBAoTE2xp\nbnV4Y29udGFpbmVycy5vcmcxFDASBgNVBAMMC3Jvb3RAZGFrYXJhMHYwEAYHKoZI\nzj0CAQYFK4EEACIDYgAEOAqDZ43q3Vc16fHY7T+isfcMQf4F64k/crvEp+tQriH9\nYBjU0gG/Iz9/dJKN0LHW8OWKbu8wdNzleVA3Xjapc/LOhqJu+nMobCNRB8EHlvNd\n7kn84febwMwxumOlvqtao2AwXjAOBgNVHQ8BAf8EBAMCBaAwEwYDVR0lBAwwCgYI\nKwYBBQUHAwEwDAYDVR0TAQH/BAIwADApBgNVHREEIjAgggZkYWthcmGHBH8AAAGH\nEAAAAAAAAAAAAAAAAAAAAAEwCgYIKoZIzj0EAwMDZwAwZAIwB24TGLrC74OTAe9h\nnVHFRdA80EaKHOg/p/wX0P4cCtllQxs7VrfY/8ryp6OKTluvAjBYlBcDJQrab9lk\nsCtiXBUPbYR7/u6Wfe7nL8NT+NsFKXcLLy5SMBiD0CDrnQdUyXM=\n-----END CERTIFICATE-----\n",
            "protocol": "lxd",
            "server": "https://[2602:fc62:b:1000:5436:5b25:64e4:d81a]:8443",
            "image_type": "",
            "mode": "pull",
            "type": "image",
            "url": "",
            "name": "",
            "fingerprint": "0f6f7fb06662d5922ce2b1a431f7f7ac95726b09b43009909a09d07c3611bb81",
            "secret": "39c56f90fad5a4d10b96e0723035396d52f51e061cea7bf572bd69689fffa623"
        },
        "compression_algorithm": "",
        "aliases": null
    } 
Error: Failed remote image download: Fetch project database object: No such object

We can see the CLI correctly asking for a token from the source but then the target fails to connect to the source as the project isn't specified.

It feels like this should grow a project field as part of the source information:

    {
        "auto_update": false,
        "properties": null,
        "public": false,
        "expires_at": "0001-01-01T00:00:00Z",
        "profiles": null,
        "filename": "",
        "source": {
            "alias": "",
            "certificate": "-----BEGIN CERTIFICATE-----\nMIICAjCCAYmgAwIBAgIQa/k9an3krBObfO6DNtW/lTAKBggqhkjOPQQDAzA0MRww\nGgYDVQQKExNsaW51eGNvbnRhaW5lcnMub3JnMRQwEgYDVQQDDAtyb290QGRha2Fy\nYTAeFw0yMTA4MjAwMjQzMTFaFw0zMTA4MTgwMjQzMTFaMDQxHDAaBgNVBAoTE2xp\nbnV4Y29udGFpbmVycy5vcmcxFDASBgNVBAMMC3Jvb3RAZGFrYXJhMHYwEAYHKoZI\nzj0CAQYFK4EEACIDYgAEOAqDZ43q3Vc16fHY7T+isfcMQf4F64k/crvEp+tQriH9\nYBjU0gG/Iz9/dJKN0LHW8OWKbu8wdNzleVA3Xjapc/LOhqJu+nMobCNRB8EHlvNd\n7kn84febwMwxumOlvqtao2AwXjAOBgNVHQ8BAf8EBAMCBaAwEwYDVR0lBAwwCgYI\nKwYBBQUHAwEwDAYDVR0TAQH/BAIwADApBgNVHREEIjAgggZkYWthcmGHBH8AAAGH\nEAAAAAAAAAAAAAAAAAAAAAEwCgYIKoZIzj0EAwMDZwAwZAIwB24TGLrC74OTAe9h\nnVHFRdA80EaKHOg/p/wX0P4cCtllQxs7VrfY/8ryp6OKTluvAjBYlBcDJQrab9lk\nsCtiXBUPbYR7/u6Wfe7nL8NT+NsFKXcLLy5SMBiD0CDrnQdUyXM=\n-----END CERTIFICATE-----\n",
            "protocol": "lxd",
            "server": "https://172.17.0.232:8443",
            "image_type": "",
            "mode": "pull",
            "type": "image",
            "url": "",
            "name": "",
            "fingerprint": "0f6f7fb06662d5922ce2b1a431f7f7ac95726b09b43009909a09d07c3611bb81",
            "secret": "39c56f90fad5a4d10b96e0723035396d52f51e061cea7bf572bd69689fffa623"
        },
        "compression_algorithm": "",
        "aliases": null
    }