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

lxd_container: support lxd instance types #3661

Merged
merged 11 commits into from
Nov 20, 2021

Conversation

rchicoli
Copy link
Contributor

@rchicoli rchicoli commented Nov 3, 2021

SUMMARY

This MR adds support to lxd_container module to deploy either containers or virtual-machines. For that it was necessary to create two new parameters api_endpoint and type:

  • api_endpoint: is required in order to be backwards compatible with old LXD servers, which does not support the new API endpoint /1.0/virtual-machines or /1.0/instances (implemented in lxd 3.19)
  • type: there is two different types of instances, “container” or “virtual-machine”

It is related to:

It is a different approach of an open MR:

ISSUE TYPE
  • Feature Pull Request
COMPONENT NAME

lxd_container

ADDITIONAL INFORMATION

The old functionality has not changed. Additionally I've tested the changes only with the latest LXD version 4.19.

  • it is possible to create virtual-machine or container
  • if an instance already exists and someone tries to update its "type" from container to virtual-machine or vice-versa, it fails with a warning, otherwise I was only getting changed as status, which is not correct.

@ansibullbot

This comment has been minimized.

@ansibullbot ansibullbot added ci_verified Push fixes to PR branch to re-run CI cloud feature This issue/PR relates to a feature request module module needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR new_contributor Help guide this first time contributor plugins plugin (any type) labels Nov 3, 2021
@ansibullbot ansibullbot removed ci_verified Push fixes to PR branch to re-run CI needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR labels Nov 3, 2021
@conloos
Copy link
Contributor

conloos commented Nov 3, 2021

Hi @rchicoli,

there are several people who are currently working on providing the functionality of the 4.x lxd-api version.
See: lxd_virtual_machine - new module #3480.
@felix I prefer to have only one plugin. My suggestion is to rename lxd_container to lxd, as far as possible and e.g. add a symbolic link for backward compatibility.

It is to late so i didn't look deep on the changes.

Thanks Frank

@rchicoli
Copy link
Contributor Author

rchicoli commented Nov 4, 2021

Hi @conloos

I've seen the open MR 3480, but like I mentioned I've took a different approach. If the machine "type" is given, then it uses /containers or /virtual-machines.
It is also possible to change the default API endpoint to /1.0/instances, at the end it will work in the same way, but I guess it would be easier to understand the API calls, if someone is following the LXD documentation

FYI: this new API exists since LXD v3.19.

I wouldn't rename or symlink to lxd, because is too generic. For example, if we would create a different module for managing LXC Volumes, then I would expect another module named lxc_volume, just like lxd_profile. For that reason my suggestion are:

  1. to keep the module name as lxd_container, as the LXC CLI stands for Linux Containers
  2. to rename to lxd_instance (similar to the new API endpoint), symlink to lxd_container for backward compatibility and add the deprecation warning

What do you think?

Have a good day

It can be set to "/1.0/instances" for creating instances of type container or virtual-machine.
- If the LXD server version is older than 3.19, then this should be set to "/1.0/containers".
type: str
required: false
architecture:
description:
- 'The architecture for the container (for example C(x86_64) or C(i686)).
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've seen in the documentation that there are multiple lines that reference to container and not an instance. I can update this tonight, if this MR is welcome

Update the lxd_container module to enable the new LXD API endpoint,
which supports different types of instances, such as containers and virtual machines.
The type attributes can be set explicitly to create containers or virtual machines.
@ansibullbot ansibullbot added needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR and removed needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR labels Nov 4, 2021
@conloos
Copy link
Contributor

conloos commented Nov 6, 2021

Hi @rchicoli,

I looked at your changes and was wondering if "api_endpoint" is needed at all.
By checking whether the branch "/1.0/instances" exists, it can, from my point of view, be configured automatically so that the user does not have to do a configuration by hand and we minimize the configuration options.

I agree that all documentation have to be updated, like it is done in #3519.

Greetings Frank

@felixfontein
Copy link
Collaborator

I think something like api_endpoint should not really be exposed to users, it should be selected somehow by the module (based on auto-detection and/or user input).

@rchicoli
Copy link
Contributor Author

rchicoli commented Nov 6, 2021

Ok. the changes should always be backwards compatible, imho to give the user the opportunity to switch to a new endpoint would have been the better choice. Let me try to explain.

I see only one problem. If the user deploys a container and for whatever reason, he changes its "type" to virtual-machine and set the "state" to absent, then the container won't be deleted and Ansible will report that there is nothing to be changed.
The reason is, if you query /1.0/instances, LXD responses everything. Now if the type has changed, then the internal API endpoints will be switched to /1.0/virtual-machines, LXD says, nothing found. Ansible is happy, because he thinks the instance does not exist.
I guess you get my point. If the API would be /instances LXD is clever enough to remove the running instance, even though the type wouldn't match

Anyways, I will revert that..

  • remove the /1.0/instances api
  • and reuse only /1.0/containers or /1.0/virtual-machines based on the type provided

@ansibullbot ansibullbot added needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR labels Nov 6, 2021
@ansibullbot ansibullbot removed ci_verified Push fixes to PR branch to re-run CI needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR labels Nov 7, 2021
plugins/modules/cloud/lxd/lxd_container.py Show resolved Hide resolved
plugins/modules/cloud/lxd/lxd_container.py Outdated Show resolved Hide resolved
plugins/modules/cloud/lxd/lxd_container.py Outdated Show resolved Hide resolved
plugins/modules/cloud/lxd/lxd_container.py Outdated Show resolved Hide resolved
changelogs/fragments/3661-lxd_container-add-vm-support.yml Outdated Show resolved Hide resolved
plugins/modules/cloud/lxd/lxd_container.py Show resolved Hide resolved
@rchicoli
Copy link
Contributor Author

hey @felixfontein thanks for the review.
The changes are already done. Please let me know if you find something else, otherwise I would rebase and force push everything.

@felixfontein felixfontein added the check-before-release PR will be looked at again shortly before release and merged if possible. label Nov 12, 2021
Copy link
Collaborator

@felixfontein felixfontein left a comment

Choose a reason for hiding this comment

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

Looks good to me. Just some nit comments. @conloos what do you think?

plugins/modules/cloud/lxd/lxd_container.py Outdated Show resolved Hide resolved
plugins/modules/cloud/lxd/lxd_container.py Outdated Show resolved Hide resolved
plugins/modules/cloud/lxd/lxd_container.py Outdated Show resolved Hide resolved
@russoz
Copy link
Collaborator

russoz commented Nov 13, 2021

Hi, sorry for not participating in the discussion earlier but, isn't a bit misleading to have a module named lxd_container that can manage something that is not a container? I see all the replace("container", "instance") throughout the file, but the module name remains the same. Wouldn't it be more consistent to copy this PR's code to a new lxd_instance and deprecate lxd_container? Just my $0.02.

Otherwise, disregarding that terminology conundrum, it LGTM.

@felixfontein
Copy link
Collaborator

there are several people who are currently working on providing the functionality of the 4.x lxd-api version. See: lxd_virtual_machine - new module #3480. @felix I prefer to have only one plugin. My suggestion is to rename lxd_container to lxd, as far as possible and e.g. add a symbolic link for backward compatibility.

It is to late so i didn't look deep on the changes.

@russoz at the beginning of this PR @conloos wrote the above. I think renaming it is a good idea (I don't care whether lxd or lxd_instance), instead of having suddenly two mostly identical modules to maintain.

@conloos
Copy link
Contributor

conloos commented Nov 13, 2021

Hello @ALL,

from my point of view everything looks fine and the changes don't break anything.

In the next step, the module should be completely reviewed/rewritten.
I checked the swagger documentation and the endpoints "containers" and "virtual-machines" are not listed. So I think there are only for backward compatibility. So in the next release, the module should be check the exported endpoints and use "containers" for compatibility and "instances" if there is support for.

I can make that, but if @rchicoli is interested I'm fine with that.

The second thing is the name of the module. I vote for "renaming to: lxd_instance and set a deprecate message by using lxd_container" as @rchicoli it has already suggested. Maybe a symlink and check which name was used to call up the module is possible?

Thanks Frank

@felixfontein
Copy link
Collaborator

from my point of view everything looks fine and the changes don't break anything.

In that case I will merge once the final nits are fixed.

The second thing is the name of the module. I vote for "renaming to: lxd_instance and set a deprecate message by using lxd_container" as @rchicoli it has already suggested. Maybe a symlink and check which name was used to call up the module is possible?

The symlink will mainly be needed for Ansible 2.9, for ansible-base 2.10 and later an entry in meta/runtime.yml will be needed. I can help with the rename; if someone starts a PR for the rename which renames the module (and does all the documentation updates etc. that are necessary) and keeps the checkbox for maintainer pushes enabled, I can push the remaining technical parts to it.

@rchicoli
Copy link
Contributor Author

In the next step, the module should be completely reviewed/rewritten.

I agree.

I checked the swagger documentation and the endpoints "containers" and "virtual-machines" are not listed. So I think there are only for backward compatibility. So in the next release, the module should be check the exported endpoints and use "containers" for compatibility and "instances" if there is support for.

Exactly. I have the feeling they didn't want to duplicate the swagger api to add the "containers" and "virtual-machines" endpoints. So you are wondering here is the API implementation:
https://github.com/lxc/lxd/blob/e6523c33b1c6987f794623a5abbc857b8a32a729/lxd/instances.go#L41-L45

I can make that, but if @rchicoli is interested I'm fine with that.

you can take over this time. At the moment I am kinda busy with other projects.

I want to be a bit precautious before squashing all commits, since it is my first contribution.
So would it be fine to rebase everything now?

If I understood it right, further changes will be done as a nextt step in a separated MR.

I am glad to contribute to the code. Thanks everyone for the support.

@felixfontein felixfontein removed the check-before-release PR will be looked at again shortly before release and merged if possible. label Nov 20, 2021
@felixfontein
Copy link
Collaborator

Merging since there's an implicit 'shipit' by @conloos in #3661 (comment) (sorry, missed that).

@felixfontein felixfontein merged commit 58eb94f into ansible-collections:main Nov 20, 2021
@patchback
Copy link

patchback bot commented Nov 20, 2021

Backport to stable-4: 💚 backport PR created

✅ Backport PR branch: patchback/backports/stable-4/58eb94fff38b7eba8d5116bd4b3c35c9ced08bbe/pr-3661

Backported as #3761

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

patchback bot pushed a commit that referenced this pull request Nov 20, 2021
* lxd_container: support lxd instance types

Update the lxd_container module to enable the new LXD API endpoint,
which supports different types of instances, such as containers and virtual machines.
The type attributes can be set explicitly to create containers or virtual machines.

* lxd_container: rename references from containers to instances

* lxd_container: add an example of creating vms

* lxd_container: update doc

* lxd_container: fix pylint

* resolve converstation

* remove type from config

* remove outdated validation related to the instance api

* correct diff

* changing last bits

* add missing dot

(cherry picked from commit 58eb94f)
@felixfontein
Copy link
Collaborator

@rchicoli thanks for your contribution!
@conloos @russoz thanks for reviewing!

felixfontein pushed a commit that referenced this pull request Nov 20, 2021
* lxd_container: support lxd instance types

Update the lxd_container module to enable the new LXD API endpoint,
which supports different types of instances, such as containers and virtual machines.
The type attributes can be set explicitly to create containers or virtual machines.

* lxd_container: rename references from containers to instances

* lxd_container: add an example of creating vms

* lxd_container: update doc

* lxd_container: fix pylint

* resolve converstation

* remove type from config

* remove outdated validation related to the instance api

* correct diff

* changing last bits

* add missing dot

(cherry picked from commit 58eb94f)

Co-authored-by: rchicoli <rchicoli@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cloud feature This issue/PR relates to a feature request module module new_contributor Help guide this first time contributor plugins plugin (any type)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants