Skip to content

Commit

Permalink
feat(k8s): support immutable build success
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitchell Friedman authored and edvald committed May 14, 2020
1 parent df9e4e4 commit 1d4dcd7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
7 changes: 6 additions & 1 deletion garden-service/src/plugins/kubernetes/container/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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 })
}
}
Expand Down Expand Up @@ -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,
Expand Down
58 changes: 58 additions & 0 deletions garden-service/test/unit/src/plugins/kubernetes/container/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2018-2020 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 { 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
})
})

0 comments on commit 1d4dcd7

Please sign in to comment.