@@ -5,12 +5,18 @@ import {
55 ERROR ,
66 UPDATE ,
77 KubernetesObject ,
8+ BatchV1beta1Api ,
9+ BatchV1Api ,
810} from '@kubernetes/client-node' ;
911
1012import { logger } from '../../../common/logger' ;
1113import { WorkloadKind } from '../../types' ;
1214import { podWatchHandler , podDeletedHandler , paginatedPodList } from './pod' ;
13- import { cronJobWatchHandler , paginatedCronJobList } from './cron-job' ;
15+ import {
16+ cronJobWatchHandler ,
17+ paginatedCronJobList ,
18+ paginatedCronJobV1Beta1List ,
19+ } from './cron-job' ;
1420import { daemonSetWatchHandler , paginatedDaemonSetList } from './daemon-set' ;
1521import { deploymentWatchHandler , paginatedDeploymentList } from './deployment' ;
1622import { jobWatchHandler , paginatedJobList } from './job' ;
@@ -75,6 +81,13 @@ const workloadWatchMetadata: Readonly<IWorkloadWatchMetadata> = {
7581 } ,
7682 listFactory : ( namespace ) => ( ) => paginatedCronJobList ( namespace ) ,
7783 } ,
84+ [ WorkloadKind . CronJobV1Beta1 ] : {
85+ endpoint : '/apis/batch/v1beta1/watch/namespaces/{namespace}/cronjobs' ,
86+ handlers : {
87+ [ DELETE ] : cronJobWatchHandler ,
88+ } ,
89+ listFactory : ( namespace ) => ( ) => paginatedCronJobV1Beta1List ( namespace ) ,
90+ } ,
7891 [ WorkloadKind . Job ] : {
7992 endpoint : '/apis/batch/v1/watch/namespaces/{namespace}/jobs' ,
8093 handlers : {
@@ -125,10 +138,75 @@ async function isSupportedWorkload(
125138 namespace : string ,
126139 workloadKind : WorkloadKind ,
127140) : Promise < boolean > {
128- if ( workloadKind !== WorkloadKind . DeploymentConfig ) {
129- return true ;
141+ switch ( workloadKind ) {
142+ case WorkloadKind . DeploymentConfig :
143+ return await isDeploymentConfigSupported ( namespace ) ;
144+ case WorkloadKind . CronJobV1Beta1 :
145+ return await isCronJobVersionSupported (
146+ workloadKind ,
147+ namespace ,
148+ k8sApi . batchUnstableClient ,
149+ ) ;
150+ case WorkloadKind . CronJob :
151+ return await isCronJobVersionSupported (
152+ workloadKind ,
153+ namespace ,
154+ k8sApi . batchClient ,
155+ ) ;
156+ default :
157+ return true ;
130158 }
159+ }
131160
161+ async function isCronJobVersionSupported (
162+ workloadKind : WorkloadKind ,
163+ namespace : string ,
164+ client : BatchV1Api | BatchV1beta1Api ,
165+ ) : Promise < boolean > {
166+ try {
167+ const pretty = undefined ;
168+ const allowWatchBookmarks = undefined ;
169+ const continueToken = undefined ;
170+ const fieldSelector = undefined ;
171+ const labelSelector = undefined ;
172+ const limit = 1 ; // Try to grab only a single object
173+ const resourceVersion = undefined ; // List anything in the cluster
174+ const resourceVersionMatch = undefined ;
175+ const timeoutSeconds = 10 ; // Don't block the snyk-monitor indefinitely
176+ const attemptedApiCall =
177+ await kubernetesApiWrappers . retryKubernetesApiRequest ( ( ) =>
178+ client . listNamespacedCronJob (
179+ namespace ,
180+ pretty ,
181+ allowWatchBookmarks ,
182+ continueToken ,
183+ fieldSelector ,
184+ labelSelector ,
185+ limit ,
186+ resourceVersion ,
187+ resourceVersionMatch ,
188+ timeoutSeconds ,
189+ ) ,
190+ ) ;
191+ return (
192+ attemptedApiCall !== undefined &&
193+ attemptedApiCall . response !== undefined &&
194+ attemptedApiCall . response . statusCode !== undefined &&
195+ attemptedApiCall . response . statusCode >= 200 &&
196+ attemptedApiCall . response . statusCode < 300
197+ ) ;
198+ } catch ( error ) {
199+ logger . debug (
200+ { error, workloadKind : workloadKind } ,
201+ 'Failed on Kubernetes API call to list CronJob or v1beta1 CronJob' ,
202+ ) ;
203+ return false ;
204+ }
205+ }
206+
207+ async function isDeploymentConfigSupported (
208+ namespace : string ,
209+ ) : Promise < boolean > {
132210 try {
133211 const pretty = undefined ;
134212 const continueToken = undefined ;
@@ -162,7 +240,7 @@ async function isSupportedWorkload(
162240 ) ;
163241 } catch ( error ) {
164242 logger . debug (
165- { error, workloadKind } ,
243+ { error, workloadKind : WorkloadKind . DeploymentConfig } ,
166244 'Failed on Kubernetes API call to list DeploymentConfig' ,
167245 ) ;
168246 return false ;
0 commit comments