@@ -3,20 +3,27 @@ import {
33 V1CronJobList ,
44 V1beta1CronJob ,
55 V1beta1CronJobList ,
6+ BatchV1Api ,
7+ BatchV1beta1Api ,
68} from '@kubernetes/client-node' ;
7- import { deleteWorkload , trimWorkload } from './workload' ;
9+ import { deleteWorkload } from './workload' ;
810import { WorkloadKind } from '../../types' ;
911import { FALSY_WORKLOAD_NAME_MARKER } from './types' ;
1012import { IncomingMessage } from 'http' ;
1113import { k8sApi } from '../../cluster' ;
12- import { paginatedList } from './pagination' ;
14+ import { paginatedClusterList , paginatedNamespacedList } from './pagination' ;
1315import {
1416 deleteWorkloadAlreadyScanned ,
1517 deleteWorkloadImagesAlreadyScanned ,
1618 kubernetesObjectToWorkloadAlreadyScanned ,
1719} from '../../../state' ;
20+ import { logger } from '../../../common/logger' ;
21+ import { retryKubernetesApiRequest } from '../../kuberenetes-api-wrappers' ;
22+ import { trimWorkload } from '../../workload-sanitization' ;
1823
19- export async function paginatedCronJobList ( namespace : string ) : Promise < {
24+ export async function paginatedNamespacedCronJobList (
25+ namespace : string ,
26+ ) : Promise < {
2027 response : IncomingMessage ;
2128 body : V1CronJobList ;
2229} > {
@@ -25,14 +32,31 @@ export async function paginatedCronJobList(namespace: string): Promise<{
2532 v1CronJobList . kind = 'CronJobList' ;
2633 v1CronJobList . items = new Array < V1CronJob > ( ) ;
2734
28- return await paginatedList (
35+ return await paginatedNamespacedList (
2936 namespace ,
3037 v1CronJobList ,
3138 k8sApi . batchClient . listNamespacedCronJob . bind ( k8sApi . batchClient ) ,
3239 ) ;
3340}
3441
35- export async function paginatedCronJobV1Beta1List ( namespace : string ) : Promise < {
42+ export async function paginatedClusterCronJobList ( ) : Promise < {
43+ response : IncomingMessage ;
44+ body : V1CronJobList ;
45+ } > {
46+ const v1CronJobList = new V1CronJobList ( ) ;
47+ v1CronJobList . apiVersion = 'batch/v1' ;
48+ v1CronJobList . kind = 'CronJobList' ;
49+ v1CronJobList . items = new Array < V1CronJob > ( ) ;
50+
51+ return await paginatedClusterList (
52+ v1CronJobList ,
53+ k8sApi . batchClient . listCronJobForAllNamespaces . bind ( k8sApi . batchClient ) ,
54+ ) ;
55+ }
56+
57+ export async function paginatedNamespacedCronJobV1Beta1List (
58+ namespace : string ,
59+ ) : Promise < {
3660 response : IncomingMessage ;
3761 body : V1beta1CronJobList ;
3862} > {
@@ -41,7 +65,7 @@ export async function paginatedCronJobV1Beta1List(namespace: string): Promise<{
4165 cronJobList . kind = 'CronJobList' ;
4266 cronJobList . items = new Array < V1beta1CronJob > ( ) ;
4367
44- return await paginatedList (
68+ return await paginatedNamespacedList (
4569 namespace ,
4670 cronJobList ,
4771 k8sApi . batchUnstableClient . listNamespacedCronJob . bind (
@@ -50,6 +74,23 @@ export async function paginatedCronJobV1Beta1List(namespace: string): Promise<{
5074 ) ;
5175}
5276
77+ export async function paginatedClusterCronJobV1Beta1List ( ) : Promise < {
78+ response : IncomingMessage ;
79+ body : V1beta1CronJobList ;
80+ } > {
81+ const cronJobList = new V1beta1CronJobList ( ) ;
82+ cronJobList . apiVersion = 'batch/v1beta1' ;
83+ cronJobList . kind = 'CronJobList' ;
84+ cronJobList . items = new Array < V1beta1CronJob > ( ) ;
85+
86+ return await paginatedClusterList (
87+ cronJobList ,
88+ k8sApi . batchUnstableClient . listCronJobForAllNamespaces . bind (
89+ k8sApi . batchUnstableClient ,
90+ ) ,
91+ ) ;
92+ }
93+
5394export async function cronJobWatchHandler (
5495 cronJob : V1CronJob | V1beta1CronJob ,
5596) : Promise < void > {
@@ -92,3 +133,91 @@ export async function cronJobWatchHandler(
92133 workloadName ,
93134 ) ;
94135}
136+
137+ export async function isNamespacedCronJobSupported (
138+ workloadKind : WorkloadKind ,
139+ namespace : string ,
140+ client : BatchV1Api | BatchV1beta1Api ,
141+ ) : Promise < boolean > {
142+ try {
143+ const pretty = undefined ;
144+ const allowWatchBookmarks = undefined ;
145+ const continueToken = undefined ;
146+ const fieldSelector = undefined ;
147+ const labelSelector = undefined ;
148+ const limit = 1 ; // Try to grab only a single object
149+ const resourceVersion = undefined ; // List anything in the cluster
150+ const resourceVersionMatch = undefined ;
151+ const timeoutSeconds = 10 ; // Don't block the snyk-monitor indefinitely
152+ const attemptedApiCall = await retryKubernetesApiRequest ( ( ) =>
153+ client . listNamespacedCronJob (
154+ namespace ,
155+ pretty ,
156+ allowWatchBookmarks ,
157+ continueToken ,
158+ fieldSelector ,
159+ labelSelector ,
160+ limit ,
161+ resourceVersion ,
162+ resourceVersionMatch ,
163+ timeoutSeconds ,
164+ ) ,
165+ ) ;
166+ return (
167+ attemptedApiCall !== undefined &&
168+ attemptedApiCall . response !== undefined &&
169+ attemptedApiCall . response . statusCode !== undefined &&
170+ attemptedApiCall . response . statusCode >= 200 &&
171+ attemptedApiCall . response . statusCode < 300
172+ ) ;
173+ } catch ( error ) {
174+ logger . debug (
175+ { error, workloadKind : workloadKind } ,
176+ 'Failed on Kubernetes API call to list CronJob or v1beta1 CronJob' ,
177+ ) ;
178+ return false ;
179+ }
180+ }
181+
182+ export async function isClusterCronJobSupported (
183+ workloadKind : WorkloadKind ,
184+ client : BatchV1Api | BatchV1beta1Api ,
185+ ) : Promise < boolean > {
186+ try {
187+ const pretty = undefined ;
188+ const allowWatchBookmarks = undefined ;
189+ const continueToken = undefined ;
190+ const fieldSelector = undefined ;
191+ const labelSelector = undefined ;
192+ const limit = 1 ; // Try to grab only a single object
193+ const resourceVersion = undefined ; // List anything in the cluster
194+ const resourceVersionMatch = undefined ;
195+ const timeoutSeconds = 10 ; // Don't block the snyk-monitor indefinitely
196+ const attemptedApiCall = await retryKubernetesApiRequest ( ( ) =>
197+ client . listCronJobForAllNamespaces (
198+ allowWatchBookmarks ,
199+ continueToken ,
200+ fieldSelector ,
201+ labelSelector ,
202+ limit ,
203+ pretty ,
204+ resourceVersion ,
205+ resourceVersionMatch ,
206+ timeoutSeconds ,
207+ ) ,
208+ ) ;
209+ return (
210+ attemptedApiCall !== undefined &&
211+ attemptedApiCall . response !== undefined &&
212+ attemptedApiCall . response . statusCode !== undefined &&
213+ attemptedApiCall . response . statusCode >= 200 &&
214+ attemptedApiCall . response . statusCode < 300
215+ ) ;
216+ } catch ( error ) {
217+ logger . debug (
218+ { error, workloadKind : workloadKind } ,
219+ 'Failed on Kubernetes API call to list CronJob or v1beta1 CronJob' ,
220+ ) ;
221+ return false ;
222+ }
223+ }
0 commit comments