Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add schema loader config #569

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sixty-panthers-matter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@monokle/validation": patch
---

Add schema loader config
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import {isKnownResourceKind} from '../../utils/knownResourceKinds.js';
export type FullSchema = {definitions: Record<string, ResourceSchema>};
export type ResourceSchema = any;

const CORE_SCHEMA_BASE = 'https://plugins.monokle.com/schemas';
const CRD_SCHEMA_BASE = 'https://plugins.monokle.com/schemas';
const BASE_URL = 'https://plugins.monokle.com/schemas';

export class SchemaLoader {
private schemaCache = new Map<string, ResourceSchema | undefined>();

constructor(private baseUrl = BASE_URL) {}

async getResourceSchema(
schemaVersion: string,
resource: Resource | undefined,
Expand Down Expand Up @@ -49,11 +50,11 @@ export class SchemaLoader {
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`;
return `${this.baseUrl}/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`;
return `${this.baseUrl}/${kubernetesVersion}-standalone/${kind}.json`;
}
}

Expand All @@ -65,7 +66,7 @@ export class SchemaLoader {
const cacheKey = schemaVersion;
const cachedSchema = this.schemaCache.get(cacheKey);
const kubernetesVersion = this.getKubernetesVersion(schemaVersion);
const schemaUri = `${CORE_SCHEMA_BASE}/${kubernetesVersion}-standalone/definitions.json`;
const schemaUri = `${this.baseUrl}/${kubernetesVersion}-standalone/definitions.json`;

if (cachedSchema) {
return {schema: cachedSchema, url: schemaUri};
Expand Down
Loading