Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cloud): emit ns statuses for in-cluster builds #4628

Merged
merged 3 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions core/src/config/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { namespaceStatusesSchema } from "../types/namespace"
import { createSchema, joi, joiVariables } from "./common"

export const environmentStatusSchema = createSchema({
Expand All @@ -19,7 +18,6 @@ export const environmentStatusSchema = createSchema({
.optional()
.meta({ extendable: true })
.description("Use this to include additional information that is specific to the provider."),
namespaceStatuses: namespaceStatusesSchema().optional(),
outputs: joiVariables()
.meta({ extendable: true })
.description("Output variables that modules and other variables can reference."),
Expand Down
1 change: 0 additions & 1 deletion core/src/events/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ export function makeActionCompletePayload<
deployStatus = pick(deployResult.detail, [
"createdAt",
"mode",
"namespaceStatuses",
"externalId",
"externalVersion",
"forwardablePorts",
Expand Down
2 changes: 0 additions & 2 deletions core/src/graph/results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ function filterResultForExport(result: any) {
"mode",
"syncMode",
"localMode",
"namespaceStatuses",
"externalId",
"externalVersion",
"forwardablePorts",
Expand All @@ -219,7 +218,6 @@ function filterResultForExport(result: any) {
"exitCode",
"startedAt",
"completedAt",
"namespaceStatus"
),
detail: filteredDetail,
}
Expand Down
4 changes: 3 additions & 1 deletion core/src/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { EventEmitter } from "eventemitter3"
import { CreateEventLogParams, EventLogger, LogLevel, StringLogLevel } from "./logger/logger"
import { Memoize } from "typescript-memoize"
import type { ParameterValues } from "./cli/params"
import { NamespaceStatus } from "./types/namespace"

type WrappedFromGarden = Pick<
Garden,
Expand Down Expand Up @@ -116,13 +117,14 @@ export type PluginEventLogMessage = PluginEventLogContext & {
msg: string
}

// Define your emitter's types like that:
// Define your emitter's types as follows:
// Key: Event name; Value: Listener function signature
type PluginEvents = {
abort: (reason?: string) => void
done: () => void
failed: (error?: Error) => void
log: (msg: PluginEventLogMessage) => void
namespaceStatus: (status: NamespaceStatus) => void
}

type PluginEventType = keyof PluginEvents
Expand Down
3 changes: 1 addition & 2 deletions core/src/plugin/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type { BuildAction } from "../actions/build"
import type { DeployAction } from "../actions/deploy"
import type { RunAction } from "../actions/run"
import type { TestAction } from "../actions/test"
import { NamespaceStatus, namespaceStatusSchema } from "../types/namespace"
import { NamespaceStatus } from "../types/namespace"
import Joi from "@hapi/joi"
import { memoize } from "lodash"
import { BaseProviderConfig } from "../config/provider"
Expand Down Expand Up @@ -131,7 +131,6 @@ export const runResultSchema = createSchema({
startedAt: joi.date().required().description("When the module run was started."),
completedAt: joi.date().required().description("When the module run was completed."),
log: joi.string().allow("").default("").description("The output log from the run."),
namespaceStatus: namespaceStatusSchema().optional(),
}),
allowUnknown: true,
})
Expand Down
7 changes: 2 additions & 5 deletions core/src/plugin/handlers/Provider/cleanupEnvironment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@
import { PluginActionParamsBase, projectActionParamsSchema } from "../../base"
import { dedent } from "../../../util/string"
import { joi } from "../../../config/common"
import { NamespaceStatus, namespaceStatusesSchema } from "../../../types/namespace"
import type { BaseProviderConfig } from "../../../config/provider"

export interface CleanupEnvironmentParams<C extends BaseProviderConfig = any> extends PluginActionParamsBase<C> {}

export interface CleanupEnvironmentResult {
namespaceStatuses?: NamespaceStatus[]
}
export interface CleanupEnvironmentResult {}

export const cleanupEnvironment = () => ({
description: dedent`
Expand All @@ -28,5 +25,5 @@ export const cleanupEnvironment = () => ({
Called by the \`garden delete environment\` command.
`,
paramsSchema: projectActionParamsSchema(),
resultSchema: joi.object().keys({ namespaceStatuses: namespaceStatusesSchema().optional() }),
resultSchema: joi.object(),
})
2 changes: 0 additions & 2 deletions core/src/plugin/handlers/Provider/getEnvironmentStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@
import { PluginActionParamsBase, projectActionParamsSchema } from "../../base"
import { dedent } from "../../../util/string"
import { environmentStatusSchema } from "../../../config/status"
import type { NamespaceStatus } from "../../../types/namespace"
import type { BaseProviderConfig } from "../../../config/provider"

export interface GetEnvironmentStatusParams<C extends BaseProviderConfig = any> extends PluginActionParamsBase<C> {}

export interface EnvironmentStatus<O extends {} = any, D extends {} = any> {
ready: boolean
detail?: D
namespaceStatuses?: NamespaceStatus[]
outputs: O
disableCache?: boolean
cached?: boolean
Expand Down
6 changes: 3 additions & 3 deletions core/src/plugins/kubernetes/container/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { createIngressResources } from "./ingress"
import { createServiceResources } from "./service"
import { waitForResources } from "../status/status"
import { apply, deleteObjectsBySelector, deleteResourceKeys, KUBECTL_DEFAULT_TIMEOUT } from "../kubectl"
import { getAppNamespace, getAppNamespaceStatus } from "../namespace"
import { getAppNamespace, getNamespaceStatus } from "../namespace"
import { PluginContext } from "../../../plugin-context"
import { KubeApi } from "../api"
import { KubernetesPluginContext, KubernetesProvider } from "../config"
Expand Down Expand Up @@ -60,7 +60,7 @@ export const k8sContainerDeploy: DeployActionHandler<"deploy", ContainerDeployAc
const status = await k8sGetContainerDeployStatus(params)
const specChangedResourceKeys: string[] = status.detail?.detail.selectorChangedResourceKeys || []
if (specChangedResourceKeys.length > 0) {
const namespaceStatus = await getAppNamespaceStatus(k8sCtx, log, k8sCtx.provider)
const namespaceStatus = await getNamespaceStatus({ ctx: k8sCtx, log, provider: k8sCtx.provider })
await handleChangedSelector({
action,
specChangedResourceKeys,
Expand Down Expand Up @@ -135,7 +135,7 @@ export const deployContainerServiceRolling = async (
const { ctx, api, action, log, imageId } = params
const k8sCtx = <KubernetesPluginContext>ctx

const namespaceStatus = await getAppNamespaceStatus(k8sCtx, log, k8sCtx.provider)
const namespaceStatus = await getNamespaceStatus({ ctx: k8sCtx, log, provider: k8sCtx.provider })
const namespace = namespaceStatus.namespaceName

const { manifests } = await createContainerManifests({
Expand Down
4 changes: 2 additions & 2 deletions core/src/plugins/kubernetes/container/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { runAndCopy } from "../run"
import { KubernetesPluginContext } from "../config"
import { storeRunResult } from "../run-results"
import { makePodName } from "../util"
import { getAppNamespaceStatus } from "../namespace"
import { getNamespaceStatus } from "../namespace"
import { RunActionHandler } from "../../../plugin/action-types"
import { getDeployedImageId } from "./util"
import { runResultToActionState } from "../../../actions/base"
Expand All @@ -35,7 +35,7 @@ export const k8sContainerRun: RunActionHandler<"run", ContainerRunAction> = asyn
const timeout = action.getConfig("timeout")
const k8sCtx = ctx as KubernetesPluginContext
const image = getDeployedImageId(action, k8sCtx.provider)
const namespaceStatus = await getAppNamespaceStatus(k8sCtx, log, k8sCtx.provider)
const namespaceStatus = await getNamespaceStatus({ ctx: k8sCtx, log, provider: k8sCtx.provider })

const runResult = await runAndCopy({
...params,
Expand Down
9 changes: 2 additions & 7 deletions core/src/plugins/kubernetes/container/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ import { ContainerDeployAction, ContainerDeployOutputs } from "../../container/m
import { KubeApi } from "../api"
import { compareDeployedResources } from "../status/status"
import { getIngresses } from "./ingress"
import { getAppNamespaceStatus } from "../namespace"
import { getNamespaceStatus } from "../namespace"
import { KubernetesPluginContext } from "../config"
import { KubernetesServerResource, KubernetesWorkload } from "../types"
import { DeployActionHandler } from "../../../plugin/action-types"
import { getDeployedImageId } from "./util"
import { ActionMode, Resolved } from "../../../actions/types"
import { deployStateToActionState, DeployStatus } from "../../../plugin/handlers/Deploy/get-status"
import { NamespaceStatus } from "../../../types/namespace"

interface ContainerStatusDetail {
remoteResources: KubernetesServerResource[]
Expand All @@ -36,7 +35,7 @@ export const k8sGetContainerDeployStatus: DeployActionHandler<"getStatus", Conta
// TODO: hash and compare all the configuration files (otherwise internal changes don't get deployed)
const provider = k8sCtx.provider
const api = await KubeApi.factory(log, ctx, provider)
const namespaceStatus = await getAppNamespaceStatus(k8sCtx, log, k8sCtx.provider)
const namespaceStatus = await getNamespaceStatus({ ctx: k8sCtx, log, provider: k8sCtx.provider })
const namespace = namespaceStatus.namespaceName
const imageId = getDeployedImageId(action, provider)

Expand Down Expand Up @@ -65,7 +64,6 @@ export const k8sGetContainerDeployStatus: DeployActionHandler<"getStatus", Conta
workload,
selectorChangedResourceKeys,
state,
namespaceStatus,
ingresses,
})
}
Expand All @@ -78,7 +76,6 @@ export function prepareContainerDeployStatus({
workload,
selectorChangedResourceKeys,
state,
namespaceStatus,
ingresses,
}: {
action: Resolved<ContainerDeployAction>
Expand All @@ -88,7 +85,6 @@ export function prepareContainerDeployStatus({
workload: KubernetesWorkload
selectorChangedResourceKeys: string[]
state: DeployState
namespaceStatus: NamespaceStatus
ingresses: ServiceIngress[] | undefined
}): DeployStatus<ContainerDeployAction> {
// Local mode has its own port-forwarding configuration
Expand All @@ -114,7 +110,6 @@ export function prepareContainerDeployStatus({
forwardablePorts,
ingresses,
state,
namespaceStatuses: [namespaceStatus],
detail: { remoteResources, workload, selectorChangedResourceKeys },
mode: deployedMode,
outputs,
Expand Down
4 changes: 2 additions & 2 deletions core/src/plugins/kubernetes/container/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { ContainerTestAction } from "../../container/moduleConfig"
import { storeTestResult } from "../test-results"
import { runAndCopy } from "../run"
import { makePodName } from "../util"
import { getAppNamespaceStatus } from "../namespace"
import { getNamespaceStatus } from "../namespace"
import { KubernetesPluginContext } from "../config"
import { TestActionHandler } from "../../../plugin/action-types"
import { getDeployedImageId } from "./util"
Expand All @@ -34,7 +34,7 @@ export const k8sContainerTest: TestActionHandler<"run", ContainerTestAction> = a
const k8sCtx = ctx as KubernetesPluginContext

const image = getDeployedImageId(action, k8sCtx.provider)
const namespaceStatus = await getAppNamespaceStatus(k8sCtx, log, k8sCtx.provider)
const namespaceStatus = await getNamespaceStatus({ ctx: k8sCtx, log, provider: k8sCtx.provider })

const res = await runAndCopy({
...params,
Expand Down
1 change: 0 additions & 1 deletion core/src/plugins/kubernetes/helm/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ export const helmDeploy: DeployActionHandler<"deploy", HelmDeployAction> = async
state: "ready",
version: action.versionString(),
detail: { remoteResources: statuses.map((s) => s.resource) },
namespaceStatuses: [namespaceStatus],
},
attached,
// TODO-0.13.1
Expand Down
1 change: 0 additions & 1 deletion core/src/plugins/kubernetes/helm/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ export const getHelmDeployStatus: DeployActionHandler<"getStatus", HelmDeployAct
version: state === "ready" ? action.versionString() : undefined,
detail,
mode: deployedMode,
namespaceStatuses: [namespaceStatus],
ingresses,
},
// TODO-0.13.1
Expand Down
12 changes: 7 additions & 5 deletions core/src/plugins/kubernetes/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
prepareNamespaces,
deleteNamespaces,
getSystemNamespace,
getAppNamespaceStatus,
getNamespaceStatus,
clearNamespaceCache,
} from "./namespace"
import { KubernetesPluginContext, KubernetesConfig, KubernetesProvider, ProviderSecretRef } from "./config"
Expand Down Expand Up @@ -187,9 +187,9 @@ export async function prepareEnvironment(

// Prepare system services
await prepareSystem({ ...params, clusterInit: false })
const ns = await getAppNamespaceStatus(k8sCtx, log, k8sCtx.provider)

return { status: { namespaceStatuses: [ns], ready: true, outputs: status.outputs } }
const nsStatus = await getNamespaceStatus({ ctx: k8sCtx, log, provider: k8sCtx.provider })
ctx.events.emit("namespaceStatus", nsStatus)
vvagaytsev marked this conversation as resolved.
Show resolved Hide resolved
return { status: { ready: true, outputs: status.outputs } }
}

export async function prepareSystem({
Expand Down Expand Up @@ -337,7 +337,9 @@ export async function cleanupEnvironment({
// Since we've deleted one or more namespaces, we invalidate the NS cache for this provider instance.
clearNamespaceCache(provider)

return { namespaceStatuses: [{ namespaceName: namespace, state: "missing", pluginName: provider.name }] }
ctx.events.emit("namespaceStatus", { namespaceName: namespace, state: "missing", pluginName: provider.name })

return {}
}

export function getKubernetesSystemVariables(config: KubernetesConfig) {
Expand Down
38 changes: 16 additions & 22 deletions core/src/plugins/kubernetes/kubernetes-type/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import Bluebird from "bluebird"
import { isEmpty, omit, partition, uniq } from "lodash"
import type { NamespaceStatus } from "../../../types/namespace"
import type { ModuleActionHandlers } from "../../../plugin/plugin"
import { ServiceStatus } from "../../../types/service"
import { gardenAnnotationKey } from "../../../util/string"
Expand Down Expand Up @@ -210,7 +209,6 @@ export const getKubernetesDeployStatus: DeployActionHandler<"getStatus", Kuberne
version: state === "ready" ? action.versionString() : undefined,
detail: { remoteResources },
mode: deployedMode,
namespaceStatuses: [namespaceStatus],
ingresses: getK8sIngresses(remoteResources),
},
// TODO-0.13.1
Expand Down Expand Up @@ -310,27 +308,21 @@ export const kubernetesDeploy: DeployActionHandler<"deploy", KubernetesDeployAct
}
}

const namespaceStatuses = [namespaceStatus]
ctx.events.emit("namespaceStatus", namespaceStatus)

if (namespaceManifests.length > 0) {
namespaceStatuses.push(
...namespaceManifests.map(
(m) =>
({
pluginName: provider.name,
namespaceName: m.metadata.name,
state: "ready",
} as NamespaceStatus)
)
)
for (const ns of namespaceManifests) {
ctx.events.emit("namespaceStatus", {
pluginName: provider.name,
namespaceName: ns.metadata.name,
state: "ready",
})
}
}

return {
...status,
detail: {
...status.detail!,
namespaceStatuses,
},
detail: status.detail!,
// Tell the framework that the mutagen process is attached, if applicable
attached,
}
Expand Down Expand Up @@ -386,11 +378,13 @@ export const deleteKubernetesDeploy: DeployActionHandler<"delete", KubernetesDep
const status: KubernetesServiceStatus = { state: "missing", detail: { remoteResources: [] } }

if (namespaceManifests.length > 0) {
status.namespaceStatuses = namespaceManifests.map((m) => ({
namespaceName: m.metadata.name,
state: "missing",
pluginName: provider.name,
}))
for (const ns of namespaceManifests) {
ctx.events.emit("namespaceStatus", {
namespaceName: ns.metadata.name,
state: "missing",
pluginName: provider.name,
})
}
}

return {
Expand Down
Loading