Skip to content

Commit

Permalink
feat: add schema loader config
Browse files Browse the repository at this point in the history
  • Loading branch information
WitoDelnat committed Nov 21, 2023
1 parent eeb3410 commit e68a4ed
Showing 1 changed file with 6 additions and 5 deletions.
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

0 comments on commit e68a4ed

Please sign in to comment.