Skip to content

Commit

Permalink
fix(k8s): allow null in spec.files for deploy config (#4881)
Browse files Browse the repository at this point in the history
* fix(k8s): allow null in spec.files for deploy config

* chore: add tests and use helper isTruthy
  • Loading branch information
shumailxyz authored Jul 24, 2023
1 parent 05d0f44 commit 4fc3a09
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 20 deletions.
4 changes: 3 additions & 1 deletion core/src/plugins/kubernetes/kubernetes-type/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { uniq } from "lodash"
import { DOCS_BASE_URL } from "../../../constants"
import { kubernetesGetSyncStatus, kubernetesStartSync } from "./sync"
import { k8sContainerStopSync } from "../container/sync"
import { isTruthy } from "../../../util/util"

export const kubernetesDeployDocs = dedent`
Specify one or more Kubernetes manifests to deploy.
Expand All @@ -44,7 +45,8 @@ export const kubernetesDeployDefinition = (): DeployActionDefinition<KubernetesD
configure: async ({ ctx, config }) => {
// The `spec` field hasn't been validated here yet,
// so the value of `files` might be `undefined` instead of a default empty array
let files = config.spec.files || []
// also we discard any falsy file values in files array
let files = (config.spec.files || []).filter(isTruthy)

if (files.length > 0 && !config.spec.kustomize) {
if (!config.include) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
kind: "Deploy"
type: "kubernetes"
name: "config-map-list"
variables:
foo: false
spec:
files:
- "${var.foo ? 'manifests.yaml' : null}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: v1
items:
- apiVersion: v1
data:
foo.json: test
kind: ConfigMap
metadata:
name: foo
- apiVersion: v1
data:
bar.json: test
kind: ConfigMap
metadata:
name: bar
- apiVersion: v1
data:
baz.json: test
kind: ConfigMap
metadata:
name: baz
kind: ConfigMapList
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
apiVersion: garden.io/v1
kind: Project
name: test-kubernetes-type-conditional-manifests
environments:
- name: local
providers:
- name: local-kubernetes
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
apiVersion: v1
items:
- apiVersion: v1
data:
foo.json: test
kind: ConfigMap
metadata:
name: foo
- apiVersion: v1
data:
bar.json: test
kind: ConfigMap
metadata:
name: bar
- apiVersion: v1
data:
baz.json: test
kind: ConfigMap
metadata:
name: baz
- apiVersion: v1
data:
foo.json: test
kind: ConfigMap
metadata:
name: foo
- apiVersion: v1
data:
bar.json: test
kind: ConfigMap
metadata:
name: bar
- apiVersion: v1
data:
baz.json: test
kind: ConfigMap
metadata:
name: baz
kind: ConfigMapList
17 changes: 16 additions & 1 deletion core/test/integ/src/plugins/kubernetes/kubernetes-type/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import { expect } from "chai"
import { cloneDeep } from "lodash"

import { TestGarden } from "../../../../../helpers"
import { TestGarden, getDataDir, makeTestGarden } from "../../../../../helpers"
import { ModuleConfig } from "../../../../../../src/config/module"
import { apply } from "json-merge-patch"
import { getKubernetesTestGarden } from "./common"
Expand Down Expand Up @@ -174,3 +174,18 @@ describe("configureKubernetesModule", () => {
expect(configExclude.include).to.be.undefined
})
})

describe("configureKubernetesType", () => {
let garden: TestGarden

before(async () => {
const projectRoot = getDataDir("test-projects", "kubernetes-type-conditional-manifests")
garden = await makeTestGarden(projectRoot)
})

it("should resolve fine with null values for manifests in spec.files", async () => {
const graph = await garden.getConfigGraph({ log: garden.log, emit: false })
const action = graph.getDeploy("config-map-list")
expect(action["_config"].spec.files).to.eql([null])
})
})

0 comments on commit 4fc3a09

Please sign in to comment.