From 66e6974cd65d6473533b179edfa65db5e7bb688c Mon Sep 17 00:00:00 2001 From: swist Date: Mon, 15 Jun 2020 19:38:44 +0100 Subject: [PATCH] feat(kaniko): make kaniko image configurable in provider config --- docs/reference/providers/kubernetes.md | 25 +++++++++++++++++++ docs/reference/providers/local-kubernetes.md | 25 +++++++++++++++++++ .../src/plugins/kubernetes/config.ts | 18 +++++++++++++ .../src/plugins/kubernetes/container/build.ts | 12 +++++---- .../data/test-projects/container/garden.yml | 7 ++++++ .../src/plugins/kubernetes/container/build.ts | 17 +++++++++++++ 6 files changed, 99 insertions(+), 5 deletions(-) diff --git a/docs/reference/providers/kubernetes.md b/docs/reference/providers/kubernetes.md index a8b3c6b7a3..b8164de47c 100644 --- a/docs/reference/providers/kubernetes.md +++ b/docs/reference/providers/kubernetes.md @@ -54,6 +54,11 @@ providers: # performant, but we're opting to keep it optional until it's enabled by default in Docker. enableBuildKit: false + # Configuration options for the `kaniko` build mode. + kaniko: + # Change the kaniko image (repository/image:tag) to use when building in kaniko mode. + image: 'gcr.io/kaniko-project/executor:debug-v0.23.0' + # A default hostname to use when no hostname is explicitly configured for a service. defaultHostname: @@ -387,6 +392,26 @@ Enable [BuildKit](https://github.com/moby/buildkit) support. This should in most | --------- | ------- | -------- | | `boolean` | `false` | No | +### `providers[].kaniko` + +[providers](#providers) > kaniko + +Configuration options for the `kaniko` build mode. + +| Type | Required | +| -------- | -------- | +| `object` | No | + +### `providers[].kaniko.image` + +[providers](#providers) > [kaniko](#providerskaniko) > image + +Change the kaniko image (repository/image:tag) to use when building in kaniko mode. + +| Type | Default | Required | +| -------- | ------------------------------------------------ | -------- | +| `string` | `"gcr.io/kaniko-project/executor:debug-v0.23.0"` | No | + ### `providers[].defaultHostname` [providers](#providers) > defaultHostname diff --git a/docs/reference/providers/local-kubernetes.md b/docs/reference/providers/local-kubernetes.md index 23dc2eeaea..68070d56c9 100644 --- a/docs/reference/providers/local-kubernetes.md +++ b/docs/reference/providers/local-kubernetes.md @@ -50,6 +50,11 @@ providers: # performant, but we're opting to keep it optional until it's enabled by default in Docker. enableBuildKit: false + # Configuration options for the `kaniko` build mode. + kaniko: + # Change the kaniko image (repository/image:tag) to use when building in kaniko mode. + image: 'gcr.io/kaniko-project/executor:debug-v0.23.0' + # A default hostname to use when no hostname is explicitly configured for a service. defaultHostname: @@ -355,6 +360,26 @@ Enable [BuildKit](https://github.com/moby/buildkit) support. This should in most | --------- | ------- | -------- | | `boolean` | `false` | No | +### `providers[].kaniko` + +[providers](#providers) > kaniko + +Configuration options for the `kaniko` build mode. + +| Type | Required | +| -------- | -------- | +| `object` | No | + +### `providers[].kaniko.image` + +[providers](#providers) > [kaniko](#providerskaniko) > image + +Change the kaniko image (repository/image:tag) to use when building in kaniko mode. + +| Type | Default | Required | +| -------- | ------------------------------------------------ | -------- | +| `string` | `"gcr.io/kaniko-project/executor:debug-v0.23.0"` | No | + ### `providers[].defaultHostname` [providers](#providers) > defaultHostname diff --git a/garden-service/src/plugins/kubernetes/config.ts b/garden-service/src/plugins/kubernetes/config.ts index fec667daf4..9190e411e4 100644 --- a/garden-service/src/plugins/kubernetes/config.ts +++ b/garden-service/src/plugins/kubernetes/config.ts @@ -27,6 +27,7 @@ import { baseTestSpecSchema, BaseTestSpec } from "../../config/test" import { ArtifactSpec } from "../../config/validation" import { V1Toleration } from "@kubernetes/client-node" +export const DEFAULT_KANIKO_IMAGE = "gcr.io/kaniko-project/executor:debug-v0.23.0" export interface ProviderSecretRef { name: string namespace: string @@ -91,6 +92,9 @@ export interface KubernetesConfig extends ProviderConfig { clusterDocker?: { enableBuildKit?: boolean } + kaniko?: { + image?: string + } context: string defaultHostname?: string defaultUsername?: string @@ -327,6 +331,20 @@ export const kubernetesConfigBase = providerConfigBaseSchema().keys({ }) .default(() => {}) .description("Configuration options for the `cluster-docker` build mode."), + kaniko: joi + .object() + .keys({ + image: joi + .string() + .default(DEFAULT_KANIKO_IMAGE) + .description( + deline` + Change the kaniko image (repository/image:tag) to use when building in kaniko mode. + ` + ), + }) + .default(() => {}) + .description("Configuration options for the `kaniko` build mode."), defaultHostname: joi .string() .description("A default hostname to use when no hostname is explicitly configured for a service.") diff --git a/garden-service/src/plugins/kubernetes/container/build.ts b/garden-service/src/plugins/kubernetes/container/build.ts index e324e9009d..696d5edada 100644 --- a/garden-service/src/plugins/kubernetes/container/build.ts +++ b/garden-service/src/plugins/kubernetes/container/build.ts @@ -28,8 +28,8 @@ import { KubeApi } from "../api" import { kubectl } from "../kubectl" import { LogEntry } from "../../../logger/log-entry" import { getDockerAuthVolume } from "../util" -import { KubernetesProvider, ContainerBuildMode, KubernetesPluginContext } from "../config" -import { PluginError, InternalError, RuntimeError, BuildError } from "../../../exceptions" +import { KubernetesProvider, ContainerBuildMode, KubernetesPluginContext, DEFAULT_KANIKO_IMAGE } from "../config" +import { PluginError, InternalError, RuntimeError, BuildError, ConfigurationError } from "../../../exceptions" import { PodRunner } from "../run" import { getRegistryHostname, getKubernetesSystemVariables } from "../init" import { normalizeLocalRsyncPath } from "../../../util/fs" @@ -44,8 +44,6 @@ import chalk = require("chalk") import { loadImageToMicrok8s, getMicrok8sImageStatus } from "../local/microk8s" import { RunResult } from "../../../types/plugin/base" -const kanikoImage = "gcr.io/kaniko-project/executor:debug-v0.23.0" - const registryPort = 5000 export const buildSyncDeploymentName = "garden-build-sync" @@ -329,7 +327,7 @@ const remoteBuild: BuildHandler = async (params) => { const pushRes = await execInPod({ provider, log, args: pushArgs, timeout: 300, podName, containerName, stdout }) buildLog += pushRes.stdout + pushRes.stderr - } else { + } else if (provider.config.buildMode === "kaniko") { // build with Kaniko const args = [ "--context", @@ -355,6 +353,8 @@ const remoteBuild: BuildHandler = async (params) => { if (kanikoBuildFailed(buildRes)) { throw new BuildError(`Failed building module ${chalk.bold(module.name)}:\n\n${buildLog}`, { buildLog }) } + } else { + throw new ConfigurationError("Uknown build mode", { buildMode: provider.config.buildMode }) } log.silly(buildLog) @@ -472,6 +472,8 @@ async function runKaniko({ provider, namespace, log, module, args, outputStream done ` + const kanikoImage = provider.config.kaniko?.image || DEFAULT_KANIKO_IMAGE + const runner = new PodRunner({ api, podName, diff --git a/garden-service/test/data/test-projects/container/garden.yml b/garden-service/test/data/test-projects/container/garden.yml index 46d2c401a5..fefdd312cd 100644 --- a/garden-service/test/data/test-projects/container/garden.yml +++ b/garden-service/test/data/test-projects/container/garden.yml @@ -8,6 +8,7 @@ environments: - name: cluster-docker-auth - name: cluster-docker-remote-registry - name: kaniko + - name: kaniko-image-override - name: kaniko-remote-registry providers: - name: local-kubernetes @@ -41,3 +42,9 @@ providers: environments: [kaniko-remote-registry] buildMode: kaniko deploymentRegistry: *deploymentRegistry + - <<: *clusterDocker + environments: [kaniko-image-override] + buildMode: kaniko + kaniko: + image: gcr.io/kaniko-project/executor:debug-perf + deploymentRegistry: *deploymentRegistry diff --git a/garden-service/test/integ/src/plugins/kubernetes/container/build.ts b/garden-service/test/integ/src/plugins/kubernetes/container/build.ts index 82a6d02489..6f178928d7 100644 --- a/garden-service/test/integ/src/plugins/kubernetes/container/build.ts +++ b/garden-service/test/integ/src/plugins/kubernetes/container/build.ts @@ -428,4 +428,21 @@ describe("kubernetes build flow", () => { expect(status.ready).to.be.false }) }) + + grouped("kaniko", "image-override", "remote-only").context("kaniko - image - override mode", () => { + before(async () => { + await init("kaniko-image-override") + }) + + it("should push to configured deploymentRegistry if specified", async () => { + const module = graph.getModule("remote-registry-test") + await garden.buildDir.syncFromSrc(module, garden.log) + + await k8sBuildContainer({ + ctx, + log: garden.log, + module, + }) + }) + }) })