From c4ee2703c002b4bcef68b11bd84ddf902619316d Mon Sep 17 00:00:00 2001 From: Ivan Chvets Date: Wed, 30 Aug 2023 15:57:08 -0400 Subject: [PATCH] feat: istio-pilot configurable images https://github.com/canonical/istio-operators/issues/322 Summary of changes: - Added unit tests. - Modified config.yaml to make it consistent. - Added get config helper to allow for better testing. This can also be expanded further. --- charms/istio-pilot/config.yaml | 22 ++++++------ charms/istio-pilot/src/charm.py | 37 ++++++++++++--------- charms/istio-pilot/tests/unit/test_charm.py | 9 +++++ 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/charms/istio-pilot/config.yaml b/charms/istio-pilot/config.yaml index 9a0c0380..2e1c2388 100644 --- a/charms/istio-pilot/config.yaml +++ b/charms/istio-pilot/config.yaml @@ -1,20 +1,20 @@ options: - image_configuration: + default-gateway: + type: string + default: istio-gateway + description: Name to use as a default gateway + image-configuration: default: | - pilot_image: 'pilot' # values.pilot.image - global_tag: '1.17.3' # values.global.tag - global_hub: 'docker.io/istio' # values.global.hub - global_proxy_image: 'proxyv2' # values.global.proxy.image - global_proxy_init_image: 'proxyv2' # values.global.proxy_init.image - grpc_bootstrap_init: 'busybox:1.28' + pilot-image: 'pilot' # values.pilot.image + global-tag: '1.17.3' # values.global.tag + global-hub: 'docker.io/istio' # values.global.hub + global-proxy-image: 'proxyv2' # values.global.proxy.image + global-proxy-init-image: 'proxyv2' # values.global.proxy_init.image + grpc-bootstrap-init: 'busybox:1.28' description: > YAML or JSON formatted input defining image configuration to use when installing the Istio control plane. For reference https://istio.io/v1.5/docs/reference/config/installation-options/ type: string - default-gateway: - type: string - default: istio-gateway - description: Name to use as a default gateway gateway-service-name: type: string default: istio-ingressgateway-workload diff --git a/charms/istio-pilot/src/charm.py b/charms/istio-pilot/src/charm.py index c4d7f82f..84d0d863 100755 --- a/charms/istio-pilot/src/charm.py +++ b/charms/istio-pilot/src/charm.py @@ -2,11 +2,10 @@ import logging import subprocess -import yaml -from pathlib import Path from typing import List, Optional import tenacity +import yaml from charmed_kubeflow_chisme.exceptions import ErrorWithStatus, GenericCharmRuntimeError from charmed_kubeflow_chisme.kubernetes import ( KubernetesResourceHandler, @@ -19,7 +18,6 @@ ) from charms.istio_pilot.v0.istio_gateway_info import GatewayProvider from charms.prometheus_k8s.v0.prometheus_scrape import MetricsEndpointProvider -from jinja2 import Template from lightkube import Client from lightkube.core.exceptions import ApiError from lightkube.generic_resource import create_namespaced_resource @@ -56,7 +54,7 @@ } GATEWAY_TEMPLATE_FILES = ["src/manifests/gateway.yaml.j2"] DEFAULT_IMAGES = {} -IMAGE_CONFIGURATION = "image_configuration" +IMAGE_CONFIGURATION = "image-configuration" KRH_GATEWAY_SCOPE = "gateway" METRICS_PORT = 15014 INGRESS_AUTH_RELATION_NAME = "ingress-auth" @@ -160,18 +158,24 @@ def __init__(self, *args): ) self.grafana_dashboards = GrafanaDashboardProvider(self, relation_name="grafana-dashboard") + def _get_config(self): + """Retrieve and return configuration.""" + config = {} + config[IMAGE_CONFIGURATION] = yaml.safe_load(self.model.config[IMAGE_CONFIGURATION]) + return config + def install(self, _): """Install charm.""" self._log_and_set_status(MaintenanceStatus("Deploying Istio control plane")) - image_configuration = yaml.safe_load(self.model.config[IMAGE_CONFIGURATION]) - pilot_image = image_configuration["pilot_image"] - global_tag = image_configuration["global_tag"] - global_hub= image_configuration["global_hub"] - global_proxy_image = image_configuration["global_proxy_image"] - global_proxy_init_image = image_configuration["global_proxy_init_image"] + config = self._get_config() + pilot_image = config[IMAGE_CONFIGURATION]["pilot-image"] + global_tag = config[IMAGE_CONFIGURATION]["global-tag"] + global_hub = config[IMAGE_CONFIGURATION]["global-hub"] + global_proxy_image = config[IMAGE_CONFIGURATION]["global-proxy-image"] + global_proxy_init_image = config[IMAGE_CONFIGURATION]["global-proxy-init-image"] - # Call istioctl install and set paramters based on image configuratiob + # Call istioctl install and set parameters based on image configuration subprocess.check_call( [ "./istioctl", @@ -182,15 +186,15 @@ def install(self, _): "--set", "values.global.istioNamespace=kubeflow", "--set", - f"values.pilot.image='{pilot_image}'", + f"values.pilot.image={pilot_image}", "--set", - f"values.global.tag='{global_tag}'", + f"values.global.tag={global_tag}", "--set", - f"values.global.hub='{global_hub}'", + f"values.global.hub={global_hub}", "--set", - f"values.global.proxy.image='{global_proxy_image}'", + f"values.global.proxy.image={global_proxy_image}", "--set", - f"values.global.proxy_init.image='{global_proxy_init_image}'", + f"values.global.proxy_init.image={global_proxy_init_image}", ] ) @@ -838,6 +842,7 @@ def _remove_envoyfilter(name: str, namespace: str, logger: Optional[logging.Logg return raise e + def _validate_upgrade_version(versions) -> bool: """Validates that the version of istioctl can upgrade the currently deployed Istio. diff --git a/charms/istio-pilot/tests/unit/test_charm.py b/charms/istio-pilot/tests/unit/test_charm.py index 2d45da37..b3f5e10b 100644 --- a/charms/istio-pilot/tests/unit/test_charm.py +++ b/charms/istio-pilot/tests/unit/test_charm.py @@ -1039,6 +1039,15 @@ def test_xor(self, left, right, expected): """Test that the xor helper function works as expected.""" assert _xor(left, right) is expected + def test_get_config(self, harness): + """Test configuration retrieval function.""" + harness.begin() + config = harness.charm._get_config() + assert "image-configuration" in config.keys() + assert "pilot-image" in config["image-configuration"].keys() + assert "pilot" == config["image-configuration"]["pilot-image"] + assert "proxyv2" == config["image-configuration"]["global-proxy-image"] + class TestCharmUpgrade: """Tests for charm upgrade handling."""