|
| 1 | +import { CLIOptions, Inquirerer, Question } from 'inquirerer'; |
| 2 | +import { ParsedArgs } from 'minimist'; |
| 3 | +import chalk from 'chalk'; |
| 4 | +import { KubernetesClient } from 'kubernetesjs'; |
| 5 | +import { readYamlFile, inferResourceType } from '../config'; |
| 6 | +import * as fs from 'fs'; |
| 7 | +import * as path from 'path'; |
| 8 | + |
| 9 | +async function promptYamlFilePath(prompter: Inquirerer, argv: Partial<ParsedArgs>): Promise<string> { |
| 10 | + const question: Question = { |
| 11 | + type: 'text', |
| 12 | + name: 'filePath', |
| 13 | + message: 'Enter path to YAML file', |
| 14 | + required: true |
| 15 | + }; |
| 16 | + |
| 17 | + const { filePath } = await prompter.prompt(argv, [question]); |
| 18 | + return filePath; |
| 19 | +} |
| 20 | + |
| 21 | +async function applyResource(client: KubernetesClient, resource: any, namespace: string): Promise<void> { |
| 22 | + const kind = resource.kind.toLowerCase(); |
| 23 | + const name = resource.metadata?.name; |
| 24 | + |
| 25 | + if (!name) { |
| 26 | + throw new Error('Resource must have a name'); |
| 27 | + } |
| 28 | + |
| 29 | + console.log(chalk.blue(`Applying ${kind} "${name}" in namespace ${namespace}...`)); |
| 30 | + |
| 31 | + try { |
| 32 | + switch (kind) { |
| 33 | + case 'deployment': |
| 34 | + await client.createAppsV1NamespacedDeployment({ |
| 35 | + path: { namespace }, |
| 36 | + query: { |
| 37 | + pretty: 'true', |
| 38 | + fieldManager: 'kubernetesjs-cli' |
| 39 | + }, |
| 40 | + body: resource |
| 41 | + }); |
| 42 | + console.log(chalk.green(`Deployment "${name}" created/updated successfully`)); |
| 43 | + break; |
| 44 | + |
| 45 | + case 'service': |
| 46 | + await client.createCoreV1NamespacedService({ |
| 47 | + path: { namespace }, |
| 48 | + query: { |
| 49 | + pretty: 'true', |
| 50 | + fieldManager: 'kubernetesjs-cli' |
| 51 | + }, |
| 52 | + body: resource |
| 53 | + }); |
| 54 | + console.log(chalk.green(`Service "${name}" created/updated successfully`)); |
| 55 | + break; |
| 56 | + |
| 57 | + case 'pod': |
| 58 | + await client.createCoreV1NamespacedPod({ |
| 59 | + path: { namespace }, |
| 60 | + query: { |
| 61 | + pretty: 'true', |
| 62 | + fieldManager: 'kubernetesjs-cli' |
| 63 | + }, |
| 64 | + body: resource |
| 65 | + }); |
| 66 | + console.log(chalk.green(`Pod "${name}" created/updated successfully`)); |
| 67 | + break; |
| 68 | + |
| 69 | + case 'configmap': |
| 70 | + await client.createCoreV1NamespacedConfigMap({ |
| 71 | + path: { namespace }, |
| 72 | + query: { |
| 73 | + pretty: 'true', |
| 74 | + fieldManager: 'kubernetesjs-cli' |
| 75 | + }, |
| 76 | + body: resource |
| 77 | + }); |
| 78 | + console.log(chalk.green(`ConfigMap "${name}" created/updated successfully`)); |
| 79 | + break; |
| 80 | + |
| 81 | + case 'secret': |
| 82 | + await client.createCoreV1NamespacedSecret({ |
| 83 | + path: { namespace }, |
| 84 | + query: { |
| 85 | + pretty: 'true', |
| 86 | + fieldManager: 'kubernetesjs-cli' |
| 87 | + }, |
| 88 | + body: resource |
| 89 | + }); |
| 90 | + console.log(chalk.green(`Secret "${name}" created/updated successfully`)); |
| 91 | + break; |
| 92 | + |
| 93 | + default: |
| 94 | + console.log(chalk.yellow(`Resource kind "${kind}" not implemented yet`)); |
| 95 | + } |
| 96 | + } catch (error) { |
| 97 | + console.error(chalk.red(`Error applying ${kind} "${name}": ${error}`)); |
| 98 | + throw error; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +export default async ( |
| 103 | + argv: Partial<ParsedArgs>, |
| 104 | + prompter: Inquirerer, |
| 105 | + _options: CLIOptions |
| 106 | +) => { |
| 107 | + try { |
| 108 | + const client = new KubernetesClient({ |
| 109 | + restEndpoint: 'http://localhost:8001' // Default kube-proxy endpoint |
| 110 | + }); |
| 111 | + |
| 112 | + const filePath = argv.f || argv._?.[0] || await promptYamlFilePath(prompter, argv); |
| 113 | + |
| 114 | + if (!filePath) { |
| 115 | + console.error(chalk.red('No file path provided')); |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + if (!fs.existsSync(filePath)) { |
| 120 | + console.error(chalk.red(`File not found: ${filePath}`)); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + let resources: any[]; |
| 125 | + try { |
| 126 | + const content = readYamlFile(filePath); |
| 127 | + |
| 128 | + if (Array.isArray(content)) { |
| 129 | + resources = content; |
| 130 | + } else if (content.kind === 'List' && Array.isArray(content.items)) { |
| 131 | + resources = content.items; |
| 132 | + } else { |
| 133 | + resources = [content]; |
| 134 | + } |
| 135 | + } catch (error) { |
| 136 | + console.error(chalk.red(`Error parsing YAML file: ${error}`)); |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + for (const resource of resources) { |
| 141 | + try { |
| 142 | + const namespace = resource.metadata?.namespace || argv.n || argv.namespace || 'default'; |
| 143 | + await applyResource(client, resource, namespace); |
| 144 | + } catch (error) { |
| 145 | + console.error(chalk.red(`Failed to apply resource: ${error}`)); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + console.log(chalk.green('Apply completed')); |
| 150 | + } catch (error) { |
| 151 | + console.error(chalk.red(`Error: ${error}`)); |
| 152 | + } |
| 153 | +}; |
0 commit comments