From e79fc541098fee4bd2bf6263b0b0934a440495a8 Mon Sep 17 00:00:00 2001 From: Steffen Neubauer Date: Mon, 23 Oct 2023 16:16:30 +0200 Subject: [PATCH] Revert "fix: resolve multiline variables in kubernetes manifests correctly (#5270)" (#5283) This reverts commit 946e74b97abb382b58c6ab4ab022fe63c43859a8. --- core/package.json | 4 +- .../kubernetes/kubernetes-type/common.ts | 9 ++- .../multiline-var-in-manifest/deployment.yml | 6 -- .../multiline-var-in-manifest/garden.yml | 11 ---- .../kubernetes/kubernetes-type/common.ts | 58 ++----------------- 5 files changed, 12 insertions(+), 76 deletions(-) delete mode 100644 core/test/data/test-projects/kubernetes-type/multiline-var-in-manifest/deployment.yml delete mode 100644 core/test/data/test-projects/kubernetes-type/multiline-var-in-manifest/garden.yml diff --git a/core/package.json b/core/package.json index 73db795da2..2360134e55 100644 --- a/core/package.json +++ b/core/package.json @@ -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 npm run build", + "dev": "nodemon --watch src/template-string/*.pegjs --exec yarn 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", @@ -292,4 +292,4 @@ "fsevents": "^2.3.3" }, "gitHead": "b0647221a4d2ff06952bae58000b104215aed922" -} +} \ No newline at end of file diff --git a/core/src/plugins/kubernetes/kubernetes-type/common.ts b/core/src/plugins/kubernetes/kubernetes-type/common.ts index 30bce1620f..b4a85b3ea5 100644 --- a/core/src/plugins/kubernetes/kubernetes-type/common.ts +++ b/core/src/plugins/kubernetes/kubernetes-type/common.ts @@ -424,14 +424,13 @@ async function readFileManifests( resolvedFiles.map(async (path): Promise => { const absPath = resolve(manifestPath, path) log.debug(`Reading manifest for ${action.longDescription()} from path ${absPath}`) - // parse manifests as yaml before resolving the template strings - // to avoid the whole yaml file being treated as single string. + const str = (await readFile(absPath)).toString() + const resolved = ctx.resolveTemplateStrings(str, { allowPartial: true, unescape: true }) const manifests = await parseKubernetesManifests( - (await readFile(absPath)).toString(), + resolved, `${basename(absPath)} in directory ${dirname(absPath)} (specified in ${action.longDescription()})` ) - const resolved = ctx.resolveTemplateStrings(manifests, { allowPartial: true, unescape: true }) - return resolved.map((manifest, index) => ({ + return manifests.map((manifest, index) => ({ declaration: { type: "file", filename: absPath, diff --git a/core/test/data/test-projects/kubernetes-type/multiline-var-in-manifest/deployment.yml b/core/test/data/test-projects/kubernetes-type/multiline-var-in-manifest/deployment.yml deleted file mode 100644 index 3ab680887d..0000000000 --- a/core/test/data/test-projects/kubernetes-type/multiline-var-in-manifest/deployment.yml +++ /dev/null @@ -1,6 +0,0 @@ -apiVersion: v1 -kind: ConfigMap -metadata: - name: test-configmap -data: - hello: ${var.multiline-var} diff --git a/core/test/data/test-projects/kubernetes-type/multiline-var-in-manifest/garden.yml b/core/test/data/test-projects/kubernetes-type/multiline-var-in-manifest/garden.yml deleted file mode 100644 index 86eb2cf02c..0000000000 --- a/core/test/data/test-projects/kubernetes-type/multiline-var-in-manifest/garden.yml +++ /dev/null @@ -1,11 +0,0 @@ -kind: Deploy -type: kubernetes -name: multiline-var-in-manifest-deploy -spec: - files: [ "deployment.yml" ] -variables: - multiline-var: | - this is a - multiline - variable - foo: bar diff --git a/core/test/integ/src/plugins/kubernetes/kubernetes-type/common.ts b/core/test/integ/src/plugins/kubernetes/kubernetes-type/common.ts index 61aeca23d8..e13674a905 100644 --- a/core/test/integ/src/plugins/kubernetes/kubernetes-type/common.ts +++ b/core/test/integ/src/plugins/kubernetes/kubernetes-type/common.ts @@ -9,18 +9,16 @@ 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 { KubeApi } from "../../../../../../src/plugins/kubernetes/api" -import { KubernetesProvider } from "../../../../../../src/plugins/kubernetes/config" 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" +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" let kubernetesTestGarden: TestGarden @@ -151,50 +149,6 @@ describe("getManifests", () => { }) }) - context("resolve template strings in manifests", () => { - let action: Resolved - - 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({ - 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")