1 | "use strict";
|
2 | var __importStar = (this && this.__importStar) || function (mod) {
|
3 | if (mod && mod.__esModule) return mod;
|
4 | var result = {};
|
5 | if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
|
6 | result["default"] = mod;
|
7 | return result;
|
8 | };
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | const fs = __importStar(require("fs"));
|
11 | const yaml = __importStar(require("yamljs"));
|
12 | class GenericClientConfig {
|
13 | constructor(kubeconfig) {
|
14 | this.kubeconfig = kubeconfig;
|
15 | const context = this.kubeconfig.contexts.find(c => c.name === this.kubeconfig["current-context"]);
|
16 | const cluster = this.kubeconfig.clusters.find(c => c.name === context.context.cluster);
|
17 | const user = this.kubeconfig.users.find(c => c.name === context.context.user);
|
18 | this.apiServerURL = cluster.cluster.server.replace(/\/$/, "");
|
19 | this.namespace = context.context.namespace || "default";
|
20 | }
|
21 | mapRequestOptions(opts) {
|
22 | const context = this.kubeconfig.contexts.find(c => c.name === this.kubeconfig["current-context"]);
|
23 | const cluster = this.kubeconfig.clusters.find(c => c.name === context.context.cluster);
|
24 | const user = this.kubeconfig.users.find(c => c.name === context.context.user);
|
25 | const ca = cluster.cluster["certificate-authority-data"];
|
26 | if (ca) {
|
27 | opts.ca = Buffer.from(ca, "base64");
|
28 | }
|
29 | const caFile = cluster.cluster["certificate-authority"];
|
30 | if (caFile) {
|
31 | opts.ca = fs.readFileSync(caFile);
|
32 | }
|
33 | if (!opts.headers) {
|
34 | opts.headers = {};
|
35 | }
|
36 | if (user.user.token) {
|
37 | opts.headers.Authorization = "Bearer " + user.user.token;
|
38 | }
|
39 | const clientCert = user.user["client-certificate-data"];
|
40 | if (clientCert) {
|
41 | opts.cert = Buffer.from(clientCert, "base64");
|
42 | }
|
43 | const clientCertFile = user.user["client-certificate"];
|
44 | if (clientCertFile) {
|
45 | opts.cert = fs.readFileSync(clientCertFile);
|
46 | }
|
47 | const clientKey = user.user["client-key-data"];
|
48 | if (clientKey) {
|
49 | opts.key = Buffer.from(clientKey, "base64");
|
50 | }
|
51 | const clientKeyFile = user.user["client-key"];
|
52 | if (clientKeyFile) {
|
53 | opts.key = fs.readFileSync(clientKeyFile);
|
54 | }
|
55 | if (user.user.username && user.user.password) {
|
56 | opts.auth = { username: user.user.username, password: user.user.password };
|
57 | }
|
58 | return opts;
|
59 | }
|
60 | }
|
61 | exports.GenericClientConfig = GenericClientConfig;
|
62 | class FileBasedConfig extends GenericClientConfig {
|
63 | constructor(kubeconfigFile) {
|
64 |
|
65 | const kubeconfig = yaml.load(kubeconfigFile);
|
66 | super(kubeconfig);
|
67 | }
|
68 | }
|
69 | exports.FileBasedConfig = FileBasedConfig;
|
70 | class InClusterConfig extends GenericClientConfig {
|
71 | constructor(namespace) {
|
72 | const kubeconfig = {
|
73 | "apiVersion": "v1",
|
74 | "clusters": [{
|
75 | name: "local",
|
76 | cluster: {
|
77 | "certificate-authority": "/var/run/secrets/kubernetes.io/serviceaccount/ca.crt",
|
78 | "server": "https://kubernetes.default",
|
79 | },
|
80 | }],
|
81 | "users": [{
|
82 | name: "serviceaccount",
|
83 | user: {
|
84 | token: fs.readFileSync("/var/run/secrets/kubernetes.io/serviceaccount/token", "utf-8"),
|
85 | },
|
86 | }],
|
87 | "contexts": [{
|
88 | name: "local",
|
89 | context: { cluster: "local", user: "serviceaccount", namespace: namespace || "default" },
|
90 | }],
|
91 | "current-context": "local",
|
92 | };
|
93 | super(kubeconfig);
|
94 | }
|
95 | }
|
96 | exports.InClusterConfig = InClusterConfig;
|
97 |
|
\ | No newline at end of file |