mirror of
https://github.com/kubevirt/kubevirt.core.git
synced 2026-05-07 05:42:38 +00:00
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>
This commit is contained in:
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.
|
||||
type: bool
|
||||
default: True
|
||||
create_groups:
|
||||
description:
|
||||
- Enable the creation of groups from labels on VirtualMachines.
|
||||
type: bool
|
||||
default: False
|
||||
api_version:
|
||||
description:
|
||||
- Specify the used KubeVirt API version.
|
||||
@@ -211,6 +216,7 @@ class GetVmiOptions:
|
||||
network_name: Optional[str] = None
|
||||
kube_secondary_dns: Optional[bool] = None
|
||||
use_service: Optional[bool] = None
|
||||
create_groups: Optional[bool] = None
|
||||
base_domain: Optional[str] = None
|
||||
host_format: Optional[str] = None
|
||||
|
||||
@@ -222,6 +228,8 @@ class GetVmiOptions:
|
||||
self.kube_secondary_dns = False
|
||||
if self.use_service is None:
|
||||
self.use_service = True
|
||||
if self.create_groups is None:
|
||||
self.create_groups = False
|
||||
if self.host_format is None:
|
||||
self.host_format = "{namespace}-{name}"
|
||||
|
||||
@@ -374,6 +382,7 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
||||
connection.get("network_name", connection.get("interface_name")),
|
||||
connection.get("kube_secondary_dns"),
|
||||
connection.get("use_service"),
|
||||
connection.get("create_groups"),
|
||||
connection.get("base_domain", self.get_cluster_domain(client)),
|
||||
self.host_format,
|
||||
)
|
||||
@@ -473,30 +482,33 @@ class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
|
||||
name=vmi.metadata.name,
|
||||
uid=vmi.metadata.uid,
|
||||
)
|
||||
vmi_groups = []
|
||||
vmi_annotations = (
|
||||
{}
|
||||
if not 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:
|
||||
# create a group for each label_value
|
||||
for key, value in vmi.metadata.labels:
|
||||
group_name = f"label_{key}_{value}"
|
||||
group_name = self._sanitize_group_name(group_name)
|
||||
if group_name not in vmi_groups:
|
||||
vmi_groups.append(group_name)
|
||||
self.inventory.add_group(group_name)
|
||||
vmi_labels = self.__resource_field_to_dict(vmi.metadata.labels)
|
||||
else:
|
||||
vmi_labels = {}
|
||||
|
||||
# Add vmi to the namespace group, and to each label_value group
|
||||
# Add vmi to the namespace group
|
||||
self.inventory.add_host(vmi_name)
|
||||
self.inventory.add_child(namespace_group, vmi_name)
|
||||
for group in vmi_groups:
|
||||
self.inventory.add_child(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:
|
||||
group_name = self._sanitize_group_name(f"label_{key}_{value}")
|
||||
if group_name not in vmi_groups:
|
||||
vmi_groups.append(group_name)
|
||||
# Add vmi to each label_value group
|
||||
for group in vmi_groups:
|
||||
self.inventory.add_group(group)
|
||||
self.inventory.add_child(group, vmi_name)
|
||||
|
||||
# Set up the connection
|
||||
self.inventory.set_variable(vmi_name, "ansible_connection", "ssh")
|
||||
|
||||
@@ -2,3 +2,4 @@ plugin: kubevirt.core.kubevirt
|
||||
connections:
|
||||
- namespaces:
|
||||
- default
|
||||
create_groups: yes
|
||||
|
||||
@@ -2,4 +2,5 @@ plugin: kubevirt.core.kubevirt
|
||||
connections:
|
||||
- namespaces:
|
||||
- default
|
||||
create_groups: yes
|
||||
label_selector: app=test
|
||||
|
||||
@@ -2,4 +2,5 @@ plugin: kubevirt.core.kubevirt
|
||||
connections:
|
||||
- namespaces:
|
||||
- default
|
||||
create_groups: yes
|
||||
network_name: bridge-network
|
||||
|
||||
Reference in New Issue
Block a user