|
| 1 | +import { makeInformer, ADD, DELETE, UPDATE, KubernetesObject } from '@kubernetes/client-node'; |
1 | 2 | import logger = require('../../../common/logger'); |
2 | 3 | import WorkloadWorker = require('../../../kube-scanner'); |
3 | 4 | import { buildWorkloadMetadata } from '../../metadata-extractor'; |
4 | | -import { KubeObjectMetadata } from '../../types'; |
| 5 | +import { KubeObjectMetadata, WorkloadKind } from '../../types'; |
| 6 | +import { podWatchHandler } from './pod'; |
| 7 | +import { cronJobWatchHandler } from './cron-job'; |
| 8 | +import { daemonSetWatchHandler } from './daemon-set'; |
| 9 | +import { deploymentWatchHandler } from './deployment'; |
| 10 | +import { jobWatchHandler } from './job'; |
| 11 | +import { replicaSetWatchHandler } from './replica-set'; |
| 12 | +import { replicationControllerWatchHandler } from './replication-controller'; |
| 13 | +import { statefulSetWatchHandler } from './stateful-set'; |
| 14 | +import { k8sApi, kubeConfig } from '../../cluster'; |
| 15 | +import { IWorkloadWatchMetadata, FALSY_WORKLOAD_NAME_MARKER } from './types'; |
| 16 | + |
| 17 | +/** |
| 18 | + * This map is used in combination with the kubernetes-client Informer API |
| 19 | + * to abstract which resources to watch, what their endpoint is, how to grab |
| 20 | + * a list of the resources, and which watch actions to handle (e.g. a newly added resource). |
| 21 | + * |
| 22 | + * The Informer API is just a wrapper around Kubernetes watches that makes sure the watch |
| 23 | + * gets restarted if it dies and it also efficiently tracks changes to the watched workloads |
| 24 | + * by comparing their resourceVersion. |
| 25 | + * |
| 26 | + * The map is keyed by the "WorkloadKind" -- the type of resource we want to watch. |
| 27 | + * Legal verbs for the "handlers" are pulled from '@kubernetes/client-node'. You can |
| 28 | + * set a different handler for every verb. |
| 29 | + * (e.g. ADD-ed workloads are processed differently than DELETE-d ones) |
| 30 | + * |
| 31 | + * The "listFunc" is a callback used by the kubernetes-client to grab the watched resource |
| 32 | + * whenever Kubernetes fires a "workload changed" event and it uses the result to figure out |
| 33 | + * if the workload actually changed (by inspecting the resourceVersion). |
| 34 | + */ |
| 35 | +const workloadWatchMetadata: Readonly<IWorkloadWatchMetadata> = { |
| 36 | + [WorkloadKind.Pod]: { |
| 37 | + endpoint: '/api/v1/namespaces/{namespace}/pods', |
| 38 | + handlers: { |
| 39 | + [ADD]: podWatchHandler, |
| 40 | + [UPDATE]: podWatchHandler, |
| 41 | + }, |
| 42 | + listFactory: (namespace) => () => k8sApi.coreClient.listNamespacedPod(namespace), |
| 43 | + }, |
| 44 | + [WorkloadKind.ReplicationController]: { |
| 45 | + endpoint: '/api/v1/watch/namespaces/{namespace}/replicationcontrollers', |
| 46 | + handlers: { |
| 47 | + [DELETE]: replicationControllerWatchHandler, |
| 48 | + }, |
| 49 | + listFactory: (namespace) => () => k8sApi.coreClient.listNamespacedReplicationController(namespace), |
| 50 | + }, |
| 51 | + [WorkloadKind.CronJob]: { |
| 52 | + endpoint: '/apis/batch/v1beta1/watch/namespaces/{namespace}/cronjobs', |
| 53 | + handlers: { |
| 54 | + [DELETE]: cronJobWatchHandler, |
| 55 | + }, |
| 56 | + listFactory: (namespace) => () => k8sApi.batchUnstableClient.listNamespacedCronJob(namespace), |
| 57 | + }, |
| 58 | + [WorkloadKind.Job]: { |
| 59 | + endpoint: '/apis/batch/v1/watch/namespaces/{namespace}/jobs', |
| 60 | + handlers: { |
| 61 | + [DELETE]: jobWatchHandler, |
| 62 | + }, |
| 63 | + listFactory: (namespace) => () => k8sApi.batchClient.listNamespacedJob(namespace), |
| 64 | + }, |
| 65 | + [WorkloadKind.DaemonSet]: { |
| 66 | + endpoint: '/apis/apps/v1/watch/namespaces/{namespace}/daemonsets', |
| 67 | + handlers: { |
| 68 | + [DELETE]: daemonSetWatchHandler, |
| 69 | + }, |
| 70 | + listFactory: (namespace) => () => k8sApi.appsClient.listNamespacedDaemonSet(namespace), |
| 71 | + }, |
| 72 | + [WorkloadKind.Deployment]: { |
| 73 | + endpoint: '/apis/apps/v1/watch/namespaces/{namespace}/deployments', |
| 74 | + handlers: { |
| 75 | + [DELETE]: deploymentWatchHandler, |
| 76 | + }, |
| 77 | + listFactory: (namespace) => () => k8sApi.appsClient.listNamespacedDeployment(namespace), |
| 78 | + }, |
| 79 | + [WorkloadKind.ReplicaSet]: { |
| 80 | + endpoint: '/apis/apps/v1/watch/namespaces/{namespace}/replicasets', |
| 81 | + handlers: { |
| 82 | + [DELETE]: replicaSetWatchHandler, |
| 83 | + }, |
| 84 | + listFactory: (namespace) => () => k8sApi.appsClient.listNamespacedReplicaSet(namespace), |
| 85 | + }, |
| 86 | + [WorkloadKind.StatefulSet]: { |
| 87 | + endpoint: '/apis/apps/v1/watch/namespaces/{namespace}/statefulsets', |
| 88 | + handlers: { |
| 89 | + [DELETE]: statefulSetWatchHandler, |
| 90 | + }, |
| 91 | + listFactory: (namespace) => () => k8sApi.appsClient.listNamespacedStatefulSet(namespace), |
| 92 | + }, |
| 93 | +}; |
| 94 | + |
| 95 | +export function setupInformer(namespace: string, workloadKind: WorkloadKind) { |
| 96 | + const workloadMetadata = workloadWatchMetadata[workloadKind]; |
| 97 | + const namespacedEndpoint = workloadMetadata.endpoint.replace('{namespace}', namespace); |
| 98 | + |
| 99 | + const informer = makeInformer<KubernetesObject>(kubeConfig, namespacedEndpoint, |
| 100 | + workloadMetadata.listFactory(namespace)); |
| 101 | + |
| 102 | + for (const informerVerb of Object.keys(workloadMetadata.handlers)) { |
| 103 | + informer.on(informerVerb, async (watchedWorkload) => { |
| 104 | + try { |
| 105 | + await workloadMetadata.handlers[informerVerb](watchedWorkload); |
| 106 | + } catch (error) { |
| 107 | + const name = watchedWorkload.metadata && watchedWorkload.metadata.name || FALSY_WORKLOAD_NAME_MARKER; |
| 108 | + logger.warn({error, namespace, name, workloadKind}, 'could not execute the informer handler for a workload'); |
| 109 | + } |
| 110 | + }); |
| 111 | + } |
| 112 | + |
| 113 | + informer.start(); |
| 114 | +} |
5 | 115 |
|
6 | 116 | export async function deleteWorkload(kubernetesMetadata: KubeObjectMetadata, workloadName: string) { |
7 | 117 | try { |
|
0 commit comments