Skip to content

Commit

Permalink
fix(k8s): handle CRDs properly
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed Mar 8, 2019
1 parent 2b8c8ee commit 73f48bf
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 41 deletions.
85 changes: 63 additions & 22 deletions garden-service/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions garden-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"normalize-url": "^4.1.0",
"p-queue": "^3.0.0",
"path-is-inside": "^1.0.2",
"request": "^2.88.0",
"split": "^1.0.1",
"string-width": "^3.0.0",
"strip-ansi": "^5.0.0",
Expand Down Expand Up @@ -129,6 +130,7 @@
"@types/p-queue": "^3.0.0",
"@types/path-is-inside": "^1.0.0",
"@types/prettyjson": "0.0.28",
"@types/request": "^2.48.1",
"@types/string-width": "^2.0.0",
"@types/supertest": "^2.0.7",
"@types/tar": "^4.0.0",
Expand Down
21 changes: 14 additions & 7 deletions garden-service/src/plugins/kubernetes/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ import {
Policy_v1beta1Api,
} from "@kubernetes/client-node"
import { join } from "path"
import request = require("request")
import { readFileSync, pathExistsSync } from "fs-extra"
import { safeLoad } from "js-yaml"
import { zip, omitBy, isObject } from "lodash"
import { GardenBaseError, ConfigurationError } from "../../exceptions"
import { GardenBaseError } from "../../exceptions"
import { homedir } from "os"
import { KubernetesProvider } from "./kubernetes"
import { KubernetesResource } from "./types"
Expand Down Expand Up @@ -72,6 +73,7 @@ export class KubernetesError extends GardenBaseError {

export class KubeApi {
public context: string
private config: KubeConfig

public apiExtensions: Apiextensions_v1beta1Api
public apps: Apps_v1Api
Expand All @@ -82,11 +84,11 @@ export class KubeApi {

constructor(public provider: KubernetesProvider) {
this.context = provider.config.context
const config = getConfig(this.context)
this.config = getConfig(this.context)

for (const [name, cls] of Object.entries(apiTypes)) {
const api = new cls(config.getCurrentCluster()!.server)
this[name] = this.proxyApi(api, config)
const api = new cls(this.config.getCurrentCluster()!.server)
this[name] = this.proxyApi(api, this.config)
}
}

Expand Down Expand Up @@ -140,9 +142,14 @@ export class KubeApi {
case "PodDisruptionBudget":
return this.policy.readNamespacedPodDisruptionBudget(name, namespace)
default:
throw new ConfigurationError(`Unsupported Kubernetes spec kind: ${spec.kind}`, {
spec,
})
const apiVersion = spec.apiVersion
const url = `${this.config.getCurrentCluster()!.server}/apis/${apiVersion}` +
`/namespaces/${namespace}/${spec.kind.toLowerCase()}/${name || spec.metadata.name}`

const opts: request.Options = { method: "get", url, json: true }
this.config.applyToRequest(opts)

return request(opts)
}
}

Expand Down
24 changes: 13 additions & 11 deletions garden-service/src/plugins/kubernetes/helm/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { find, isEmpty } from "lodash"
import { find, isEmpty, isPlainObject } from "lodash"
import { join } from "path"
import { pathExists, writeFile, remove } from "fs-extra"
import cryptoRandomString = require("crypto-random-string")
Expand Down Expand Up @@ -54,23 +54,13 @@ export async function getChartResources(ctx: PluginContext, module: Module, log:

return objects
.filter(obj => {
if (obj === null) {
return false
}

const helmHook = getAnnotation(obj, "helm.sh/hook")
if (helmHook && helmHook.startsWith("test-")) {
return false
}

return true
})
.map((obj) => {
if (!obj.metadata.annotations) {
obj.metadata.annotations = {}
}
return obj
})
}

/**
Expand Down Expand Up @@ -310,4 +300,16 @@ async function renderHelmTemplateString(
*/
function loadTemplate(template: string) {
return loadAll(template, undefined, { json: true })
.filter(obj => obj !== null)
.map((obj) => {
if (isPlainObject(obj)) {
if (!obj.metadata) {
obj.metadata = {}
}
if (!obj.metadata.annotations) {
obj.metadata.annotations = {}
}
}
return obj
})
}
1 change: 1 addition & 0 deletions garden-service/src/plugins/kubernetes/helm/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export async function getReleaseStatus(
namespace: string, context: string, releaseName: string, log: LogEntry,
): Promise<ServiceStatus> {
try {
log.silly(`Getting the release status for ${releaseName}`)
const res = JSON.parse(await helm(namespace, context, log, "status", releaseName, "--output", "json"))
const statusCode = res.info.status.code
return {
Expand Down
2 changes: 1 addition & 1 deletion garden-service/src/plugins/kubernetes/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ async function getDeployedObject(

try {
const res = await api.readBySpec(namespace, obj)
return res.body
return <KubernetesResource>res.body
} catch (err) {
if (err.code === 404) {
return null
Expand Down

0 comments on commit 73f48bf

Please sign in to comment.