@monokle/validation
Advanced tools
Comparing version 0.12.0 to 0.12.1
@@ -1,2 +0,1 @@ | ||
import { JsonObject } from "type-fest"; | ||
import { ZodType } from "zod"; | ||
@@ -20,3 +19,3 @@ /** | ||
rules?: RuleMap; | ||
settings?: JsonObject; | ||
settings?: any; | ||
}; | ||
@@ -23,0 +22,0 @@ export declare const configSchema: ZodType<Config>; |
@@ -8,6 +8,2 @@ import { Resource } from "../../common/types.js"; | ||
private schemaCache; | ||
private fetcher; | ||
constructor(init?: { | ||
fetch?: typeof fetch; | ||
}); | ||
getResourceSchema(schemaVersion: string, resource: Resource | undefined, signal?: AbortSignal): Promise<{ | ||
@@ -17,2 +13,4 @@ url?: string; | ||
} | undefined>; | ||
private getKubernetesVersion; | ||
private getUrl; | ||
getFullSchema(schemaVersion: string, signal?: AbortSignal): Promise<{ | ||
@@ -19,0 +17,0 @@ url: string; |
import { isKnownResourceKind } from "../../utils/knownResourceKinds.js"; | ||
const SCHEMA_BASE = "https://raw.githubusercontent.com/yannh/kubernetes-json-schema/master"; | ||
const CORE_SCHEMA_BASE = "https://plugins.monokle.com/schemas"; | ||
const CRD_SCHEMA_BASE = "https://plugins.monokle.com/schemas"; | ||
export class SchemaLoader { | ||
schemaCache = new Map(); | ||
fetcher; | ||
constructor(init) { | ||
this.fetcher = init?.fetch ?? fetch; | ||
} | ||
async getResourceSchema(schemaVersion, resource, signal) { | ||
@@ -13,13 +10,12 @@ if (!resource) | ||
try { | ||
const cacheKey = `${schemaVersion}-${resource.kind}`; | ||
const kubernetesVersion = this.getKubernetesVersion(schemaVersion); | ||
const cacheKey = `${kubernetesVersion}-${resource.kind}`; | ||
const cachedSchema = this.schemaCache.get(cacheKey); | ||
const schemaUri = `${SCHEMA_BASE}/v${schemaVersion}-standalone/${resource.kind.toLowerCase()}.json`; | ||
const schemaUri = this.getUrl(resource, kubernetesVersion); | ||
if (cachedSchema) { | ||
return { schema: cachedSchema, url: schemaUri }; | ||
} | ||
if (!isKnownResourceKind(resource.kind)) { | ||
return undefined; | ||
} | ||
const response = await this.fetcher(schemaUri, { signal }); | ||
const response = await fetch(schemaUri, { signal }); | ||
const schema = await response.json(); | ||
this.schemaCache.set(cacheKey, schema); | ||
return { url: schemaUri, schema }; | ||
@@ -31,2 +27,20 @@ } | ||
} | ||
getKubernetesVersion(version) { | ||
// Support both with and without v-prefix to avoid mistakes. | ||
// example: you easily enter `v1.24.2` while we expect `1.24.2`. | ||
return version.startsWith("v") ? version : `v${version}`; | ||
} | ||
getUrl(resource, kubernetesVersion) { | ||
if (!isKnownResourceKind(resource.kind)) { | ||
const kind = resource.kind.toLowerCase(); | ||
const [group, apiVersion] = resource.apiVersion.split("/"); | ||
// e.g. https://plugins.monokle.com/schemas/crds/argoproj.io/v1alpha1/application.json | ||
return `${CRD_SCHEMA_BASE}/crds/${group}/${apiVersion}/${kind}.json`; | ||
} | ||
else { | ||
const kind = resource.kind.toLowerCase(); | ||
// e.g. https://plugins.monokle.com/schemas/v1.24.2-standalone/service.json | ||
return `${CORE_SCHEMA_BASE}/${kubernetesVersion}-standalone/${kind}.json`; | ||
} | ||
} | ||
async getFullSchema(schemaVersion, signal) { | ||
@@ -36,3 +50,4 @@ try { | ||
const cachedSchema = this.schemaCache.get(cacheKey); | ||
const schemaUri = `${SCHEMA_BASE}/v${schemaVersion}/_definitions.json`; | ||
const kubernetesVersion = this.getKubernetesVersion(schemaVersion); | ||
const schemaUri = `${CORE_SCHEMA_BASE}/${kubernetesVersion}-standalone/definitions.json`; | ||
if (cachedSchema) { | ||
@@ -39,0 +54,0 @@ return { schema: cachedSchema, url: schemaUri }; |
@@ -14,3 +14,3 @@ import { loadPolicy } from "@open-policy-agent/opa-wasm"; | ||
.string() | ||
.default("https://saas.monokle.io/assets/trivy.18d95eb9.wasm"), | ||
.default("https://plugins.monokle.com/validation/open-policy-agent/trivy.wasm"), | ||
}); | ||
@@ -17,0 +17,0 @@ const CONTROLLER_KINDS = [ |
{ | ||
"name": "@monokle/validation", | ||
"version": "0.12.0", | ||
"version": "0.12.1", | ||
"description": "Kubernetes resource validation", | ||
@@ -5,0 +5,0 @@ "author": "Kubeshop", |
286397
7067