"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getClusterDeploymentCpuArchitecture = exports.getClusterProperties = exports.getClusterApiUrl = exports.getConsoleUrl = exports.getClusterDeploymentResource = exports.getAnnotationsFromAgentSelector = exports.getAgentSelectorFieldsFromAnnotations = void 0;
const tslib_1 = require("tslib");
const isEqual_js_1 = tslib_1.__importDefault(require("lodash-es/isEqual.js"));
const pick_js_1 = tslib_1.__importDefault(require("lodash-es/pick.js"));
const common_1 = require("../../../common");
const common_2 = require("../common");
const status_1 = require("./status");
const getAgentSelectorFieldsFromAnnotations = (annotations = {}) => {
    var _a;
    const locations = ((_a = annotations[common_2.AGENT_LOCATION_LABEL_KEY]) === null || _a === void 0 ? void 0 : _a.split(',')) || [];
    const labels = Object.keys(annotations)
        .map((key) => {
        if (key.startsWith(common_2.AGENT_SELECTOR)) {
            const label = key.substr(common_2.AGENT_SELECTOR.length);
            return `${label}=${annotations[key]}`;
        }
        return undefined;
    })
        .filter(Boolean);
    return {
        labels,
        locations,
        autoSelect: !annotations[common_2.AGENT_AUTO_SELECT_ANNOTATION_KEY] ||
            annotations[common_2.AGENT_AUTO_SELECT_ANNOTATION_KEY] === 'true',
    };
};
exports.getAgentSelectorFieldsFromAnnotations = getAgentSelectorFieldsFromAnnotations;
const getAnnotationsFromAgentSelector = (clusterDeployment, values) => {
    var _a;
    const { agentLabels: labelValuePairs, locations, autoSelectHosts } = values;
    const agentLabels = (0, common_1.parseStringLabels)(labelValuePairs || []);
    const annotations = Object.assign({}, (((_a = clusterDeployment === null || clusterDeployment === void 0 ? void 0 : clusterDeployment.metadata) === null || _a === void 0 ? void 0 : _a.annotations) || {}));
    Object.keys(annotations).forEach((key) => {
        if (key.startsWith(common_2.AGENT_SELECTOR)) {
            delete annotations[key];
        }
    });
    Object.keys(agentLabels).forEach((key) => {
        annotations[`${common_2.AGENT_SELECTOR}${key}`] = agentLabels[key];
    });
    delete annotations[common_2.AGENT_AUTO_SELECT_ANNOTATION_KEY];
    if (locations.length) {
        annotations[common_2.AGENT_LOCATION_LABEL_KEY] = locations.join(',');
    }
    else {
        delete annotations[common_2.AGENT_LOCATION_LABEL_KEY];
    }
    if (!autoSelectHosts) {
        annotations[common_2.AGENT_AUTO_SELECT_ANNOTATION_KEY] = 'false';
    }
    return annotations;
};
exports.getAnnotationsFromAgentSelector = getAnnotationsFromAgentSelector;
const getClusterDeploymentResource = ({ name, namespace, baseDnsDomain, annotations, pullSecretName, }) => {
    return {
        apiVersion: 'hive.openshift.io/v1',
        kind: 'ClusterDeployment',
        metadata: {
            name: name,
            namespace,
            annotations,
        },
        spec: {
            baseDomain: baseDnsDomain,
            clusterInstallRef: {
                group: 'extensions.hive.openshift.io',
                kind: 'AgentClusterInstall',
                name: name,
                version: 'v1beta1',
            },
            clusterName: name,
            platform: {
                agentBareMetal: {
                    agentSelector: {
                    /* So far left empty, annotations are used instead.
                       Needs https://issues.redhat.com/browse/HIVE-1636 to be fixed.
                     */
                    },
                },
            },
            pullSecretRef: {
                name: pullSecretName,
            },
        },
    };
};
exports.getClusterDeploymentResource = getClusterDeploymentResource;
// Temporary workaround until https://issues.redhat.com/browse/HIVE-1666
const WORKAROUND_COMPUTEDURLS_CLUSTER_STATUS = ['adding-hosts', 'installed', 'finalizing'];
const getConsoleUrl = (clusterDeployment, agentClusterInstall) => {
    var _a, _b, _c;
    return ((_a = clusterDeployment === null || clusterDeployment === void 0 ? void 0 : clusterDeployment.status) === null || _a === void 0 ? void 0 : _a.webConsoleURL) ||
        (WORKAROUND_COMPUTEDURLS_CLUSTER_STATUS.includes((0, status_1.getClusterStatus)(agentClusterInstall)[0])
            ? `https://console-openshift-console.apps.${((_b = clusterDeployment === null || clusterDeployment === void 0 ? void 0 : clusterDeployment.spec) === null || _b === void 0 ? void 0 : _b.clusterName) || ''}.${((_c = clusterDeployment === null || clusterDeployment === void 0 ? void 0 : clusterDeployment.spec) === null || _c === void 0 ? void 0 : _c.baseDomain) || ''}`
            : undefined);
};
exports.getConsoleUrl = getConsoleUrl;
const getClusterApiUrl = (clusterDeployment, agentClusterInstall) => {
    var _a, _b, _c;
    return ((_a = clusterDeployment === null || clusterDeployment === void 0 ? void 0 : clusterDeployment.status) === null || _a === void 0 ? void 0 : _a.apiURL) ||
        (WORKAROUND_COMPUTEDURLS_CLUSTER_STATUS.includes((0, status_1.getClusterStatus)(agentClusterInstall)[0])
            ? `https://api.${((_b = clusterDeployment === null || clusterDeployment === void 0 ? void 0 : clusterDeployment.spec) === null || _b === void 0 ? void 0 : _b.clusterName) || ''}.${((_c = clusterDeployment === null || clusterDeployment === void 0 ? void 0 : clusterDeployment.spec) === null || _c === void 0 ? void 0 : _c.baseDomain) || ''}`
            : undefined);
};
exports.getClusterApiUrl = getClusterApiUrl;
const getClusterProperties = (clusterDeployment, agentClusterInstall) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
    const serviceNetworks = (_b = (_a = agentClusterInstall.spec) === null || _a === void 0 ? void 0 : _a.networking) === null || _b === void 0 ? void 0 : _b.serviceNetwork;
    const clusterNetworks = (_d = (_c = agentClusterInstall.spec) === null || _c === void 0 ? void 0 : _c.networking) === null || _d === void 0 ? void 0 : _d.clusterNetwork;
    const apiIPs = ((_e = agentClusterInstall.spec) === null || _e === void 0 ? void 0 : _e.apiVIPs) || [(_f = agentClusterInstall.spec) === null || _f === void 0 ? void 0 : _f.apiVIP];
    const ingressIPs = ((_g = agentClusterInstall.spec) === null || _g === void 0 ? void 0 : _g.ingressVIPs) || [
        (_h = agentClusterInstall.spec) === null || _h === void 0 ? void 0 : _h.ingressVIP,
    ];
    return {
        // TODO: we should translate following keys since they are used "as it is" in AcmDescriptionList.tsx / List component of the stolostron project
        name: {
            key: 'Name',
            value: (_j = clusterDeployment.metadata) === null || _j === void 0 ? void 0 : _j.name,
        },
        openshiftVersion: {
            key: 'OpenShift version',
            value: (_k = clusterDeployment.status) === null || _k === void 0 ? void 0 : _k.installVersion,
        },
        clusterId: {
            key: 'Cluster ID',
            value: (_l = clusterDeployment.metadata) === null || _l === void 0 ? void 0 : _l.uid,
        },
        baseDnsDomain: {
            key: 'Base DNS domain',
            value: (_m = clusterDeployment.spec) === null || _m === void 0 ? void 0 : _m.baseDomain,
        },
        apiVip: {
            key: apiIPs.length > 1 ? 'API IPs' : 'API IP',
            value: apiIPs.join(', '),
        },
        ingressVip: {
            key: ingressIPs.length > 1 ? 'Ingress IPs' : 'Ingress IP',
            value: ingressIPs.join(', '),
        },
        clusterNetworkCidr: {
            key: ((clusterNetworks === null || clusterNetworks === void 0 ? void 0 : clusterNetworks.length) || 0) > 1 ? 'Cluster network CIDRs' : 'Cluster network CIDR',
            value: clusterNetworks === null || clusterNetworks === void 0 ? void 0 : clusterNetworks.map((n) => n.cidr).join(', '),
        },
        clusterNetworkHostPrefix: {
            key: ((clusterNetworks === null || clusterNetworks === void 0 ? void 0 : clusterNetworks.length) || 0) > 1
                ? 'Cluster network host prefixes'
                : 'Cluster network host prefix',
            value: clusterNetworks === null || clusterNetworks === void 0 ? void 0 : clusterNetworks.map((n) => n.hostPrefix).join(', '),
        },
        serviceNetworkCidr: {
            key: ((serviceNetworks === null || serviceNetworks === void 0 ? void 0 : serviceNetworks.length) || 0) > 1 ? 'Service network CIDRs' : 'Service network CIDR',
            value: serviceNetworks === null || serviceNetworks === void 0 ? void 0 : serviceNetworks.join(', '),
        },
        installedTimestamp: {
            key: 'Installed at',
            value: (0, common_1.getHumanizedDateTime)((_o = clusterDeployment.status) === null || _o === void 0 ? void 0 : _o.installedTimestamp),
        },
    };
};
exports.getClusterProperties = getClusterProperties;
const getClusterDeploymentCpuArchitecture = (clusterDeployment, infraEnv) => {
    var _a, _b, _c, _d;
    let arch;
    if (infraEnv &&
        (0, isEqual_js_1.default)((_a = infraEnv.spec) === null || _a === void 0 ? void 0 : _a.clusterRef, (0, pick_js_1.default)(clusterDeployment.metadata, ['name', 'namespace']))) {
        arch = (_b = infraEnv.spec) === null || _b === void 0 ? void 0 : _b.cpuArchitecture;
    }
    else {
        arch = (_d = (_c = clusterDeployment.metadata) === null || _c === void 0 ? void 0 : _c.annotations) === null || _d === void 0 ? void 0 : _d[common_2.CPU_ARCHITECTURE_ANNOTATION_KEY];
    }
    switch (arch) {
        case common_1.CpuArchitecture.ARM:
            return common_1.CpuArchitecture.ARM;
        case common_1.CpuArchitecture.s390x:
            return common_1.CpuArchitecture.s390x;
        default:
            return common_1.CpuArchitecture.x86;
    }
};
exports.getClusterDeploymentCpuArchitecture = getClusterDeploymentCpuArchitecture;
//# sourceMappingURL=clusterDeployment.js.map