56 Commits

Author SHA1 Message Date
Felix Matouschek
4a8b4ead2f feat(inventory): Use OCP projects if available
If no namespaces were specified in the inventory config try to get all
available namespaces by trying to list OCP projects first. If the resource
was not found (no OCP cluster) fall back to regular namespaces.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2025-04-28 15:45:38 +02:00
Felix Matouschek
93473cdd47 fix: Ensure compatibility with ansible-core >= 2.19
ansible-core 2.19 changes the way templates are trusted and provides a
new way of patching module args in unit tests.

With this commit the following changes are made to ensure compatibility
with ansible-core >= 2.19:

- Mark inputs to composable as trusted to align with the new template
  trust model.
- Utilize the updated method for patching module arguments in unit tests
  if available.
- Replace direct access to the self._cache attribute with the inventory's
  cache property.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2025-04-23 11:25:51 +02:00
Felix Matouschek
da8e04243b feat(inventory): Add unset_ansible_port option
This adds the unset_ansible_port option to the inventory, which allows to
control if the value of ansible_port should be unset if no non-default
value was found.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2025-04-08 16:38:50 +02:00
Felix Matouschek
17ddb44b8a feat(inventory): Lookup winrm services for Windows hosts
This feature adds looking up winrm services and tries to populate the
ansible_host and ansible_port variables with the values from a
found service for the host. It looks up both winrm HTTP and HTTPS
services and prefers HTTPS if it found both for a given host.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2025-04-03 15:24:34 +02:00
Felix Matouschek
7495c34766 cleanup,doc(inventory): Add missing docstring
Add missing docstring for the _connections_compatibility method.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-07-12 11:09:23 +02:00
Felix Matouschek
9029574f7c cleanup(inventory): Mark methods as private
Mark all methods of the inventory which should be private with a underscore
prefix in their name.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-07-12 11:09:23 +02:00
Felix Matouschek
060ac97b68 fix(inventory): Fix inventory source caching
Fix inventory source caching by separating the fetching of objects and
populating the inventory. This way objects can be fetched from the K8S
API or from a configured cached and the cache related parameters on the
plugin now actually work.

The inventory source cache was tested with the ansible.builtin.jsonfile
cache plugin and 100k hosts (~2G JSON file). Though it took a noticeable
amount of time for the inventory plugin to run it worked fine and no
failures could be observed.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-07-12 10:32:24 +02:00
Felix Matouschek
a228fe2c26 cleanup(inventory): Rename default hostname method
Rename method get_default_host_name to get_default_hostname.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-07-11 15:28:32 +02:00
Felix Matouschek
c4570b2089 cleanup(inventory): Use dicts where possible
Use dicts instead of ResourceFields where possible to allow
easier serialization/deserialization of objects fetched from the K8S
API.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-07-11 12:07:09 +02:00
Felix Matouschek
033f373c94 fix(inventory): Align versions for connections compatibility
Align the version in which backwards compatibility for the connections
parameter will be removed.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-07-11 12:07:09 +02:00
Felix Matouschek
fd9c30103d cleanup(inventory): Drop support for multiple connections
The support for connections to multiple clusters in the inventory
plugin is dropped to better align with user expectations and how other
inventories work. If inventories of multiple clusters are needed the
inventory can be run multiple times with different configurations.
This also helps to clean up the code and make it simpler.

For now this adds a compatibility helper so that configurations with a
single connection entry remain supported and a warning is emitted.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-07-10 15:32:40 +02:00
Felix Matouschek
463142c280 chore: Prepare release 1.5.0
Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-07-10 14:21:34 +02:00
Felix Matouschek
ff6a067c84 feat(inventory): Deprecate connections parameter
Deprecate the connections parameter of the inventory plugin. For now
this adds only a warning message while keeping the functionality.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-07-10 13:45:11 +02:00
Felix Matouschek
a94eda613f feat(inventory): Support stopped VMs
This enables the inventory to list stopped VMs by refactoring
the add_from_namespace method. The method is now looking up the related
VM and VMIs for a host and adds all status attributes of both objects
to the host's vars. To distinguish the origin of vars an appropriate
prefix (vm_ or vmi_) is applied to their name. The inventory now supports
hosts comprising just a VM (stopped), just a VMI or both.

This changes some of the hostvars previously reported by the inventory
plugin:

Dropped:
- object_type
- cluster_name

Now prefixed with vm_ or vmi_ according to the origin:
- labels
- annotations
- resource_version
- uid

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-07-03 16:04:47 +02:00
Felix Matouschek
8ae9a395e9 cleanup(inventory): Consolidate fetching resources with K8SClient
This consolidates fetching of resources with K8SClient by introducing
the get_resources method, which is can be called by getters for certain
kinds of objects.

The former get_vmis_for_namespace method, which contains the main logic
of the inventory is renamed to populate_inventory_from_namespace.

This refactors the following getters:

- get_available_namespaces
- get_ssh_services_for_namespace

This introduces the following getters:

- get_vms_for_namespace (to be used in the following commit)
- get_vmis_for_namespace

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-07-01 11:56:04 +02:00
Felix Matouschek
01a0e535e2 cleanup(inventory): Move set_composable_vars to bottom of file
This suits the control flow a bit better and helps refactoring the
inventory plugin.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-07-01 11:56:04 +02:00
Felix Matouschek
63a03d75b2 cleanup(inventory): Rename GetVmiOptions to InventoryOptions
... and ensure base_domain is always initialized with the cluster
domain. This is done in preparation for a major refactoring of the
inventory plugin.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-07-01 11:56:04 +02:00
Felix Matouschek
241ca75b28 cleanup(inventory): Make methods more robust
Make the logic in the following methods more robust and add type hints
where appropriate.

- get_host_from_service
- get_port_from_service
- is_windows
- setup
- fetch_objects
- set_ansible_host_and_port
- set_composable_vars

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-07-01 11:55:50 +02:00
Felix Matouschek
83bcffedd7 cleanup(inventory): Drop unneeded connection_plugin and transport
These two variables seem to be unused and removing them did not have any
effect.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-07-01 11:54:43 +02:00
Felix Matouschek
4b70e0471f cleanup(inventory): Apply pylint suggestions
Make pylint happy by applying some simple suggestions.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-07-01 11:54:43 +02:00
Felix Matouschek
5d31db3ea2 chore: Change license Apache 2.0
Change the license of the collection to Apache 2.0 to be compliant with
the CNCF licensing requirements.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-06-11 11:39:32 +02:00
Felix Matouschek
a9c6296794 fix: Ensure compatibility with kubernetes.core >=3.10,<4.1.0
Ensure compatibility with kubernetes.core >=3.10,<4.1.0 by replacing
deprecated imports of HAS_K8S_MODULE_HELPER and k8s_import_exception.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-05-29 10:11:05 +02:00
kubevirt-bot
131aacec43 Merge pull request #95 from 0xFelix/improved-docs
Provide links for docsite and improve docs by adding markup
2024-05-17 11:26:25 +02:00
Javier Cano Cano
a1b0971b7f fix(kubevirt): fix inventory labels loop
Fix the for loop ranging over vmi labels. This loop is missing the
.items() statement, which it causes the following error:

```
for key, value in vmi.metadata.labels:
ValueError: too many values to unpack (expected 2)
```

Signed-off-by: Javier Cano Cano <jcanocan@redhat.com>
2024-05-15 16:23:45 +02:00
Javier Cano Cano
48b2a81679 fix(kubevirt): comment typo
Fix a small comment typo showing `LoadBalancer` instead of `NodePort`.

Signed-off-by: Javier Cano Cano <jcanocan@redhat.com>
2024-05-15 16:12:19 +02:00
Felix Matouschek
4851e5d20d docs: Improve docs by adding markup
Improve the generated documentations by adding Ansible documentation markup
where appropriate.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-05-08 11:06:57 +02:00
Felix Matouschek
b232464dbe docs: Use proper type for connections parameter
The connections parameter is of type list of dicts.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-04-24 13:47:55 +02:00
Felix Matouschek
a3abcbedd4 feat: Set ansible_connection to winrm for Windows hosts
This changes the inventory plugin so that it sets the ansible_connection
to winrm if it detected a Windows host. If it did not detect a Windows
host the ansible_connection is no longer set, so Ansible falls back to
its default value of ssh. The detection of SSH services for hosts using
winrm is disabled.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-04-16 16:38:54 +02:00
Felix Matouschek
7c5de4adf9 cleanup: Run make format
Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-04-16 16:38:54 +02:00
kubevirt-bot
b392d8077d Merge pull request #77 from l3acon/add-composite-vars
fix: add composite vars to inventory
2024-04-09 17:23:56 +02:00
matt
c058de50cd fix: add composite vars to inventory
Docs seem to indicate compose variables should work, but they aren't actually set.
Fix that by adding the composite variables to the inventory.

Signed-off-by: matt <l3acon@gmail.com>
2024-04-09 08:53:51 -06:00
Felix Matouschek
7dc7312a8b feat: Give secondary interfaces a higher priority over services
To give secondary interfaces a higher priority over services the use of
services is disabled if a network_name was provided.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-04-05 14:35:44 +02:00
kubevirt-bot
fd9fa2f3a3 Merge pull request #73 from 0xFelix/avoid-empty-groups
fix: Return early to avoid adding empty groups.
2024-04-05 10:00:40 +02:00
Felix Matouschek
3560818167 fix: Return early to avoid adding empty groups.
If no VMIs were found in a namespace then return early to avoid adding
empty groups.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-04-05 09:44:38 +02:00
Felix Matouschek
ae17f5cc57 feat: Add append_base_domain option to connections
This option allows to append the base domain of a cluster to host names
constructed from SSH services of type NodePort. Also add the missing
documentation for option base_domain.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-04-03 16:34:07 +02:00
Felix Matouschek
9f33a3059c fix: Explicity set ansible_port
Explicitly set ansible_port so it is reset in AWX inventories if the
value changes from set to unset.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-04-03 15:39:42 +02:00
Felix Matouschek
2a6255233e docs: Update inventory matches on string
Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-03-11 15:14:12 +01:00
Felix Matouschek
48734484f9 chore: Bump dependency on kubernetes.core
Bump the dependency on kubernetes.core to versions >=3.0.1 and remove
the workaround introduced by 4429ac8c03.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-03-04 16:01:26 +01:00
kubevirt-bot
c9a7b6a818 Merge pull request #43 from 0xFelix/update-requirements
cleanup: Update requirements throughout the project
2024-02-29 15:56:57 +01:00
kubevirt-bot
dc8d38433b Merge pull request #42 from 0xFelix/update-readme
chore: Update README.md
2024-02-29 15:54:57 +01:00
Felix Matouschek
44e1c0a719 cleanup: Update requirements throughout the project
Update the requirements throughout the project to be in sync and to
reflect the current state of dependencies.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-02-29 14:03:19 +01:00
Felix Matouschek
e0cde14f30 cleanup: Remove __resource_field_to_dict workaround from inventory
Since the minimum required version of the kubernetes python client was
updated to a version not requiring this workaround anymore, the
workaround is replaced with the to_dict() method found in the new
client.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-02-29 13:56:33 +01:00
Felix Matouschek
65f7c27487 chore: Update README.md
Update README.md to include all modules of the collection and keep
module descriptions in sync.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-02-29 13:32:37 +01:00
kubevirt-bot
4429ac8c03 Merge pull request #39 from 0xFelix/bump-version-e2e
Bump e2e-setup.sh version and ensure compatibility with KubeVirt >=1.1.0
2024-02-29 13:08:57 +01:00
Felix Matouschek
77ece9d466 ci: Add yamllint to CI linter job
This adds yamllint to the CI linter job and sets the maximum line
length to 140 like in kubevirt/kubevirt.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-02-29 11:41:49 +01:00
Felix Matouschek
44fce1cc31 fix: Ensure compatibility with KubeVirt >=1.1.0
Ensure compatibility with KubeVirt >=1.1.0 by monkey patching
kubernetes.core to handle apis of the format a/b/c and requiring
kubernetes>=28.1.0.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-02-29 11:21:17 +01:00
Felix Matouschek
6ee5a9c7c9 ansible-lint: Groom the .ansible-lint file
- Enable production profile
- Remove excludes no longer necessary
- Try to not skip any tests

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2024-02-26 16:31:56 +01:00
Guido Grazioli
8f8e7eda6e update sanity/unit tests matrix
Signed-off-by: Guido Grazioli <ggraziol@redhat.com>
2023-11-14 11:58:06 +01:00
Guido Grazioli
91e2a27987 update minimum ansible-core to 2.14+
linter

Signed-off-by: Guido Grazioli <ggraziol@redhat.com>
2023-11-14 11:42:08 +01:00
Felix Matouschek
e33b479326 inventory: Add create_groups option
This adds the create_groups option to the inventory, which allows to
control the creation of groups from labels on VirtualMachines. By
default it is disabled.

Signed-off-by: Felix Matouschek <fmatouschek@redhat.com>
2023-09-05 15:56:46 +02:00