Skip to content

Commit

Permalink
fix: resolve multiline variables in kubernetes manifests correctly (#…
Browse files Browse the repository at this point in the history
…5270)

* fix: resolve multiline variables in kubernetes manifests correctly

Co-authored-by: Steffen Neubauer <[email protected]>
Co-authored-by: Mikael Högqvist Tabor
 <[email protected]>

* chore: remove context to fix test

---------

Co-authored-by: Steffen Neubauer <[email protected]>
Co-authored-by: Mikael Högqvist Tabor <[email protected]>
  • Loading branch information
3 people authored Oct 19, 2023
1 parent e256812 commit 946e74b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 12 deletions.
4 changes: 2 additions & 2 deletions core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@
"check-package-lock": "git diff-index --quiet HEAD -- package-lock.json || (echo 'package-lock.json is dirty!' && exit 1)",
"check-types": "tsc -p . --noEmit",
"clean": "shx rm -rf build",
"dev": "nodemon --watch src/template-string/*.pegjs --exec yarn build",
"dev": "nodemon --watch src/template-string/*.pegjs --exec npm run build",
"fix-format": "npm run lint -- --fix --quiet",
"lint": "eslint --ignore-pattern 'src/lib/**' --ext .ts src/ test/",
"migration:generate": "typeorm migration:generate --config ormconfig.js -n",
Expand All @@ -292,4 +292,4 @@
"fsevents": "^2.3.3"
},
"gitHead": "b0647221a4d2ff06952bae58000b104215aed922"
}
}
9 changes: 5 additions & 4 deletions core/src/plugins/kubernetes/kubernetes-type/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,13 +424,14 @@ async function readFileManifests(
resolvedFiles.map(async (path): Promise<DeclaredManifest[]> => {
const absPath = resolve(manifestPath, path)
log.debug(`Reading manifest for ${action.longDescription()} from path ${absPath}`)
const str = (await readFile(absPath)).toString()
const resolved = ctx.resolveTemplateStrings(str, { allowPartial: true, unescape: true })
// parse manifests as yaml before resolving the template strings
// to avoid the whole yaml file being treated as single string.
const manifests = await parseKubernetesManifests(
resolved,
(await readFile(absPath)).toString(),
`${basename(absPath)} in directory ${dirname(absPath)} (specified in ${action.longDescription()})`
)
return manifests.map((manifest, index) => ({
const resolved = ctx.resolveTemplateStrings(manifests, { allowPartial: true, unescape: true })
return resolved.map((manifest, index) => ({
declaration: {
type: "file",
filename: absPath,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: test-configmap
data:
hello: ${var.multiline-var}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
kind: Deploy
type: kubernetes
name: multiline-var-in-manifest-deploy
spec:
files: [ "deployment.yml" ]
variables:
multiline-var: |
this is a
multiline
variable
foo: bar
58 changes: 52 additions & 6 deletions core/test/integ/src/plugins/kubernetes/kubernetes-type/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@
import { expect } from "chai"
import cloneDeep from "fast-copy"

import dedent from "dedent"
import { omit } from "lodash"
import { dirname, join } from "path"
import { Resolved } from "../../../../../../src/actions/types"
import { ProviderConfigContext } from "../../../../../../src/config/template-contexts/provider"
import { ConfigGraph } from "../../../../../../src/graph/config-graph"
import { PluginContext } from "../../../../../../src/plugin-context"
import { getManifests } from "../../../../../../src/plugins/kubernetes/kubernetes-type/common"
import { expectError, getDataDir, getExampleDir, makeTestGarden, TestGarden } from "../../../../../helpers"
import { KubernetesDeployAction } from "../../../../../../src/plugins/kubernetes/kubernetes-type/config"
import { Resolved } from "../../../../../../src/actions/types"
import { KubeApi } from "../../../../../../src/plugins/kubernetes/api"
import { KubernetesProvider } from "../../../../../../src/plugins/kubernetes/config"
import dedent from "dedent"
import { dirname, join } from "path"
import { getManifests } from "../../../../../../src/plugins/kubernetes/kubernetes-type/common"
import { KubernetesDeployAction } from "../../../../../../src/plugins/kubernetes/kubernetes-type/config"
import { expectError, getDataDir, getExampleDir, makeTestGarden, TestGarden } from "../../../../../helpers"

let kubernetesTestGarden: TestGarden

Expand Down Expand Up @@ -149,6 +151,50 @@ describe("getManifests", () => {
})
})

context("resolve template strings in manifests", () => {
let action: Resolved<KubernetesDeployAction>

before(async () => {
garden = await getKubernetesTestGarden()
})

beforeEach(async () => {
graph = await garden.getConfigGraph({ log: garden.log, emit: false })
})

it("resolves multi-line template strings in the manifests correctly", async () => {
action = await garden.resolveAction<KubernetesDeployAction>({
action: cloneDeep(graph.getDeploy("multiline-var-in-manifest-deploy")),
log: garden.log,
graph,
})
const provider = (await garden.resolveProvider(garden.log, "local-kubernetes")) as KubernetesProvider
ctx = await garden.getPluginContext({
provider,
templateContext: new ProviderConfigContext(garden, provider.dependencies, action.getVariables()),
events: undefined,
})
api = await KubeApi.factory(garden.log, ctx, provider)

const manifests = await getManifests({ ctx, api, action, log: garden.log, defaultNamespace })
const configmap = manifests.find((m) => m.metadata.name === "test-configmap")
const expectedConfigMap = {
...configmap,
// remove annotations/labels added by garden
metadata: omit(configmap?.metadata, ["annotations", "labels"]),
}
expect(expectedConfigMap).eql({
apiVersion: "v1",
kind: "ConfigMap",
metadata: {
name: "test-configmap",
namespace: "foobar",
},
data: { hello: "this is a\nmultiline\nvariable\n" },
})
})
})

context("kustomize", () => {
const exampleDir = getExampleDir("kustomize")

Expand Down

0 comments on commit 946e74b

Please sign in to comment.