Skip to content

Commit

Permalink
fix(k8s): always flatten resources of kind List
Browse files Browse the repository at this point in the history
  • Loading branch information
eysi09 committed Jul 8, 2019
1 parent 2b5457c commit b6368f7
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 3 deletions.
6 changes: 4 additions & 2 deletions garden-service/src/plugins/kubernetes/helm/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { ConfigurationError, PluginError } from "../../../exceptions"
import { Module } from "../../../types/module"
import { findByName } from "../../../util/util"
import { deline } from "../../../util/string"
import { getAnnotation } from "../util"
import { getAnnotation, flattenResources } from "../util"
import { KubernetesPluginContext } from "../config"

/**
Expand Down Expand Up @@ -60,7 +60,7 @@ export async function getChartResources(ctx: PluginContext, module: Module, log:
chartPath,
))

return objects
const resources = objects
.filter(obj => {
const helmHook = getAnnotation(obj, "helm.sh/hook")
if (helmHook && helmHook.startsWith("test-")) {
Expand All @@ -69,6 +69,8 @@ export async function getChartResources(ctx: PluginContext, module: Module, log:

return true
})

return flattenResources(resources)
}

/**
Expand Down
15 changes: 15 additions & 0 deletions garden-service/src/plugins/kubernetes/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,18 @@ export async function upsertConfigMap(
}
}
}

/**
* Flattens an array of Kubernetes resources that contain `List` resources.
*
* If an array of resources contains a resource of kind `List`, the list items of that resource are
* flattened and included with the top-level resources.
*
* For example (simplified):
* `[{ metadata: { name: a }}, { kind: "List", items: [{ metadata: { name: b }}, { metadata: { name: c }}]}]`
* becomes
* `[{ metadata: { name: a }}, { metadata: { name: b }}, { metadata: { name: b }}]`
*/
export function flattenResources(resources: KubernetesResource[]) {
return flatten(resources.map((r: any) => r.apiVersion === "v1" && r.kind === "List" ? r.items : [r]))
}
136 changes: 135 additions & 1 deletion garden-service/test/unit/src/plugins/kubernetes/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from "chai"
import { millicpuToString, kilobytesToString } from "../../../../../src/plugins/kubernetes/util"
import { millicpuToString, kilobytesToString, flattenResources } from "../../../../../src/plugins/kubernetes/util"

describe("millicpuToString", () => {
it("should return a string suffixed with 'm'", () => {
Expand Down Expand Up @@ -44,3 +44,137 @@ describe("kilobytesToString", () => {
expect(kilobytesToString(100.5)).to.equal("100Ki")
})
})

describe("flattenResources", () => {
it("should return resources that don't include resources of kind List as they were", () => {
const resources = [
{
apiVersion: "v1",
kind: "ServiceAccount",
metadata: {
name: "a",
},
},
{
apiVersion: "v1",
kind: "ServiceAccount",
metadata: {
name: "b",
},
},
]
expect(flattenResources(resources).map(r => r.metadata.name)).to.eql(["a", "b"])
})
it("should flatten resourcess that contain resources of kind List", () => {
const resources = [
{
apiVersion: "v1",
items: [
{
apiVersion: "v1",
kind: "ServiceAccount",
metadata: {
name: "a",
},
},
{
apiVersion: "v1",
kind: "ServiceAccount",
metadata: {
name: "b",
},
},
],
kind: "List",
metadata: {
name: "foo",
},
},
]
expect(flattenResources(resources).map(r => r.metadata.name)).to.eql(["a", "b"])
})
it("should flatten resources that contain List and non-List resources", () => {
const resources = [
{
apiVersion: "v1",
kind: "ServiceAccount",
metadata: {
name: "a",
},
},
{
apiVersion: "v1",
items: [
{
apiVersion: "v1",
kind: "ServiceAccount",
metadata: {
name: "b",
},
},
{
apiVersion: "v1",
kind: "ServiceAccount",
metadata: {
name: "c",
},
},
],
kind: "List",
metadata: {
name: "foo",
},
},
{
apiVersion: "v1",
kind: "ServiceAccount",
metadata: {
name: "d",
},
},
]
expect(flattenResources(resources).map(r => r.metadata.name)).to.eql(["a", "b", "c", "d"])
})
it("should not flatten List resources that don't have apiVersion v1", () => {
const resources = [
{
apiVersion: "v1",
kind: "ServiceAccount",
metadata: {
name: "a",
},
},
{
apiVersion: "v2",
items: [
{
apiVersion: "v1",
kind: "ServiceAccount",
metadata: {
name: "b",
},
},
{
apiVersion: "v1",
kind: "ServiceAccount",
metadata: {
name: "c",
},
},
],
kind: "List",
metadata: {
name: "d",
},
},
{
apiVersion: "v2",
kind: "ServiceAccount",
metadata: {
name: "e",
},
},
]
expect(flattenResources(resources).map(r => r.metadata.name)).to.eql(["a", "d", "e"])
})
})

0 comments on commit b6368f7

Please sign in to comment.