"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
function __export(m) {
    for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
const KubernetesClient = require("kubernetes-client");
const __1 = require("..");
__export(require("./KubernetesServer"));
__export(require("./KubernetesSyncWorker"));
__export(require("./ResourceWatcher"));
function retryApiRequest(fn) {
    return fn().catch((err) => {
        if (err.code === 'ECONNRESET' || err.code === 429) {
            console.log("got API error", err.code, "- retrying in 1s");
            return __1.delay(1000).then(() => retryApiRequest(fn));
        }
        throw err;
    });
}
exports.retryApiRequest = retryApiRequest;
function getKubernetesConfig() {
    try {
        return KubernetesClient.config.getInCluster();
    }
    catch (err) {
        return KubernetesClient.config.fromKubeconfig(KubernetesClient.config.loadKubeconfig());
    }
}
exports.getKubernetesConfig = getKubernetesConfig;
function getPodInfo() {
    return __awaiter(this, void 0, void 0, function* () {
        const kubernetesConfig = getKubernetesConfig();
        const namespace = kubernetesConfig.namespace;
        if (!namespace) {
            throw new Error("Namespace not defined in kubernetes configuration");
        }
        const coreApi = new KubernetesClient.Core(kubernetesConfig).ns(namespace);
        const response = yield coreApi.pods(process.env.HOSTNAME).get();
        const podIp = response.status.podIP;
        if (!podIp) {
            console.log("podIP not defined on pod resource. Retying in 1s");
            return __1.delay(1000).then(getPodInfo);
        }
        return response;
    });
}
exports.getPodInfo = getPodInfo;
//# sourceMappingURL=index.js.map