Skip to content

Commit

Permalink
fix(k8s): error in status checks for missing CRD manifests
Browse files Browse the repository at this point in the history
Fixes #2122
  • Loading branch information
edvald authored and thsig committed Feb 4, 2021
1 parent c4dac8b commit 4713cbd
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
4 changes: 3 additions & 1 deletion core/src/plugins/kubernetes/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,10 +280,12 @@ export class KubeApi {
const resource = resourceMap[kind]

if (!resource) {
throw new KubernetesError(`Unrecognized resource type ${apiVersion}/${kind}`, {
const err = new KubernetesError(`Unrecognized resource type ${apiVersion}/${kind}`, {
apiVersion,
kind,
})
err.statusCode = 404
throw err
}

return resource
Expand Down
13 changes: 10 additions & 3 deletions core/src/plugins/kubernetes/kubernetes-module/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,17 @@ export async function getManifests({
return Bluebird.map(manifests, async (manifest) => {
// Ensure a namespace is set, if not already set, and if required by the resource type
if (!manifest.metadata.namespace) {
const info = await api.getApiResourceInfo(log, manifest.apiVersion, manifest.kind)
try {
const info = await api.getApiResourceInfo(log, manifest.apiVersion, manifest.kind)

if (info.namespaced) {
manifest.metadata.namespace = defaultNamespace
if (info.namespaced) {
manifest.metadata.namespace = defaultNamespace
}
} catch (err) {
// We can get a 404 if a resource type can't be found, e.g. a missing CRD
if (err.statusCode !== 404) {
throw err
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import { getDeployedResource } from "../../../../../../src/plugins/kubernetes/st
import { ModuleConfig } from "../../../../../../src/config/module"
import { KubernetesResource, BaseResource } from "../../../../../../src/plugins/kubernetes/types"
import { DeleteServiceTask } from "../../../../../../src/tasks/delete-service"
import { deployKubernetesService } from "../../../../../../src/plugins/kubernetes/kubernetes-module/handlers"
import {
deployKubernetesService,
getKubernetesServiceStatus,
} from "../../../../../../src/plugins/kubernetes/kubernetes-module/handlers"
import { emptyRuntimeContext } from "../../../../../../src/runtime-context"
import Bluebird from "bluebird"
import { buildHelmModules } from "../helm/common"
Expand Down Expand Up @@ -113,6 +116,33 @@ describe("kubernetes-module handlers", () => {
await tmpDir.cleanup()
})

describe("getServiceStatus", () => {
it("should return missing status for a manifest with a missing resource type", async () => {
const graph = await garden.getConfigGraph(garden.log)
const service = graph.getService("module-simple")
const deployParams = {
ctx,
log: garden.log,
module: service.module,
service,
force: false,
hotReload: false,
runtimeContext: emptyRuntimeContext,
}
service.module.spec.manifests = [
{
apiVersion: "foo.bar/baz",
kind: "Whatever",
metadata: { name: "foo" },
spec: {},
},
]

const status = await getKubernetesServiceStatus(deployParams)
expect(status.state).to.equal("missing")
})
})

describe("deployKubernetesService", () => {
it("should successfully deploy when serviceResource doesn't have a containerModule", async () => {
const graph = await garden.getConfigGraph(garden.log)
Expand Down

0 comments on commit 4713cbd

Please sign in to comment.