@fbl-plugins/k8s-kubectl
Version:
FBL plugin for K8s Kubectl CLI
109 lines • 4.74 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetOneActionProcessor = void 0;
const Joi = require("joi");
const BaseActionProcessor_1 = require("./BaseActionProcessor");
const K8sObjectJoiValidationSchema_1 = require("../joi/K8sObjectJoiValidationSchema");
const fbl_1 = require("fbl");
const path_1 = require("path");
class GetOneActionProcessor extends BaseActionProcessor_1.BaseActionProcessor {
/**
* @inheritdoc
*/
getValidationSchema() {
return GetOneActionProcessor.validationSchema;
}
/**
* @inheritdoc
*/
execute() {
return __awaiter(this, void 0, void 0, function* () {
const args = yield this.prepareCLIArgs();
const result = yield this.execKubectlCommand(args, this.options.debug, '');
const response = JSON.parse(result.stdout);
let items;
if (response.kind !== 'List') {
items = [response];
}
else {
items = response.items;
}
if (!items || !items.length) {
throw new fbl_1.ActionError('Unable to find any resources', 'NOT_FOUND');
}
fbl_1.ContextUtil.assignTo(this.context, this.parameters, this.snapshot, this.options.assignResourceTo, items[0]);
fbl_1.ContextUtil.pushTo(this.context, this.parameters, this.snapshot, this.options.pushResourceTo, items[0]);
});
}
/**
* Prepare CLI args
*/
prepareCLIArgs() {
return __awaiter(this, void 0, void 0, function* () {
const args = ['get'];
this.pushWithValue(args, '-n', this.options.namespace);
this.pushWithValue(args, '--chunk-size', '1');
if (this.options.labels) {
for (const label of Object.keys(this.options.labels)) {
const value = this.options.labels[label];
args.push('-l', `${label}=${value}`);
}
}
if (this.options.extra) {
args.push(...this.options.extra);
}
if (this.options.resource) {
args.push(this.options.resource);
}
if (this.options.name) {
args.push(this.options.name);
}
if (this.options.path) {
const tempPathsRegistry = fbl_1.TempPathsRegistry.instance;
const targetDir = yield tempPathsRegistry.createTempDir();
args.push('-R', '-f', targetDir);
const path = fbl_1.FSUtil.getAbsolutePath(this.options.path, this.snapshot.wd);
yield this.processTemplateFile(path, path_1.join(targetDir, path_1.basename(path)));
}
if (this.options.inline) {
const tmp = yield this.writeYamlToTempFile(this.options.inline);
args.push('-f', tmp);
}
this.pushWithValue(args, '-o', 'json');
return args;
});
}
}
exports.GetOneActionProcessor = GetOneActionProcessor;
GetOneActionProcessor.validationSchema = Joi.object({
resource: Joi.string().min(1),
name: Joi.string().min(1),
path: Joi.string().min(1),
inline: K8sObjectJoiValidationSchema_1.K8sObjectJoiValidationSchema,
labels: Joi.object().pattern(Joi.string().min(1).required(), Joi.string().required().min(1)),
// enable verbose output
debug: Joi.boolean(),
// Namespace
namespace: Joi.string(),
// extra arguments to append to the command
// refer to `kubectl apply --help` for all available options
extra: Joi.array().items(Joi.string()),
assignResourceTo: fbl_1.FBL_ASSIGN_TO_SCHEMA,
pushResourceTo: fbl_1.FBL_PUSH_TO_SCHEMA,
})
.with('name', 'resource')
.without('path', 'resource')
.without('labels', 'name')
.or('resource', 'name', 'path', 'inline', 'labels')
.required()
.options({ abortEarly: true, allowUnknown: false });
//# sourceMappingURL=GetOneActionProcessor.js.map