Skip to content

Commit

Permalink
fix: 0.13 export the status state-field consistently across action ty…
Browse files Browse the repository at this point in the history
…pes (#4073)

Update exported api to contain the detailed action status

---------

Co-authored-by: Vladimir Vagaytsev <[email protected]>
  • Loading branch information
mkhq and vvagaytsev authored Apr 20, 2023
1 parent b399d08 commit 0e8fea7
Show file tree
Hide file tree
Showing 30 changed files with 99 additions and 103 deletions.
2 changes: 1 addition & 1 deletion core/src/commands/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class ExecCommand extends Command<Args, Opts> {
const executed = await executeAction({ garden, graph, action, log, statusOnly: true })
const status: DeployStatus = executed.getStatus()

const deployState = status.detail?.deployState
const deployState = status.detail?.state
switch (deployState) {
// Warn if the deployment is not ready yet or unhealthy, but still proceed.
case undefined:
Expand Down
2 changes: 1 addition & 1 deletion core/src/commands/sync/sync-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export class SyncStartCommand extends Command<Args, Opts> {
const result = statusResult.results.getResult(task)

const mode = result?.result?.detail?.mode
const state = result?.result?.detail?.deployState
const state = result?.result?.detail?.state
const executedAction = result?.result?.executedAction

if (executedAction && (state === "outdated" || state === "ready")) {
Expand Down
8 changes: 5 additions & 3 deletions core/src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import { omit } from "lodash"
import { EventEmitter2 } from "eventemitter2"
import type { LogEntryEventPayload } from "./cloud/buffered-event-stream"
import type { ServiceStatus } from "./types/service"
import type { RunStatusForEventPayload } from "./plugin/base"
import type { DeployState, ServiceStatus } from "./types/service"
import type { RunState, RunStatusForEventPayload } from "./plugin/base"
import type { Omit } from "./util/util"
import type { AuthTokenResponse } from "./cloud/api"
import type { RenderedActionGraph } from "./graph/config-graph"
Expand Down Expand Up @@ -102,7 +102,9 @@ export function toGraphResultEventPayload(result: GraphResult): GraphResultEvent
return payload
}

export interface ActionStatusPayload<S = {}> {
export type ActionStatusDetailedState = DeployState | BuildState | RunState

export interface ActionStatusPayload<S = { state: ActionStatusDetailedState }> {
actionName: string
actionVersion: string
actionUid: string
Expand Down
14 changes: 7 additions & 7 deletions core/src/plugins/exec/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const getExecDeployStatus: DeployActionHandler<"getStatus", ExecDeploy> =
return {
state: deployStateToActionState(state),
detail: {
deployState: state,
state,
version: action.versionString(),
detail: { statusCommandOutput: result.all },
},
Expand All @@ -65,7 +65,7 @@ export const getExecDeployStatus: DeployActionHandler<"getStatus", ExecDeploy> =

return {
state: deployStateToActionState(state),
detail: { deployState: state, version: action.versionString(), detail: {} },
detail: { state, version: action.versionString(), detail: {} },
outputs: {
log: "",
},
Expand Down Expand Up @@ -101,7 +101,7 @@ export const execDeployAction: DeployActionHandler<"deploy", ExecDeploy> = async

if (spec.deployCommand.length === 0) {
log.info({ msg: "No deploy command found. Skipping.", symbol: "info" })
return { state: "ready", detail: { deployState: "ready", detail: { skipped: true } }, outputs: {} }
return { state: "ready", detail: { state: "ready", detail: { skipped: true } }, outputs: {} }
} else if (spec.persistent) {
return deployPersistentExecService({ action, log, ctx, env, deployName: action.name })
} else {
Expand All @@ -122,7 +122,7 @@ export const execDeployAction: DeployActionHandler<"deploy", ExecDeploy> = async

return {
state: "ready",
detail: { deployState: "ready", detail: { deployCommandOutput: result.all } },
detail: { state: "ready", detail: { deployCommandOutput: result.all } },
outputs: {},
}
}
Expand Down Expand Up @@ -231,7 +231,7 @@ export async function deployPersistentExecService({

return {
state: "ready",
detail: { deployState: "ready", detail: { persistent: true, pid: proc.pid } },
detail: { state: "ready", detail: { persistent: true, pid: proc.pid } },
outputs: {},
}
}
Expand All @@ -255,7 +255,7 @@ export const deleteExecDeploy: DeployActionHandler<"delete", ExecDeploy> = async

return {
state: "not-ready",
detail: { deployState: "missing", detail: { cleanupCommandOutput: result.all } },
detail: { state: "missing", detail: { cleanupCommandOutput: result.all } },
outputs: {},
}
} else {
Expand All @@ -264,7 +264,7 @@ export const deleteExecDeploy: DeployActionHandler<"delete", ExecDeploy> = async
symbol: "warning",
msg: chalk.gray(`Missing cleanupCommand, unable to clean up service`),
})
return { state: "unknown", detail: { deployState: "unknown", detail: {} }, outputs: {} }
return { state: "unknown", detail: { state: "unknown", detail: {} }, outputs: {} }
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/plugins/kubernetes/container/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ export const deleteContainerDeploy: DeployActionHandler<"delete", ContainerDeplo
includeUninitialized: false,
})

return { state: "ready", detail: { deployState: "missing", detail: {} }, outputs: {} }
return { state: "ready", detail: { state: "missing", detail: {} }, outputs: {} }
}

/**
Expand Down
2 changes: 1 addition & 1 deletion core/src/plugins/kubernetes/container/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const execInContainer: DeployActionHandler<"exec", ContainerDeployAction>
const namespace = await getAppNamespace(k8sCtx, log, k8sCtx.provider)

// TODO: this check should probably live outside of the plugin
if (!status.detail?.detail.workload || !includes(["ready", "outdated"], status.detail?.deployState)) {
if (!status.detail?.detail.workload || !includes(["ready", "outdated"], status.detail?.state)) {
throw new DeploymentError(`${action.longDescription()} is not running`, {
name: action.name,
state: status.state,
Expand Down
4 changes: 2 additions & 2 deletions core/src/plugins/kubernetes/container/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export function prepareContainerDeployStatus({
const detail: ContainerServiceStatus = {
forwardablePorts,
ingresses,
deployState: state,
state,
namespaceStatuses: [namespaceStatus],
detail: { remoteResources, workload, selectorChangedResourceKeys },
mode: deployedMode,
Expand Down Expand Up @@ -150,7 +150,7 @@ export async function waitForContainerService(
action,
})

const deployState = status.detail?.deployState
const deployState = status.detail?.state

if (deployState === "ready" || deployState === "outdated") {
return
Expand Down
6 changes: 3 additions & 3 deletions core/src/plugins/kubernetes/helm/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const helmDeploy: DeployActionHandler<"deploy", HelmDeployAction> = async
commonArgs.push("--atomic")
}

if (releaseStatus.deployState === "missing") {
if (releaseStatus.state === "missing") {
log.silly(`Installing Helm release ${releaseName}`)
const installArgs = ["install", releaseName, ...reference, ...commonArgs]
if (force && !ctx.production) {
Expand Down Expand Up @@ -170,7 +170,7 @@ export const helmDeploy: DeployActionHandler<"deploy", HelmDeployAction> = async
state: "ready",
detail: {
forwardablePorts,
deployState: "ready",
state: "ready",
version: action.versionString(),
detail: { remoteResources: statuses.map((s) => s.resource) },
namespaceStatuses: [namespaceStatus],
Expand Down Expand Up @@ -204,5 +204,5 @@ export const deleteHelmDeploy: DeployActionHandler<"delete", HelmDeployAction> =

log.success("Service deleted")

return { state: "not-ready", outputs: {}, detail: { deployState: "missing", detail: { remoteResources: [] } } }
return { state: "not-ready", outputs: {}, detail: { state: "missing", detail: { remoteResources: [] } } }
}
4 changes: 2 additions & 2 deletions core/src/plugins/kubernetes/helm/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ export const execInHelmDeploy: DeployActionHandler<"exec", HelmDeployAction> = a
})

// TODO: this check should probably live outside of the plugin
if (!target || !includes(["ready", "outdated"], status.detail?.deployState)) {
if (!target || !includes(["ready", "outdated"], status.detail?.state)) {
throw new DeploymentError(`${action.longDescription()} is not running`, {
name: action.name,
state: status.detail?.deployState || status.state,
state: status.detail?.state || status.state,
})
}

Expand Down
8 changes: 4 additions & 4 deletions core/src/plugins/kubernetes/helm/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const getHelmDeployStatus: DeployActionHandler<"getStatus", HelmDeployAct

try {
helmStatus = await getReleaseStatus({ ctx: k8sCtx, action, releaseName, log })
state = helmStatus.deployState
state = helmStatus.state
deployedMode = helmStatus.detail.mode || "default"
} catch (err) {
state = "missing"
Expand Down Expand Up @@ -113,7 +113,7 @@ export const getHelmDeployStatus: DeployActionHandler<"getStatus", HelmDeployAct
state: deployStateToActionState(state),
detail: {
forwardablePorts,
deployState: state,
state,
version: state === "ready" ? action.versionString() : undefined,
detail,
mode: deployedMode,
Expand Down Expand Up @@ -241,12 +241,12 @@ export async function getReleaseStatus({
}

return {
deployState: state,
state,
detail: { ...res, values, mode: deployedMode },
}
} catch (err) {
if (err.message.includes("release: not found")) {
return { deployState: "missing", detail: {} }
return { state: "missing", detail: {} }
} else {
throw err
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/plugins/kubernetes/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export async function prepareSystem({
}

const deployStatuses: DeployStatusMap = (status.detail && status.detail.deployStatuses) || {}
const serviceStates = Object.values(deployStatuses).map((s) => s.detail?.deployState || "unknown")
const serviceStates = Object.values(deployStatuses).map((s) => s.detail?.state || "unknown")
const combinedState = combineStates(serviceStates)

const remoteCluster = provider.name !== "local-kubernetes"
Expand Down
2 changes: 1 addition & 1 deletion core/src/plugins/kubernetes/kubernetes-type/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const execInKubernetesDeploy: DeployActionHandler<"exec", KubernetesDeplo
})

// TODO: this check should probably live outside of the plugin
if (!target || !includes(["ready", "outdated"], status.detail?.deployState)) {
if (!target || !includes(["ready", "outdated"], status.detail?.state)) {
throw new DeploymentError(`${action.longDescription()} is not running`, {
name: action.name,
state: status.state,
Expand Down
16 changes: 7 additions & 9 deletions core/src/plugins/kubernetes/kubernetes-type/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,11 @@ export const getKubernetesDeployStatus: DeployActionHandler<"getStatus", Kuberne
})
const preparedManifests = prepareResult.manifests

let { state, remoteResources, mode: deployedMode } = await compareDeployedResources(
k8sCtx,
api,
namespace,
preparedManifests,
log
)
let {
state,
remoteResources,
mode: deployedMode,
} = await compareDeployedResources(k8sCtx, api, namespace, preparedManifests, log)

// Local mode has its own port-forwarding configuration
const forwardablePorts = deployedMode === "local" ? [] : getForwardablePorts(remoteResources, action)
Expand All @@ -206,7 +204,7 @@ export const getKubernetesDeployStatus: DeployActionHandler<"getStatus", Kuberne
state: deployStateToActionState(state),
detail: {
forwardablePorts,
deployState: state,
state,
version: state === "ready" ? action.versionString() : undefined,
detail: { remoteResources },
mode: deployedMode,
Expand Down Expand Up @@ -384,7 +382,7 @@ export const deleteKubernetesDeploy: DeployActionHandler<"delete", KubernetesDep
})
}

const status: KubernetesServiceStatus = { deployState: "missing", detail: { remoteResources: [] } }
const status: KubernetesServiceStatus = { state: "missing", detail: { remoteResources: [] } }

if (namespaceManifests.length > 0) {
status.namespaceStatuses = namespaceManifests.map((m) => ({
Expand Down
2 changes: 1 addition & 1 deletion core/src/plugins/kubernetes/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export async function getSystemServiceStatus({ sysGarden, log, names }: GetSyste
graph,
names,
})
const state = combineStates(Object.values(serviceStatuses).map((s) => s.detail?.deployState || "unknown"))
const state = combineStates(Object.values(serviceStatuses).map((s) => s.detail?.state || "unknown"))

return {
state,
Expand Down
8 changes: 4 additions & 4 deletions core/src/router/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const deployRouter = (baseParams: BaseRouterParams) =>
garden.events.emit("deployStatus", {
...payloadAttrs,
state: "processing",
status: { deployState: "deploying" },
status: { state: "deploying" },
})

const output = await router.callHandler({ params, handlerType: "deploy" })
Expand Down Expand Up @@ -90,7 +90,7 @@ export const deployRouter = (baseParams: BaseRouterParams) =>
const statusOutput = await handlers.getStatus({ ...params })
const status = statusOutput.result

if (status.detail?.deployState === "missing") {
if (status.detail?.state === "missing") {
log.success({
section: action.key(),
msg: "Not found",
Expand All @@ -106,7 +106,7 @@ export const deployRouter = (baseParams: BaseRouterParams) =>
p.log.error(msg)
return {
state: "not-ready" as ActionState,
detail: { deployState: "missing" as DeployState, detail: {} },
detail: { state: "missing" as DeployState, detail: {} },
outputs: {},
}
},
Expand Down Expand Up @@ -160,7 +160,7 @@ export const deployRouter = (baseParams: BaseRouterParams) =>
garden.events.emit("deployStatus", {
...payloadAttrs,
state: "getting-status",
status: { deployState: "unknown" },
status: { state: "unknown" },
})

const output = await router.callHandler({ params, handlerType: "getStatus" })
Expand Down
2 changes: 1 addition & 1 deletion core/src/tasks/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class DeployTask extends ExecuteActionTask<DeployAction, DeployStatus> {
log.info(chalk.green(`${action.longDescription()} is already deployed.`))
printIngresses(status, log)
} else {
const state = status.detail?.deployState || displayState(status.state)
const state = status.detail?.state || displayState(status.state)
log.info(chalk.green(`${action.longDescription()} is ${state}.`))
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/types/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export interface ServiceStatus<D = any, O = PrimitiveMap> {
lastError?: string
outputs?: O
runningReplicas?: number
deployState: DeployState
state: DeployState
updatedAt?: string
}

Expand Down Expand Up @@ -227,7 +227,7 @@ export const serviceStatusSchema = () =>
lastError: joi.string().description("Latest error status message of the service (if any)."),
outputs: joiVariables().description("A map of values output from the deployment."),
runningReplicas: joi.number().description("How many replicas of the service are currently running."),
deployState: joi
state: joi
.string()
.valid(...deployStates)
.default("unknown")
Expand Down
4 changes: 2 additions & 2 deletions core/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,11 @@ export const testPlugin = () =>
return { config, supportedModes: { sync: !!config.spec.syncMode, local: true } }
},
deploy: async ({}) => {
return { state: "ready", detail: { deployState: "ready", detail: {} }, outputs: {} }
return { state: "ready", detail: { state: "ready", detail: {} }, outputs: {} }
},
getStatus: async ({ ctx, action }) => {
const result = get(ctx.provider, ["_actionStatuses", action.kind, action.name])
return result || { state: "ready", detail: { deployState: "ready", detail: {} }, outputs: {} }
return result || { state: "ready", detail: { state: "ready", detail: {} }, outputs: {} }
},
exec: async ({ command }) => {
return { code: 0, output: "Ran command: " + command.join(" ") }
Expand Down
Loading

0 comments on commit 0e8fea7

Please sign in to comment.