33 V1CronJobList ,
44 V1beta1CronJob ,
55 V1beta1CronJobList ,
6+ BatchV1Api ,
7+ BatchV1beta1Api ,
68} from '@kubernetes/client-node' ;
79import { deleteWorkload , trimWorkload } from './workload' ;
810import { WorkloadKind } from '../../types' ;
@@ -15,6 +17,8 @@ import {
1517 deleteWorkloadImagesAlreadyScanned ,
1618 kubernetesObjectToWorkloadAlreadyScanned ,
1719} from '../../../state' ;
20+ import { logger } from '../../../common/logger' ;
21+ import { retryKubernetesApiRequest } from '../../kuberenetes-api-wrappers' ;
1822
1923export async function paginatedNamespacedCronJobList (
2024 namespace : string ,
@@ -128,3 +132,91 @@ export async function cronJobWatchHandler(
128132 workloadName ,
129133 ) ;
130134}
135+
136+ export async function isNamespacedCronJobSupported (
137+ workloadKind : WorkloadKind ,
138+ namespace : string ,
139+ client : BatchV1Api | BatchV1beta1Api ,
140+ ) : Promise < boolean > {
141+ try {
142+ const pretty = undefined ;
143+ const allowWatchBookmarks = undefined ;
144+ const continueToken = undefined ;
145+ const fieldSelector = undefined ;
146+ const labelSelector = undefined ;
147+ const limit = 1 ; // Try to grab only a single object
148+ const resourceVersion = undefined ; // List anything in the cluster
149+ const resourceVersionMatch = undefined ;
150+ const timeoutSeconds = 10 ; // Don't block the snyk-monitor indefinitely
151+ const attemptedApiCall = await retryKubernetesApiRequest ( ( ) =>
152+ client . listNamespacedCronJob (
153+ namespace ,
154+ pretty ,
155+ allowWatchBookmarks ,
156+ continueToken ,
157+ fieldSelector ,
158+ labelSelector ,
159+ limit ,
160+ resourceVersion ,
161+ resourceVersionMatch ,
162+ timeoutSeconds ,
163+ ) ,
164+ ) ;
165+ return (
166+ attemptedApiCall !== undefined &&
167+ attemptedApiCall . response !== undefined &&
168+ attemptedApiCall . response . statusCode !== undefined &&
169+ attemptedApiCall . response . statusCode >= 200 &&
170+ attemptedApiCall . response . statusCode < 300
171+ ) ;
172+ } catch ( error ) {
173+ logger . debug (
174+ { error, workloadKind : workloadKind } ,
175+ 'Failed on Kubernetes API call to list CronJob or v1beta1 CronJob' ,
176+ ) ;
177+ return false ;
178+ }
179+ }
180+
181+ export async function isClusterCronJobSupported (
182+ workloadKind : WorkloadKind ,
183+ client : BatchV1Api | BatchV1beta1Api ,
184+ ) : Promise < boolean > {
185+ try {
186+ const pretty = undefined ;
187+ const allowWatchBookmarks = undefined ;
188+ const continueToken = undefined ;
189+ const fieldSelector = undefined ;
190+ const labelSelector = undefined ;
191+ const limit = 1 ; // Try to grab only a single object
192+ const resourceVersion = undefined ; // List anything in the cluster
193+ const resourceVersionMatch = undefined ;
194+ const timeoutSeconds = 10 ; // Don't block the snyk-monitor indefinitely
195+ const attemptedApiCall = await retryKubernetesApiRequest ( ( ) =>
196+ client . listCronJobForAllNamespaces (
197+ allowWatchBookmarks ,
198+ continueToken ,
199+ fieldSelector ,
200+ labelSelector ,
201+ limit ,
202+ pretty ,
203+ resourceVersion ,
204+ resourceVersionMatch ,
205+ timeoutSeconds ,
206+ ) ,
207+ ) ;
208+ return (
209+ attemptedApiCall !== undefined &&
210+ attemptedApiCall . response !== undefined &&
211+ attemptedApiCall . response . statusCode !== undefined &&
212+ attemptedApiCall . response . statusCode >= 200 &&
213+ attemptedApiCall . response . statusCode < 300
214+ ) ;
215+ } catch ( error ) {
216+ logger . debug (
217+ { error, workloadKind : workloadKind } ,
218+ 'Failed on Kubernetes API call to list CronJob or v1beta1 CronJob' ,
219+ ) ;
220+ return false ;
221+ }
222+ }
0 commit comments