Skip to content

Commit b1c31db

Browse files
Refactor
1. Move figuring out of chart dir from helm.py to other places to Operator() and _install_multi_cluster_operator 2. Run precommit
1 parent f134b43 commit b1c31db

File tree

5 files changed

+65
-29
lines changed

5 files changed

+65
-29
lines changed

docker/mongodb-kubernetes-tests/kubetester/helm.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
logger = test_logger.get_test_logger(__name__)
1212

13+
1314
def helm_template(
1415
helm_args: Dict,
1516
helm_chart_path: Optional[str] = "helm_chart",
@@ -143,6 +144,7 @@ def process_run_and_check(args, **kwargs):
143144
logger.error(f"output: {exc.output}")
144145
raise
145146

147+
146148
def oci_helm_registry_login(helm_registry: str, region: str):
147149
logger.info(f"Attempting to log into ECR registry: {helm_registry}, using helm registry login.")
148150

@@ -191,6 +193,7 @@ def oci_helm_registry_login(helm_registry: str, region: str):
191193
except Exception as e:
192194
raise Exception(f"An unexpected error occurred: {e}.")
193195

196+
194197
def helm_upgrade(
195198
name: str,
196199
namespace: str,
@@ -208,18 +211,8 @@ def helm_upgrade(
208211
chart_dir = helm_chart_path if helm_override_path else _helm_chart_dir(helm_chart_path)
209212

210213
if apply_crds_first:
211-
# right now tests image has the entire helm_chart directory, maybe we should just copy the CRDs
212214
apply_crds_from_chart(chart_dir)
213215

214-
# login to helm registry because we are going to install published helm chart
215-
try:
216-
registry, repository, region = oci_chart_info()
217-
218-
oci_helm_registry_login(registry, region)
219-
except Exception as e:
220-
raise Exception(f"Failed logging in to the helm registry {registry}. Error: {e}")
221-
222-
chart_uri = f"oci://{registry}/{repository}"
223216
command_args = _create_helm_args(helm_args, helm_options)
224217
args = [
225218
"helm",
@@ -229,20 +222,16 @@ def helm_upgrade(
229222
*command_args,
230223
name,
231224
]
232-
225+
233226
if custom_operator_version:
234227
args.append(f"--version={custom_operator_version}")
235-
else:
236-
published_chart_version = os.environ.get("OPERATOR_VERSION")
237-
if not published_chart_version:
238-
logger.info("OPERATOR_VERSION env var is not set")
239-
args.append(f"--version=0.0.0+{published_chart_version}")
240228

241-
args.append(chart_uri)
229+
args.append(chart_dir)
242230

243231
command = " ".join(args)
244232
process_run_and_check(command, check=True, capture_output=True, shell=True)
245233

234+
246235
def oci_chart_info():
247236
registry = os.environ.get("OCI_HELM_REGISTRY")
248237
repository = os.environ.get("OCI_HELM_REPOSITORY")
@@ -252,6 +241,7 @@ def oci_chart_info():
252241

253242
return registry, f"{repository}/mongodb-kubernetes", region
254243

244+
255245
def apply_crds_from_chart(chart_dir: str):
256246
crd_files = glob.glob(os.path.join(chart_dir, "crds", "*.yaml"))
257247

docker/mongodb-kubernetes-tests/kubetester/operator.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import logging
4+
import os
45
import time
56
from typing import Dict, List, Optional
67

@@ -16,6 +17,8 @@
1617
helm_template,
1718
helm_uninstall,
1819
helm_upgrade,
20+
oci_chart_info,
21+
oci_helm_registry_login,
1922
)
2023
from tests import test_logger
2124

@@ -43,13 +46,27 @@ def __init__(
4346
namespace: str,
4447
helm_args: Optional[Dict] = None,
4548
helm_options: Optional[List[str]] = None,
46-
helm_chart_path: Optional[str] = "helm_chart",
49+
helm_chart_path: Optional[str] = None,
4750
name: Optional[str] = "mongodb-kubernetes-operator",
4851
api_client: Optional[client.api_client.ApiClient] = None,
52+
operator_version: Optional[str] = None,
4953
):
54+
if not helm_chart_path:
55+
# login to the OCI container registry
56+
registry, repository, region = oci_chart_info()
57+
try:
58+
oci_helm_registry_login(registry, region)
59+
except Exception as e:
60+
raise e
5061

51-
# The Operator will be installed from the following repo, so adding it first
52-
helm_repo_add("mongodb", "https://mongodb.github.io/helm-charts")
62+
# figure out the registry URI, based on dev/staging scenario
63+
chart_uri = f"oci://{registry}/{repository}"
64+
helm_chart_path = chart_uri
65+
66+
if not operator_version:
67+
# most probably we are trying to install current operator which will be installed
68+
# from OCI registry. The version (dev/staging) is set in `OPERATOR_VERSION`
69+
operator_version = os.environ.get("OPERATOR_VERSION")
5370

5471
if helm_args is None:
5572
helm_args = {}
@@ -69,6 +86,7 @@ def __init__(
6986
self.helm_chart_path = helm_chart_path
7087
self.name = name
7188
self.api_client = api_client
89+
self.operator_version = operator_version
7290

7391
def install_from_template(self):
7492
"""Uses helm to generate yaml specification and then uses python K8s client to apply them to the cluster
@@ -82,6 +100,9 @@ def install_from_template(self):
82100

83101
def install(self, custom_operator_version: Optional[str] = None) -> Operator:
84102
"""Installs the Operator to Kubernetes cluster using 'helm install', waits until it's running"""
103+
if not custom_operator_version:
104+
custom_operator_version = self.operator_version
105+
85106
helm_install(
86107
self.name,
87108
self.namespace,
@@ -99,6 +120,9 @@ def upgrade(
99120
self, multi_cluster: bool = False, custom_operator_version: Optional[str] = None, apply_crds_first: bool = False
100121
) -> Operator:
101122
"""Upgrades the Operator in Kubernetes cluster using 'helm upgrade', waits until it's running"""
123+
if not custom_operator_version:
124+
custom_operator_version = self.operator_version
125+
102126
helm_upgrade(
103127
self.name,
104128
self.namespace,

docker/mongodb-kubernetes-tests/tests/conftest.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@
2020
update_configmap,
2121
)
2222
from kubetester.awss3client import AwsS3Client
23-
from kubetester.helm import helm_install_from_chart, helm_repo_add
23+
from kubetester.helm import (
24+
helm_install_from_chart,
25+
helm_repo_add,
26+
oci_chart_info,
27+
oci_helm_registry_login,
28+
)
2429
from kubetester.kubetester import KubernetesTester
2530
from kubetester.kubetester import fixture as _fixture
2631
from kubetester.kubetester import running_locally
@@ -827,14 +832,31 @@ def _install_multi_cluster_operator(
827832
helm_opts: dict[str, str],
828833
central_cluster_name: str,
829834
operator_name: Optional[str] = MULTI_CLUSTER_OPERATOR_NAME,
830-
helm_chart_path: Optional[str] = LOCAL_HELM_CHART_DIR,
835+
helm_chart_path: Optional[str] = None,
831836
custom_operator_version: Optional[str] = None,
832837
apply_crds_first: bool = False,
833838
) -> Operator:
834839
multi_cluster_operator_installation_config.update(helm_opts)
835840

836841
# The Operator will be installed from the following repo, so adding it first
837-
helm_repo_add("mongodb", "https://mongodb.github.io/helm-charts")
842+
# helm_repo_add("mongodb", "https://mongodb.github.io/helm-charts")
843+
844+
# login to the OCI container registry
845+
registry, repository, region = oci_chart_info()
846+
try:
847+
oci_helm_registry_login(registry, region)
848+
except Exception as e:
849+
raise e
850+
851+
# figure out the registry URI, based on dev/staging scenario
852+
chart_uri = f"oci://{registry}/{repository}"
853+
if not helm_chart_path:
854+
helm_chart_path = chart_uri
855+
856+
if not custom_operator_version:
857+
# most probably we are trying to install current operator which will be installed
858+
# from OCI registry. The version (dev/staging) is set in `OPERATOR_VERSION`
859+
custom_operator_version = os.environ.get("OPERATOR_VERSION")
838860

839861
prepare_multi_cluster_namespaces(
840862
namespace,

scripts/evergreen/e2e/single_e2e.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ deploy_test_app() {
4949
BUILD_VARIANT="${BUILD_VARIANT:-default_build_variant}"
5050

5151
chart_info=$(scripts/dev/run_python.sh scripts/release/oci_chart_info.py)
52-
helm_oci_regisry=$(echo "$chart_info" | jq -r '.registry' )
53-
helm_oci_repository=$(echo "$chart_info" | jq -r '.repository' )
54-
helm_oci_registry_region=$(echo "$chart_info" | jq -r '.region' )
52+
helm_oci_regisry=$(echo "${chart_info}" | jq -r '.registry' )
53+
helm_oci_repository=$(echo "${chart_info}" | jq -r '.repository' )
54+
helm_oci_registry_region=$(echo "${chart_info}" | jq -r '.region' )
5555

5656
# note, that the 4 last parameters are used only for Mongodb resource testing - not for Ops Manager
5757
helm_params=(

scripts/release/oci_chart_info.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import json
12
import os
23
import sys
3-
import json
4-
54
from dataclasses import asdict
5+
66
from lib.base_logger import logger
77
from scripts.release.build.build_info import load_build_info
88

9+
910
def main():
1011
build_scenario = os.environ.get("BUILD_SCENARIO")
1112
build_info = load_build_info(build_scenario)
@@ -15,7 +16,6 @@ def main():
1516
print(j)
1617

1718

18-
1919
if __name__ == "__main__":
2020
try:
2121
main()

0 commit comments

Comments
 (0)