"use strict";
var K8sUtils_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.K8sUtils = void 0;
const tslib_1 = require("tslib");
const inversify_1 = require("inversify");
const _ = require("lodash");
const client_node_1 = require("@kubernetes/client-node");
const k8s = require("@kubernetes/client-node");
const codiac_common_1 = require("@codiac.io/codiac-common");
const codiac_relay_1 = require("../../apis/codiac-relay");
const kube_config_utils_1 = require("./kube-config-utils");
/** Shallow front-end-layer utility functions for connecting to a k8s cluster. */
let K8sUtils = K8sUtils_1 = class K8sUtils extends kube_config_utils_1.KubeConfigRepo {
    constructor(relayClient, cache, logger) {
        super(cache, logger);
        this.relayClient = relayClient;
        this.cache = cache;
    }
    static bootstrap(ioc, cmd) {
        // KubeConfigRepo.bootstrap(ioc, cmd);
        super.bootstrap(ioc, cmd);
        ioc.bind(K8sUtils_1).toDynamicValue(ctx => new K8sUtils_1(cmd.genRelayClient, ctx.container.get(k8s.KubeConfig), cmd.logger));
    }
    /** Requests the relay to connect to the Cloud Service Provider
     * for the remote cluster
     * and download the cluster connection data
     * (aka the KubeConfig: cluster url, user, tokens, etc.).  */
    fetchKubeConfigForCluster(cluster, forceRefresh) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            // Retrieve cluster connection specs
            let req = { cluster: cluster };
            if (forceRefresh)
                req.forceRefresh = true;
            let res = yield this.relayClient.Call.getClusterConnectionSpecs(req);
            if (res.error)
                throw res.error;
            if (res.output == undefined || _.isEmpty(res.output)) {
                this.logger.error(`Failed loading cluster spec: ${res.message}.`);
                throw new Error(`Failed loading cluster spec: ${res.message}.`);
            }
            // Create the k8s client
            let k8sConfig = new client_node_1.KubeConfig();
            k8sConfig.loadFromString(res.output);
            return k8sConfig;
        });
    }
    getKubeConfig(context) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            // Retrieve cluster connection specs
            let res = yield this.relayClient.Call.getClusterConnectionSpecs({
                cabinet: {
                    cabinet: context.cabinet,
                    enterprise: context.enterprise,
                    environment: context.environment,
                }
            });
            if (res.error)
                throw res.error;
            if (res.output == undefined || _.isEmpty(res.output)) {
                this.logger.error(`Failed loading cluster spec: ${res.message}.`);
                throw new Error(`Failed loading cluster spec: ${res.message}.`);
            }
            // Create the k8s client
            let k8sConfig = new client_node_1.KubeConfig();
            k8sConfig.loadFromString(res.output);
            return k8sConfig;
        });
    }
    getK8sClient(context, kube) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            let k8sConfig = kube !== null && kube !== void 0 ? kube : (yield this.getKubeConfig(context));
            let theirApi = k8sConfig.makeApiClient(k8s.CoreV1Api);
            return theirApi;
        });
    }
};
K8sUtils = K8sUtils_1 = tslib_1.__decorate([
    (0, inversify_1.injectable)(),
    tslib_1.__param(0, (0, inversify_1.inject)(codiac_relay_1.RelayClient)),
    tslib_1.__param(1, (0, inversify_1.inject)(k8s.KubeConfig)),
    tslib_1.__param(2, (0, inversify_1.inject)(codiac_common_1.INTERFACES.IBetterLogger)),
    tslib_1.__metadata("design:paramtypes", [codiac_relay_1.RelayClient, k8s.KubeConfig, Object])
], K8sUtils);
exports.K8sUtils = K8sUtils;
//# sourceMappingURL=k8s-utils.js.map