Skip to content

Commit

Permalink
refactor: (5) inline functions into class methods
Browse files Browse the repository at this point in the history
  • Loading branch information
vvagaytsev committed Nov 8, 2023
1 parent 30d0401 commit b37ca44
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 122 deletions.
88 changes: 40 additions & 48 deletions core/src/plugins/kubernetes/nginx/nginx-kind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,52 @@ const nginxKindMainResource = {
}

export class KindGardenIngressController implements GardenIngressController {
install(ctx: KubernetesPluginContext, log: Log): Promise<void> {
return kindNginxInstall(ctx, log)
async install(ctx: KubernetesPluginContext, log: Log): Promise<void> {
const status = await kindNginxStatus(ctx, log)
if (status === "ready") {
return
}

const provider = ctx.provider
const config = provider.config
const namespace = config.gardenSystemNamespace
const api = await KubeApi.factory(log, ctx, provider)

const manifests = kindNginxGetManifests(namespace)

log.info("Installing ingress controller for kind cluster")
await apply({ log, ctx, api, provider, manifests, namespace })

await waitForResources({
// setting the action name to providers is necessary to display the logs in provider-section
actionName: "providers",
namespace,
ctx,
provider,
resources: [nginxKindMainResource],
log,
timeoutSec: 60,
})
}

async ready(ctx: KubernetesPluginContext, log: Log): Promise<boolean> {
return (await kindNginxStatus(ctx, log)) === "ready"
}

uninstall(ctx: KubernetesPluginContext, log: Log): Promise<void> {
return kindNginxUninstall(ctx, log)
async uninstall(ctx: KubernetesPluginContext, log: Log): Promise<void> {
const status = await kindNginxStatus(ctx, log)
if (status === "missing") {
return
}

const provider = ctx.provider
const config = provider.config
const namespace = config.gardenSystemNamespace

const manifests = kindNginxGetManifests(namespace)

log.info("Uninstalling ingress controller for kind cluster")
await deleteResources({ log, ctx, provider, namespace, resources: manifests })
}
}

Expand All @@ -49,47 +85,3 @@ async function kindNginxStatus(ctx: KubernetesPluginContext, log: Log): Promise<
log.debug(chalk.yellow(`Status of ingress controller: ${deploymentStatus.state}`))
return deploymentStatus.state
}

async function kindNginxInstall(ctx: KubernetesPluginContext, log: Log) {
const status = await kindNginxStatus(ctx, log)
if (status === "ready") {
return
}

const provider = ctx.provider
const config = provider.config
const namespace = config.gardenSystemNamespace
const api = await KubeApi.factory(log, ctx, provider)

const manifests = kindNginxGetManifests(namespace)

log.info("Installing ingress controller for kind cluster")
await apply({ log, ctx, api, provider, manifests, namespace })

await waitForResources({
// setting the action name to providers is necessary to display the logs in provider-section
actionName: "providers",
namespace,
ctx,
provider,
resources: [nginxKindMainResource],
log,
timeoutSec: 60,
})
}

async function kindNginxUninstall(ctx: KubernetesPluginContext, log: Log) {
const status = await kindNginxStatus(ctx, log)
if (status === "missing") {
return
}

const provider = ctx.provider
const config = provider.config
const namespace = config.gardenSystemNamespace

const manifests = kindNginxGetManifests(namespace)

log.info("Uninstalling ingress controller for kind cluster")
await deleteResources({ log, ctx, provider, namespace, resources: manifests })
}
74 changes: 33 additions & 41 deletions core/src/plugins/kubernetes/nginx/nginx-microk8s.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,45 @@ import { waitForResources } from "../status/status.js"
import { GardenIngressController } from "./ingress-controller.js"

export class Microk8sGardenIngressController implements GardenIngressController {
install(ctx: KubernetesPluginContext, log: Log): Promise<void> {
return microk8sNginxInstall(ctx, log)
async install(ctx: KubernetesPluginContext, log: Log): Promise<void> {
const provider = ctx.provider

const status = await microk8sNginxStatus(log)
if (status === "ready") {
return
}
log.info("Enabling microk8s ingress controller addon")
await configureMicrok8sAddons(log, ["ingress"])
const nginxMainResource = {
apiVersion: "apps/v1",
kind: "DaemonSet",
metadata: {
name: "nginx-ingress-microk8s-controller",
},
}
await waitForResources({
// setting the action name to providers is necessary to display the logs in provider-section
actionName: "providers",
namespace: "ingress",
ctx,
provider,
resources: [nginxMainResource],
log,
timeoutSec: 60,
})
}

async ready(_ctx: KubernetesPluginContext, log: Log): Promise<boolean> {
return (await microk8sNginxStatus(log)) === "ready"
}

uninstall(ctx: KubernetesPluginContext, log: Log): Promise<void> {
return microk8sNginxUninstall(ctx, log)
async uninstall(ctx: KubernetesPluginContext, log: Log): Promise<void> {
const status = await microk8sNginxStatus(log)
if (status === "missing") {
return
}
log.info("Disabling microk8s ingress controller addon")
await exec("microk8s", ["disable", "ingress"])
}
}

Expand All @@ -38,40 +67,3 @@ async function microk8sNginxStatus(log: Log): Promise<DeployState> {
log.debug(chalk.yellow(`Status of microk8s ingress controller addon: ${addonEnabled ? "enabled" : "disabled"}`))
return addonEnabled ? "ready" : "missing"
}

async function microk8sNginxInstall(ctx: KubernetesPluginContext, log: Log) {
const provider = ctx.provider

const status = await microk8sNginxStatus(log)
if (status === "ready") {
return
}
log.info("Enabling microk8s ingress controller addon")
await configureMicrok8sAddons(log, ["ingress"])
const nginxMainResource = {
apiVersion: "apps/v1",
kind: "DaemonSet",
metadata: {
name: "nginx-ingress-microk8s-controller",
},
}
await waitForResources({
// setting the action name to providers is necessary to display the logs in provider-section
actionName: "providers",
namespace: "ingress",
ctx,
provider,
resources: [nginxMainResource],
log,
timeoutSec: 60,
})
}

async function microk8sNginxUninstall(ctx: KubernetesPluginContext, log: Log) {
const status = await microk8sNginxStatus(log)
if (status === "missing") {
return
}
log.info("Disabling microk8s ingress controller addon")
await exec("microk8s", ["disable", "ingress"])
}
58 changes: 25 additions & 33 deletions core/src/plugins/kubernetes/nginx/nginx-minikube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,37 @@ import { checkResourceStatus, waitForResources } from "../status/status.js"
import { GardenIngressController } from "./ingress-controller.js"

export class MinikubeGardenIngressController implements GardenIngressController {
install(ctx: KubernetesPluginContext, log: Log): Promise<void> {
return minikubeNginxInstall(ctx, log)
async install(ctx: KubernetesPluginContext, log: Log): Promise<void> {
const provider = ctx.provider
const status = await minikubeNginxStatus(ctx, log)
if (status === "ready") {
return
}
log.info("Enabling minikube ingress controller addon")
await exec("minikube", ["addons", "enable", "ingress"])
await waitForResources({
// setting the action name to providers is necessary to display the logs in provider-section
actionName: "providers",
namespace: "ingress-nginx",
ctx,
provider,
resources: [nginxKindMainResource],
log,
timeoutSec: 60,
})
}

async ready(ctx: KubernetesPluginContext, log: Log): Promise<boolean> {
return (await minikubeNginxStatus(ctx, log)) === "ready"
}

uninstall(ctx: KubernetesPluginContext, log: Log): Promise<void> {
return minikubeNginxUninstall(ctx, log)
async uninstall(ctx: KubernetesPluginContext, log: Log): Promise<void> {
const status = await minikubeNginxStatus(ctx, log)
if (status === "missing") {
return
}
log.info("Disabling minikube ingress controller addon")
await exec("minikube", ["addons", "disable", "ingress"])
}
}

Expand Down Expand Up @@ -66,32 +87,3 @@ async function minikubeNginxStatus(ctx: KubernetesPluginContext, log: Log): Prom
log.debug(chalk.yellow(`Status of minikube ingress controller addon: ${deploymentStatus.state}`))
return deploymentStatus.state
}

async function minikubeNginxInstall(ctx: KubernetesPluginContext, log: Log) {
const provider = ctx.provider
const status = await minikubeNginxStatus(ctx, log)
if (status === "ready") {
return
}
log.info("Enabling minikube ingress controller addon")
await exec("minikube", ["addons", "enable", "ingress"])
await waitForResources({
// setting the action name to providers is necessary to display the logs in provider-section
actionName: "providers",
namespace: "ingress-nginx",
ctx,
provider,
resources: [nginxKindMainResource],
log,
timeoutSec: 60,
})
}

async function minikubeNginxUninstall(ctx: KubernetesPluginContext, log: Log) {
const status = await minikubeNginxStatus(ctx, log)
if (status === "missing") {
return
}
log.info("Disabling minikube ingress controller addon")
await exec("minikube", ["addons", "disable", "ingress"])
}

0 comments on commit b37ca44

Please sign in to comment.