Skip to content

Commit

Permalink
refactor: (8) 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 3ec92d2 commit 5bcaa29
Showing 1 changed file with 105 additions and 129 deletions.
234 changes: 105 additions & 129 deletions core/src/plugins/kubernetes/nginx/nginx-helm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,115 @@ const HELM_INGRESS_NGINX_DEPLOYMENT_TIMEOUT = "300s"
type _HelmValue = number | string | boolean | object | null | undefined

export abstract class HelmGardenIngressController extends GardenIngressController {
override install(ctx: KubernetesPluginContext, log: Log): Promise<void> {
return helmNginxInstall(ctx, log, this.helmValuesGetter())
override async install(ctx: KubernetesPluginContext, log: Log): Promise<void> {
const ingressControllerReady = await this.ready(ctx, log)
if (ingressControllerReady) {
return
}

const provider = ctx.provider
const config = provider.config
const namespace = config.gardenSystemNamespace
const systemVars: SystemVars = getKubernetesSystemVariables(config)
const values = this.helmValuesGetter()(systemVars)

const valueArgs: string[] = []
for (const key in values) {
if (values.hasOwnProperty(key)) {
valueArgs.push(`${key}=${JSON.stringify(values[key])}`)
}
}

const args = [
"upgrade",
"--install",
HELM_INGRESS_NGINX_RELEASE_NAME,
HELM_INGRESS_NGINX_CHART,
"--version",
HELM_INGRESS_NGINX_VERSION,
"--repo",
HELM_INGRESS_NGINX_REPO,
"--timeout",
HELM_INGRESS_NGINX_DEPLOYMENT_TIMEOUT,
"--set-json",
`${valueArgs.join(",")}`,
]

log.info(`Installing nginx in ${namespace} namespace...`)
await defaultBackendInstall(ctx, log)
await helm({ ctx, namespace, log, args, emitLogEvents: false })

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

log.success(`nginx successfully installed in ${namespace} namespace`)
}

override async ready(ctx: KubernetesPluginContext, log: Log): Promise<boolean> {
const nginxStatus = await this.getStatus(ctx, log)
const backendStatus = await defaultBackendStatus(ctx, log)

return nginxStatus === "ready" && backendStatus === "ready"
}

override ready(ctx: KubernetesPluginContext, log: Log): Promise<boolean> {
return helmIngressControllerReady(ctx, log, this.helmValuesGetter())
override async getStatus(ctx: KubernetesPluginContext, log: Log): Promise<DeployState> {
const provider = ctx.provider
const config = provider.config
const api = await KubeApi.factory(log, ctx, provider)
const systemVars: SystemVars = getKubernetesSystemVariables(config)
const values = this.helmValuesGetter()(systemVars)

const namespace = config.gardenSystemNamespace
try {
const statusRes = JSON.parse(
await helm({
ctx,
log,
namespace,
args: ["status", HELM_INGRESS_NGINX_RELEASE_NAME, "--output", "json"],
// do not send JSON output to Garden Cloud or CLI verbose log
emitLogEvents: false,
})
)
const releaseStatus = statusRes.info?.status || "unknown"

if (releaseStatus !== "deployed") {
log.debug(chalk.yellow(`Helm release status for ${HELM_INGRESS_NGINX_RELEASE_NAME}: ${releaseStatus}`))
return helmStatusMap[releaseStatus] || "unknown"
}

// we check that the deployment or daemonset is ready because the status of the helm release
// can be "deployed" even if the deployed resource is not ready.
const nginxHelmMainResource = getNginxHelmMainResource(values)
const deploymentStatus = await checkResourceStatus({ api, namespace, manifest: nginxHelmMainResource, log })
return deploymentStatus.state
} catch (error) {
log.debug(chalk.yellow(`Helm release ${HELM_INGRESS_NGINX_RELEASE_NAME} missing.`))
return "missing"
}
}

override uninstall(ctx: KubernetesPluginContext, log: Log): Promise<void> {
return helmNginxUninstall(ctx, log, this.helmValuesGetter())
override async uninstall(ctx: KubernetesPluginContext, log: Log): Promise<void> {
const status = await this.getStatus(ctx, log)
if (status === "missing") {
return
}

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

await helm({ ctx, namespace, log, args: ["uninstall", HELM_INGRESS_NGINX_RELEASE_NAME], emitLogEvents: false })
await defaultBackendUninstall(ctx, log)
}

abstract helmValuesGetter(): NginxHelmValuesGetter
Expand Down Expand Up @@ -89,126 +188,3 @@ function getNginxHelmMainResource(values: NginxHelmValues) {
},
}
}

async function helmIngressControllerReady(
ctx: KubernetesPluginContext,
log: Log,
nginxHelmValuesGetter: NginxHelmValuesGetter
) {
const nginxStatus = await helmNginxStatus(ctx, log, nginxHelmValuesGetter)
const backendStatus = await defaultBackendStatus(ctx, log)

return nginxStatus === "ready" && backendStatus === "ready"
}

async function helmNginxStatus(
ctx: KubernetesPluginContext,
log: Log,
nginxHelmValuesGetter: NginxHelmValuesGetter
): Promise<DeployState> {
const provider = ctx.provider
const config = provider.config
const api = await KubeApi.factory(log, ctx, provider)
const systemVars: SystemVars = getKubernetesSystemVariables(config)
const values = nginxHelmValuesGetter(systemVars)

const namespace = config.gardenSystemNamespace
try {
const statusRes = JSON.parse(
await helm({
ctx,
log,
namespace,
args: ["status", HELM_INGRESS_NGINX_RELEASE_NAME, "--output", "json"],
// do not send JSON output to Garden Cloud or CLI verbose log
emitLogEvents: false,
})
)
const releaseStatus = statusRes.info?.status || "unknown"

if (releaseStatus !== "deployed") {
log.debug(chalk.yellow(`Helm release status for ${HELM_INGRESS_NGINX_RELEASE_NAME}: ${releaseStatus}`))
return helmStatusMap[releaseStatus] || "unknown"
}

// we check that the deployment or daemonset is ready because the status of the helm release
// can be "deployed" even if the deployed resource is not ready.
const nginxHelmMainResource = getNginxHelmMainResource(values)
const deploymentStatus = await checkResourceStatus({ api, namespace, manifest: nginxHelmMainResource, log })
return deploymentStatus.state
} catch (error) {
log.debug(chalk.yellow(`Helm release ${HELM_INGRESS_NGINX_RELEASE_NAME} missing.`))
return "missing"
}
}

async function helmNginxInstall(ctx: KubernetesPluginContext, log: Log, nginxHelmValuesGetter: NginxHelmValuesGetter) {
const ingressControllerReady = await helmIngressControllerReady(ctx, log, nginxHelmValuesGetter)
if (ingressControllerReady) {
return
}

const provider = ctx.provider
const config = provider.config
const namespace = config.gardenSystemNamespace
const systemVars: SystemVars = getKubernetesSystemVariables(config)
const values = nginxHelmValuesGetter(systemVars)

const valueArgs: string[] = []
for (const key in values) {
if (values.hasOwnProperty(key)) {
valueArgs.push(`${key}=${JSON.stringify(values[key])}`)
}
}

const args = [
"upgrade",
"--install",
HELM_INGRESS_NGINX_RELEASE_NAME,
HELM_INGRESS_NGINX_CHART,
"--version",
HELM_INGRESS_NGINX_VERSION,
"--repo",
HELM_INGRESS_NGINX_REPO,
"--timeout",
HELM_INGRESS_NGINX_DEPLOYMENT_TIMEOUT,
"--set-json",
`${valueArgs.join(",")}`,
]

log.info(`Installing nginx in ${namespace} namespace...`)
await defaultBackendInstall(ctx, log)
await helm({ ctx, namespace, log, args, emitLogEvents: false })

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

log.success(`nginx successfully installed in ${namespace} namespace`)
}

async function helmNginxUninstall(
ctx: KubernetesPluginContext,
log: Log,
nginxHelmValuesGetter: NginxHelmValuesGetter
) {
const status = await helmNginxStatus(ctx, log, nginxHelmValuesGetter)
if (status === "missing") {
return
}

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

await helm({ ctx, namespace, log, args: ["uninstall", HELM_INGRESS_NGINX_RELEASE_NAME], emitLogEvents: false })
await defaultBackendUninstall(ctx, log)
}

0 comments on commit 5bcaa29

Please sign in to comment.