Skip to content

Commit 19b6341

Browse files
authored
Merge pull request #718 from snyk/feat/deployment-config
Feat/deployment config
2 parents 498a017 + 9c08163 commit 19b6341

File tree

16 files changed

+301
-138
lines changed

16 files changed

+301
-138
lines changed

Dockerfile

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ RUN chmod 755 /usr/bin/dumb-init
3434
RUN groupadd -g 10001 snyk
3535
RUN useradd -g snyk -d /srv/app -u 10001 snyk
3636

37-
# @kubernetes/[email protected] started using net-keepalive, which requires the following packages to build modules
38-
RUN yum --disableplugin=subscription-manager install -y make gcc gcc-c++
39-
4037
WORKDIR /srv/app
4138

4239
COPY --chown=snyk:snyk --from=skopeo-build /usr/bin/skopeo /usr/bin/skopeo
@@ -53,8 +50,6 @@ RUN mkdir -p .config
5350

5451
RUN npm install
5552

56-
RUN yum remove -y make gcc gcc-c++
57-
5853
# add the rest of the app files
5954
ADD --chown=snyk:snyk . .
6055

package-lock.json

Lines changed: 6 additions & 96 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"lint": "eslint \"src/**/*.ts\" && (cd test && eslint \"**/*.ts\")"
3434
},
3535
"dependencies": {
36-
"@kubernetes/client-node": "^0.14.2",
36+
"@kubernetes/client-node": "^0.14.3",
3737
"@snyk/dep-graph": "^1.28.0",
3838
"async": "^3.2.0",
3939
"aws-sdk": "^2.873.0",

snyk-monitor/templates/clusterrole.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ rules:
5353
- get
5454
- list
5555
- watch
56+
- apiGroups:
57+
- apps.openshift.io
58+
resources:
59+
- deploymentconfigs
60+
verbs:
61+
- get
62+
- list
63+
- watch
5664
{{- if .Values.psp.enabled }}
5765
- apiGroups:
5866
- policy

snyk-monitor/templates/role.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ rules:
5151
- get
5252
- list
5353
- watch
54+
- apiGroups:
55+
- apps.openshift.io
56+
resources:
57+
- deploymentconfigs
58+
verbs:
59+
- get
60+
- list
61+
- watch
5462
{{- if .Values.psp.enabled }}
5563
- apiGroups:
5664
- policy

snyk-operator/deploy/olm-catalog/snyk-operator/0.0.0/snyk-operator.v0.0.0.clusterserviceversion.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@ spec:
221221
- "*"
222222
verbs:
223223
- "*"
224+
- apiGroups:
225+
- apps.openshift.io
226+
resources:
227+
- deploymentconfigs
228+
verbs:
229+
- "*"
224230
serviceAccountName: snyk-operator
225231
deployments:
226232
- name: snyk-operator

src/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ process.on('uncaughtException', (err) => {
2323
}
2424
});
2525

26-
process.on('unhandledRejection', (reason) => {
26+
process.on('unhandledRejection', (reason, promise) => {
2727
if (state.shutdownInProgress) {
2828
return;
2929
}
3030

3131
try {
32-
logger.error({reason}, 'UNHANDLED REJECTION!');
32+
logger.error({ reason, promise }, 'UNHANDLED REJECTION!');
3333
} catch (ignore) {
34-
console.log('UNHANDLED REJECTION!', reason);
34+
console.log('UNHANDLED REJECTION!', reason, promise);
3535
} finally {
3636
process.exit(1);
3737
}
@@ -47,10 +47,10 @@ function cleanUpTempStorage() {
4747
}
4848
};
4949

50-
function monitor(): void {
50+
async function monitor(): Promise<void> {
5151
try {
5252
logger.info({cluster: currentClusterName}, 'starting to monitor');
53-
beginWatchingWorkloads();
53+
await beginWatchingWorkloads();
5454
} catch (error) {
5555
logger.error({error}, 'an error occurred while monitoring the cluster');
5656
process.exit(1);
@@ -63,5 +63,5 @@ cleanUpTempStorage();
6363
// Allow running in an async context
6464
setImmediate(async function setUpAndMonitor(): Promise<void> {
6565
await loadAndSendWorkloadAutoImportPolicy();
66-
monitor();
66+
await monitor();
6767
});

src/supervisor/types.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1-
import { IncomingMessage } from 'http';
2-
import { AppsV1Api, BatchV1Api, BatchV1beta1Api, CoreV1Api, KubeConfig,
3-
V1ObjectMeta, V1OwnerReference, V1PodSpec } from '@kubernetes/client-node';
1+
import { IncomingMessage } from 'http';
2+
import {
3+
AppsV1Api,
4+
BatchV1Api,
5+
BatchV1beta1Api,
6+
CoreV1Api,
7+
CustomObjectsApi,
8+
KubeConfig,
9+
V1ObjectMeta,
10+
V1OwnerReference,
11+
V1PodSpec,
12+
} from '@kubernetes/client-node';
413

514
export enum WorkloadKind {
615
Deployment = 'Deployment',
@@ -11,6 +20,7 @@ export enum WorkloadKind {
1120
CronJob = 'CronJob',
1221
ReplicationController = 'ReplicationController',
1322
Pod = 'Pod',
23+
DeploymentConfig = 'DeploymentConfig',
1424
}
1525

1626
export interface IRequestError {
@@ -32,6 +42,7 @@ export interface IK8sClients {
3242
readonly coreClient: CoreV1Api;
3343
readonly batchClient: BatchV1Api;
3444
readonly batchUnstableClient: BatchV1beta1Api;
45+
readonly customObjectsClient: CustomObjectsApi;
3546
}
3647

3748
export class K8sClients implements IK8sClients {
@@ -41,12 +52,17 @@ export class K8sClients implements IK8sClients {
4152
// TODO: Keep an eye on this! We need v1beta1 API for CronJobs.
4253
// https://kubernetes.io/docs/concepts/overview/kubernetes-api/#api-versioning
4354
// CronJobs will appear in v2 API, but for now there' only v2alpha1, so it's a bad idea to use it.
55+
// TODO: https://kubernetes.io/blog/2021/04/09/kubernetes-release-1.21-cronjob-ga/
56+
// CronJobs are now GA in Kubernetes 1.21 in the batch/v1 API, we should add support for it!
4457
public readonly batchUnstableClient: BatchV1beta1Api;
58+
/** This client is used to access Custom Resources in the cluster, e.g. DeploymentConfig on OpenShift. */
59+
public readonly customObjectsClient: CustomObjectsApi;
4560

4661
constructor(config: KubeConfig) {
4762
this.appsClient = config.makeApiClient(AppsV1Api);
4863
this.coreClient = config.makeApiClient(CoreV1Api);
4964
this.batchClient = config.makeApiClient(BatchV1Api);
5065
this.batchUnstableClient = config.makeApiClient(BatchV1beta1Api);
66+
this.customObjectsClient = config.makeApiClient(CustomObjectsApi);
5167
}
5268
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { deleteWorkload } from './workload';
2+
import { WorkloadKind } from '../../types';
3+
import { FALSY_WORKLOAD_NAME_MARKER, V1DeploymentConfig } from './types';
4+
5+
export async function deploymentConfigWatchHandler(
6+
deploymentConfig: V1DeploymentConfig,
7+
): Promise<void> {
8+
if (
9+
!deploymentConfig.metadata ||
10+
!deploymentConfig.spec ||
11+
!deploymentConfig.spec.template.metadata ||
12+
!deploymentConfig.spec.template.spec ||
13+
!deploymentConfig.status
14+
) {
15+
return;
16+
}
17+
18+
const workloadName =
19+
deploymentConfig.metadata.name || FALSY_WORKLOAD_NAME_MARKER;
20+
21+
await deleteWorkload(
22+
{
23+
kind: WorkloadKind.DeploymentConfig,
24+
objectMeta: deploymentConfig.metadata,
25+
specMeta: deploymentConfig.spec.template.metadata,
26+
ownerRefs: deploymentConfig.metadata.ownerReferences,
27+
revision: deploymentConfig.status.observedGeneration,
28+
podSpec: deploymentConfig.spec.template.spec,
29+
},
30+
workloadName,
31+
);
32+
}

0 commit comments

Comments
 (0)