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

66.66% Statements 48/72
100% Branches 1/1
0% Functions 0/1
66.66% Lines 48/72

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 73193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x                                                 193x 193x  
import { z } from 'zod';
import { defineCommand } from '../../lib/define-command.js';
import { defineOption } from '../../lib/define-option.js';
import { k8sUpdate } from '../../lib/k8s.js';
import { styleText } from '../../lib/style-text.js';
import { Logger } from '../../logger.js';
import { tags } from '../../parsers.js';
import { orgaIdOrNameOption } from '../global.options.js';
import { k8sIdOrNameArg } from './k8s.args.js';
 
export const k8sUpdateCommand = defineCommand({
  description: 'Update a Kubernetes cluster metadata or features',
  since: '4.9.0',
  options: {
    name: defineOption({
      name: 'name',
      schema: z.string().max(128).optional(),
      description: 'Rename the cluster',
      placeholder: 'name',
    }),
    description: defineOption({
      name: 'description',
      schema: z.string().max(4096).optional(),
      description: 'Free-form cluster description',
      placeholder: 'description',
    }),
    tag: defineOption({
      name: 'tag',
      schema: z.string().transform(tags).optional(),
      description: 'Replace tags (comma-separated, e.g.: env:prod,team:platform)',
      placeholder: 'tag[,tag...]',
    }),
    autoscaling: defineOption({
      name: 'autoscaling',
      schema: z.boolean().default(false),
      description: 'Enable the cluster autoscaler',
    }),
    disableAutoscaling: defineOption({
      name: 'disable-autoscaling',
      schema: z.boolean().default(false),
      description: 'Disable the cluster autoscaler',
    }),
    org: orgaIdOrNameOption,
  },
  args: [k8sIdOrNameArg],
  async handler(options, clusterIdOrName) {
    const { org: orgIdOrName } = options;

    if (options.autoscaling && options.disableAutoscaling) {
      throw new Error('--autoscaling and --disable-autoscaling are mutually exclusive');
    }

    const updates = {};
    if (options.name != null) updates.name = options.name;
    if (options.description != null) updates.description = options.description;
    if (options.tag != null) updates.tags = options.tag;

    const features = {};
    if (options.autoscaling) features.autoscalingEnabled = true;
    if (options.disableAutoscaling) features.autoscalingEnabled = false;
    if (Object.keys(features).length > 0) updates.features = features;

    if (Object.keys(updates).length === 0) {
      throw new Error(
        'No update specified. Provide at least one of --name, --description, --tag, --autoscaling, --disable-autoscaling',
      );
    }

    const cluster = await k8sUpdate(orgIdOrName, clusterIdOrName, updates);
    Logger.printSuccess(`Cluster ${styleText('green', cluster.name)} updated`);
  },
});