Skip to content

Commit

Permalink
Merge pull request #301 from garden-io/fix-openfaas-builds
Browse files Browse the repository at this point in the history
fix OpenFaaS issues
  • Loading branch information
eysi09 authored Oct 9, 2018
2 parents bb59304 + b080d55 commit 53fefa6
Show file tree
Hide file tree
Showing 19 changed files with 175 additions and 73 deletions.
4 changes: 0 additions & 4 deletions examples/hello-world/services/hello-function/garden.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,3 @@ module:
name: hello-function
type: openfaas
lang: node
tests:
- name: unit
command: [npm, test]

31 changes: 27 additions & 4 deletions garden-service/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions garden-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"cli-cursor": "^2.1.0",
"cli-highlight": "^2.0.0",
"cli-truncate": "^1.1.0",
"cross-spawn": "^6.0.5",
"cryo": "0.0.6",
"dedent": "^0.7.0",
"deep-diff": "^1.0.1",
Expand Down Expand Up @@ -82,6 +83,7 @@
"@types/async-lock": "^1.1.0",
"@types/bluebird": "^3.5.23",
"@types/chai": "^4.1.4",
"@types/cross-spawn": "^6.0.0",
"@types/dedent": "^0.7.0",
"@types/deep-diff": "0.0.31",
"@types/dockerode": "^2.5.5",
Expand Down
23 changes: 17 additions & 6 deletions garden-service/src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Bluebird = require("bluebird")
import chalk from "chalk"
import { Garden } from "./garden"
import { PrimitiveMap } from "./config/common"
import { Module } from "./types/module"
import { Module, ModuleMap } from "./types/module"
import { ModuleActions, ServiceActions, PluginActions } from "./types/plugin/plugin"
import {
BuildResult,
Expand Down Expand Up @@ -90,10 +90,10 @@ export interface DeployServicesParams {
type ActionHelperParams<T extends PluginActionParamsBase> =
Omit<T, keyof PluginActionContextParams> & { pluginName?: string }
type ModuleActionHelperParams<T extends PluginModuleActionParamsBase> =
Omit<T, keyof PluginActionContextParams> & { pluginName?: string }
Omit<T, "buildDependencies" | keyof PluginActionContextParams> & { pluginName?: string }
// additionally make runtimeContext param optional
type ServiceActionHelperParams<T extends PluginServiceActionParamsBase> =
Omit<T, "module" | "runtimeContext" | keyof PluginActionContextParams>
Omit<T, "module" | "buildDependencies" | "runtimeContext" | keyof PluginActionContextParams>
& { runtimeContext?: RuntimeContext, pluginName?: string }

type RequirePluginName<T> = T & { pluginName: string }
Expand Down Expand Up @@ -292,13 +292,18 @@ export class ActionHelper implements TypeGuard {
//region Helper Methods
//===========================================================================

private async getBuildDependencies(module: Module): Promise<ModuleMap> {
const dependencies = await this.garden.resolveModuleDependencies(module.build.dependencies, [])
return keyBy(dependencies, "name")
}

async getStatus(): Promise<ContextStatus> {
const envStatus: EnvironmentStatusMap = await this.getEnvironmentStatus({})
const services = keyBy(await this.garden.getServices(), "name")

const serviceStatus = await Bluebird.props(mapValues(services, async (service: Service) => {
const dependencies = await this.garden.getServices(service.config.dependencies)
const runtimeContext = await prepareRuntimeContext(this.garden, service.module, dependencies)
const serviceDependencies = await this.garden.getServices(service.config.dependencies)
const runtimeContext = await prepareRuntimeContext(this.garden, service.module, serviceDependencies)
return this.getServiceStatus({ service, runtimeContext })
}))

Expand Down Expand Up @@ -372,10 +377,14 @@ export class ActionHelper implements TypeGuard {
pluginName,
defaultHandler,
})

const buildDependencies = await this.getBuildDependencies(module)

const handlerParams: any = {
...this.commonParams(handler),
...omit(<object>params, ["module"]),
...<object>params,
module: omit(module, ["_ConfigType"]),
buildDependencies,
}
// TODO: figure out why this doesn't compile without the function cast
return (<Function>handler)(handlerParams)
Expand All @@ -396,6 +405,7 @@ export class ActionHelper implements TypeGuard {
})

// TODO: figure out why this doesn't compile without the casts
const buildDependencies = await this.getBuildDependencies(module)
const deps = await this.garden.getServices(service.config.dependencies)
const runtimeContext = ((<any>params).runtimeContext || await prepareRuntimeContext(this.garden, module, deps))

Expand All @@ -404,6 +414,7 @@ export class ActionHelper implements TypeGuard {
...<object>params,
module,
runtimeContext,
buildDependencies,
}

return (<Function>handler)(handlerParams)
Expand Down
2 changes: 1 addition & 1 deletion garden-service/src/plugins/google/gcloud.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 { spawn } from "child_process"
import * as spawn from "cross-spawn"
import { extend } from "lodash"
import { spawnPty } from "../../util/util"

Expand Down
4 changes: 2 additions & 2 deletions garden-service/src/plugins/google/google-cloud-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export const gardenPlugin = (): GardenPlugin => ({
validate: parseGcfModule,

async deployService(
{ ctx, module, service, runtimeContext, logEntry }: DeployServiceParams<GcfModule>,
{ ctx, module, service, runtimeContext, logEntry, buildDependencies }: DeployServiceParams<GcfModule>,
) {
// TODO: provide env vars somehow to function
const project = getProject(service, ctx.provider)
Expand All @@ -126,7 +126,7 @@ export const gardenPlugin = (): GardenPlugin => ({
"--trigger-http",
])

return getServiceStatus({ ctx, module, service, runtimeContext, logEntry })
return getServiceStatus({ ctx, module, service, runtimeContext, logEntry, buildDependencies })
},

async getServiceOutputs({ ctx, service }: GetServiceOutputsParams<GcfModule>) {
Expand Down
6 changes: 4 additions & 2 deletions garden-service/src/plugins/kubernetes/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export async function runModule(
}

export async function runService(
{ ctx, service, interactive, runtimeContext, silent, timeout, logEntry }:
{ ctx, service, interactive, runtimeContext, silent, timeout, logEntry, buildDependencies }:
RunServiceParams<ContainerModule>,
) {
return runModule({
Expand All @@ -187,11 +187,12 @@ export async function runService(
silent,
timeout,
logEntry,
buildDependencies,
})
}

export async function testModule(
{ ctx, interactive, module, runtimeContext, silent, testConfig, logEntry }:
{ ctx, interactive, module, runtimeContext, silent, testConfig, logEntry, buildDependencies }:
TestModuleParams<ContainerModule>,
): Promise<TestResult> {
const testName = testConfig.name
Expand All @@ -208,6 +209,7 @@ export async function testModule(
silent,
timeout,
logEntry,
buildDependencies,
})

const api = new KubeApi(ctx.provider)
Expand Down
25 changes: 17 additions & 8 deletions garden-service/src/plugins/kubernetes/helm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export const helmHandlers: Partial<ModuleAndServiceActions<HelmModule>> = {
getServiceStatus,

async deployService(
{ ctx, module, service, logEntry }: DeployServiceParams<HelmModule>,
{ ctx, module, service, logEntry, force }: DeployServiceParams<HelmModule>,
): Promise<ServiceStatus> {
const provider = ctx.provider
const chartPath = await getChartPath(module)
Expand All @@ -147,20 +147,29 @@ export const helmHandlers: Partial<ModuleAndServiceActions<HelmModule>> = {
const releaseStatus = await getReleaseStatus(ctx.provider, releaseName, logEntry)

if (releaseStatus.state === "missing") {
await helm(provider, logEntry,
const installArgs = [
"install", chartPath,
"--name", releaseName,
"--namespace", namespace,
"--values", valuesPath,
"--wait",
)
]
if (force) {
installArgs.push("--replace")
}
await helm(provider, logEntry, ...installArgs)
} else {
await helm(provider, logEntry,
const upgradeArgs = [
"upgrade", releaseName, chartPath,
"--install",
"--namespace", namespace,
"--values", valuesPath,
"--wait",
)
]
if (force) {
upgradeArgs.push("--force")
}
await helm(provider, logEntry, ...upgradeArgs)
}

const objects = await getChartObjects(ctx, service, logEntry)
Expand Down Expand Up @@ -295,12 +304,12 @@ async function getChartObjects(ctx: PluginContext, service: Service, logEntry?:
}

async function getServiceStatus(
{ ctx, service, module, logEntry }: GetServiceStatusParams<HelmModule>,
{ ctx, service, module, logEntry, buildDependencies }: GetServiceStatusParams<HelmModule>,
): Promise<ServiceStatus> {
// need to build to be able to check the status
const buildStatus = await getGenericModuleBuildStatus({ ctx, module, logEntry })
const buildStatus = await getGenericModuleBuildStatus({ ctx, module, logEntry, buildDependencies })
if (!buildStatus.ready) {
await build({ ctx, module, logEntry })
await build({ ctx, module, logEntry, buildDependencies })
}

// first check if the installed objects on the cluster match the current code
Expand Down
3 changes: 2 additions & 1 deletion garden-service/src/plugins/kubernetes/kubectl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
*/

import chalk from "chalk"
import { ChildProcess, spawn } from "child_process"
import { ChildProcess } from "child_process"
import * as spawn from "cross-spawn"
import { extend } from "lodash"
import { encodeYamlMulti, spawnPty } from "../../util/util"
import { RuntimeError } from "../../exceptions"
Expand Down
16 changes: 12 additions & 4 deletions garden-service/src/plugins/local/local-docker-swarm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const gardenPlugin = (): GardenPlugin => ({
getServiceStatus,

async deployService(
{ ctx, module, service, runtimeContext, logEntry }: DeployServiceParams<ContainerModule>,
{ ctx, module, service, runtimeContext, logEntry, buildDependencies }: DeployServiceParams<ContainerModule>,
) {
// TODO: split this method up and test
const { versionString } = service.module.version
Expand Down Expand Up @@ -116,7 +116,14 @@ export const gardenPlugin = (): GardenPlugin => ({
}

const docker = getDocker()
const serviceStatus = await getServiceStatus({ ctx, service, module, runtimeContext, logEntry })
const serviceStatus = await getServiceStatus({
ctx,
service,
module,
runtimeContext,
logEntry,
buildDependencies,
})
let swarmServiceStatus
let serviceId

Expand Down Expand Up @@ -172,7 +179,7 @@ export const gardenPlugin = (): GardenPlugin => ({
msg: `Ready`,
})

return getServiceStatus({ ctx, module, service, runtimeContext, logEntry })
return getServiceStatus({ ctx, module, service, runtimeContext, logEntry, buildDependencies })
},

async getServiceOutputs({ ctx, service }: GetServiceOutputsParams<ContainerModule>) {
Expand All @@ -182,14 +189,15 @@ export const gardenPlugin = (): GardenPlugin => ({
},

async execInService(
{ ctx, service, command, runtimeContext, logEntry }: ExecInServiceParams<ContainerModule>,
{ ctx, service, command, runtimeContext, logEntry, buildDependencies }: ExecInServiceParams<ContainerModule>,
) {
const status = await getServiceStatus({
ctx,
service,
module: service.module,
runtimeContext,
logEntry,
buildDependencies,
})

if (!status.state || status.state !== "ready") {
Expand Down
24 changes: 24 additions & 0 deletions garden-service/src/plugins/openfaas/faas-cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 { BinaryCmd } from "../../util/ext-tools"

export const faasCli = new BinaryCmd({
name: "faas-cli",
specs: {
darwin: {
url: "https://github.com/openfaas/faas-cli/releases/download/0.7.3/faas-cli-darwin",
},
linux: {
url: "https://github.com/openfaas/faas-cli/releases/download/0.7.3/faas-cli",
},
win32: {
url: "https://github.com/openfaas/faas-cli/releases/download/0.7.3/faas-cli.exe",
},
},
})
Loading

0 comments on commit 53fefa6

Please sign in to comment.