From 8d66f8a88fe41fca66243f505cdd68f64da2d062 Mon Sep 17 00:00:00 2001 From: Jon Edvald Date: Wed, 27 Nov 2019 16:18:46 +0100 Subject: [PATCH] fix(k8s): env vars weren't passed to services with `garden run service` --- .../src/plugins/kubernetes/container/run.ts | 4 +- .../test-projects/container/simple/garden.yml | 7 +++ .../plugins/kubernetes/container/container.ts | 62 +++++++++++++++++++ .../test/unit/src/tasks/get-service-status.ts | 1 - 4 files changed, 72 insertions(+), 2 deletions(-) diff --git a/garden-service/src/plugins/kubernetes/container/run.ts b/garden-service/src/plugins/kubernetes/container/run.ts index 3df9b7f884..f7d09d3a29 100644 --- a/garden-service/src/plugins/kubernetes/container/run.ts +++ b/garden-service/src/plugins/kubernetes/container/run.ts @@ -36,7 +36,9 @@ export async function runContainerService({ timeout, log, }: RunServiceParams): Promise { - const { command, args } = service.spec + const { command, args, env } = service.spec + + runtimeContext.envVars = { ...runtimeContext.envVars, ...env } return runContainerModule({ ctx, diff --git a/garden-service/test/data/test-projects/container/simple/garden.yml b/garden-service/test/data/test-projects/container/simple/garden.yml index 38ca49bf33..ef7622fcd0 100644 --- a/garden-service/test/data/test-projects/container/simple/garden.yml +++ b/garden-service/test/data/test-projects/container/simple/garden.yml @@ -2,6 +2,13 @@ kind: Module name: simple type: container image: busybox +services: + - name: echo-service + command: [sh, -c, "echo ok"] + - name: env-service + command: [sh, -c, "echo $ENV_VAR"] + env: + ENV_VAR: foo tasks: - name: echo-task command: [sh, -c, "echo ok"] diff --git a/garden-service/test/integ/src/plugins/kubernetes/container/container.ts b/garden-service/test/integ/src/plugins/kubernetes/container/container.ts index bae1b578ea..c256d3f5b7 100644 --- a/garden-service/test/integ/src/plugins/kubernetes/container/container.ts +++ b/garden-service/test/integ/src/plugins/kubernetes/container/container.ts @@ -12,6 +12,8 @@ import { TaskTask } from "../../../../../../src/tasks/task" import { runAndCopy } from "../../../../../../src/plugins/kubernetes/run" import { Provider } from "../../../../../../src/config/provider" import { containerHelpers } from "../../../../../../src/plugins/container/helpers" +import { runContainerService } from "../../../../../../src/plugins/kubernetes/container/run" +import { prepareRuntimeContext } from "../../../../../../src/runtime-context" describe("kubernetes container module handlers", () => { let garden: Garden @@ -234,6 +236,66 @@ describe("kubernetes container module handlers", () => { }) }) + describe("runContainerService", () => { + it("should run a service", async () => { + const service = await graph.getService("echo-service") + + const runtimeContext = await prepareRuntimeContext({ + garden, + graph, + dependencies: { + build: [], + service: [], + task: [], + test: [], + }, + module: service.module, + serviceStatuses: {}, + taskResults: {}, + }) + + const result = await runContainerService({ + ctx: garden.getPluginContext(provider), + log: garden.log, + service, + module: service.module, + interactive: false, + runtimeContext, + }) + + expect(result.log.trim()).to.eql("ok") + }) + + it("should add configured env vars to the runtime context", async () => { + const service = await graph.getService("env-service") + + const runtimeContext = await prepareRuntimeContext({ + garden, + graph, + dependencies: { + build: [], + service: [], + task: [], + test: [], + }, + module: service.module, + serviceStatuses: {}, + taskResults: {}, + }) + + const result = await runContainerService({ + ctx: garden.getPluginContext(provider), + log: garden.log, + service, + module: service.module, + interactive: false, + runtimeContext, + }) + + expect(result.log.trim()).to.eql("foo") + }) + }) + describe("runContainerTask", () => { it("should run a basic task", async () => { const task = await graph.getTask("echo-task") diff --git a/garden-service/test/unit/src/tasks/get-service-status.ts b/garden-service/test/unit/src/tasks/get-service-status.ts index e322c9cc5a..1950fe9e64 100644 --- a/garden-service/test/unit/src/tasks/get-service-status.ts +++ b/garden-service/test/unit/src/tasks/get-service-status.ts @@ -7,7 +7,6 @@ import { Garden } from "../../../../src/garden" import { GardenPlugin } from "../../../../src/types/plugin/plugin" import { joi } from "../../../../src/config/common" import { ServiceState } from "../../../../src/types/service" -import { DeployServiceParams } from "../../../../src/types/plugin/service/deployService" import { RunTaskParams } from "../../../../src/types/plugin/task/runTask" import { expect } from "chai" import { GetServiceStatusTask } from "../../../../src/tasks/get-service-status"