From f57fc275166eb5dce1b25c58f43ed8ef16e42ac2 Mon Sep 17 00:00:00 2001 From: Nir Soffer Date: Wed, 28 Aug 2024 15:59:19 +0300 Subject: [PATCH] Add external provider Before this change we had only one provider, so this was internal implementation detail. This change adds the second provider, allowing users to configure the provider in the environment file. Replace the `external: true` option with `provider: external`. With this we can remove the special handling or external cluster with calls to the external provider which does the right thing. The external provider basically does nothing, since we do not manage this cluster. However in start() we ensure that the cluster exists and then wait until the cluster is ready. This helps to debug issues with external cluster and reduces log noise. Signed-off-by: Nir Soffer --- test/README.md | 8 ++-- test/drenv/__main__.py | 26 +++++------- test/drenv/providers/external.py | 73 ++++++++++++++++++++++++++++++++ test/envs/external.yaml | 8 ++-- 4 files changed, 92 insertions(+), 23 deletions(-) create mode 100644 test/drenv/providers/external.py diff --git a/test/README.md b/test/README.md index 6114581b05..3ecfca624a 100644 --- a/test/README.md +++ b/test/README.md @@ -539,9 +539,11 @@ $ drenv delete envs/example.yaml - `templates`: templates for creating new profiles. - `name`: profile name. - - `external`: true if this is existing external cluster. In this - case the tool will not start a minikube cluster and all other - options are ignored. + - `provider`: cluster provider. The default provider is "minikube", + creating cluster using VM or containers. Use "external" to use + exsiting clusters not managed by `drenv`. Use the special value + "$provider" to select the best provider for the host. (default + "$provider") - `driver`: The minikube driver. On Linux, the default drivers are kvm2 and docker for VMs and containers. On MacOS, the defaults are hyperkit and podman. Use "$vm" and "$container" values to use the recommended VM and diff --git a/test/drenv/__main__.py b/test/drenv/__main__.py index 9da2de492a..69681e6c3c 100644 --- a/test/drenv/__main__.py +++ b/test/drenv/__main__.py @@ -359,14 +359,13 @@ def collect_addons(env): def start_cluster(profile, hooks=(), args=None, **options): provider = providers.get(profile["provider"]) - if profile["external"]: - logging.debug("[%s] Skipping external cluster", profile["name"]) - else: - existing = provider.exists(profile) - provider.start(profile, verbose=args.verbose) - provider.configure(profile, existing=existing) - if existing: - restart_failed_deployments(profile) + existing = provider.exists(profile) + + provider.start(profile, verbose=args.verbose) + provider.configure(profile, existing=existing) + + if existing: + restart_failed_deployments(profile) if hooks: execute( @@ -391,19 +390,14 @@ def stop_cluster(profile, hooks=(), **options): allow_failure=True, ) - provider = providers.get(profile["provider"]) - if profile["external"]: - logging.debug("[%s] Skipping external cluster", profile["name"]) - elif cluster_status != cluster.UNKNOWN: + if cluster_status != cluster.UNKNOWN: + provider = providers.get(profile["provider"]) provider.stop(profile) def delete_cluster(profile, **options): provider = providers.get(profile["provider"]) - if profile["external"]: - logging.debug("[%s] Skipping external cluster", profile["name"]) - else: - provider.delete(profile) + provider.delete(profile) profile_config = drenv.config_dir(profile["name"]) if os.path.exists(profile_config): diff --git a/test/drenv/providers/external.py b/test/drenv/providers/external.py new file mode 100644 index 0000000000..a528e35464 --- /dev/null +++ b/test/drenv/providers/external.py @@ -0,0 +1,73 @@ +# SPDX-FileCopyrightText: The RamenDR authors +# SPDX-License-Identifier: Apache-2.0 + +import logging +import time +from functools import partial + +from drenv import cluster + +# Provider scope + + +def setup(): + logging.info("[external] Skipping setup for external provider") + + +def cleanup(): + logging.info("[external] Skipping cleanup for external provider") + + +# Cluster scope + + +def exists(profile): + return True + + +def start(profile, verbose=False): + start = time.monotonic() + logging.info("[%s] Checking external cluster status", profile["name"]) + + # Fail fast if cluster is not configured, we cannot recover from this. + status = cluster.status(profile["name"]) + if status == cluster.UNKNOWN: + raise RuntimeError(f"Cluster '{profile['name']}' does not exist") + + # Otherwise handle temporary outage gracefuly. + debug = partial(logging.debug, f"[{profile['name']}] %s") + cluster.wait_until_ready(profile["name"], timeout=60, log=debug) + + logging.info( + "[%s] Cluster ready in %.2f seconds", + profile["name"], + time.monotonic() - start, + ) + + +def configure(profile, existing=False): + logging.info("[%s] Skipping configure for external cluster", profile["name"]) + + +def stop(profile): + logging.info("[%s] Skipping stop for external cluster", profile["name"]) + + +def delete(profile): + logging.info("[%s] Skipping delete for external cluster", profile["name"]) + + +def suspend(profile): + logging.info("[%s] Skipping suspend for external cluster", profile["name"]) + + +def resume(profile): + logging.info("[%s] Skipping resume for external cluster", profile["name"]) + + +def cp(name, src, dst): + logging.warning("[%s] cp not implemented yet for external cluster", name) + + +def ssh(name, command): + logging.warning("[%s] ssh not implemented yet for external cluster", name) diff --git a/test/envs/external.yaml b/test/envs/external.yaml index 51da25131c..98238dd99f 100644 --- a/test/envs/external.yaml +++ b/test/envs/external.yaml @@ -1,12 +1,12 @@ # SPDX-FileCopyrightText: The RamenDR authors # SPDX-License-Identifier: Apache-2.0 -# Example environment using external clusters. The cluster `test` must exist -# when this environment is started. +# Example environment using external clusters. The cluster must exist when this +# environment is started. # # To try this example, create the cluster with: # -# drenv start envs/test.yaml +# drenv start envs/vm.yaml # # Now you can start this environment with: # @@ -20,7 +20,7 @@ name: external profiles: - name: cluster - external: true + provider: external workers: - addons: - name: example