|
| 1 | +import { IncomingMessage } from 'http'; |
| 2 | +import { deleteWorkload } from './workload'; |
| 3 | +import { WorkloadKind } from '../../types'; |
| 4 | +import { FALSY_WORKLOAD_NAME_MARKER, Rollout, RolloutList } from './types'; |
| 5 | +import { paginatedClusterList, paginatedNamespacedList } from './pagination'; |
| 6 | +import { k8sApi } from '../../cluster'; |
| 7 | +import { |
| 8 | + deleteWorkloadAlreadyScanned, |
| 9 | + deleteWorkloadImagesAlreadyScanned, |
| 10 | + kubernetesObjectToWorkloadAlreadyScanned, |
| 11 | +} from '../../../state'; |
| 12 | +import { retryKubernetesApiRequest } from '../../kuberenetes-api-wrappers'; |
| 13 | +import { logger } from '../../../common/logger'; |
| 14 | +import { deleteWorkloadFromScanQueue } from './queue'; |
| 15 | +import { trimWorkload } from '../../workload-sanitization'; |
| 16 | + |
| 17 | +export async function paginatedNamespacedArgoRolloutList( |
| 18 | + namespace: string, |
| 19 | +): Promise<{ |
| 20 | + response: IncomingMessage; |
| 21 | + body: RolloutList; |
| 22 | +}> { |
| 23 | + const rolloutList = new RolloutList(); |
| 24 | + rolloutList.apiVersion = 'argoproj.io/v1alpha1'; |
| 25 | + rolloutList.kind = 'RolloutList'; |
| 26 | + rolloutList.items = new Array<Rollout>(); |
| 27 | + |
| 28 | + return await paginatedNamespacedList( |
| 29 | + namespace, |
| 30 | + rolloutList, |
| 31 | + async ( |
| 32 | + namespace: string, |
| 33 | + pretty?: string, |
| 34 | + _allowWatchBookmarks?: boolean, |
| 35 | + _continue?: string, |
| 36 | + fieldSelector?: string, |
| 37 | + labelSelector?: string, |
| 38 | + limit?: number, |
| 39 | + ) => |
| 40 | + k8sApi.customObjectsClient.listNamespacedCustomObject( |
| 41 | + 'argoproj.io', |
| 42 | + 'v1alpha1', |
| 43 | + namespace, |
| 44 | + 'rollouts', |
| 45 | + pretty, |
| 46 | + _continue, |
| 47 | + fieldSelector, |
| 48 | + labelSelector, |
| 49 | + limit, |
| 50 | + // TODO: Why any? |
| 51 | + ) as any, |
| 52 | + ); |
| 53 | +} |
| 54 | + |
| 55 | +export async function paginatedClusterArgoRolloutList(): Promise<{ |
| 56 | + response: IncomingMessage; |
| 57 | + body: RolloutList; |
| 58 | +}> { |
| 59 | + const rolloutList = new RolloutList(); |
| 60 | + rolloutList.apiVersion = 'argoproj.io/v1'; |
| 61 | + rolloutList.kind = 'RolloutList'; |
| 62 | + rolloutList.items = new Array<Rollout>(); |
| 63 | + |
| 64 | + return await paginatedClusterList( |
| 65 | + rolloutList, |
| 66 | + async ( |
| 67 | + _allowWatchBookmarks?: boolean, |
| 68 | + _continue?: string, |
| 69 | + fieldSelector?: string, |
| 70 | + labelSelector?: string, |
| 71 | + limit?: number, |
| 72 | + pretty?: string, |
| 73 | + ) => |
| 74 | + k8sApi.customObjectsClient.listClusterCustomObject( |
| 75 | + 'argoproj.io', |
| 76 | + 'v1alpha1', |
| 77 | + 'rollouts', |
| 78 | + pretty, |
| 79 | + _continue, |
| 80 | + fieldSelector, |
| 81 | + labelSelector, |
| 82 | + limit, |
| 83 | + ) as any, |
| 84 | + ); |
| 85 | +} |
| 86 | + |
| 87 | +export async function ArgoRolloutWatchHandler(rollout: Rollout): Promise<void> { |
| 88 | + rollout = trimWorkload(rollout); |
| 89 | + |
| 90 | + if ( |
| 91 | + !rollout.metadata || |
| 92 | + !rollout.spec || |
| 93 | + !rollout.spec.template.metadata || |
| 94 | + !rollout.spec.template.spec || |
| 95 | + !rollout.status |
| 96 | + ) { |
| 97 | + return; |
| 98 | + } |
| 99 | + |
| 100 | + const workloadAlreadyScanned = |
| 101 | + kubernetesObjectToWorkloadAlreadyScanned(rollout); |
| 102 | + if (workloadAlreadyScanned !== undefined) { |
| 103 | + await Promise.all([ |
| 104 | + deleteWorkloadAlreadyScanned(workloadAlreadyScanned), |
| 105 | + deleteWorkloadImagesAlreadyScanned({ |
| 106 | + ...workloadAlreadyScanned, |
| 107 | + imageIds: rollout.spec.template.spec.containers |
| 108 | + .filter((container) => container.image !== undefined) |
| 109 | + .map((container) => container.image!), |
| 110 | + }), |
| 111 | + deleteWorkloadFromScanQueue(workloadAlreadyScanned), |
| 112 | + ]); |
| 113 | + } |
| 114 | + |
| 115 | + const workloadName = rollout.metadata.name || FALSY_WORKLOAD_NAME_MARKER; |
| 116 | + |
| 117 | + await deleteWorkload( |
| 118 | + { |
| 119 | + kind: WorkloadKind.ArgoRollout, |
| 120 | + objectMeta: rollout.metadata, |
| 121 | + specMeta: rollout.spec.template.metadata, |
| 122 | + ownerRefs: rollout.metadata.ownerReferences, |
| 123 | + revision: rollout.status.observedGeneration, |
| 124 | + podSpec: rollout.spec.template.spec, |
| 125 | + }, |
| 126 | + workloadName, |
| 127 | + ); |
| 128 | +} |
| 129 | + |
| 130 | +export async function isNamespacedArgoRolloutSupported( |
| 131 | + namespace: string, |
| 132 | +): Promise<boolean> { |
| 133 | + try { |
| 134 | + const pretty = undefined; |
| 135 | + const continueToken = undefined; |
| 136 | + const fieldSelector = undefined; |
| 137 | + const labelSelector = undefined; |
| 138 | + const limit = 1; // Try to grab only a single object |
| 139 | + const resourceVersion = undefined; // List anything in the cluster |
| 140 | + const timeoutSeconds = 10; // Don't block the snyk-monitor indefinitely |
| 141 | + const attemptedApiCall = await retryKubernetesApiRequest(() => |
| 142 | + k8sApi.customObjectsClient.listNamespacedCustomObject( |
| 143 | + 'argoproj.io', |
| 144 | + 'v1alpha1', |
| 145 | + namespace, |
| 146 | + 'rollouts', |
| 147 | + pretty, |
| 148 | + continueToken, |
| 149 | + fieldSelector, |
| 150 | + labelSelector, |
| 151 | + limit, |
| 152 | + resourceVersion, |
| 153 | + timeoutSeconds, |
| 154 | + ), |
| 155 | + ); |
| 156 | + return ( |
| 157 | + attemptedApiCall !== undefined && |
| 158 | + attemptedApiCall.response !== undefined && |
| 159 | + attemptedApiCall.response.statusCode !== undefined && |
| 160 | + attemptedApiCall.response.statusCode >= 200 && |
| 161 | + attemptedApiCall.response.statusCode < 300 |
| 162 | + ); |
| 163 | + } catch (error) { |
| 164 | + logger.debug( |
| 165 | + { error, workloadKind: WorkloadKind.ArgoRollout }, |
| 166 | + 'Failed on Kubernetes API call to list namespaced argoproj.io/Rollout', |
| 167 | + ); |
| 168 | + return false; |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +export async function isClusterArgoRolloutSupported(): Promise<boolean> { |
| 173 | + try { |
| 174 | + const pretty = undefined; |
| 175 | + const continueToken = undefined; |
| 176 | + const fieldSelector = undefined; |
| 177 | + const labelSelector = undefined; |
| 178 | + const limit = 1; // Try to grab only a single object |
| 179 | + const resourceVersion = undefined; // List anything in the cluster |
| 180 | + const timeoutSeconds = 10; // Don't block the snyk-monitor indefinitely |
| 181 | + const attemptedApiCall = await retryKubernetesApiRequest(() => |
| 182 | + k8sApi.customObjectsClient.listClusterCustomObject( |
| 183 | + 'argoproj.io', |
| 184 | + 'v1alpha1', |
| 185 | + 'rollouts', |
| 186 | + pretty, |
| 187 | + continueToken, |
| 188 | + fieldSelector, |
| 189 | + labelSelector, |
| 190 | + limit, |
| 191 | + resourceVersion, |
| 192 | + timeoutSeconds, |
| 193 | + ), |
| 194 | + ); |
| 195 | + return ( |
| 196 | + attemptedApiCall !== undefined && |
| 197 | + attemptedApiCall.response !== undefined && |
| 198 | + attemptedApiCall.response.statusCode !== undefined && |
| 199 | + attemptedApiCall.response.statusCode >= 200 && |
| 200 | + attemptedApiCall.response.statusCode < 300 |
| 201 | + ); |
| 202 | + } catch (error) { |
| 203 | + logger.debug( |
| 204 | + { error, workloadKind: WorkloadKind.ArgoRollout }, |
| 205 | + 'Failed on Kubernetes API call to list cluster argoproj.io/Rollout', |
| 206 | + ); |
| 207 | + return false; |
| 208 | + } |
| 209 | +} |
0 commit comments