Skip to content

Commit

Permalink
fix(openfaas): better cross-platform support
Browse files Browse the repository at this point in the history
The previous method wasn't working on Windows, and this approach should
be more portable in general.
  • Loading branch information
edvald authored and thsig committed Sep 17, 2018
1 parent b07372a commit d1c59d4
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 42 deletions.
8 changes: 8 additions & 0 deletions garden-cli/src/config/config-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,18 @@ class ModuleContext extends ConfigContext {
@schema(Joi.string().description("The current version of the module.").example(exampleVersion))
public version: string

@schema(
Joi.string()
.description("The build path of the module.")
.example("/home/me/code/my-project/.garden/build/my-module"),
)
public buildPath: string

constructor(root: ConfigContext, module: Module) {
super(root)
this.path = module.path
this.version = module.version.versionString
this.buildPath = module.buildPath
}
}

Expand Down
46 changes: 31 additions & 15 deletions garden-cli/src/plugins/openfaas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ import { BaseServiceSpec } from "../config/service"
import { GardenPlugin } from "../types/plugin/plugin"
import { deleteContainerService } from "./kubernetes/deployment"
import { Provider, providerConfigBaseSchema } from "../config/project"
import dedent = require("dedent")

const systemProjectPath = join(STATIC_DIR, "openfaas", "system")
const stackFilename = "stack.yml"
const builderWorkDir = "/wd"

export interface OpenFaasModuleSpec extends GenericModuleSpec {
handler: string
Expand Down Expand Up @@ -91,9 +93,12 @@ const configSchema = providerConfigBaseSchema
.keys({
hostname: Joi.string()
.hostname()
.description(
"The hostname to configure for the function gateway. " +
"Defaults to the default hostname of the configured Kubernetes provider.",
.description(dedent`
The hostname to configure for the function gateway.
Defaults to the default hostname of the configured Kubernetes provider.
Important: If you have other types of services, this should be different from their ingress hostnames,
or the other services should not expose paths under /function and /system to avoid routing conflicts.`,
)
.example("functions.mydomain.com"),
})
Expand All @@ -104,7 +109,7 @@ export function gardenPlugin({ config }: { config: OpenFaasConfig }): GardenPlug
config = validate(config, configSchema, { context: "OpenFaaS provider config" })

return {
modules: [join(STATIC_DIR, "openfaas", "openfaas-builder")],
modules: [join(STATIC_DIR, "openfaas", "builder")],
actions: {
async getEnvironmentStatus({ ctx }: GetEnvironmentStatusParams) {
const ofGarden = await getOpenFaasGarden(ctx)
Expand Down Expand Up @@ -145,15 +150,20 @@ export function gardenPlugin({ config }: { config: OpenFaasConfig }): GardenPlug
{ context: `module ${moduleConfig.name}` },
)

// stack.yml is populated in the build handler below
moduleConfig.build.command = ["./faas-cli", "build", "-f", stackFilename]
// FIXME: this feels too magicky and convoluted, we should make this type of flow feel more natural
moduleConfig.build.command = [
"docker", "run", "-i",
"-v", `\${modules.${moduleConfig.name}.buildPath}:${builderWorkDir}`,
"-v", "/var/run/docker.sock:/var/run/docker.sock",
"--workdir", builderWorkDir,
`openfaas--builder:\${modules.openfaas--builder.version}`,
"faas-cli", "build", "-f", stackFilename,
]

moduleConfig.build.dependencies.push({
name: "openfaas-builder",
name: "builder",
plugin: "openfaas",
copy: [
{ source: "*", target: "." },
],
copy: [],
})

moduleConfig.serviceConfigs = [{
Expand Down Expand Up @@ -206,9 +216,15 @@ export function gardenPlugin({ config }: { config: OpenFaasConfig }): GardenPlug
await writeStackFile(ctx, module, runtimeContext.envVars)

// use faas-cli to do the deployment
await execa("./faas-cli", ["deploy", "-f", stackFilename], {
cwd: module.buildPath,
})
await execa("docker", [
"run", "-i",
"-v", `${module.buildPath}:${builderWorkDir}`,
"-v", "/var/run/docker.sock:/var/run/docker.sock",
"--workdir", builderWorkDir,
"--net", "host",
"openfaas/faas-cli:0.7.3",
"faas-cli", "deploy", "-f", stackFilename,
])

// wait until deployment is ready
const k8sProvider = getK8sProvider(ctx)
Expand Down Expand Up @@ -252,7 +268,7 @@ async function writeStackFile(
functions: {
[module.name]: {
lang: module.spec.lang,
handler: resolve(module.path, module.spec.handler),
handler: resolve(builderWorkDir, module.spec.handler),
image,
environment: envVars,
},
Expand Down Expand Up @@ -404,7 +420,7 @@ export async function getOpenFaasGarden(ctx: PluginContext): Promise<Garden> {
dirname: "system",
path: systemProjectPath,
project: {
name: "garden-openfaas-system",
name: `${ctx.projectName}-openfaas`,
environmentDefaults: {
providers: [],
variables: {},
Expand Down
6 changes: 6 additions & 0 deletions garden-cli/static/openfaas/builder/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM openfaas/faas-cli:0.7.3

WORKDIR /build

RUN apk add docker
RUN faas-cli template pull
4 changes: 4 additions & 0 deletions garden-cli/static/openfaas/builder/garden.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module:
description: Base image used for building openfaas modules
name: builder
type: container
17 changes: 0 additions & 17 deletions garden-cli/static/openfaas/openfaas-builder/bootstrap.sh

This file was deleted.

8 changes: 0 additions & 8 deletions garden-cli/static/openfaas/openfaas-builder/garden.yml

This file was deleted.

8 changes: 6 additions & 2 deletions garden-cli/static/openfaas/system/openfaas-system/garden.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module:
type: helm
repo: https://openfaas.github.io/faas-netes/
chart: openfaas
version: 1.2.2
version: 1.2.3
parameters:
exposeServices: false
functionNamespace: ${variables.function-namespace}
Expand All @@ -14,7 +14,11 @@ module:
- host: ${variables.gateway-hostname}
serviceName: gateway
servicePort: 8080
path: /
path: /function/
- host: ${variables.gateway-hostname}
serviceName: gateway
servicePort: 8080
path: /system/
faasnetesd:
imagePullPolicy: IfNotPresent
securityContext: false

0 comments on commit d1c59d4

Please sign in to comment.