Skip to content

Commit

Permalink
chore: save
Browse files Browse the repository at this point in the history
  • Loading branch information
eysi09 committed Dec 10, 2024
1 parent 3c65e49 commit 53c8184
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 65 deletions.
5 changes: 3 additions & 2 deletions core/src/plugin/handlers/Provider/getEnvironmentStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ export interface EnvironmentStatusMap {
[providerName: string]: EnvironmentStatus
}

// TODO @eysi: Update text
export const getEnvironmentStatus = () => ({
description: dedent`
Check if the current environment is ready for use by this plugin. Use this action in combination
with \`prepareEnvironment\`.
Helper handler to check if the current environment is ready for use by this plugin. Only called
with commands that set \`statusOnly: true\`.
Called before \`prepareEnvironment\`. If this returns \`ready: true\`, the
\`prepareEnvironment\` action is not called.
Expand Down
7 changes: 2 additions & 5 deletions core/src/plugin/handlers/Provider/prepareEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ import { joi } from "../../../config/common.js"
import { environmentStatusSchema } from "../../../config/status.js"
import type { GenericProviderConfig } from "../../../config/provider.js"

export interface PrepareEnvironmentParams<
C extends GenericProviderConfig = any,
T extends EnvironmentStatus = EnvironmentStatus,
> extends PluginActionParamsBase<C> {
status: T
export interface PrepareEnvironmentParams<C extends GenericProviderConfig = any> extends PluginActionParamsBase<C> {
force: boolean
}

Expand All @@ -27,6 +23,7 @@ export interface PrepareEnvironmentResult<O extends {} = any, D extends {} = any
}

export const prepareEnvironment = () => ({
// TODO @eysi: Update text
description: dedent`
Make sure the environment is set up for this plugin. Use this action to do any bootstrapping required
before deploying services.
Expand Down
5 changes: 0 additions & 5 deletions core/src/plugins/exec/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@ export const execProvider = execPlugin.createProvider({
}),
})

execProvider.addHandler("getEnvironmentStatus", async ({ ctx }) => {
// Return ready if there is no initScript to run
return { ready: !ctx.provider.config.initScript, outputs: {} }
})

execProvider.addHandler("prepareEnvironment", async ({ ctx, log }) => {
const execLog = log.createLog({ name: "exec" })
if (ctx.provider.config.initScript) {
Expand Down
1 change: 0 additions & 1 deletion core/src/plugins/kubernetes/commands/cluster-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export const clusterInit: PluginCommand = {
ctx,
log,
force: true,
status,
})
}

Expand Down
34 changes: 28 additions & 6 deletions core/src/plugins/kubernetes/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,28 +145,50 @@ export async function getIngressMisconfigurationWarnings(

/**
* Deploys system services (if any) and creates the default app namespace.
* TODO @eysi: Ensure secrets here
*/
export async function prepareEnvironment(
params: PrepareEnvironmentParams<KubernetesConfig, KubernetesEnvironmentStatus>
params: PrepareEnvironmentParams<KubernetesConfig>
): Promise<PrepareEnvironmentResult> {
const { ctx, log, status } = params
const { ctx, log } = params
const k8sCtx = <KubernetesPluginContext>ctx
const provider = k8sCtx.provider
const config = provider.config
const api = await KubeApi.factory(log, ctx, provider)

// create default app namespace if it doesn't exist
await prepareNamespaces({ ctx, log })
const nsStatuses = await prepareNamespaces({ ctx, log })
// make sure that the system namespace exists
await getSystemNamespace(ctx, ctx.provider, log)

// TODO-0.13/TODO-0.14: remove this option for remote kubernetes clusters?
if (config.setupIngressController === "nginx") {
// Install nginx ingress controller
await ingressControllerInstall(k8sCtx, log)
} else {
// We only need to warn about missing ingress classes if we're not using garden installed nginx
const ingressApiVersion = await getIngressApiVersion(log, api, supportedIngressApiVersions)
const ingressWarnings = await getIngressMisconfigurationWarnings(
provider.config.ingressClass,
ingressApiVersion,
log,
api
)
ingressWarnings.forEach((w) => log.warn(w))
}

const nsStatus = await getNamespaceStatus({ ctx: k8sCtx, log, provider })
ctx.events.emit("namespaceStatus", nsStatus)
return { status: { ready: true, outputs: status.outputs } }
// prepareNamespaces already does this
// const nsStatus = await getNamespaceStatus({ ctx: k8sCtx, log, provider })
// ctx.events.emit("namespaceStatus", nsStatus)
return {
status: {
ready: true,
outputs: {
"app-namespace": nsStatuses["app-namespace"].namespaceName,
"default-hostname": provider.config.defaultHostname || null,
},
},
}
}

export async function cleanupEnvironment({
Expand Down
10 changes: 8 additions & 2 deletions core/src/plugins/kubernetes/local/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ async function prepareEnvironment(
const { ctx, log } = params
const provider = ctx.provider

const result = await _prepareEnvironmentBase(params)
// This should be set in the configureProvider handler but we need the
// plugin context to get the cluster type
if (!provider.config.clusterType) {
provider.config.clusterType = await getClusterType(ctx, log)
}

const prepareEnvResult = await _prepareEnvironmentBase(params)

if (provider.config.clusterType === "minikube") {
await setMinikubeDockerEnv()
Expand All @@ -76,7 +82,7 @@ async function prepareEnvironment(
await configureMicrok8sAddons(log, microk8sAddons)
}

return result
return prepareEnvResult
}

async function getClusterType(ctx: KubernetesPluginContext, log: Log): Promise<LocalKubernetesClusterType> {
Expand Down
4 changes: 0 additions & 4 deletions core/src/plugins/otel-collector/otel-collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,6 @@ const providerConfigSchema = s.object({

export const provider = gardenPlugin.createProvider({ configSchema: providerConfigSchema, outputsSchema: s.object({}) })

provider.addHandler("getEnvironmentStatus", async () => {
return { ready: false, outputs: {} }
})

provider.addHandler("prepareEnvironment", async ({ ctx, log }) => {
const scopedLog = log.createLog({ name: "otel-collector" })
scopedLog.debug("Preparing the environment for the otel-collector")
Expand Down
51 changes: 25 additions & 26 deletions core/src/tasks/resolve-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import { stableStringify } from "../util/string.js"
import { OtelTraced } from "../util/open-telemetry/decorators.js"
import { LogLevel } from "../logger/logger.js"
import type { Log } from "../logger/log-entry.js"
import { styles } from "../logger/styles.js"
import type { ObjectPath } from "../config/base.js"
import fsExtra from "fs-extra"
import { RemoteSourceConfigContext } from "../config/template-contexts/project.js"
Expand Down Expand Up @@ -412,43 +411,43 @@ export class ResolveProviderTask extends BaseTask<Provider> {
return cachedStatus
}

// TODO: avoid calling the handler manually (currently doing it to override the plugin context)
const handler = await actions.provider["getPluginHandler"]({
handlerType: "getEnvironmentStatus",
pluginName,
defaultHandler: async () => defaultEnvironmentStatus,
})

let status = await handler!({ ctx, log: providerLog })

if (!statusOnly && (this.forceInit || !status.ready)) {
const statusMsg = status.ready
? `${styles.highlight("Ready")}, will ${styles.highlight("force re-initialize")}`
: `${styles.highlight("Not ready")}, will initialize`
providerLog.info(statusMsg)
// TODO: avoid calling the handler manually
const prepareHandler = await actions.provider["getPluginHandler"]({
handlerType: "prepareEnvironment",
if (statusOnly) {
// TODO: avoid calling the handler manually (currently doing it to override the plugin context)
const getStatusHandler = await actions.provider["getPluginHandler"]({
handlerType: "getEnvironmentStatus",
pluginName,
defaultHandler: async () => ({ status }),
defaultHandler: async () => defaultEnvironmentStatus,
})

const result = await prepareHandler!({ ctx, log: providerLog, force: this.forceInit, status })
const envStatus = await getStatusHandler!({ ctx, log: providerLog })
if (envStatus.ready) {
providerLog.success(`Provider is ready`)
} else {
providerLog.warn(`Provider is not ready`)
}

status = result.status
return envStatus
}

if (!status.ready && !statusOnly) {
providerLog.info(`Preparing provider environment`)
// TODO: avoid calling the handler manually
const prepareHandler = await actions.provider["getPluginHandler"]({
handlerType: "prepareEnvironment",
pluginName,
defaultHandler: async () => ({ status: { ready: true, outputs: {} } }),
})

const result = await prepareHandler!({ ctx, log: providerLog, force: this.forceInit })

const status = result.status

if (!status.ready) {
providerLog.error("Failed initializing provider")
throw new PluginError({
message: `Provider ${pluginName} reports status as not ready and could not prepare the configured environment.`,
})
}

if (!status.ready && statusOnly) {
providerLog.success("Provider not ready. Current command only checks status, not preparing environment")
return status
}
providerLog.success("Provider ready")

if (!status.disableCache) {
Expand Down
4 changes: 0 additions & 4 deletions core/test/integ/src/plugins/kubernetes/ingress-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { ingressControllerReady } from "../../../../../src/plugins/kubernetes/ng
import { uninstallGardenServices } from "../../../../../src/plugins/kubernetes/commands/uninstall-garden-services.js"
import { prepareEnvironment } from "../../../../../src/plugins/kubernetes/init.js"
import type { PrepareEnvironmentParams } from "../../../../../src/plugin/handlers/Provider/prepareEnvironment.js"
import { defaultEnvironmentStatus } from "../../../../../src/plugin/handlers/Provider/getEnvironmentStatus.js"
import { getContainerTestGarden } from "./container/container.js"
import type { Garden } from "../../../../../src/garden.js"

Expand Down Expand Up @@ -59,7 +58,6 @@ describe("It should manage ingress controller for respective cluster type", () =
const params: PrepareEnvironmentParams = {
ctx,
log: garden.log,
status: defaultEnvironmentStatus,
force: false,
}
ctx.provider.config.setupIngressController = "nginx"
Expand All @@ -72,7 +70,6 @@ describe("It should manage ingress controller for respective cluster type", () =
const params: PrepareEnvironmentParams = {
ctx,
log: garden.log,
status: defaultEnvironmentStatus,
force: false,
}
ctx.provider.config.setupIngressController = "null"
Expand All @@ -86,7 +83,6 @@ describe("It should manage ingress controller for respective cluster type", () =
const params: PrepareEnvironmentParams = {
ctx,
log: garden.log,
status: defaultEnvironmentStatus,
force: false,
}
ctx.provider.config.setupIngressController = "nginx"
Expand Down
2 changes: 0 additions & 2 deletions core/test/integ/src/plugins/kubernetes/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,9 @@ describe("kubernetes provider handlers", () => {
})

it("should prepare the environment with the prepareEnvironment handler", async () => {
const status = await getEnvironmentStatus({ ctx, log })
const params: PrepareEnvironmentParams = {
ctx,
log: garden.log,
status,
force: false,
}
const envStatus = await prepareEnvironment(params)
Expand Down
4 changes: 2 additions & 2 deletions core/test/unit/src/commands/get/get-outputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ describe("GetOutputsCommand", () => {
const plugin = createGardenPlugin({
name: "test",
handlers: {
async getEnvironmentStatus() {
return { ready: true, outputs: { test: "test-value" } }
async prepareEnvironment() {
return { status: { ready: true, outputs: { test: "test-value" } } }
},
},
})
Expand Down
8 changes: 5 additions & 3 deletions core/test/unit/src/garden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2497,10 +2497,12 @@ describe("Garden", () => {
const testA = createGardenPlugin({
name: "test-a",
handlers: {
getEnvironmentStatus: async () => {
prepareEnvironment: async () => {
return {
ready: true,
outputs: { foo: "bar" },
status: {
ready: true,
outputs: { foo: "bar" },
},
}
},
},
Expand Down
4 changes: 2 additions & 2 deletions core/test/unit/src/outputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ describe("resolveProjectOutputs", () => {
const plugin = createGardenPlugin({
name: "test",
handlers: {
async getEnvironmentStatus() {
return { ready: true, outputs: { test: "test-value" } }
async prepareEnvironment() {
return { status: { ready: true, outputs: { test: "test-value" } } }
},
},
})
Expand Down
1 change: 0 additions & 1 deletion core/test/unit/src/router/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ describe("provider actions", async () => {
log,
pluginName: "test-plugin-a",
force: false,
status: { ready: true, outputs: {} },
events: undefined,
})
expect(result).to.eql({
Expand Down
7 changes: 7 additions & 0 deletions plugins/terraform/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ export const prepareEnvironment: ProviderHandlers["prepareEnvironment"] = async
return { status: { ready: true, outputs: {} } }
}

const envStatus = await getEnvironmentStatus({ ctx, log })
if (envStatus.ready) {
return {
status: envStatus,
}
}

const root = getRoot(ctx, provider)
const workspace = provider.config.workspace || null

Expand Down

0 comments on commit 53c8184

Please sign in to comment.