Skip to content

Commit 4f8abcb

Browse files
committed
chore: split the Kubernetes-Monitor to a supervisor/scanner
- the supervisor aims to be responsible for listening on the Kubernetes-API, setting up watches, handle metadata and scheduling workers/scans - the scanner aims to be responsible for pulling images and scanning them for their dependency graph this commit attempts to serve two purposes: 1. organise our code a bit more so each bit has clearer responsibilities 2. prepare for splitting to two different software bits so we could scale our workers/scanners
1 parent 53ce3a8 commit 4f8abcb

29 files changed

+75
-75
lines changed

src/images/skopeo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { SpawnPromiseResult } from 'child-process-promise';
33
import * as processWrapper from '../common/process';
44
import * as config from'../common/config';
55
import * as credentials from './credentials';
6-
import { SkopeoRepositoryType } from '../scanner/types';
6+
import { SkopeoRepositoryType } from './types';
77

88
function getUniqueIdentifier(): string {
99
const [seconds, nanoseconds] = process.hrtime();

src/images/types.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,13 @@ export interface IPullableImage {
22
imageName: string;
33
fileSystemPath: string;
44
}
5+
6+
/**
7+
* https://github.com/containers/skopeo
8+
*/
9+
export enum SkopeoRepositoryType {
10+
DockerArchive = 'docker-archive',
11+
OciArchive = 'oci',
12+
ImageRegistry = 'docker',
13+
Directory = 'dir', // Note, skopeo marks this as a non-standard format
14+
}

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as SourceMapSupport from 'source-map-support';
22
import logger = require('./common/logger');
3-
import { currentClusterName } from './scanner/cluster';
4-
import { beginWatchingWorkloads } from './scanner/watchers';
3+
import { currentClusterName } from './supervisor/cluster';
4+
import { beginWatchingWorkloads } from './supervisor/watchers';
55

66
process.on('uncaughtException', (err) => {
77
try {

src/scanner/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export = class WorkloadWorker {
3434
}
3535
}
3636

37+
// TODO: should be extracted from here and moved to the supervisor
3738
public async delete(localWorkloadLocator: ILocalWorkloadLocator): Promise<void> {
3839
const deletePayload = constructDeleteWorkloadPayload(localWorkloadLocator);
3940
logger.info({workloadName: this.name, workload: localWorkloadLocator},

src/scanner/types.ts

Lines changed: 4 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,7 @@
1-
import { AppsV1Api, BatchV1Api, BatchV1beta1Api, CoreV1Api, KubeConfig,
2-
V1ObjectMeta, V1OwnerReference, V1PodSpec } from '@kubernetes/client-node';
3-
4-
export enum WorkloadKind {
5-
Deployment = 'Deployment',
6-
ReplicaSet = 'ReplicaSet',
7-
StatefulSet = 'StatefulSet',
8-
DaemonSet = 'DaemonSet',
9-
Job = 'Job',
10-
CronJob = 'CronJob',
11-
ReplicationController = 'ReplicationController',
12-
Pod = 'Pod',
13-
}
14-
15-
/**
16-
* https://github.com/containers/skopeo
17-
*/
18-
export enum SkopeoRepositoryType {
19-
DockerArchive = 'docker-archive',
20-
OciArchive = 'oci',
21-
ImageRegistry = 'docker',
22-
Directory = 'dir', // Note, skopeo marks this as a non-standard format
1+
export interface IScanResult {
2+
image: string;
3+
imageWithTag: string;
4+
pluginResult: any;
235
}
246

257
export enum StaticAnalysisImageType {
@@ -31,42 +13,3 @@ export interface IStaticAnalysisOptions {
3113
imageType: StaticAnalysisImageType;
3214
tmpDirPath: string;
3315
}
34-
35-
export interface IKubeObjectMetadata {
36-
kind: string;
37-
objectMeta: V1ObjectMeta;
38-
specMeta: V1ObjectMeta;
39-
podSpec: V1PodSpec;
40-
ownerRefs: V1OwnerReference[] | undefined;
41-
revision?: number;
42-
}
43-
44-
export interface IScanResult {
45-
image: string;
46-
imageWithTag: string;
47-
pluginResult: any;
48-
}
49-
50-
export interface IK8sClients {
51-
readonly appsClient: AppsV1Api;
52-
readonly coreClient: CoreV1Api;
53-
readonly batchClient: BatchV1Api;
54-
readonly batchUnstableClient: BatchV1beta1Api;
55-
}
56-
57-
export class K8sClients implements IK8sClients {
58-
public readonly appsClient: AppsV1Api;
59-
public readonly coreClient: CoreV1Api;
60-
public readonly batchClient: BatchV1Api;
61-
// TODO: Keep an eye on this! We need v1beta1 API for CronJobs.
62-
// https://kubernetes.io/docs/concepts/overview/kubernetes-api/#api-versioning
63-
// CronJobs will appear in v2 API, but for now there' only v2alpha1, so it's a bad idea to use it.
64-
public readonly batchUnstableClient: BatchV1beta1Api;
65-
66-
constructor(config: KubeConfig) {
67-
this.appsClient = config.makeApiClient(AppsV1Api);
68-
this.coreClient = config.makeApiClient(CoreV1Api);
69-
this.batchClient = config.makeApiClient(BatchV1Api);
70-
this.batchUnstableClient = config.makeApiClient(BatchV1beta1Api);
71-
}
72-
}
File renamed without changes.
File renamed without changes.

src/supervisor/types.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { AppsV1Api, BatchV1Api, BatchV1beta1Api, CoreV1Api, KubeConfig,
2+
V1ObjectMeta, V1OwnerReference, V1PodSpec } from '@kubernetes/client-node';
3+
4+
export enum WorkloadKind {
5+
Deployment = 'Deployment',
6+
ReplicaSet = 'ReplicaSet',
7+
StatefulSet = 'StatefulSet',
8+
DaemonSet = 'DaemonSet',
9+
Job = 'Job',
10+
CronJob = 'CronJob',
11+
ReplicationController = 'ReplicationController',
12+
Pod = 'Pod',
13+
}
14+
15+
export interface IKubeObjectMetadata {
16+
kind: string;
17+
objectMeta: V1ObjectMeta;
18+
specMeta: V1ObjectMeta;
19+
podSpec: V1PodSpec;
20+
ownerRefs: V1OwnerReference[] | undefined;
21+
revision?: number;
22+
}
23+
24+
export interface IK8sClients {
25+
readonly appsClient: AppsV1Api;
26+
readonly coreClient: CoreV1Api;
27+
readonly batchClient: BatchV1Api;
28+
readonly batchUnstableClient: BatchV1beta1Api;
29+
}
30+
31+
export class K8sClients implements IK8sClients {
32+
public readonly appsClient: AppsV1Api;
33+
public readonly coreClient: CoreV1Api;
34+
public readonly batchClient: BatchV1Api;
35+
// TODO: Keep an eye on this! We need v1beta1 API for CronJobs.
36+
// https://kubernetes.io/docs/concepts/overview/kubernetes-api/#api-versioning
37+
// CronJobs will appear in v2 API, but for now there' only v2alpha1, so it's a bad idea to use it.
38+
public readonly batchUnstableClient: BatchV1beta1Api;
39+
40+
constructor(config: KubeConfig) {
41+
this.appsClient = config.makeApiClient(AppsV1Api);
42+
this.coreClient = config.makeApiClient(CoreV1Api);
43+
this.batchClient = config.makeApiClient(BatchV1Api);
44+
this.batchUnstableClient = config.makeApiClient(BatchV1beta1Api);
45+
}
46+
}

0 commit comments

Comments
 (0)