mirror of
https://github.com/kubevirt/kubevirt.core.git
synced 2026-07-28 02:14:35 +00:00
Merge pull request #23 from 0xFelix/flatten-groups
Flatten groups and allow to control creation of additional groups
This commit is contained in:
2
changelogs/fragments/0_flatten_groups.yml
Normal file
2
changelogs/fragments/0_flatten_groups.yml
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
minor_changes:
|
||||||
|
- "inventory: Drop creation of the namespace_vmis_group as it is redundant"
|
||||||
2
changelogs/fragments/1_add_create_groups.yml
Normal file
2
changelogs/fragments/1_add_create_groups.yml
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
minor_changes:
|
||||||
|
- "inventory: Allow to control creation of additional groups"
|
||||||
@@ -112,6 +112,11 @@ options:
|
|||||||
- Enable the use of services to establish an SSH connection to the VirtualMachine.
|
- Enable the use of services to establish an SSH connection to the VirtualMachine.
|
||||||
type: bool
|
type: bool
|
||||||
default: True
|
default: True
|
||||||
|
create_groups:
|
||||||
|
description:
|
||||||
|
- Enable the creation of groups from labels on VirtualMachines.
|
||||||
|
type: bool
|
||||||
|
default: False
|
||||||
api_version:
|
api_version:
|
||||||
description:
|
description:
|
||||||
- Specify the used KubeVirt API version.
|
- Specify the used KubeVirt API version.
|
||||||
@@ -211,6 +216,7 @@ class GetVmiOptions:
|
|||||||
network_name: Optional[str] = None
|
network_name: Optional[str] = None
|
||||||
kube_secondary_dns: Optional[bool] = None
|
kube_secondary_dns: Optional[bool] = None
|
||||||
use_service: Optional[bool] = None
|
use_service: Optional[bool] = None
|
||||||
|
create_groups: Optional[bool] = None
|
||||||
base_domain: Optional[str] = None
|
base_domain: Optional[str] = None
|
||||||
host_format: Optional[str] = None
|
host_format: Optional[str] = None
|
||||||
|
|
||||||
@@ -222,6 +228,8 @@ class GetVmiOptions:
|
|||||||
self.kube_secondary_dns = False
|
self.kube_secondary_dns = False
|
||||||
if self.use_service is None:
|
if self.use_service is None:
|
||||||
self.use_service = True
|
self.use_service = True
|
||||||
|
if self.create_groups is None:
|
||||||
|
self.create_groups = False
|
||||||
if self.host_format is None:
|
if self.host_format is None:
|
||||||
self.host_format = "{namespace}-{name}"
|
self.host_format = "{namespace}-{name}"
|
||||||
|
|
||||||
@@ -374,6 +382,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
|||||||
connection.get("network_name", connection.get("interface_name")),
|
connection.get("network_name", connection.get("interface_name")),
|
||||||
connection.get("kube_secondary_dns"),
|
connection.get("kube_secondary_dns"),
|
||||||
connection.get("use_service"),
|
connection.get("use_service"),
|
||||||
|
connection.get("create_groups"),
|
||||||
connection.get("base_domain", self.get_cluster_domain(client)),
|
connection.get("base_domain", self.get_cluster_domain(client)),
|
||||||
self.host_format,
|
self.host_format,
|
||||||
)
|
)
|
||||||
@@ -444,18 +453,12 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
|||||||
|
|
||||||
services = self.get_ssh_services_for_namespace(client, namespace)
|
services = self.get_ssh_services_for_namespace(client, namespace)
|
||||||
|
|
||||||
namespace_group = f"namespace_{namespace}"
|
|
||||||
namespace_vmis_group = f"{namespace_group}_vmis"
|
|
||||||
|
|
||||||
name = self._sanitize_group_name(name)
|
name = self._sanitize_group_name(name)
|
||||||
namespace_group = self._sanitize_group_name(namespace_group)
|
namespace_group = self._sanitize_group_name(f"namespace_{namespace}")
|
||||||
namespace_vmis_group = self._sanitize_group_name(namespace_vmis_group)
|
|
||||||
|
|
||||||
self.inventory.add_group(name)
|
self.inventory.add_group(name)
|
||||||
self.inventory.add_group(namespace_group)
|
self.inventory.add_group(namespace_group)
|
||||||
self.inventory.add_child(name, namespace_group)
|
self.inventory.add_child(name, namespace_group)
|
||||||
self.inventory.add_group(namespace_vmis_group)
|
|
||||||
self.inventory.add_child(namespace_group, namespace_vmis_group)
|
|
||||||
|
|
||||||
for vmi in vmi_list.items:
|
for vmi in vmi_list.items:
|
||||||
if not (vmi.status and vmi.status.interfaces):
|
if not (vmi.status and vmi.status.interfaces):
|
||||||
@@ -479,30 +482,33 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
|||||||
name=vmi.metadata.name,
|
name=vmi.metadata.name,
|
||||||
uid=vmi.metadata.uid,
|
uid=vmi.metadata.uid,
|
||||||
)
|
)
|
||||||
vmi_groups = []
|
|
||||||
vmi_annotations = (
|
vmi_annotations = (
|
||||||
{}
|
{}
|
||||||
if not vmi.metadata.annotations
|
if not vmi.metadata.annotations
|
||||||
else self.__resource_field_to_dict(vmi.metadata.annotations)
|
else self.__resource_field_to_dict(vmi.metadata.annotations)
|
||||||
)
|
)
|
||||||
|
vmi_labels = (
|
||||||
|
{}
|
||||||
|
if not vmi.metadata.labels
|
||||||
|
else self.__resource_field_to_dict(vmi.metadata.labels)
|
||||||
|
)
|
||||||
|
|
||||||
if vmi.metadata.labels:
|
# Add vmi to the namespace group
|
||||||
# create a group for each label_value
|
self.inventory.add_host(vmi_name)
|
||||||
|
self.inventory.add_child(namespace_group, vmi_name)
|
||||||
|
|
||||||
|
# Create label groups and add vmi to it if enabled
|
||||||
|
if vmi.metadata.labels and opts.create_groups:
|
||||||
|
# Create a group for each label_value
|
||||||
|
vmi_groups = []
|
||||||
for key, value in vmi.metadata.labels:
|
for key, value in vmi.metadata.labels:
|
||||||
group_name = f"label_{key}_{value}"
|
group_name = self._sanitize_group_name(f"label_{key}_{value}")
|
||||||
group_name = self._sanitize_group_name(group_name)
|
|
||||||
if group_name not in vmi_groups:
|
if group_name not in vmi_groups:
|
||||||
vmi_groups.append(group_name)
|
vmi_groups.append(group_name)
|
||||||
self.inventory.add_group(group_name)
|
# Add vmi to each label_value group
|
||||||
vmi_labels = self.__resource_field_to_dict(vmi.metadata.labels)
|
for group in vmi_groups:
|
||||||
else:
|
self.inventory.add_group(group)
|
||||||
vmi_labels = {}
|
self.inventory.add_child(group, vmi_name)
|
||||||
|
|
||||||
# Add vmi to the namespace group, and to each label_value group
|
|
||||||
self.inventory.add_host(vmi_name)
|
|
||||||
self.inventory.add_child(namespace_vmis_group, vmi_name)
|
|
||||||
for group in vmi_groups:
|
|
||||||
self.inventory.add_child(group, vmi_name)
|
|
||||||
|
|
||||||
# Set up the connection
|
# Set up the connection
|
||||||
self.inventory.set_variable(vmi_name, "ansible_connection", "ssh")
|
self.inventory.set_variable(vmi_name, "ansible_connection", "ssh")
|
||||||
|
|||||||
@@ -2,3 +2,4 @@ plugin: kubevirt.core.kubevirt
|
|||||||
connections:
|
connections:
|
||||||
- namespaces:
|
- namespaces:
|
||||||
- default
|
- default
|
||||||
|
create_groups: yes
|
||||||
|
|||||||
@@ -2,4 +2,5 @@ plugin: kubevirt.core.kubevirt
|
|||||||
connections:
|
connections:
|
||||||
- namespaces:
|
- namespaces:
|
||||||
- default
|
- default
|
||||||
|
create_groups: yes
|
||||||
label_selector: app=test
|
label_selector: app=test
|
||||||
|
|||||||
@@ -2,4 +2,5 @@ plugin: kubevirt.core.kubevirt
|
|||||||
connections:
|
connections:
|
||||||
- namespaces:
|
- namespaces:
|
||||||
- default
|
- default
|
||||||
|
create_groups: yes
|
||||||
network_name: bridge-network
|
network_name: bridge-network
|
||||||
|
|||||||
Reference in New Issue
Block a user