From 1d4dcd786408e034ad1312f4ad039d3781f3e763 Mon Sep 17 00:00:00 2001 From: Mitchell Friedman Date: Wed, 13 May 2020 11:27:03 +0100 Subject: [PATCH] feat(k8s): support immutable build success --- .../src/plugins/kubernetes/container/build.ts | 7 ++- .../src/plugins/kubernetes/container/build.ts | 58 +++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 garden-service/test/unit/src/plugins/kubernetes/container/build.ts diff --git a/garden-service/src/plugins/kubernetes/container/build.ts b/garden-service/src/plugins/kubernetes/container/build.ts index b40e20b602..98d4fa0cfd 100644 --- a/garden-service/src/plugins/kubernetes/container/build.ts +++ b/garden-service/src/plugins/kubernetes/container/build.ts @@ -42,6 +42,7 @@ import { getSystemNamespace, getAppNamespace } from "../namespace" import { dedent } from "../../../util/string" 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.21.0" @@ -352,7 +353,7 @@ const remoteBuild: BuildHandler = async (params) => { const buildRes = await runKaniko({ provider, namespace, log, module, args, outputStream: stdout }) buildLog = buildRes.log - if (!buildRes.success) { + if (kanikoBuildFailed(buildRes)) { throw new BuildError(`Failed building module ${chalk.bold(module.name)}:\n\n${buildLog}`, { buildLog }) } } @@ -380,6 +381,10 @@ export interface BuilderExecParams { stderr?: Writable } +export function kanikoBuildFailed(buildRes: RunResult) { + return !buildRes.success && !buildRes.log.includes("cannot be overwritten because the repository is immutable.") +} + const buildHandlers: { [mode in ContainerBuildMode]: BuildHandler } = { "local-docker": localBuild, "cluster-docker": remoteBuild, diff --git a/garden-service/test/unit/src/plugins/kubernetes/container/build.ts b/garden-service/test/unit/src/plugins/kubernetes/container/build.ts new file mode 100644 index 0000000000..fc8513f593 --- /dev/null +++ b/garden-service/test/unit/src/plugins/kubernetes/container/build.ts @@ -0,0 +1,58 @@ +/* + * Copyright (C) 2018-2020 Garden Technologies, Inc. + * + * 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 { kanikoBuildFailed } from "../../../../../../src/plugins/kubernetes/container/build" +import { expect } from "chai" + +describe("kaniko build", () => { + it("should return as successful when immutable tag already exists in destination", () => { + const errorMessage = `error pushing image: failed to push to destination dockerhub.com/garden/backend:v-1234567: TAG_INVALID: The image tag 'v-1234567' already exists in the 'garden/backend' repository and cannot be overwritten because the repository is immutable.` + + expect( + kanikoBuildFailed({ + moduleName: "foo", + command: [], + version: "", + startedAt: new Date(), + completedAt: new Date(), + success: false, + log: errorMessage, + }) + ).to.be.false + }) + + it("should return as failure when other error messages are present", () => { + const errorMessage = "error pushing" + + expect( + kanikoBuildFailed({ + moduleName: "foo", + command: [], + version: "", + startedAt: new Date(), + completedAt: new Date(), + success: false, + log: errorMessage, + }) + ).to.be.true + }) + + it("should return as success when the build succeeded", () => { + expect( + kanikoBuildFailed({ + moduleName: "foo", + command: [], + version: "", + startedAt: new Date(), + completedAt: new Date(), + success: true, + log: "", + }) + ).to.be.false + }) +})