-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathstatus.ts
655 lines (563 loc) · 21.5 KB
/
status.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
/*
* Copyright (C) 2018-2023 Garden Technologies, Inc. <[email protected]>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import Bluebird from "bluebird"
import { diffString } from "json-diff"
import { DeploymentError } from "../../../exceptions"
import { PluginContext } from "../../../plugin-context"
import { KubeApi } from "../api"
import { getAppNamespace } from "../namespace"
import { KubernetesResource, KubernetesServerResource, BaseResource, KubernetesWorkload } from "../types"
import { zip, isArray, isPlainObject, pickBy, mapValues, flatten, cloneDeep, omit, isEqual, keyBy } from "lodash"
import { KubernetesProvider, KubernetesPluginContext } from "../config"
import { isSubset } from "../../../util/is-subset"
import { Log } from "../../../logger/log-entry"
import {
V1ReplicationController,
V1ReplicaSet,
V1Pod,
V1PersistentVolumeClaim,
V1Service,
V1Container,
KubernetesObject,
V1Job,
} from "@kubernetes/client-node"
import dedent = require("dedent")
import { getPods, getResourceKey, hashManifest } from "../util"
import { checkWorkloadStatus } from "./workload"
import { checkWorkloadPodStatus } from "./pod"
import { deline, gardenAnnotationKey, stableStringify } from "../../../util/string"
import { SyncableResource } from "../types"
import { ActionMode } from "../../../actions/types"
import { deepMap } from "../../../util/objects"
import { DeployState, combineStates } from "../../../types/service"
import { isTruthy, sleep } from "../../../util/util"
export interface ResourceStatus<T extends BaseResource | KubernetesObject = BaseResource> {
state: DeployState
resource: KubernetesServerResource<T>
lastMessage?: string
warning?: true
logs?: string
}
export interface StatusHandlerParams<T extends BaseResource | KubernetesObject = BaseResource> {
api: KubeApi
namespace: string
resource: KubernetesServerResource<T>
log: Log
resourceVersion?: number
waitForJobs?: boolean
}
interface StatusHandler<T extends BaseResource | KubernetesObject = BaseResource> {
(params: StatusHandlerParams<T>): Promise<ResourceStatus<T>>
}
const pvcPhaseMap: { [key: string]: DeployState } = {
Available: "ready",
Bound: "ready",
Released: "stopped",
Failed: "unhealthy",
// This is confusing, but this basically means it's waiting to be bound
Pending: "ready",
}
// Handlers to check the rollout status for K8s objects where that applies.
// Using https://github.com/kubernetes/helm/blob/master/pkg/kube/wait.go as a reference here.
const objHandlers: { [kind: string]: StatusHandler } = {
DaemonSet: checkWorkloadStatus,
Deployment: checkWorkloadStatus,
StatefulSet: checkWorkloadStatus,
PersistentVolumeClaim: async ({ resource }: StatusHandlerParams<V1PersistentVolumeClaim>) => {
const pvc = <KubernetesServerResource<V1PersistentVolumeClaim>>resource
const state: DeployState = pvcPhaseMap[pvc.status.phase!] || "unknown"
return { state, resource }
},
Pod: async ({ resource }: StatusHandlerParams<V1Pod>) => {
return checkWorkloadPodStatus(resource, [<KubernetesServerResource<V1Pod>>resource])
},
ReplicaSet: async ({ api, namespace, resource }: StatusHandlerParams<V1ReplicaSet>) => {
return checkWorkloadPodStatus(
resource,
await getPods(api, namespace, (<KubernetesServerResource<V1ReplicaSet>>resource).spec.selector!.matchLabels!)
)
},
ReplicationController: async ({ api, namespace, resource }: StatusHandlerParams<V1ReplicationController>) => {
return checkWorkloadPodStatus(resource, await getPods(api, namespace, resource.spec!.selector!))
},
Service: async ({ resource }: StatusHandlerParams<V1Service>) => {
if (resource.spec.type === "ExternalName") {
return { state: "ready", resource }
}
const service = <KubernetesServerResource<V1Service>>resource
if (resource.spec.clusterIP !== "None" && service.spec.clusterIP === "") {
return { state: "deploying", resource }
}
if (resource.spec.type === "LoadBalancer" && !service.status.loadBalancer!.ingress) {
return { state: "deploying", resource }
}
return { state: "ready", resource }
},
Job: async ({ resource, waitForJobs }: StatusHandlerParams<V1Job>) => {
if (
resource.status?.failed &&
resource.spec?.backoffLimit &&
resource.status?.failed >= resource.spec?.backoffLimit
) {
// job has failed
return { state: "unhealthy", resource }
}
if (
resource.spec?.completions &&
resource.status?.succeeded &&
resource.status?.succeeded < resource.spec.completions
) {
// job is not yet completed
return { state: "deploying", resource }
}
// job has succeeded
if (resource.status.succeeded) {
return { state: "ready", resource }
}
// wait for job only if waitForJobs is set, otherwise
// mark it as ready and proceed.
if (waitForJobs) {
return { state: "deploying", resource }
} else {
return { state: "ready", resource }
}
},
}
/**
* Check if the specified Kubernetes objects are deployed and fully rolled out
*/
export async function checkResourceStatuses(
api: KubeApi,
namespace: string,
manifests: KubernetesResource[],
log: Log,
waitForJobs?: boolean
): Promise<ResourceStatus[]> {
return Bluebird.map(manifests, async (manifest) => {
return checkResourceStatus({ api, namespace, manifest, log, waitForJobs })
})
}
export async function checkResourceStatus({
api,
namespace,
manifest,
log,
waitForJobs,
}: {
api: KubeApi
namespace: string
manifest: KubernetesResource
log: Log
waitForJobs?: boolean
}) {
const handler = objHandlers[manifest.kind]
if (manifest.metadata?.namespace) {
namespace = manifest.metadata.namespace
}
let resource: KubernetesServerResource
let resourceVersion: number | undefined
try {
resource = await api.readBySpec({ namespace, manifest, log })
resourceVersion = parseInt(resource.metadata.resourceVersion!, 10)
} catch (err) {
if (err.statusCode === 404) {
return { state: <DeployState>"missing", resource: manifest }
} else {
throw err
}
}
let status: ResourceStatus
if (handler) {
status = await handler({ api, namespace, resource, log, resourceVersion, waitForJobs })
} else {
// if there is no explicit handler to check the status, we assume there's no rollout phase to wait for
status = { state: "ready", resource: manifest }
}
return status
}
interface WaitParams {
namespace: string
ctx: PluginContext
provider: KubernetesProvider
actionName?: string
resources: KubernetesResource[]
log: Log
timeoutSec: number
waitForJobs?: boolean
}
/**
* Wait until the rollout is complete for each of the given Kubernetes objects
*/
export async function waitForResources({
namespace,
ctx,
provider,
actionName,
resources,
log,
timeoutSec,
waitForJobs,
}: WaitParams) {
let loops = 0
const startTime = new Date().getTime()
const logEventContext = {
origin: "kubernetes-plugin",
level: "verbose" as const,
}
const emitLog = (msg: string) =>
ctx.events.emit("log", { timestamp: new Date().toISOString(), msg, ...logEventContext })
const waitingMsg = `Waiting for resources to be ready...`
const statusLine = log
.createLog({
// TODO: Avoid setting fallback, the action name should be known
name: actionName || "<kubernetes>",
})
.info(waitingMsg)
emitLog(waitingMsg)
if (resources.length === 0) {
const noResourcesMsg = `No resources to wait for`
emitLog(noResourcesMsg)
statusLine.info(noResourcesMsg)
return []
}
const api = await KubeApi.factory(log, ctx, provider)
const results: { [key: string]: ResourceStatus } = {}
const pendingResources = keyBy(resources, getResourceKey)
while (true) {
await sleep(2000 + 500 * loops)
loops += 1
const statuses = await checkResourceStatuses(api, namespace, Object.values(pendingResources), log, waitForJobs)
for (const status of statuses) {
const key = getResourceKey(status.resource)
const lastMessage = results[key]?.lastMessage
results[key] = status
// Avoid unnecessary polling
if (status.state === "ready") {
delete pendingResources[key]
}
const resource = status.resource
const statusMessage = `${resource.kind} ${resource.metadata.name} is "${status.state}"`
const statusLogMsg = `Status of ${statusMessage}`
log.debug(statusLogMsg)
emitLog(statusLogMsg)
if (status.state === "unhealthy") {
let msg = `Error deploying ${actionName || "resources"}: ${status.lastMessage || statusMessage}`
if (status.logs) {
msg += "\n\n" + status.logs
}
emitLog(msg)
throw new DeploymentError({
message: msg,
detail: {
serviceName: actionName,
status,
},
})
}
if (status.lastMessage && status.lastMessage !== lastMessage) {
const statusUpdateLogMsg = `${getResourceKey(status.resource)}: ${status.lastMessage}`
if (status.warning) {
statusLine.warn(statusUpdateLogMsg)
} else {
statusLine.info(statusUpdateLogMsg)
}
emitLog(statusUpdateLogMsg)
}
}
const combinedStates = combineStates(statuses.map((s) => s.state))
// Note: "stopped" is a normal state for Pods, which run to completion
if (combinedStates === "ready" || combinedStates === "stopped") {
break
}
const now = new Date().getTime()
if (now - startTime > timeoutSec * 1000) {
const deploymentErrMsg = deline`
Timed out waiting for ${actionName || "resources"} to deploy after ${timeoutSec} seconds
`
emitLog(deploymentErrMsg)
throw new DeploymentError({ message: deploymentErrMsg, detail: { statuses } })
}
}
const readyMsg = `Resources ready`
emitLog(readyMsg)
statusLine.info(readyMsg)
return Object.values(results)
}
interface ComparisonResult {
state: DeployState
remoteResources: KubernetesResource[]
mode: ActionMode
/**
* These resources have changes in `spec.selector`, and would need to be deleted before redeploying (since Kubernetes
* doesn't allow updates to immutable fields).
*/
selectorChangedResourceKeys: string[]
}
/**
* Check if each of the given Kubernetes objects matches what's installed in the cluster
*/
export async function compareDeployedResources({
ctx,
api,
namespace,
manifests,
log,
}: {
ctx: KubernetesPluginContext
api: KubeApi
namespace: string
manifests: KubernetesResource[]
log: Log
}): Promise<ComparisonResult> {
// Unroll any `List` resource types
manifests = flatten(manifests.map((r: any) => (r.apiVersion === "v1" && r.kind === "List" ? r.items : [r])))
// Check if any resources are missing from the cluster.
const maybeDeployedObjects = await Bluebird.map(manifests, (resource) =>
getDeployedResource(ctx, ctx.provider, resource, log)
)
const deployedResources = <KubernetesResource[]>maybeDeployedObjects.filter((o) => o !== null)
const manifestsMap = keyBy(manifests, (m) => getResourceKey(m))
const manifestKeys = Object.keys(manifestsMap)
const deployedMap = keyBy(deployedResources, (m) => getResourceKey(m))
const result: ComparisonResult = {
state: "unknown",
remoteResources: <KubernetesResource[]>deployedResources.filter((o) => o !== null),
mode: "default",
selectorChangedResourceKeys: detectChangedSpecSelector(manifestsMap, deployedMap),
}
const logDescription = (resource: KubernetesResource) => getResourceKey(resource)
const missingObjectNames = manifestKeys.filter((k) => !deployedMap[k]).map((k) => logDescription(manifestsMap[k]))
if (missingObjectNames.length === manifests.length) {
// All resources missing.
log.verbose(`All resources missing from cluster`)
result.state = "missing"
return result
} else if (missingObjectNames.length > 0) {
// One or more objects missing.
log.verbose(`Resource(s) ${missingObjectNames.join(", ")} missing from cluster`)
result.state = "outdated"
return result
}
// From here, the state can only be "ready" or "outdated", so we proceed to compare the old & new specs.
log.debug(`Getting currently deployed resource statuses...`)
const deployedObjectStatuses: ResourceStatus[] = await Bluebird.map(deployedResources, async (resource) =>
checkResourceStatus({ api, namespace, manifest: resource, log })
)
const deployedStates = deployedObjectStatuses.map((s) => s.state)
if (deployedStates.find((s) => s !== "ready")) {
const descriptions = zip(deployedResources, deployedStates)
.filter(([_, s]) => s !== "ready")
.map(([o, s]) => `${logDescription(o!)}: "${s}"`)
.join("\n")
log.silly(
dedent`
Resource(s) with non-ready status found in the cluster:
${descriptions}` + "\n"
)
result.state = combineStates(deployedStates)
return result
}
log.debug(`Comparing expected and deployed resources...`)
for (const key of Object.keys(manifestsMap)) {
let manifest = cloneDeep(manifestsMap[key])
let deployedResource = deployedMap[key]
if (!manifest.metadata.annotations) {
manifest.metadata.annotations = {}
}
// Discard any last applied config from the input manifest
const hashKey = gardenAnnotationKey("manifest-hash")
if (manifest.metadata?.annotations?.[hashKey]) {
delete manifest.metadata?.annotations?.[hashKey]
}
if (manifest.spec?.template?.metadata?.annotations?.[hashKey]) {
delete manifest.spec?.template?.metadata?.annotations?.[hashKey]
}
if (isWorkloadResource(manifest)) {
if (isConfiguredForSyncMode(manifest)) {
result.mode = "sync"
}
if (isConfiguredForLocalMode(manifest)) {
result.mode = "local"
}
}
// Start by checking for "last applied configuration" annotations and comparing against those.
// This can be more accurate than comparing against resolved resources.
if (deployedResource.metadata && deployedResource.metadata.annotations) {
const lastAppliedHashed = deployedResource.metadata.annotations[gardenAnnotationKey("manifest-hash")]
// The new manifest matches the last applied manifest
if (lastAppliedHashed && (await hashManifest(manifest)) === lastAppliedHashed) {
continue
}
// Fallback to comparing against kubectl's last-applied-configuration annotation
const lastApplied = deployedResource.metadata.annotations["kubectl.kubernetes.io/last-applied-configuration"]
if (lastApplied && stableStringify(manifest) === lastApplied) {
continue
}
}
// to avoid normalization issues, we convert all numeric values to strings and then compare
manifest = <KubernetesResource>deepMap(manifest, (v) => (typeof v === "number" ? v.toString() : v))
deployedResource = <KubernetesResource>deepMap(deployedResource, (v) => (typeof v === "number" ? v.toString() : v))
// the API version may implicitly change when deploying
manifest.apiVersion = deployedResource.apiVersion
// the namespace property is silently dropped when added to non-namespaced resources
if (manifest.metadata?.namespace && deployedResource.metadata?.namespace === undefined) {
delete manifest.metadata.namespace
}
if (!deployedResource.metadata.annotations) {
deployedResource.metadata.annotations = {}
}
// handle auto-filled properties (this is a bit of a design issue in the K8s API)
if (manifest.kind === "Service" && manifest.spec.clusterIP === "") {
delete manifest.spec.clusterIP
}
// NOTE: this approach won't fly in the long run, but hopefully we can climb out of this mess when
// `kubectl diff` is ready, or server-side apply/diff is ready
if (manifest.kind === "DaemonSet" || manifest.kind === "Deployment" || manifest.kind === "StatefulSet") {
// NOTE: this approach won't fly in the long run, but hopefully we can climb out of this mess when
// `kubectl diff` is ready, or server-side apply/diff is ready
// handle properties that are omitted in the response because they have the default value
// (another design issue in the K8s API)
if (manifest.spec.minReadySeconds === 0) {
delete manifest.spec.minReadySeconds
}
if (manifest.spec.template && manifest.spec.template.spec && manifest.spec.template.spec.hostNetwork === false) {
delete manifest.spec.template.spec.hostNetwork
}
}
// clean null and undefined values
manifest = <KubernetesResource>removeNullAndUndefined(manifest)
// The Kubernetes API currently strips out environment variables values so we remove them
// from the manifests as well
manifest = removeEmptyEnvValues(manifest)
// ...and from the deployedResource for good measure, in case the K8s API changes.
deployedResource = removeEmptyEnvValues(deployedResource)
if (!isSubset(deployedResource, manifest)) {
if (manifest) {
log.debug(`Resource ${manifest.metadata.name} is not a superset of deployed resource`)
log.silly(diffString(deployedResource, manifest))
}
// console.log(JSON.stringify(resource, null, 4))
// console.log(JSON.stringify(existingSpec, null, 4))
// console.log("----------------------------------------------------")
// throw new Error("bla")
result.state = "outdated"
return result
}
}
log.debug(`All resources match.`)
result.state = "ready"
return result
}
export function isConfiguredForSyncMode(resource: SyncableResource): boolean {
return resource.metadata.annotations?.[gardenAnnotationKey("mode")] === "sync"
}
export function isConfiguredForLocalMode(resource: SyncableResource): boolean {
return resource.metadata.annotations?.[gardenAnnotationKey("mode")] === "local"
}
function isWorkloadResource(resource: KubernetesResource): resource is KubernetesWorkload {
return (
resource.kind === "Deployment" ||
resource.kind === "DaemonSet" ||
resource.kind === "StatefulSet" ||
resource.kind === "ReplicaSet"
)
}
type KubernetesResourceMap = { [key: string]: KubernetesResource }
function detectChangedSpecSelector(manifestsMap: KubernetesResourceMap, deployedMap: KubernetesResourceMap): string[] {
const manifestKeys = Object.keys(manifestsMap)
const changedKeys: string[] = []
for (const k of manifestKeys) {
const manifest = manifestsMap[k]
const deployedResource = deployedMap[k]
if (
deployedResource && // If no corresponding resource to the local manifest has been deployed, this will be undefined.
isWorkloadResource(manifest) &&
isWorkloadResource(deployedResource) &&
!isEqual(manifest.spec.selector, deployedResource.spec.selector)
) {
changedKeys.push(getResourceKey(manifest))
}
}
return changedKeys
}
export async function getDeployedResource<ResourceKind extends KubernetesObject>(
ctx: PluginContext,
provider: KubernetesProvider,
manifest: KubernetesResource<ResourceKind>,
log: Log
): Promise<KubernetesResource<ResourceKind> | null> {
const api = await KubeApi.factory(log, ctx, provider)
const k8sCtx = ctx as KubernetesPluginContext
const namespace = manifest.metadata?.namespace || (await getAppNamespace(k8sCtx, log, provider))
try {
const res = await api.readBySpec({ namespace, manifest, log })
return <KubernetesResource<ResourceKind>>res
} catch (err) {
if (err.statusCode === 404) {
return null
} else {
throw err
}
}
}
/**
* Fetches matching deployed resources from the cluster for the provided array of manifests.
*/
export async function getDeployedResources<ResourceKind extends KubernetesObject>({
ctx,
manifests,
log,
}: {
ctx: KubernetesPluginContext
manifests: KubernetesResource<ResourceKind>[]
log: Log
}): Promise<KubernetesResource<ResourceKind>[]> {
const maybeDeployedObjects = await Bluebird.map(manifests, async (resource) =>
getDeployedResource(ctx, ctx.provider, resource, log)
)
return maybeDeployedObjects.filter(isTruthy)
}
/**
* Recursively removes all null and undefined value properties from objects
*/
function removeNullAndUndefined<T>(value: T | Iterable<T>): T | Iterable<T> | { [K in keyof T]: T[K] } {
if (isArray(value)) {
return value.map(removeNullAndUndefined)
} else if (isPlainObject(value)) {
return <{ [K in keyof T]: T[K] }>mapValues(
pickBy(<any>value, (v) => v !== null && v !== undefined),
removeNullAndUndefined
)
} else {
return value
}
}
/**
* Normalize Kubernetes container specs by removing empty environment variable values. We need
* this because the Kubernetes API strips out these empty values.
*
* That is, something like { "name": FOO, "value": "" } becomes { "name": FOO } when
* we read the deployed resource from the K8s API.
*
* Calling this function ensures a given manifest will look the same as actual deployed resource.
*/
function removeEmptyEnvValues(resource: KubernetesResource): KubernetesResource {
if (resource.spec?.template?.spec?.containers && resource.spec.template.spec.containers.length > 0) {
const containerSpecs = resource.spec.template.spec.containers.map((container: V1Container) => {
const env = container.env?.map((envKvPair) => {
return envKvPair.value === "" ? omit(envKvPair, "value") : envKvPair
})
if (env) {
container["env"] = env
}
return container
})
resource.spec.template.spec["containers"] = containerSpecs
}
return resource
}