Skip to content

Commit

Permalink
refactor(core): fold push task into build task
Browse files Browse the repository at this point in the history
The push workflow doesn't really need to be separate from builds in
the stack graph, since there is generally a 1:1 dependency relationship
between the two, and pushing to clusters isn't needed as a discrete
step.
  • Loading branch information
edvald authored and thsig committed May 6, 2019
1 parent 9ec4e7e commit 733e2db
Show file tree
Hide file tree
Showing 24 changed files with 160 additions and 339 deletions.
10 changes: 0 additions & 10 deletions garden-service/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
GetSecretResult,
GetServiceLogsResult,
ModuleActionOutputs,
PushResult,
RunResult,
ServiceActionOutputs,
SetSecretResult,
Expand All @@ -57,7 +56,6 @@ import {
PluginActionParams,
PluginActionParamsBase,
PluginServiceActionParamsBase,
PushModuleParams,
HotReloadServiceParams,
RunModuleParams,
RunServiceParams,
Expand Down Expand Up @@ -275,10 +273,6 @@ export class ActionHelper implements TypeGuard {
return this.callModuleHandler({ params, actionType: "build" })
}

async pushModule<T extends Module>(params: ModuleActionHelperParams<PushModuleParams<T>>): Promise<PushResult> {
return this.callModuleHandler({ params, actionType: "pushModule", defaultHandler: dummyPushHandler })
}

async publishModule<T extends Module>(
params: ModuleActionHelperParams<PublishModuleParams<T>>,
): Promise<PublishResult> {
Expand Down Expand Up @@ -687,10 +681,6 @@ const dummyLogStreamer = async ({ service, log }: GetServiceLogsParams) => {
return {}
}

const dummyPushHandler = async () => {
return { pushed: false }
}

const dummyPublishHandler = async ({ module }) => {
return {
message: chalk.yellow(`No publish handler available for module type ${module.type}`),
Expand Down
6 changes: 3 additions & 3 deletions garden-service/src/commands/run/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import { printRuntimeContext, runtimeContextForServiceDeps } from "./run"
import dedent = require("dedent")
import { logHeader } from "../../logger/util"
import { PushTask } from "../../tasks/push"
import { BuildTask } from "../../tasks/build"

const runArgs = {
module: new StringParameter({
Expand Down Expand Up @@ -85,8 +85,8 @@ export class RunModuleCommand extends Command<Args, Opts> {

await garden.actions.prepareEnvironment({ log })

const pushTask = new PushTask({ garden, log, module, force: opts["force-build"] })
await garden.processTasks([pushTask])
const buildTask = new BuildTask({ garden, log, module, force: opts["force-build"] })
await garden.processTasks([buildTask])

const command = args.command || []

Expand Down
6 changes: 3 additions & 3 deletions garden-service/src/commands/run/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { printRuntimeContext, runtimeContextForServiceDeps } from "./run"
import dedent = require("dedent")
import { logHeader } from "../../logger/util"
import { PushTask } from "../../tasks/push"
import { BuildTask } from "../../tasks/build"

const runArgs = {
service: new StringParameter({
Expand Down Expand Up @@ -66,8 +66,8 @@ export class RunServiceCommand extends Command<Args, Opts> {

await garden.actions.prepareEnvironment({ log })

const pushTask = new PushTask({ garden, log, module, force: opts["force-build"] })
await garden.processTasks([pushTask])
const buildTask = new BuildTask({ garden, log, module, force: opts["force-build"] })
await garden.processTasks([buildTask])

const runtimeContext = await runtimeContextForServiceDeps(garden, graph, module)
printRuntimeContext(log, runtimeContext)
Expand Down
6 changes: 3 additions & 3 deletions garden-service/src/commands/run/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { printRuntimeContext } from "./run"
import dedent = require("dedent")
import { prepareRuntimeContext } from "../../types/service"
import { logHeader } from "../../logger/util"
import { PushTask } from "../../tasks/push"
import { BuildTask } from "../../tasks/build"
import { getTestVersion } from "../../tasks/test"

const runArgs = {
Expand Down Expand Up @@ -92,8 +92,8 @@ export class RunTestCommand extends Command<Args, Opts> {

await garden.actions.prepareEnvironment({ log })

const pushTask = new PushTask({ garden, log, module, force: opts["force-build"] })
await garden.processTasks([pushTask])
const buildTask = new BuildTask({ garden, log, module, force: opts["force-build"] })
await garden.processTasks([buildTask])

const interactive = opts.interactive
const deps = await graph.getDependencies("test", testConfig.name, false)
Expand Down
7 changes: 2 additions & 5 deletions garden-service/src/config-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ import { makeTestTaskName } from "./tasks/helpers"
import { TaskType, makeBaseKey } from "./tasks/base"

// Each of these types corresponds to a Task class (e.g. BuildTask, DeployTask, ...).
export type DependencyGraphNodeType = "build" | "service" | "task" | "test"
| "push" | "publish" // these two types are currently not represented in the graph
export type DependencyGraphNodeType = "build" | "service" | "task" | "test" | "publish"

// The primary output type (for dependencies and dependants).
export type DependencyRelations = {
Expand All @@ -48,7 +47,7 @@ export type DependencyRelationFilterFn = (DependencyGraphNode) => boolean
// Output types for rendering/logging
export type RenderedGraph = { nodes: RenderedNode[], relationships: RenderedEdge[] }
export type RenderedEdge = { dependant: RenderedNode, dependency: RenderedNode }
export type RenderedNodeType = "build" | "deploy" | "run" | "test" | "push" | "publish"
export type RenderedNodeType = "build" | "deploy" | "run" | "test" | "publish"
export interface RenderedNode {
type: RenderedNodeType
name: string
Expand Down Expand Up @@ -512,7 +511,6 @@ const renderedNodeTypeMap: RenderedNodeTypeMap = {
service: "deploy",
task: "run",
test: "test",
push: "push",
publish: "publish",
}

Expand All @@ -521,7 +519,6 @@ const depNodeTaskTypeMap: DepNodeTaskTypeMap = {
service: "deploy",
task: "task",
test: "test",
push: "push",
publish: "publish",
}

Expand Down
48 changes: 48 additions & 0 deletions garden-service/src/plugins/kubernetes/container/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2018 Garden Technologies, Inc. <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { BuildModuleParams, GetBuildStatusParams } from "../../../types/plugin/params"
import { ContainerModule } from "../../container/config"
import { containerHelpers } from "../../container/helpers"
import { buildContainerModule, getContainerBuildStatus } from "../../container/build"

export async function getBuildStatus(params: GetBuildStatusParams<ContainerModule>) {
const status = await getContainerBuildStatus(params)

const { ctx } = params

if (ctx.provider.config.deploymentRegistry) {
// TODO: Check if the image exists in the remote registry
}

return status
}

export async function buildModule(params: BuildModuleParams<ContainerModule>) {
const buildResult = await buildContainerModule(params)

const { ctx, module, log } = params

if (!ctx.provider.config.deploymentRegistry) {
return buildResult
}

if (!(await containerHelpers.hasDockerfile(module))) {
return buildResult
}

const localId = await containerHelpers.getLocalImageId(module)
const remoteId = await containerHelpers.getDeploymentImageId(module, ctx.provider.config.deploymentRegistry)

log.setState({ msg: `Pushing image ${remoteId} to cluster...` })

await containerHelpers.dockerCli(module, ["tag", localId, remoteId])
await containerHelpers.dockerCli(module, ["push", remoteId])

return buildResult
}
22 changes: 1 addition & 21 deletions garden-service/src/plugins/kubernetes/container/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { extend, keyBy, set, toPairs } from "lodash"
import { DeployServiceParams, PushModuleParams, DeleteServiceParams } from "../../../types/plugin/params"
import { DeployServiceParams, DeleteServiceParams } from "../../../types/plugin/params"
import { RuntimeContext, Service, ServiceStatus } from "../../../types/service"
import { ContainerModule, ContainerService } from "../../container/config"
import { createIngressResources } from "./ingress"
Expand Down Expand Up @@ -421,23 +421,3 @@ export async function deleteContainerDeployment(
found ? log.setSuccess("Service deleted") : log.setWarn("Service not deployed")
}
}

export async function pushModule({ ctx, module, log }: PushModuleParams<ContainerModule>) {
if (!ctx.provider.config.deploymentRegistry) {
return { pushed: false }
}

if (!(await containerHelpers.hasDockerfile(module))) {
return { pushed: false }
}

const localId = await containerHelpers.getLocalImageId(module)
const remoteId = await containerHelpers.getDeploymentImageId(module, ctx.provider.config.deploymentRegistry)

log.setState({ msg: `Pushing image ${remoteId}...` })

await containerHelpers.dockerCli(module, ["tag", localId, remoteId])
await containerHelpers.dockerCli(module, ["push", remoteId])

return { pushed: true, message: `Pushed ${remoteId}` }
}
3 changes: 1 addition & 2 deletions garden-service/src/plugins/kubernetes/container/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { deployContainerService, deleteService, pushModule } from "./deployment"
import { deployContainerService, deleteService } from "./deployment"
import { hotReloadContainer } from "../hot-reload"
import { getServiceLogs } from "./logs"
import { execInService, runContainerModule, runContainerService, runContainerTask } from "./run"
Expand Down Expand Up @@ -44,7 +44,6 @@ export const containerHandlers = {
getServiceStatus: getContainerServiceStatus,
getTestResult,
hotReloadService: hotReloadContainer,
pushModule,
runModule: runContainerModule,
runService: runContainerService,
runTask: runContainerTask,
Expand Down
2 changes: 1 addition & 1 deletion garden-service/src/tasks/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Garden } from "../garden"
import { DependencyGraphNodeType } from "../config-graph"
import { LogEntry } from "../logger/log-entry"

export type TaskType = "build" | "deploy" | "publish" | "hot-reload" | "push" | "task" | "test"
export type TaskType = "build" | "deploy" | "publish" | "hot-reload" | "task" | "test"

export class TaskDefinitionError extends Error { }

Expand Down
3 changes: 1 addition & 2 deletions garden-service/src/tasks/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { BaseTask, TaskType } from "../tasks/base"
import { Garden } from "../garden"
import { DependencyGraphNodeType } from "../config-graph"
import { LogEntry } from "../logger/log-entry"
import { PushTask } from "./push"

export interface BuildTaskParams {
garden: Garden
Expand Down Expand Up @@ -45,7 +44,7 @@ export class BuildTask extends BaseTask {
const deps = (await dg.getDependencies(this.depType, this.getName(), false)).build

return Bluebird.map(deps, async (m: Module) => {
return new PushTask({
return new BuildTask({
garden: this.garden,
log: this.log,
module: m,
Expand Down
6 changes: 3 additions & 3 deletions garden-service/src/tasks/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import { LogEntry } from "../logger/log-entry"
import { BaseTask, TaskType } from "./base"
import { Service, ServiceStatus, getServiceRuntimeContext, getIngressUrl } from "../types/service"
import { Garden } from "../garden"
import { PushTask } from "./push"
import { TaskTask } from "./task"
import { DependencyGraphNodeType, ConfigGraph } from "../config-graph"
import { BuildTask } from "./build"

export interface DeployTaskParams {
garden: Garden
Expand Down Expand Up @@ -83,7 +83,7 @@ export class DeployTask extends BaseTask {
})
})

const pushTask = new PushTask({
const buildTask = new BuildTask({
garden: this.garden,
log: this.log,
module: this.service.module,
Expand All @@ -92,7 +92,7 @@ export class DeployTask extends BaseTask {
hotReloadServiceNames: this.hotReloadServiceNames,
})

return [...deployTasks, ...taskTasks, pushTask]
return [...deployTasks, ...taskTasks, buildTask]
}
}

Expand Down
14 changes: 7 additions & 7 deletions garden-service/src/tasks/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

import { intersection, uniq } from "lodash"
import { DeployTask } from "./deploy"
import { PushTask } from "./push"
import { Garden } from "../garden"
import { Module } from "../types/module"
import { Service } from "../types/service"
import { DependencyGraphNode, ConfigGraph } from "../config-graph"
import { LogEntry } from "../logger/log-entry"
import { BaseTask } from "./base"
import { BuildTask } from "./build"

export async function getDependantTasksForModule(
{ garden, log, graph, module, hotReloadServiceNames, force = false, forceBuild = false,
Expand All @@ -32,12 +32,12 @@ export async function getDependantTasksForModule(
},
): Promise<BaseTask[]> {

let pushTasks: PushTask[] = []
let buildTasks: BuildTask[] = []
let dependantBuildModules: Module[] = []
let services: Service[] = []

if (!includeDependants) {
pushTasks.push(new PushTask({ garden, log, module, force: forceBuild, fromWatch, hotReloadServiceNames }))
buildTasks.push(new BuildTask({ garden, log, module, force: forceBuild, fromWatch, hotReloadServiceNames }))
services = await graph.getServices(module.serviceNames)
} else {
const hotReloadModuleNames = await getModuleNames(graph, hotReloadServiceNames)
Expand All @@ -54,14 +54,14 @@ export async function getDependantTasksForModule(
} else {
const dependants = await graph.getDependantsForModule(module, dependantFilterFn)

pushTasks.push(new PushTask({ garden, log, module, force: true, fromWatch, hotReloadServiceNames }))
buildTasks.push(new BuildTask({ garden, log, module, force: true, fromWatch, hotReloadServiceNames }))
dependantBuildModules = dependants.build
services = (await graph.getServices(module.serviceNames)).concat(dependants.service)
}
}

pushTasks.push(...dependantBuildModules
.map(m => new PushTask({ garden, log, module: m, force: forceBuild, fromWatch, hotReloadServiceNames })))
buildTasks.push(...dependantBuildModules
.map(m => new BuildTask({ garden, log, module: m, force: forceBuild, fromWatch, hotReloadServiceNames })))

const deployTasks = services
.map(service => new DeployTask({
Expand All @@ -74,7 +74,7 @@ export async function getDependantTasksForModule(
fromWatch, hotReloadServiceNames,
}))

const outputTasks = [...pushTasks, ...deployTasks]
const outputTasks = [...buildTasks, ...deployTasks]
log.silly(`getDependantTasksForModule called for module ${module.name}, returning the following tasks:`)
log.silly(` ${outputTasks.map(t => t.getKey()).join(", ")}`)

Expand Down
Loading

0 comments on commit 733e2db

Please sign in to comment.