Skip to content

Commit 4f56dc2

Browse files
authored
Merge pull request #939 from snyk/feat/cache-control
fix: trim workload metadata everywhere we read workloads
2 parents c838d4d + 9b3a28e commit 4f56dc2

File tree

12 files changed

+77
-44
lines changed

12 files changed

+77
-44
lines changed

src/supervisor/watchers/handlers/cron-job.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { V1beta1CronJob, V1beta1CronJobList } from '@kubernetes/client-node';
2-
import { deleteWorkload } from './workload';
2+
import { deleteWorkload, trimWorkload } from './workload';
33
import { WorkloadKind } from '../../types';
44
import { FALSY_WORKLOAD_NAME_MARKER } from './types';
55
import { IncomingMessage } from 'http';
@@ -32,6 +32,8 @@ export async function paginatedCronJobList(namespace: string): Promise<{
3232
export async function cronJobWatchHandler(
3333
cronJob: V1beta1CronJob,
3434
): Promise<void> {
35+
cronJob = trimWorkload(cronJob);
36+
3537
if (
3638
!cronJob.metadata ||
3739
!cronJob.spec ||

src/supervisor/watchers/handlers/daemon-set.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { V1DaemonSet, V1DaemonSetList } from '@kubernetes/client-node';
2-
import { deleteWorkload } from './workload';
2+
import { deleteWorkload, trimWorkload } from './workload';
33
import { WorkloadKind } from '../../types';
44
import { FALSY_WORKLOAD_NAME_MARKER } from './types';
55
import { IncomingMessage } from 'http';
@@ -30,6 +30,8 @@ export async function paginatedDaemonSetList(namespace: string): Promise<{
3030
export async function daemonSetWatchHandler(
3131
daemonSet: V1DaemonSet,
3232
): Promise<void> {
33+
daemonSet = trimWorkload(daemonSet);
34+
3335
if (
3436
!daemonSet.metadata ||
3537
!daemonSet.spec ||

src/supervisor/watchers/handlers/deployment-config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { IncomingMessage } from 'http';
2-
import { deleteWorkload } from './workload';
2+
import { deleteWorkload, trimWorkload } from './workload';
33
import { WorkloadKind } from '../../types';
44
import {
55
FALSY_WORKLOAD_NAME_MARKER,
@@ -54,6 +54,8 @@ export async function paginatedDeploymentConfigList(
5454
export async function deploymentConfigWatchHandler(
5555
deploymentConfig: V1DeploymentConfig,
5656
): Promise<void> {
57+
deploymentConfig = trimWorkload(deploymentConfig);
58+
5759
if (
5860
!deploymentConfig.metadata ||
5961
!deploymentConfig.spec ||

src/supervisor/watchers/handlers/deployment.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { V1Deployment, V1DeploymentList } from '@kubernetes/client-node';
2-
import { deleteWorkload } from './workload';
2+
import { deleteWorkload, trimWorkload } from './workload';
33
import { WorkloadKind } from '../../types';
44
import { FALSY_WORKLOAD_NAME_MARKER } from './types';
55
import { IncomingMessage } from 'http';
@@ -30,6 +30,8 @@ export async function paginatedDeploymentList(namespace: string): Promise<{
3030
export async function deploymentWatchHandler(
3131
deployment: V1Deployment,
3232
): Promise<void> {
33+
deployment = trimWorkload(deployment);
34+
3335
if (
3436
!deployment.metadata ||
3537
!deployment.spec ||

src/supervisor/watchers/handlers/job.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { V1Job, V1JobList } from '@kubernetes/client-node';
2-
import { deleteWorkload } from './workload';
2+
import { deleteWorkload, trimWorkload } from './workload';
33
import { WorkloadKind } from '../../types';
44
import { FALSY_WORKLOAD_NAME_MARKER } from './types';
55
import { IncomingMessage } from 'http';
@@ -28,6 +28,8 @@ export async function paginatedJobList(namespace: string): Promise<{
2828
}
2929

3030
export async function jobWatchHandler(job: V1Job): Promise<void> {
31+
job = trimWorkload(job);
32+
3133
if (
3234
!job.metadata ||
3335
!job.spec ||

src/supervisor/watchers/handlers/pagination.ts

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
KubernetesObject,
66
} from '@kubernetes/client-node';
77

8+
import { trimWorkloads } from './workload';
89
import { calculateSleepSeconds } from '../../kuberenetes-api-wrappers';
910
import { V1NamespacedList } from './types';
1011
import type { IRequestError } from '../../types';
@@ -53,7 +54,7 @@ export async function paginatedList<
5354
list.metadata = listCall.body.metadata;
5455

5556
if (Array.isArray(listCall.body.items)) {
56-
const trimmedItems = trimItems(listCall.body.items);
57+
const trimmedItems = trimWorkloads(listCall.body.items);
5758
list.items.push(...trimmedItems);
5859
}
5960

@@ -98,29 +99,3 @@ export async function paginatedList<
9899
body: list,
99100
};
100101
}
101-
102-
/**
103-
* Pick only the minimum relevant data from the workload. Sometimes the workload
104-
* spec may be bloated with server-side information that is not necessary for vulnerability analysis.
105-
* This ensures that any data we hold in memory is minimal, which in turn allows us to hold more workloads to scan.
106-
*/
107-
function trimItems<
108-
T extends KubernetesObject & Partial<{ status: unknown; spec: unknown }>,
109-
>(items: T[]): KubernetesObject[] {
110-
return items.map((item) => ({
111-
apiVersion: item.apiVersion,
112-
kind: item.kind,
113-
metadata: {
114-
name: item.metadata?.name,
115-
namespace: item.metadata?.namespace,
116-
annotations: item.metadata?.annotations,
117-
labels: item.metadata?.labels,
118-
ownerReferences: item.metadata?.ownerReferences,
119-
uid: item.metadata?.uid,
120-
resourceVersion: item.metadata?.resourceVersion,
121-
generation: item.metadata?.generation,
122-
},
123-
spec: item.spec,
124-
status: item.status,
125-
}));
126-
}

src/supervisor/watchers/handlers/pod.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
} from '../../../state';
2222
import { FALSY_WORKLOAD_NAME_MARKER } from './types';
2323
import { WorkloadKind } from '../../types';
24-
import { deleteWorkload } from './workload';
24+
import { deleteWorkload, trimWorkload } from './workload';
2525
import { k8sApi } from '../../cluster';
2626
import { paginatedList } from './pagination';
2727

@@ -147,6 +147,8 @@ export async function podWatchHandler(pod: V1Pod): Promise<void> {
147147
return;
148148
}
149149

150+
pod = trimWorkload(pod);
151+
150152
const podName =
151153
pod.metadata && pod.metadata.name
152154
? pod.metadata.name

src/supervisor/watchers/handlers/replica-set.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { V1ReplicaSet, V1ReplicaSetList } from '@kubernetes/client-node';
2-
import { deleteWorkload } from './workload';
2+
import { deleteWorkload, trimWorkload } from './workload';
33
import { WorkloadKind } from '../../types';
44
import { FALSY_WORKLOAD_NAME_MARKER } from './types';
55
import { IncomingMessage } from 'http';
@@ -30,6 +30,8 @@ export async function paginatedReplicaSetList(namespace: string): Promise<{
3030
export async function replicaSetWatchHandler(
3131
replicaSet: V1ReplicaSet,
3232
): Promise<void> {
33+
replicaSet = trimWorkload(replicaSet);
34+
3335
if (
3436
!replicaSet.metadata ||
3537
!replicaSet.spec ||

src/supervisor/watchers/handlers/replication-controller.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
V1ReplicationController,
33
V1ReplicationControllerList,
44
} from '@kubernetes/client-node';
5-
import { deleteWorkload } from './workload';
5+
import { deleteWorkload, trimWorkload } from './workload';
66
import { WorkloadKind } from '../../types';
77
import { FALSY_WORKLOAD_NAME_MARKER } from './types';
88
import { IncomingMessage } from 'http';
@@ -37,6 +37,8 @@ export async function paginatedReplicationControllerList(
3737
export async function replicationControllerWatchHandler(
3838
replicationController: V1ReplicationController,
3939
): Promise<void> {
40+
replicationController = trimWorkload(replicationController);
41+
4042
if (
4143
!replicationController.metadata ||
4244
!replicationController.spec ||

src/supervisor/watchers/handlers/stateful-set.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { V1StatefulSet, V1StatefulSetList } from '@kubernetes/client-node';
2-
import { deleteWorkload } from './workload';
2+
import { deleteWorkload, trimWorkload } from './workload';
33
import { WorkloadKind } from '../../types';
44
import { FALSY_WORKLOAD_NAME_MARKER } from './types';
55
import { IncomingMessage } from 'http';
@@ -30,6 +30,8 @@ export async function paginatedStatefulSetList(namespace: string): Promise<{
3030
export async function statefulSetWatchHandler(
3131
statefulSet: V1StatefulSet,
3232
): Promise<void> {
33+
statefulSet = trimWorkload(statefulSet);
34+
3335
if (
3436
!statefulSet.metadata ||
3537
!statefulSet.spec ||

0 commit comments

Comments
 (0)