From 2ea6202c39cf8afd185a6cb26311bb80ca5e3ade Mon Sep 17 00:00:00 2001 From: Vladimir Vagaytsev Date: Wed, 8 Nov 2023 17:06:28 +0100 Subject: [PATCH] refactor: (13) replace conditional logic with polymorphism --- .../kubernetes/nginx/ingress-controller.ts | 46 ++++++++++--------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/core/src/plugins/kubernetes/nginx/ingress-controller.ts b/core/src/plugins/kubernetes/nginx/ingress-controller.ts index 75c98f7921..8dfcfeb649 100644 --- a/core/src/plugins/kubernetes/nginx/ingress-controller.ts +++ b/core/src/plugins/kubernetes/nginx/ingress-controller.ts @@ -14,12 +14,31 @@ import { Microk8sGardenIngressController } from "./nginx-microk8s.js" import { MinikubeGardenIngressController } from "./nginx-minikube.js" import { KindGardenIngressController } from "./nginx-kind.js" import { EphemeralHelmGardenIngressController } from "./nginx-helm-ephemeral.js" -import type { GardenIngressComponent } from "./ingress-controller-base.js" +import { GardenIngressComponent } from "./ingress-controller-base.js" +import type { DeployState } from "../../../types/service.js" -export function getGardenIngressController(ctx: KubernetesPluginContext): GardenIngressComponent | undefined { +class NoOpGardenIngressController extends GardenIngressComponent { + override install(_ctx: KubernetesPluginContext, _log: Log): Promise { + return Promise.resolve(undefined) + } + + override getStatus(_ctx: KubernetesPluginContext, _log: Log): Promise { + return Promise.resolve("missing") + } + + override async ready(_ctx: KubernetesPluginContext, _log: Log): Promise { + return false + } + + override uninstall(_ctx: KubernetesPluginContext, _log: Log): Promise { + return Promise.resolve(undefined) + } +} + +export function getGardenIngressController(ctx: KubernetesPluginContext): GardenIngressComponent { const clusterType = ctx.provider.config.clusterType if (clusterType === undefined) { - return undefined + return new NoOpGardenIngressController() } if (clusterType === "kind") { @@ -40,28 +59,13 @@ export function getGardenIngressController(ctx: KubernetesPluginContext): Garden } export async function ingressControllerReady(ctx: KubernetesPluginContext, log: Log): Promise { - const gardenIngressController = getGardenIngressController(ctx) - if (!gardenIngressController) { - return false - } - - return await gardenIngressController.ready(ctx, log) + return await getGardenIngressController(ctx).ready(ctx, log) } export async function ingressControllerInstall(ctx: KubernetesPluginContext, log: Log) { - const gardenIngressController = getGardenIngressController(ctx) - if (!gardenIngressController) { - return - } - - await gardenIngressController.install(ctx, log) + await getGardenIngressController(ctx).install(ctx, log) } export async function ingressControllerUninstall(ctx: KubernetesPluginContext, log: Log) { - const gardenIngressController = getGardenIngressController(ctx) - if (!gardenIngressController) { - return - } - - return await gardenIngressController.uninstall(ctx, log) + return await getGardenIngressController(ctx).uninstall(ctx, log) }