Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Retrieve volume types from cinder and use to make storageclasses
Browse files Browse the repository at this point in the history
Query cinder to find volume types and return set of storageclass
dictionaries.

CSI arguments as labels defaulting to config variables
  • Loading branch information
Travis Holton committed Jan 11, 2024
1 parent 6f49dde commit 4c32390
Show file tree
Hide file tree
Showing 3 changed files with 262 additions and 3 deletions.
43 changes: 42 additions & 1 deletion magnum_capi_helm/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,55 @@
),
cfg.StrOpt(
"default_helm_chart_version",
default="0.1.4",
default="0.1.7",
help=(
"Version of the helm chart specified "
"by the config: capi_driver.helm_chart_repo "
"and capi_driver.helm_chart_name. "
"A cluster label can override this."
),
),
cfg.StrOpt(
"csi_cinder_default_volume_type",
help=("Default StorageClass volume type for persistent volumes."),
),
cfg.StrOpt(
"csi_cinder_reclaim_policy",
default="Retain",
help=(
"Policy for reclaiming dynamically created "
"persistent volumes. Can be 'Retain' or 'Delete'."
),
),
cfg.BoolOpt(
"csi_cinder_allow_volume_expansion",
default=True,
help=(
"Allows the users to resize the volume by "
"editing the corresponding PVC object."
),
),
cfg.ListOpt(
"csi_cinder_allowed_topologies",
default=[],
help=(
"Select the Nodes where the application "
"Pods may be scheduled based on Node labels."
),
),
cfg.StrOpt(
"csi_cinder_fstype",
default="ext4",
help=("Filesystem type for persistent volumes."),
),
cfg.StrOpt(
"csi_cinder_volume_binding_mode",
default="Immediate",
help=(
"The volumeBindingMode field controls when "
"volume binding and dynamic provisioning should occur."
),
),
]

CONF = cfg.CONF
Expand Down
83 changes: 82 additions & 1 deletion magnum_capi_helm/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,83 @@ def _get_fixed_network_id(self, context, cluster):
context, network, source="name", target="id", external=False
)

def _get_csi_cinder_reclaim_policy(self, cluster):
return self._label(
cluster,
"csi_cinder_reclaim_policy",
CONF.capi_helm.csi_cinder_reclaim_policy,
)

def _get_csi_cinder_fstype(self, cluster):
return self._label(
cluster,
"csi_cinder_fstype",
CONF.capi_helm.csi_cinder_fstype,
)

def _get_csi_cinder_allow_volume_expansion(self, cluster):
return self._get_label_bool(
cluster,
"csi_cinder_allow_volume_expansion",
CONF.capi_helm.csi_cinder_allow_volume_expansion,
)

def _storageclass_definitions(self, context, cluster):
"""Query cinder API to retrieve list of available volume types.
@return dict(dict,list(dict)) containing storage classes
"""
LOG.debug("Retrieve volume types from cinder for StorageClasses.")
client = clients.OpenStackClients(context)
region_name = client.cinder_region_name()
c_client = client.cinder()
volume_types = [i.name for i in c_client.volume_types.list()]
# Use the default volume type if defined. Otherwise use the first
# type returned by cinder.
default_volume_type = CONF.capi_helm.csi_cinder_default_volume_type
LOG.debug(
f"Default volume type: {default_volume_type}"
f" Volume types: {volume_types}"
)
if not default_volume_type:
default_volume_type = volume_types[0]
LOG.warning(
f"Default volume type not defined."
f" Using {default_volume_type}."
)
elif default_volume_type not in volume_types:
# If default does not exist throw an error.
raise exception.MagnumException(
message=f"{default_volume_type} is not a"
" valid Cinder volume type."
)
default_storage_class = {}
additional_storage_classes = []
allow_expansion = self._get_csi_cinder_allow_volume_expansion(cluster)
reclaim_policy = self._get_csi_cinder_reclaim_policy(cluster)
allowed_topologies = CONF.capi_helm.csi_cinder_allowed_topologies
fstype = self._get_csi_cinder_fstype(cluster)

for volume_type in volume_types:
storage_class = {
"name": volume_type,
"reclaimPolicy": reclaim_policy,
"allowVolumeExpansion": allow_expansion,
"availabilityZone": region_name,
"volumeType": volume_type,
"allowedTopologies": allowed_topologies,
"fstype": fstype,
"enabled": True,
}
if volume_type == default_volume_type:
default_storage_class = storage_class
else:
additional_storage_classes.append(storage_class)
return dict(
defaultStorageClass=default_storage_class,
additionalStorageClasses=additional_storage_classes,
)

def _update_helm_release(self, context, cluster, nodegroups=None):
if nodegroups is None:
nodegroups = cluster.nodegroups
Expand All @@ -559,7 +636,6 @@ def _update_helm_release(self, context, cluster, nodegroups=None):

network_id = self._get_fixed_network_id(context, cluster)
subnet_id = neutron.get_fixed_subnet_id(context, cluster.fixed_subnet)

values = {
"kubernetesVersion": kube_version,
"machineImageId": image_id,
Expand Down Expand Up @@ -610,6 +686,11 @@ def _update_helm_release(self, context, cluster, nodegroups=None):
if ng.role != NODE_GROUP_ROLE_CONTROLLER
],
"addons": {
"openstack": {
"csiCinder": self._storageclass_definitions(
context, cluster
)
},
"monitoring": {
"enabled": self._get_monitoring_enabled(cluster)
},
Expand Down
Loading

0 comments on commit 4c32390

Please sign in to comment.