Refactor for kind/remote support

This commit is contained in:
David Boreham 2023-11-18 16:04:01 -07:00
parent 5c80887215
commit 8dc23a4eed
6 changed files with 25 additions and 15 deletions

View File

@ -14,3 +14,5 @@
# along with this program. If not, see <http:#www.gnu.org/licenses/>. # along with this program. If not, see <http:#www.gnu.org/licenses/>.
stack_file_name = "stack.yml" stack_file_name = "stack.yml"
k8s_kind_deploy_type = "k8s-kind"
k8s_deploy_type = "k8s"

View File

@ -21,7 +21,7 @@ from stack_orchestrator.deploy.deployer import Deployer, DeployerException, Depl
class DockerDeployer(Deployer): class DockerDeployer(Deployer):
name: str = "compose" name: str = "compose"
def __init__(self, deployment_dir, compose_files, compose_project_name, compose_env_file) -> None: def __init__(self, type, deployment_dir, compose_files, compose_project_name, compose_env_file) -> None:
self.docker = DockerClient(compose_files=compose_files, compose_project_name=compose_project_name, self.docker = DockerClient(compose_files=compose_files, compose_project_name=compose_project_name,
compose_env_file=compose_env_file) compose_env_file=compose_env_file)
@ -70,9 +70,8 @@ class DockerDeployer(Deployer):
class DockerDeployerConfigGenerator(DeployerConfigGenerator): class DockerDeployerConfigGenerator(DeployerConfigGenerator):
config_file_name: str = "kind-config.yml"
def __init__(self) -> None: def __init__(self, type: str) -> None:
super().__init__() super().__init__()
# Nothing needed at present for the docker deployer # Nothing needed at present for the docker deployer

View File

@ -39,7 +39,7 @@ from stack_orchestrator.deploy.deployment_create import setup as deployment_setu
@click.option("--exclude", help="don\'t start these components") @click.option("--exclude", help="don\'t start these components")
@click.option("--env-file", help="env file to be used") @click.option("--env-file", help="env file to be used")
@click.option("--cluster", help="specify a non-default cluster name") @click.option("--cluster", help="specify a non-default cluster name")
@click.option("--deploy-to", help="cluster system to deploy to (compose or k8s)") @click.option("--deploy-to", help="cluster system to deploy to (compose or k8s or k8s-kind)")
@click.pass_context @click.pass_context
def command(ctx, include, exclude, env_file, cluster, deploy_to): def command(ctx, include, exclude, env_file, cluster, deploy_to):
'''deploy a stack''' '''deploy a stack'''
@ -62,11 +62,11 @@ def command(ctx, include, exclude, env_file, cluster, deploy_to):
def create_deploy_context( def create_deploy_context(
global_context, deployment_context: DeploymentContext, stack, include, exclude, cluster, env_file, deployer): global_context, deployment_context: DeploymentContext, stack, include, exclude, cluster, env_file, deploy_to):
cluster_context = _make_cluster_context(global_context, stack, include, exclude, cluster, env_file) cluster_context = _make_cluster_context(global_context, stack, include, exclude, cluster, env_file)
deployment_dir = deployment_context.deployment_dir if deployment_context else None deployment_dir = deployment_context.deployment_dir if deployment_context else None
# See: https://gabrieldemarmiesse.github.io/python-on-whales/sub-commands/compose/ # See: https://gabrieldemarmiesse.github.io/python-on-whales/sub-commands/compose/
deployer = getDeployer(deployer, deployment_dir, compose_files=cluster_context.compose_files, deployer = getDeployer(deploy_to, deployment_dir, compose_files=cluster_context.compose_files,
compose_project_name=cluster_context.cluster, compose_project_name=cluster_context.cluster,
compose_env_file=cluster_context.env_file) compose_env_file=cluster_context.env_file)
return DeployCommandContext(stack, cluster_context, deployer) return DeployCommandContext(stack, cluster_context, deployer)

View File

@ -13,23 +13,24 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http:#www.gnu.org/licenses/>. # along with this program. If not, see <http:#www.gnu.org/licenses/>.
from stack_orchestrator import contsants
from stack_orchestrator.deploy.k8s.deploy_k8s import K8sDeployer, K8sDeployerConfigGenerator from stack_orchestrator.deploy.k8s.deploy_k8s import K8sDeployer, K8sDeployerConfigGenerator
from stack_orchestrator.deploy.compose.deploy_docker import DockerDeployer, DockerDeployerConfigGenerator from stack_orchestrator.deploy.compose.deploy_docker import DockerDeployer, DockerDeployerConfigGenerator
def getDeployerConfigGenerator(type: str): def getDeployerConfigGenerator(type: str):
if type == "compose" or type is None: if type == "compose" or type is None:
return DockerDeployerConfigGenerator() return DockerDeployerConfigGenerator(type)
elif type == "k8s": elif type == contsants.k8s_deploy_type or type == contsants.k8s_kind_deploy_type:
return K8sDeployerConfigGenerator() return K8sDeployerConfigGenerator(type)
else: else:
print(f"ERROR: deploy-to {type} is not valid") print(f"ERROR: deploy-to {type} is not valid")
def getDeployer(type: str, deployment_dir, compose_files, compose_project_name, compose_env_file): def getDeployer(type: str, deployment_dir, compose_files, compose_project_name, compose_env_file):
if type == "compose" or type is None: if type == "compose" or type is None:
return DockerDeployer(deployment_dir, compose_files, compose_project_name, compose_env_file) return DockerDeployer(type, deployment_dir, compose_files, compose_project_name, compose_env_file)
elif type == "k8s": elif type == type == contsants.k8s_deploy_type or type == contsants.k8s_kind_deploy_type:
return K8sDeployer(deployment_dir, compose_files, compose_project_name, compose_env_file) return K8sDeployer(type, deployment_dir, compose_files, compose_project_name, compose_env_file)
else: else:
print(f"ERROR: deploy-to {type} is not valid") print(f"ERROR: deploy-to {type} is not valid")

View File

@ -25,6 +25,7 @@ from stack_orchestrator.opts import opts
class K8sDeployer(Deployer): class K8sDeployer(Deployer):
name: str = "k8s" name: str = "k8s"
type: str
core_api: client.CoreV1Api core_api: client.CoreV1Api
apps_api: client.AppsV1Api apps_api: client.AppsV1Api
k8s_namespace: str = "default" k8s_namespace: str = "default"
@ -32,12 +33,14 @@ class K8sDeployer(Deployer):
cluster_info : ClusterInfo cluster_info : ClusterInfo
deployment_dir: Path deployment_dir: Path
def __init__(self, deployment_dir, compose_files, compose_project_name, compose_env_file) -> None: def __init__(self, type, deployment_dir, compose_files, compose_project_name, compose_env_file) -> None:
if (opts.o.debug): if (opts.o.debug):
print(f"Deployment dir: {deployment_dir}") print(f"Deployment dir: {deployment_dir}")
print(f"Compose files: {compose_files}") print(f"Compose files: {compose_files}")
print(f"Project name: {compose_project_name}") print(f"Project name: {compose_project_name}")
print(f"Env file: {compose_env_file}") print(f"Env file: {compose_env_file}")
print(f"Type: {type}")
self.type = type
self.deployment_dir = deployment_dir self.deployment_dir = deployment_dir
self.kind_cluster_name = compose_project_name self.kind_cluster_name = compose_project_name
self.cluster_info = ClusterInfo() self.cluster_info = ClusterInfo()
@ -124,11 +127,16 @@ class K8sDeployer(Deployer):
# We need to figure out how to do this -- check why we're being called first # We need to figure out how to do this -- check why we're being called first
pass pass
def is_kind(self):
return self.type == "k8s-kind"
class K8sDeployerConfigGenerator(DeployerConfigGenerator): class K8sDeployerConfigGenerator(DeployerConfigGenerator):
config_file_name: str = "kind-config.yml" config_file_name: str = "kind-config.yml"
type: str
def __init__(self) -> None: def __init__(self, type: str) -> None:
self.type = type
super().__init__() super().__init__()
def generate(self, deployment_dir: Path): def generate(self, deployment_dir: Path):

View File

@ -21,7 +21,7 @@ mkdir -p $CERC_REPO_BASE_DIR
# Test basic stack-orchestrator deploy # Test basic stack-orchestrator deploy
test_deployment_dir=$CERC_REPO_BASE_DIR/test-deployment-dir test_deployment_dir=$CERC_REPO_BASE_DIR/test-deployment-dir
test_deployment_spec=$CERC_REPO_BASE_DIR/test-deployment-spec.yml test_deployment_spec=$CERC_REPO_BASE_DIR/test-deployment-spec.yml
$TEST_TARGET_SO --stack test deploy --deploy-to k8s init --output $test_deployment_spec --config CERC_TEST_PARAM_1=PASSED $TEST_TARGET_SO --stack test deploy --deploy-to k8s-kind init --output $test_deployment_spec --config CERC_TEST_PARAM_1=PASSED
# Check the file now exists # Check the file now exists
if [ ! -f "$test_deployment_spec" ]; then if [ ! -f "$test_deployment_spec" ]; then
echo "deploy init test: spec file not present" echo "deploy init test: spec file not present"