feat: make node selectors work
This commit is contained in:
parent
29aaa93f17
commit
282e93d8c6
|
@ -26,7 +26,7 @@
|
||||||
"strictPropertyInitialization": true,
|
"strictPropertyInitialization": true,
|
||||||
"target": "es2017",
|
"target": "es2017",
|
||||||
"types": [
|
"types": [
|
||||||
"js-yaml"
|
"jsonpath-plus"
|
||||||
],
|
],
|
||||||
"typeRoots": [
|
"typeRoots": [
|
||||||
"../node_modules/@types",
|
"../node_modules/@types",
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
"@types/js-yaml": "^3.12.1",
|
"@types/js-yaml": "^3.12.1",
|
||||||
"@types/lodash": "^4.14.134",
|
"@types/lodash": "^4.14.134",
|
||||||
"@types/yargs-parser": "^13.0.0",
|
"@types/yargs-parser": "^13.0.0",
|
||||||
|
"jsonpath-plus": "^0.20.1",
|
||||||
"rollup": "^1.15.5",
|
"rollup": "^1.15.5",
|
||||||
"rollup-plugin-commonjs": "^10.0.0",
|
"rollup-plugin-commonjs": "^10.0.0",
|
||||||
"rollup-plugin-json": "^4.0.0",
|
"rollup-plugin-json": "^4.0.0",
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
metadata:
|
||||||
|
name: example
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
containers:
|
||||||
|
- name: test
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpu: 200m
|
||||||
|
memory: 250Mi
|
||||||
|
requests:
|
||||||
|
cpu: 200m
|
||||||
|
memory: 250Mi
|
|
@ -1,12 +1,11 @@
|
||||||
rules:
|
rules:
|
||||||
- name: require-resources
|
- name: kubernetes-require-resources
|
||||||
level: info
|
level: info
|
||||||
tags:
|
tags:
|
||||||
- cluster-health
|
- cluster-health
|
||||||
- important
|
- important
|
||||||
|
|
||||||
nodes:
|
nodes:
|
||||||
filter: ''
|
|
||||||
select: '$.spec.template.spec.containers[*]'
|
select: '$.spec.template.spec.containers[*]'
|
||||||
|
|
||||||
schema:
|
schema:
|
||||||
|
|
15
src/rule.ts
15
src/rule.ts
|
@ -1,5 +1,6 @@
|
||||||
import * as ajv from 'ajv';
|
import * as Ajv from 'ajv';
|
||||||
import { readFile } from 'fs';
|
import { readFile } from 'fs';
|
||||||
|
import { JSONPath } from 'jsonpath-plus';
|
||||||
import { intersection } from 'lodash';
|
import { intersection } from 'lodash';
|
||||||
import { LogLevel } from 'noicejs';
|
import { LogLevel } from 'noicejs';
|
||||||
import { promisify } from 'util';
|
import { promisify } from 'util';
|
||||||
|
@ -81,8 +82,14 @@ export async function resolveRules(rules: Array<Rule>, selector: RuleSelector):
|
||||||
}
|
}
|
||||||
|
|
||||||
export function checkRule(rule: Rule, data: any): boolean {
|
export function checkRule(rule: Rule, data: any): boolean {
|
||||||
const schema = new ((ajv as any).default)().compile(rule.schema);
|
const ajv = new ((Ajv as any).default)()
|
||||||
const valid = schema(data);
|
const schema = ajv.compile(rule.schema);
|
||||||
console.log(data, valid);
|
const scopes = JSONPath({
|
||||||
|
json: data,
|
||||||
|
path: rule.nodes.select,
|
||||||
|
});
|
||||||
|
const valid = scopes.every((s: any) => schema(s));
|
||||||
|
console.log(rule.nodes.select, scopes, valid, data, rule.schema);
|
||||||
|
console.log(schema.errors);
|
||||||
return !!valid;
|
return !!valid;
|
||||||
}
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
declare module 'jsonpath-plus' {
|
||||||
|
export interface JSONPathOptions {
|
||||||
|
json: any;
|
||||||
|
path: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function JSONPath(options: JSONPathOptions): any;
|
||||||
|
}
|
|
@ -538,6 +538,11 @@ jsonfile@^4.0.0:
|
||||||
optionalDependencies:
|
optionalDependencies:
|
||||||
graceful-fs "^4.1.6"
|
graceful-fs "^4.1.6"
|
||||||
|
|
||||||
|
jsonpath-plus@^0.20.1:
|
||||||
|
version "0.20.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/jsonpath-plus/-/jsonpath-plus-0.20.1.tgz#5358b8e8a5df569c541de64908d54bca55550e1c"
|
||||||
|
integrity sha512-8O4tBeXh9XGma2x2aPVwvpo9lXJAd4bx0XA0eRjYs4Cpz7e5PQy7sPttk2YmhvROJhEUu4DNUxmtLueicCqyZg==
|
||||||
|
|
||||||
kind-of@^3.0.2, kind-of@^3.0.3, kind-of@^3.2.0:
|
kind-of@^3.0.2, kind-of@^3.0.3, kind-of@^3.2.0:
|
||||||
version "3.2.2"
|
version "3.2.2"
|
||||||
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.2.2.tgz#31ea21a734bab9bbb0f32466d893aea51e4a3c64"
|
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.2.2.tgz#31ea21a734bab9bbb0f32466d893aea51e4a3c64"
|
||||||
|
|
Loading…
Reference in New Issue