Skip to content

Commit

Permalink
chore: refactor integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
mnaser committed Jul 4, 2023
1 parent 3e7d502 commit e3a93e9
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 104 deletions.
4 changes: 2 additions & 2 deletions docs/user/labels.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ deployment process.
The version of the Cinder CSI container image to use when bootstrapping the
cluster.

Default value: `v1.25.3`
Default value: Automatically detected based on `kube_tag` label.

### Manila

Expand All @@ -108,7 +108,7 @@ deployment process.
The version of the Manila CSI container image to use when bootstrapping the
cluster.

Default value: `v1.25.3`
Default value: Automatically detected based on `kube_tag` label.

* `manila_csi_share_network_id`

Expand Down
Empty file.
54 changes: 54 additions & 0 deletions magnum_cluster_api/integrations/cinder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright (c) 2023 VEXXHOST, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from magnum import objects
from magnum.common import exception
from oslo_config import cfg

from magnum_cluster_api import clients
from magnum_cluster_api.integrations import common

CONF = cfg.CONF


def is_enabled(cluster: objects.Cluster) -> bool:
return common.is_enabled(cluster, "cinder_csi_enabled", "volumev3")


def get_image(cluster: objects.Cluster) -> str:
return common.get_cloud_provider_image(
cluster, "cinder_csi_plugin_tag", "cinder-csi-plugin"
)


def get_default_boot_volume_type(context):
"""
Get the default boot volume type since the existing function
magnum.common.cinder.get_default_boot_volume_type() returns a random volume
type when CONF.cinder.default_boot_volume_type is not defined.
Instead of using a random volume type, this function uses the default
volume type.
"""

if CONF.cinder.default_boot_volume_type:
return CONF.cinder.default_boot_volume_type

osc = clients.get_openstack_api(context)
default_volume_type = osc.cinder().volume_types.default()

if default_volume_type is None:
raise exception.VolumeTypeNotFound()

return default_volume_type.name
23 changes: 23 additions & 0 deletions magnum_cluster_api/integrations/cloud_provider.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (c) 2023 VEXXHOST, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from magnum import objects

from magnum_cluster_api.integrations import common


def get_image(cluster: objects.Cluster) -> str:
return common.get_cloud_provider_image(
cluster, "cloud_provider_tag", "openstack-cloud-controller-manager"
)
96 changes: 96 additions & 0 deletions magnum_cluster_api/integrations/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Copyright (c) 2023 VEXXHOST, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import semver
from magnum import objects
from magnum.common import context, exception
from magnum.common.keystone import KeystoneClientV3
from oslo_log import log as logging

from magnum_cluster_api import utils

LOG = logging.getLogger(__name__)


def is_enabled(
cluster: objects.Cluster,
label_flag: str,
service_name: str,
) -> bool:
return utils.get_cluster_label_as_bool(
cluster, label_flag, True
) and is_service_enabled(service_name)


def is_service_enabled(service_type: str) -> bool:
"""Check if service is deployed in the cloud."""

admin_context = context.make_admin_context()
keystone = KeystoneClientV3(admin_context)

try:
service = keystone.client.services.list(type=service_type)
except Exception:
LOG.exception("Failed to list services")
raise exception.ServicesListFailed()

if service and service[0].enabled:
return True

LOG.info("There is no %s service enabled in the cloud.", service_type)
return False


def get_cloud_provider_image(
cluster: objects.Cluster, tag_label: str, image_name: str
) -> str:
tag = get_cloud_provider_tag(cluster, tag_label)
version = semver.VersionInfo.parse(tag[1:])

repository = "registry.k8s.io/provider-os"
if version.major == 1 and version.minor < 24:
repository = "docker.io/k8scloudprovider"

return f"{repository}/{image_name}:{tag}"


def get_cloud_provider_tag(cluster: objects.Cluster, label: str) -> str:
tag_label = utils.get_cluster_label(cluster, label, None)
if tag_label:
return tag_label

kube_tag = utils.get_kube_tag(cluster)
version = semver.VersionInfo.parse(kube_tag[1:])

tag = None
if version.major == 1 and version.minor == 23:
tag = "v1.23.4"
elif version.major == 1 and version.minor == 24:
tag = "v1.24.6"
elif version.major == 1 and version.minor == 25:
tag = "v1.25.6"
elif version.major == 1 and version.minor == 26:
tag = "v1.26.3"
elif version.major == 1 and version.minor == 27:
# TODO(mnaser): There is no 1.27 release yet, so we're using
# the latest 1.26 release for now.
tag = "v1.26.3"

if tag is None:
raise ValueError(
f"Unsupported Kubernetes version: {version}. "
"Please specify a supported version in the cluster template."
)

return tag
27 changes: 27 additions & 0 deletions magnum_cluster_api/integrations/manila.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright (c) 2023 VEXXHOST, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from magnum import objects

from magnum_cluster_api.integrations import common


def is_enabled(cluster: objects.Cluster) -> bool:
return common.is_enabled(cluster, "manila_csi_enabled", "sharev2")


def get_image(cluster: objects.Cluster) -> str:
return common.get_cloud_provider_image(
cluster, "manila_csi_plugin_tag", "manila-csi-plugin"
)
50 changes: 11 additions & 39 deletions magnum_cluster_api/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,18 @@
import pykube
import yaml
from magnum import objects as magnum_objects
from magnum.common import cert_manager, cinder, context, neutron
from magnum.common import cert_manager, context, neutron
from magnum.common import utils as magnum_utils
from magnum.common.x509 import operations as x509
from oslo_config import cfg
from oslo_serialization import base64
from oslo_utils import encodeutils

from magnum_cluster_api import clients, helm, image_utils, images, objects, utils
from magnum_cluster_api.integrations import cinder, cloud_provider, manila

CONF = cfg.CONF
CLOUD_PROVIDER_TAG = "v1.25.3"
CALICO_TAG = "v3.24.2"
CINDER_CSI_TAG = "v1.25.3"
MANILA_CSI_TAG = "v1.25.3"

CLUSTER_CLASS_VERSION = pkg_resources.require("magnum_cluster_api")[0].version
CLUSTER_CLASS_NAME = f"magnum-v{CLUSTER_CLASS_VERSION}"
Expand Down Expand Up @@ -166,9 +164,6 @@ def get_object(self) -> pykube.ConfigMap:
"magnum_cluster_api", "manifests"
)
calico_version = utils.get_cluster_label(self.cluster, "calico_tag", CALICO_TAG)
ccm_version = utils.get_cluster_label(
self.cluster, "cloud_provider_tag", CLOUD_PROVIDER_TAG
)

repository = utils.get_cluster_container_infra_prefix(self.cluster)

Expand All @@ -183,7 +178,7 @@ def get_object(self) -> pykube.ConfigMap:
replacements=[
(
"docker.io/k8scloudprovider/openstack-cloud-controller-manager:latest",
f"docker.io/k8scloudprovider/openstack-cloud-controller-manager:{ccm_version}",
cloud_provider.get_image(self.cluster),
),
],
)
Expand All @@ -198,12 +193,9 @@ def get_object(self) -> pykube.ConfigMap:
},
}

if utils.is_cinder_csi_enabled(self.cluster):
if cinder.is_enabled(self.cluster):
volume_types = osc.cinder().volume_types.list()
default_volume_type = osc.cinder().volume_types.default()
csi_version = utils.get_cluster_label(
self.cluster, "cinder_csi_plugin_tag", CINDER_CSI_TAG
)
data = {
**data,
**{
Expand All @@ -214,7 +206,7 @@ def get_object(self) -> pykube.ConfigMap:
replacements=[
(
"docker.io/k8scloudprovider/cinder-csi-plugin:latest",
f"docker.io/k8scloudprovider/cinder-csi-plugin:{csi_version}",
cinder.get_image(self.cluster),
),
],
)
Expand Down Expand Up @@ -249,11 +241,8 @@ def get_object(self) -> pykube.ConfigMap:
},
}

if utils.is_manila_csi_enabled(self.cluster):
if manila.is_enabled(self.cluster):
share_types = osc.manila().share_types.list()
csi_version = utils.get_cluster_label(
self.cluster, "manila_csi_plugin_tag", MANILA_CSI_TAG
)
share_network_id = utils.get_cluster_label(
self.cluster, "manila_csi_share_network_id", None
)
Expand Down Expand Up @@ -293,7 +282,7 @@ def get_object(self) -> pykube.ConfigMap:
replacements=[
(
"registry.k8s.io/provider-os/manila-csi-plugin:latest",
f"registry.k8s.io/provider-os/manila-csi-plugin:{csi_version}",
manila.get_image(self.cluster),
),
],
)
Expand Down Expand Up @@ -475,8 +464,8 @@ def get_object(self) -> pykube.Secret:
"apiVersion": pykube.Secret.version,
"kind": pykube.Secret.kind,
"metadata": {
"name": utils.get_or_generate_cluster_api_cloud_config_secret_name(
self.api, self.cluster
"name": utils.get_cluster_api_cloud_config_secret_name(
self.cluster
),
"namespace": "magnum-system",
"labels": self.labels,
Expand Down Expand Up @@ -1561,28 +1550,11 @@ def __init__(

@property
def labels(self) -> dict:
ccm_version = utils.get_cluster_label(
self.cluster, "cloud_provider_tag", CLOUD_PROVIDER_TAG
)
cni_version = utils.get_cluster_label(self.cluster, "calico_tag", CALICO_TAG)
labels = {
"cni": f"calico-{cni_version}",
"ccm": f"openstack-cloud-controller-manager-{ccm_version}",
}

if utils.is_cinder_csi_enabled(self.cluster):
csi_version = utils.get_cluster_label(
self.cluster, "cinder_csi_plugin_tag", CINDER_CSI_TAG
)
labels["csi"] = "cinder"
labels["cinder-csi-version"] = csi_version

if utils.is_manila_csi_enabled(self.cluster):
manila_version = utils.get_cluster_label(
self.cluster, "manila_csi_plugin_tag", MANILA_CSI_TAG
)
labels["manila-csi-version"] = manila_version

return {**super().labels, **labels}

def get_or_none(self) -> objects.Cluster:
Expand Down Expand Up @@ -1717,8 +1689,8 @@ def get_object(self) -> objects.Cluster:
"name": "clusterIdentityRef",
"value": {
"kind": pykube.Secret.kind,
"name": utils.get_or_generate_cluster_api_cloud_config_secret_name(
self.api, self.cluster
"name": utils.get_cluster_api_cloud_config_secret_name(
self.cluster
),
},
},
Expand Down
2 changes: 1 addition & 1 deletion magnum_cluster_api/tests/unit/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def test_generate_machine_deployments_for_cluster_with_deleting_node_group(
cluster_get_by_uuid.return_value = cluster

mock_get_default_boot_volume_type = mocker.patch(
"magnum.common.cinder.get_default_boot_volume_type"
"magnum_cluster_api.integrations.cinder.get_default_boot_volume_type"
)
mock_get_default_boot_volume_type.return_value = "foo"

Expand Down
Loading

0 comments on commit e3a93e9

Please sign in to comment.