/
kubernetesconfiguration.ts
64 lines (59 loc) · 1.93 KB
/
kubernetesconfiguration.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import * as fs from "fs";
import * as path from "path";
import { WebdaError } from "../index";
import { FileUtils } from "../utils/serializers";
import { ConfigurationService, ConfigurationServiceParameters } from "./configuration";
/**
* Allow for dynamic configuration from ConfigMap or Secrets
*
* Read a ConfigMap from Kubernetes and auto-update
* @WebdaModda
*/
export class KubernetesConfigurationService<T extends ConfigurationServiceParameters> extends ConfigurationService<T> {
/**
* @ignore
*/
async init(): Promise<this> {
// Do not call super as we diverged
if (!this.parameters.source) {
throw new WebdaError.CodeError(
"KUBE_CONFIGURATION_SOURCE_MISSING",
"Need a source for KubernetesConfigurationService"
);
}
this.parameters.source = this._webda.getAppPath(this.parameters.source);
if (!fs.existsSync(this.parameters.source)) {
throw new WebdaError.CodeError(
"KUBE_CONFIGURATION_SOURCE_MISSING",
"Need a source for KubernetesConfigurationService"
);
}
// Add webda info
this.watch("$.services", (updates: any) => this._webda.reinit(updates));
fs.watchFile(path.join(this.parameters.source, "..data"), this.checkUpdate.bind(this));
await this.checkUpdate();
return this;
}
/**
* Read the file and store it
*/
async initConfiguration(): Promise<{ [key: string]: any }> {
return this.loadAndStoreConfiguration();
}
/**
* Load configuration from a ConfigMap or a Secret
* @override
*/
async loadConfiguration(): Promise<{ [key: string]: any }> {
let result = {};
const found = fs
.readdirSync(this.parameters.source)
.filter(f => !f.startsWith(".") && f.match(/webda\.(jsonc?|ya?ml)$/i))
.pop();
if (!found) {
return this.parameters.default;
}
this.log("TRACE", "loadConfiguration", result);
return FileUtils.load(path.join(this.parameters.source, found));
}
}