From d37fe30f0d7f98bb4948ca00a6b920fbc1e542a9 Mon Sep 17 00:00:00 2001 From: Topliceanu Razvan <47887589+topliceanurazvan@users.noreply.github.com> Date: Thu, 28 Sep 2023 14:02:39 +0300 Subject: [PATCH] feat: handle failed to evaluate expression case --- .../admissionPolicy/ParamsValidatorResources.ts | 6 ++++-- .../src/validators/admission-policy/loadWasm.ts | 2 +- .../src/validators/admission-policy/rules.ts | 8 ++++++++ .../src/validators/admission-policy/validator.ts | 12 ++++++++++++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/packages/validation/src/__tests__/resources/admissionPolicy/ParamsValidatorResources.ts b/packages/validation/src/__tests__/resources/admissionPolicy/ParamsValidatorResources.ts index f8dd221a0..24ed06250 100644 --- a/packages/validation/src/__tests__/resources/admissionPolicy/ParamsValidatorResources.ts +++ b/packages/validation/src/__tests__/resources/admissionPolicy/ParamsValidatorResources.ts @@ -14,7 +14,9 @@ export const PARAMS_CONFIG_MAP: Resource = { name: 'rule-config', namespace: 'demo', }, - maxReplicas: 5, + data: { + maxReplicas: 5, + }, }, id: '174d42e877a174-0', name: 'rule-config', @@ -123,7 +125,7 @@ export const PARAMS_VALIDATING_ADMISSION_POLICY: Resource = { }, validations: [ { - expression: 'object.spec.replicas > params.maxReplicas', + expression: 'object.spec.replicas > params.data.maxReplicas', }, ], }, diff --git a/packages/validation/src/validators/admission-policy/loadWasm.ts b/packages/validation/src/validators/admission-policy/loadWasm.ts index 3c8417648..3a347e044 100644 --- a/packages/validation/src/validators/admission-policy/loadWasm.ts +++ b/packages/validation/src/validators/admission-policy/loadWasm.ts @@ -6,7 +6,7 @@ export async function loadWasm() { loadGoGlueCode(); const go = new Go(); - let buffer = pako.ungzip(await (await fetch('http://plugins.monokle.com/validation/cel.wasm.gz')).arrayBuffer()); + let buffer = pako.ungzip(await (await fetch('https://plugins.monokle.com/validation/cel.wasm.gz')).arrayBuffer()); if (buffer[0] === 0x1f && buffer[1] === 0x8b) { buffer = pako.ungzip(buffer); diff --git a/packages/validation/src/validators/admission-policy/rules.ts b/packages/validation/src/validators/admission-policy/rules.ts index 90654d8c8..a04510660 100644 --- a/packages/validation/src/validators/admission-policy/rules.ts +++ b/packages/validation/src/validators/admission-policy/rules.ts @@ -9,4 +9,12 @@ export const ADMISSION_POLICY_RULES: RuleMetadata[] = [ text: 'Check whether the admission policy conditions are met.', }, }, + { + id: 'VAP002', + name: 'admission-policy-failed-to-evaluate', + shortDescription: {text: "Admission policy couldn't be evaluated"}, + help: { + text: 'Check whether the admission policy expression is properly written.', + }, + }, ]; diff --git a/packages/validation/src/validators/admission-policy/validator.ts b/packages/validation/src/validators/admission-policy/validator.ts index 3b6d44106..7293e07cf 100644 --- a/packages/validation/src/validators/admission-policy/validator.ts +++ b/packages/validation/src/validators/admission-policy/validator.ts @@ -69,6 +69,18 @@ export class AdmissionPolicyValidator extends AbstractPlugin { return []; } + if (output.includes('failed to evaluate')) { + return [ + this.adaptToValidationResult( + resource, + ['kind'], + 'VAP002', + message ?? 'Admission policy failed to evaluate expression', + level + ), + ].filter(isDefined); + } + return [ this.adaptToValidationResult( resource,