"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAICluster = exports.getAIHosts = void 0;
const tslib_1 = require("tslib");
const cloneDeep_js_1 = tslib_1.__importDefault(require("lodash-es/cloneDeep.js"));
const status_1 = require("./status");
const network_1 = require("./network");
const constants_1 = require("../common/constants");
const agents_1 = require("./agents");
const clusterDeployment_1 = require("./clusterDeployment");
const getAIHosts = (agents, bmhs, infraEnv) => {
    const bmhAgents = [];
    const hosts = agents.map((agent) => {
        var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
        const statusKey = (0, status_1.getAgentStatusKey)(agent, true);
        const statusInfo = ((_b = (_a = agent.status) === null || _a === void 0 ? void 0 : _a.debugInfo) === null || _b === void 0 ? void 0 : _b.stateInfo) || '';
        // TODO(mlibra) Remove that workaround once https://issues.redhat.com/browse/MGMT-7052 is fixed
        const inventory = (0, cloneDeep_js_1.default)(((_c = agent.status) === null || _c === void 0 ? void 0 : _c.inventory) || {});
        /* eslint-disable */
        (_d = inventory.interfaces) === null || _d === void 0 ? void 0 : _d.forEach((intf) => {
            // @ts-ignore
            intf.ipv4Addresses = (0, cloneDeep_js_1.default)(intf.ipV4Addresses);
            // @ts-ignore
            intf.ipv6Addresses = (0, cloneDeep_js_1.default)(intf.ipV6Addresses);
        });
        /* eslint-enable */
        if ((_f = (_e = agent.metadata) === null || _e === void 0 ? void 0 : _e.labels) === null || _f === void 0 ? void 0 : _f[constants_1.AGENT_BMH_NAME_LABEL_KEY]) {
            const bmhName = (_h = (_g = agent.metadata) === null || _g === void 0 ? void 0 : _g.labels) === null || _h === void 0 ? void 0 : _h[constants_1.AGENT_BMH_NAME_LABEL_KEY];
            bmhAgents.push(bmhName);
            const bmh = bmhs === null || bmhs === void 0 ? void 0 : bmhs.find((h) => { var _a, _b, _c; return ((_a = h.metadata) === null || _a === void 0 ? void 0 : _a.name) === bmhName && ((_b = h.metadata) === null || _b === void 0 ? void 0 : _b.namespace) === ((_c = agent.metadata) === null || _c === void 0 ? void 0 : _c.namespace); });
            if ((_k = (_j = bmh === null || bmh === void 0 ? void 0 : bmh.spec) === null || _j === void 0 ? void 0 : _j.bmc) === null || _k === void 0 ? void 0 : _k.address) {
                inventory.bmcAddress = bmh.spec.bmc.address;
            }
        }
        const agentProgress = (0, agents_1.getAgentProgress)(agent);
        return {
            kind: 'Host',
            id: ((_l = agent.metadata) === null || _l === void 0 ? void 0 : _l.uid) || '',
            href: '',
            status: statusKey,
            statusInfo,
            role: (0, agents_1.getAgentRole)(agent),
            requestedHostname: agent.spec.hostname || inventory.hostname,
            createdAt: (_m = agent.metadata) === null || _m === void 0 ? void 0 : _m.creationTimestamp,
            validationsInfo: JSON.stringify(((_o = agent.status) === null || _o === void 0 ? void 0 : _o.validationsInfo) || {}),
            inventory: JSON.stringify(inventory),
            progress: agentProgress && {
                currentStage: agentProgress.currentStage,
                installationPercentage: agentProgress.installationPercentage,
                progressInfo: agentProgress.progressInfo,
                stageStartedAt: agentProgress.stageStartTime,
                stageUpdatedAt: agentProgress.stageUpdateTime,
            },
            progressStages: agentProgress === null || agentProgress === void 0 ? void 0 : agentProgress.progressStages,
            bootstrap: (_p = agent.status) === null || _p === void 0 ? void 0 : _p.bootstrap,
            installationDiskId: agent.spec.installation_disk_id || ((_q = agent.status) === null || _q === void 0 ? void 0 : _q.installation_disk_id),
            infraEnvId: `${((_r = agent.metadata) === null || _r === void 0 ? void 0 : _r.namespace) || ''}/${((_t = (_s = agent.metadata) === null || _s === void 0 ? void 0 : _s.labels) === null || _t === void 0 ? void 0 : _t['infraenvs.agent-install.openshift.io']) || ''}`,
            nodeLabels: JSON.stringify(((_u = agent.metadata) === null || _u === void 0 ? void 0 : _u.labels) || {}),
        };
    });
    const restBmhs = infraEnv && bmhs
        ? bmhs === null || bmhs === void 0 ? void 0 : bmhs.filter((h) => { var _a, _b; return ((_a = h.metadata) === null || _a === void 0 ? void 0 : _a.namespace) === ((_b = infraEnv.metadata) === null || _b === void 0 ? void 0 : _b.namespace); }).filter((h) => {
            var _a, _b;
            return (0, agents_1.getInfraEnvNameOfAgent)(h) === ((_a = infraEnv.metadata) === null || _a === void 0 ? void 0 : _a.name) && ((_b = h.metadata) === null || _b === void 0 ? void 0 : _b.name)
                ? !bmhAgents.includes(h.metadata.name)
                : true;
        }).map((h) => {
            var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
            const hostInventory = {
                hostname: ((_b = (_a = h.metadata) === null || _a === void 0 ? void 0 : _a.annotations) === null || _b === void 0 ? void 0 : _b[constants_1.BMH_HOSTNAME_ANNOTATION]) || ((_c = h.metadata) === null || _c === void 0 ? void 0 : _c.name),
                bmcAddress: (_e = (_d = h.spec) === null || _d === void 0 ? void 0 : _d.bmc) === null || _e === void 0 ? void 0 : _e.address,
                systemVendor: {
                    virtual: false,
                    productName: 'Bare Metal Host',
                },
            };
            const restBmh = {
                id: ((_f = h.metadata) === null || _f === void 0 ? void 0 : _f.uid) || '',
                href: 'bmc', // It's BMC
                kind: 'Host',
                status: 'known',
                statusInfo: '',
                inventory: JSON.stringify(hostInventory),
                requestedHostname: ((_h = (_g = h.metadata) === null || _g === void 0 ? void 0 : _g.annotations) === null || _h === void 0 ? void 0 : _h[constants_1.BMH_HOSTNAME_ANNOTATION]) || ((_j = h.metadata) === null || _j === void 0 ? void 0 : _j.name),
                role: undefined,
                createdAt: (_k = h.metadata) === null || _k === void 0 ? void 0 : _k.creationTimestamp,
            };
            return restBmh;
        })
        : [];
    return [...hosts, ...restBmhs];
};
exports.getAIHosts = getAIHosts;
const extractVersionFromImageSetRef = (imageSetRefName) => {
    if (!imageSetRefName) {
        return undefined;
    }
    // Match pattern like "4.19.18" or "4.19" in strings like "img4.19.18-multi-appsub"
    const versionMatch = imageSetRefName.match(/(\d+\.\d+)(?:\.\d+)?/);
    if (versionMatch && versionMatch[1]) {
        return versionMatch[1];
    }
    return undefined;
};
const getAICluster = ({ clusterDeployment, agentClusterInstall, agents = [], infraEnv, }) => {
    var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19;
    const imageSetRefName = (_b = (_a = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _a === void 0 ? void 0 : _a.imageSetRef) === null || _b === void 0 ? void 0 : _b.name;
    const extractedVersion = extractVersionFromImageSetRef(imageSetRefName);
    const installVersion = ((_c = clusterDeployment.status) === null || _c === void 0 ? void 0 : _c.installVersion) ||
        ((_e = (_d = clusterDeployment.metadata) === null || _d === void 0 ? void 0 : _d.labels) === null || _e === void 0 ? void 0 : _e[constants_1.OCP_VERSION_MAJOR_MINOR]) ||
        extractedVersion;
    const [status, statusInfo] = (0, status_1.getClusterStatus)(agentClusterInstall);
    const aiCluster = {
        id: ((_f = clusterDeployment.metadata) === null || _f === void 0 ? void 0 : _f.uid) || '',
        kind: 'Cluster',
        href: '',
        name: (_g = clusterDeployment.spec) === null || _g === void 0 ? void 0 : _g.clusterName,
        baseDnsDomain: (_h = clusterDeployment.spec) === null || _h === void 0 ? void 0 : _h.baseDomain,
        openshiftVersion: installVersion,
        apiVips: ((_k = (_j = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _j === void 0 ? void 0 : _j.apiVIPs) === null || _k === void 0 ? void 0 : _k.length)
            ? (((_l = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _l === void 0 ? void 0 : _l.apiVIPs) || []).map((ip) => ({ ip }))
            : ((_m = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _m === void 0 ? void 0 : _m.apiVIP)
                ? [{ ip: (_o = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _o === void 0 ? void 0 : _o.apiVIP }]
                : [],
        ingressVips: ((_q = (_p = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _p === void 0 ? void 0 : _p.ingressVIPs) === null || _q === void 0 ? void 0 : _q.length)
            ? (((_r = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _r === void 0 ? void 0 : _r.ingressVIPs) || []).map((ip) => ({ ip }))
            : ((_s = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _s === void 0 ? void 0 : _s.ingressVIP)
                ? [{ ip: (_t = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _t === void 0 ? void 0 : _t.ingressVIP }]
                : [],
        highAvailabilityMode: ((_v = (_u = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _u === void 0 ? void 0 : _u.provisionRequirements) === null || _v === void 0 ? void 0 : _v.controlPlaneAgents) === 1 ? 'None' : 'Full',
        status,
        statusInfo,
        imageInfo: {
            sshPublicKey: (_w = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _w === void 0 ? void 0 : _w.sshPublicKey,
        },
        sshPublicKey: (_x = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _x === void 0 ? void 0 : _x.sshPublicKey,
        clusterNetworkHostPrefix: (_1 = (_0 = (_z = (_y = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _y === void 0 ? void 0 : _y.networking) === null || _z === void 0 ? void 0 : _z.clusterNetwork) === null || _0 === void 0 ? void 0 : _0[0]) === null || _1 === void 0 ? void 0 : _1.hostPrefix,
        clusterNetworks: (_3 = (_2 = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _2 === void 0 ? void 0 : _2.networking) === null || _3 === void 0 ? void 0 : _3.clusterNetwork,
        serviceNetworks: (_6 = (_5 = (_4 = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _4 === void 0 ? void 0 : _4.networking) === null || _5 === void 0 ? void 0 : _5.serviceNetwork) === null || _6 === void 0 ? void 0 : _6.map((cidr) => ({
            cidr,
        })),
        machineNetworks: (_8 = (_7 = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _7 === void 0 ? void 0 : _7.networking) === null || _8 === void 0 ? void 0 : _8.machineNetwork,
        monitoredOperators: [],
        vipDhcpAllocation: false,
        userManagedNetworking: Boolean(((_10 = (_9 = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _9 === void 0 ? void 0 : _9.provisionRequirements) === null || _10 === void 0 ? void 0 : _10.controlPlaneAgents) === 1 ||
            ((_12 = (_11 = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _11 === void 0 ? void 0 : _11.networking) === null || _12 === void 0 ? void 0 : _12.userManagedNetworking)),
        hostNetworks: (0, network_1.getHostNetworks)(agents, agentClusterInstall),
        totalHostCount: agents === null || agents === void 0 ? void 0 : agents.length,
        hosts: (0, exports.getAIHosts)(agents),
        installStartedAt: (_13 = clusterDeployment.status) === null || _13 === void 0 ? void 0 : _13.installStartedTimestamp,
        installCompletedAt: (_14 = clusterDeployment.status) === null || _14 === void 0 ? void 0 : _14.installedTimestamp,
        validationsInfo: JSON.stringify(((_15 = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.status) === null || _15 === void 0 ? void 0 : _15.validationsInfo) || {}),
        cpuArchitecture: (0, clusterDeployment_1.getClusterDeploymentCpuArchitecture)(clusterDeployment, infraEnv),
        networkType: (_16 = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _16 === void 0 ? void 0 : _16.networking.networkType,
        controlPlaneCount: ((_17 = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _17 === void 0 ? void 0 : _17.provisionRequirements.controlPlaneAgents) || 3,
        platform: {
            type: (((_19 = (_18 = agentClusterInstall === null || agentClusterInstall === void 0 ? void 0 : agentClusterInstall.spec) === null || _18 === void 0 ? void 0 : _18.platformType) === null || _19 === void 0 ? void 0 : _19.toLowerCase()) || 'baremetal'),
        },
    };
    /*
    aiCluster.agentSelectorMasterLabels =
      clusterDeployment.spec?.platform?.agentBareMetal?.agentSelector?.matchLabels;
  
    // TODO(mlibra): Where to store that in K8S resources?
    //
    // Initial value of the "Auto-select control plane (master) hosts" switch
    // is driven by either undefined or actual value of agentSelectorWorkerLabels
    aiCluster.agentSelectorWorkerLabels = undefined;
  */
    return aiCluster;
};
exports.getAICluster = getAICluster;
//# sourceMappingURL=toAssisted.js.map