Skip to content

Commit

Permalink
Merge pull request #643 from garden-io/k8s-status-check-improvement
Browse files Browse the repository at this point in the history
improvement(k8s): better deployment status checking
  • Loading branch information
thsig authored Mar 22, 2019
2 parents cb65ab4 + d84c97e commit 8be1961
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 52 deletions.
86 changes: 72 additions & 14 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 @@ -75,6 +75,7 @@
"p-queue": "^3.0.0",
"path-is-inside": "^1.0.2",
"request": "^2.88.0",
"request-promise": "^4.2.4",
"split": "^1.0.1",
"string-width": "^3.0.0",
"strip-ansi": "^5.0.0",
Expand Down Expand Up @@ -133,6 +134,7 @@
"@types/path-is-inside": "^1.0.0",
"@types/prettyjson": "0.0.28",
"@types/request": "^2.48.1",
"@types/request-promise": "^4.1.42",
"@types/string-width": "^2.0.0",
"@types/supertest": "^2.0.7",
"@types/tar": "^4.0.0",
Expand Down
77 changes: 71 additions & 6 deletions garden-service/src/plugins/kubernetes/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ import {
Policy_v1beta1Api,
} from "@kubernetes/client-node"
import { join } from "path"
import request = require("request")
import request = require("request-promise")
import { readFileSync, pathExistsSync } from "fs-extra"
import { safeLoad } from "js-yaml"
import { zip, omitBy, isObject } from "lodash"
import { GardenBaseError } from "../../exceptions"
import { homedir } from "os"
import { KubernetesResource } from "./types"
import * as dedent from "dedent"
import { LogEntry } from "../../logger/log-entry"
import { splitLast, findByName } from "../../util/util"

let kubeConfigStr: string
let kubeConfig: any
Expand Down Expand Up @@ -89,7 +91,7 @@ export class KubeApi {
}
}

async readBySpec(namespace: string, spec: KubernetesResource) {
async readBySpec(namespace: string, spec: KubernetesResource, log: LogEntry) {
// this is just awful, sorry. any better ideas? - JE
const name = spec.metadata.name

Expand Down Expand Up @@ -139,15 +141,78 @@ export class KubeApi {
case "PodDisruptionBudget":
return this.policy.readNamespacedPodDisruptionBudget(name, namespace)
default:
// Handle CRDs
const apiVersion = spec.apiVersion
const url = `${this.config.getCurrentCluster()!.server}/apis/${apiVersion}` +
`/namespaces/${namespace}/${spec.kind.toLowerCase()}/${name || spec.metadata.name}`
const baseUrl = `${this.config.getCurrentCluster()!.server}/apis/${apiVersion}`

const opts: request.Options = { method: "get", url, json: true }
const [group, version] = splitLast(apiVersion, "/")

if (!group || !version) {
throw new KubernetesError(`Invalid apiVersion ${apiVersion}`, { spec })
}

let url: string

if (!group.includes(".") && group.endsWith("k8s.io")) {
// Looks like a built-in object
// TODO: this is awful, need to find out where to look this up...
let plural: string

if (spec.kind.endsWith("s")) {
plural = spec.kind + "es"
} else if (spec.kind.endsWith("y")) {
plural = spec.kind.slice(0, spec.kind.length - 1) + "ies"
} else {
plural = spec.kind + "s"
}
// /apis/networking.istio.io/v1alpha3/namespaces/gis-backend/virtualservices/gis-elasticsearch-master
// /apis/networking.istio.io/v1alpha3/namespaces/gis-backend/virtualservices/gis-elasticsearch-master
url = spec.metadata.namespace
? `${baseUrl}/namespaces/${namespace}/${plural}/${name}`
: `${baseUrl}/${plural}/${name}`

} else {
// Must be a CRD then...
const crd = await this.findCrd(group, version, spec.kind)

const plural = crd.spec.names.plural
url = crd.spec.scope === "Namespaced"
? `${baseUrl}/namespaces/${namespace}/${plural}/${name}`
: `${baseUrl}/${plural}/${name}`
}

log.silly(`GET ${url}`)

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

return request(opts)
try {
return await request(opts)
} catch (err) {
wrapError(err)
}
}
}

async findCrd(group: string, version: string, kind: string) {
const crds = (await this.apiExtensions.listCustomResourceDefinition()).body

for (const crd of crds.items) {
if (
crd.spec.group === group &&
crd.status.acceptedNames.kind === kind &&
findByName(crd.spec.versions, version)
) {
return crd
}
}

throw new KubernetesError(`Could not find resource type ${group}/${version}/${kind}`, {
group,
version,
kind,
availableCrds: crds.items,
})
}

async upsert<K extends keyof CrudMapType>(
Expand Down
2 changes: 1 addition & 1 deletion garden-service/src/plugins/kubernetes/helm/tiller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function checkTillerStatus(ctx: PluginContext, provider: Kubernetes
...await getTillerResources(ctx, provider, log),
]

const statuses = await checkResourceStatuses(api, namespace, resources)
const statuses = await checkResourceStatuses(api, namespace, resources, log)

return combineStates(statuses.map(s => s.state))
}
Expand Down
Loading

0 comments on commit 8be1961

Please sign in to comment.