1
0
Fork 0

feat: make node selectors work

This commit is contained in:
ssube 2019-06-15 17:53:07 -05:00
parent 29aaa93f17
commit 282e93d8c6
8 changed files with 41 additions and 7 deletions

View File

@ -26,7 +26,7 @@
"strictPropertyInitialization": true,
"target": "es2017",
"types": [
"js-yaml"
"jsonpath-plus"
],
"typeRoots": [
"../node_modules/@types",

View File

@ -25,6 +25,7 @@
"@types/js-yaml": "^3.12.1",
"@types/lodash": "^4.14.134",
"@types/yargs-parser": "^13.0.0",
"jsonpath-plus": "^0.20.1",
"rollup": "^1.15.5",
"rollup-plugin-commonjs": "^10.0.0",
"rollup-plugin-json": "^4.0.0",

View File

@ -0,0 +1,14 @@
metadata:
name: example
spec:
template:
spec:
containers:
- name: test
resources:
limits:
cpu: 200m
memory: 250Mi
requests:
cpu: 200m
memory: 250Mi

View File

@ -1,12 +1,11 @@
rules:
- name: require-resources
- name: kubernetes-require-resources
level: info
tags:
- cluster-health
- important
nodes:
filter: ''
select: '$.spec.template.spec.containers[*]'
schema:

View File

@ -1,5 +1,6 @@
import * as ajv from 'ajv';
import * as Ajv from 'ajv';
import { readFile } from 'fs';
import { JSONPath } from 'jsonpath-plus';
import { intersection } from 'lodash';
import { LogLevel } from 'noicejs';
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 {
const schema = new ((ajv as any).default)().compile(rule.schema);
const valid = schema(data);
console.log(data, valid);
const ajv = new ((Ajv as any).default)()
const schema = ajv.compile(rule.schema);
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;
}

8
vendor/jsonpath-plus/index.d.ts vendored Normal file
View File

@ -0,0 +1,8 @@
declare module 'jsonpath-plus' {
export interface JSONPathOptions {
json: any;
path: string;
}
export function JSONPath(options: JSONPathOptions): any;
}

View File

@ -538,6 +538,11 @@ jsonfile@^4.0.0:
optionalDependencies:
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:
version "3.2.2"
resolved "https://registry.yarnpkg.com/kind-of/-/kind-of-3.2.2.tgz#31ea21a734bab9bbb0f32466d893aea51e4a3c64"