Skip to content

Commit

Permalink
feat(kaniko): pass extraFlags to kaniko builders
Browse files Browse the repository at this point in the history
This means we can use extra features from kaniko.
The default behaviour is to try and merge the args
  • Loading branch information
swist authored and edvald committed Jun 16, 2020
1 parent 66e6974 commit deaab07
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
21 changes: 20 additions & 1 deletion garden-service/src/plugins/kubernetes/container/build.ts
Expand Up @@ -8,6 +8,7 @@

import pRetry from "p-retry"
import split2 = require("split2")
import { differenceBy } from "lodash"
import { ContainerModule, ContainerRegistryConfig } from "../../container/config"
import { containerHelpers } from "../../container/helpers"
import { buildContainerModule, getContainerBuildStatus, getDockerBuildFlags } from "../../container/build"
Expand Down Expand Up @@ -336,7 +337,7 @@ const remoteBuild: BuildHandler = async (params) => {
dockerfile,
"--destination",
deploymentImageId,
"--cache=true",
...getKanikoFlags(module.spec.extraFlags),
]

if (provider.config.deploymentRegistry?.hostname === inClusterRegistryHostname) {
Expand Down Expand Up @@ -380,6 +381,24 @@ export interface BuilderExecParams {
stderr?: Writable
}

export const DEFAULT_KANIKO_FLAGS = ["--cache=true"]

export const getKanikoFlags = (flags?: string[]): string[] => {
if (!flags) {
return DEFAULT_KANIKO_FLAGS
}
const flagToKey = (flag) => {
const found = flag.match(/--([a-zA-Z]*)/)
if (found === null) {
throw new ConfigurationError(`Invalid format for a kaniko flag`, { flag })
}
return found[0]
}
const defaultsToKeep = differenceBy(DEFAULT_KANIKO_FLAGS, flags, flagToKey)

return [...flags, ...defaultsToKeep]
}

export function kanikoBuildFailed(buildRes: RunResult) {
return !buildRes.success && !buildRes.log.includes("cannot be overwritten because the repository is immutable.")
}
Expand Down
35 changes: 33 additions & 2 deletions garden-service/test/unit/src/plugins/kubernetes/container/build.ts
Expand Up @@ -6,12 +6,16 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { kanikoBuildFailed } from "../../../../../../src/plugins/kubernetes/container/build"
import {
kanikoBuildFailed,
getKanikoFlags,
DEFAULT_KANIKO_FLAGS,
} 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.`
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({
Expand Down Expand Up @@ -55,4 +59,31 @@ describe("kaniko build", () => {
})
).to.be.false
})

describe("getKanikoFlags", () => {
it("should only keep all declarations of each flag + the defaults", () => {
expect(getKanikoFlags(["--here=first", "--here=again"])).to.deep.equal([
"--here=first",
"--here=again",
"--cache=true",
])
})
it("should allow overriding default flags", () => {
const overridenFlags = DEFAULT_KANIKO_FLAGS.map((f) => f + "cat")
expect(getKanikoFlags(overridenFlags)).to.deep.equal(overridenFlags)
})

it("should allow toggles", () => {
expect(getKanikoFlags(["--myToggle"])).to.deep.equal(["--myToggle", "--cache=true"])
})

it("should throw if a flag is malformed", () => {
expect(() => getKanikoFlags(["--here=first", "-my-flag"])).to.throw(/Invalid format for a kaniko flag/)
})

it("should return --cache=true when extraFlags is empty", () => {
expect(getKanikoFlags([])).to.deep.equal(DEFAULT_KANIKO_FLAGS)
expect(getKanikoFlags()).to.deep.equal(DEFAULT_KANIKO_FLAGS)
})
})
})

0 comments on commit deaab07

Please sign in to comment.