All files / src/commands/k8s k8s.get.command.js

18.26% Statements 19/104
100% Branches 1/1
0% Functions 0/2
18.26% Lines 19/104

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x                                                                                                                                                                 193x 193x 193x            
import { defineCommand } from '../../lib/define-command.js';
import { getK8sCluster } from '../../lib/k8s.js';
import { styleText } from '../../lib/style-text.js';
import { Logger } from '../../logger.js';
import { humanJsonOutputFormatOption, orgaIdOrNameOption } from '../global.options.js';
import { k8sIdOrNameArg } from './k8s.args.js';
 
export const k8sGetCommand = defineCommand({
  description: 'Get information about a Kubernetes cluster',
  since: '4.3.0',
  options: {
    org: orgaIdOrNameOption,
    format: humanJsonOutputFormatOption,
  },
  args: [k8sIdOrNameArg],
  async handler(options, clusterIdOrName) {
    const { format, org: orgIdOrName } = options;

    const k8sInfo = await getK8sCluster(orgIdOrName, clusterIdOrName);

    switch (format) {
      case 'json':
        Logger.printJson(k8sInfo);
        break;
      case 'human':
      default: {
        const topo = k8sInfo.topologyConfig;
        const overview = {
          Name: k8sInfo.name,
          ID: k8sInfo.id,
          Status: k8sInfo.status,
          Version: k8sInfo.version,
          Topology: formatTopology(topo),
          Autoscaling: k8sInfo.features?.autoscalingEnabled ? 'enabled' : 'disabled',
          'Persistent storage': k8sInfo.features?.csi != null ? 'enabled' : 'disabled',
        };
        if (k8sInfo.tags?.length) overview.Tags = k8sInfo.tags.join(', ');
        if (k8sInfo.description) overview.Description = k8sInfo.description;
        if (k8sInfo.storageUsageBytes != null) {
          overview.Storage = `${Math.round((k8sInfo.storageUsageBytes / 1024 ** 3) * 100) / 100} GB`;
        }
        console.table(overview);

        if (topo?.topology === 'DISTRIBUTED' && topo.components) {
          Logger.println('');
          Logger.println('🧩 Control plane components');
          console.table(
            Object.fromEntries(
              Object.entries(topo.components).map(([name, c]) => [
                name,
                { Flavor: c?.flavor ?? '-', RF: c?.replicationFactor ?? '-' },
              ]),
            ),
          );
        }

        if (k8sInfo.nodeGroups?.length) {
          Logger.println('');
          Logger.println(`🧮 Node groups (${k8sInfo.nodeGroups.length})`);
          console.table(
            Object.fromEntries(
              k8sInfo.nodeGroups.map((ng) => [
                ng.id,
                {
                  Name: ng.name,
                  Status: ng.status,
                  Flavor: ng.flavor,
                  Nodes: `${ng.currentNodeCount}/${ng.targetNodeCount}`,
                },
              ]),
            ),
          );
        }

        if (k8sInfo.loadBalancers?.length) {
          Logger.println('');
          Logger.println(`🔀 Load balancers (${k8sInfo.loadBalancers.length})`);
          k8sInfo.loadBalancers.forEach((lb) => {
            console.table({
              ID: lb.id,
              Flavor: lb.flavor,
              IPs: lb.ips?.join(', ') ?? '-',
              Domain: lb.domainName ?? '-',
            });
          });
        }

        Logger.println('');
        const orgMessageComplement = orgIdOrName ? `--org "${orgIdOrName.orga_id || orgIdOrName.orga_name}"` : '';

        Logger.println(
          `Once ACTIVE, get the kubeconfig with ${styleText('blue', `clever k8s get-kubeconfig ${k8sInfo.id} ${orgMessageComplement}`)}`,
        );
        break;
      }
    }
  },
});
 
function formatTopology(topo) {
  if (topo == null) return '-';
  if (topo.topology === 'DISTRIBUTED') return 'DISTRIBUTED';
  return `${topo.topology} (${topo.flavor}, rf=${topo.replicationFactor})`;
}