@fbl-plugins/k8s-kubectl
Version:
FBL plugin for K8s Kubectl CLI
108 lines • 4.88 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.GetAllActionProcessor = void 0;
const Joi = require("joi");
const BaseActionProcessor_1 = require("./BaseActionProcessor");
const K8sObjectJoiValidationSchema_1 = require("../joi/K8sObjectJoiValidationSchema");
const fbl_1 = require("fbl");
class GetAllActionProcessor extends BaseActionProcessor_1.BaseActionProcessor {
/**
* @inheritdoc
*/
getValidationSchema() {
return GetAllActionProcessor.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) {
items = [];
this.snapshot.log('Unable to find any resources', true);
}
fbl_1.ContextUtil.assignTo(this.context, this.parameters, this.snapshot, this.options.assignResourcesTo, items);
fbl_1.ContextUtil.pushTo(this.context, this.parameters, this.snapshot, this.options.pushResourcesTo, items);
});
}
/**
* Prepare CLI args
*/
prepareCLIArgs() {
return __awaiter(this, void 0, void 0, function* () {
const args = ['get'];
this.pushWithValue(args, '-n', this.options.namespace);
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.resources && this.options.resources.length) {
args.push(this.options.resources.join(','));
}
if (this.options.names && this.options.names.length) {
args.push(this.options.names.join(' '));
}
if (this.options.paths && this.options.paths.length) {
const tempPathsRegistry = fbl_1.TempPathsRegistry.instance;
const targetDir = yield tempPathsRegistry.createTempDir();
args.push('-R', '-f', targetDir);
for (const path of this.options.paths) {
yield this.processGlobPath(path, targetDir);
}
}
yield this.pushMultipleWithValue(args, '-f', this.options.inline, (config) => __awaiter(this, void 0, void 0, function* () {
return yield this.writeYamlToTempFile(config);
}));
this.pushWithValue(args, '-o', 'json');
return args;
});
}
}
exports.GetAllActionProcessor = GetAllActionProcessor;
GetAllActionProcessor.validationSchema = Joi.object({
resources: Joi.array().items(Joi.string().min(1).required()).min(1),
names: Joi.array().items(Joi.string().min(1).required()),
paths: Joi.array().items(Joi.string().min(1).required()),
inline: Joi.array().items(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()),
assignResourcesTo: fbl_1.FBL_ASSIGN_TO_SCHEMA,
pushResourcesTo: fbl_1.FBL_PUSH_TO_SCHEMA,
})
.with('names', 'resources')
.without('paths', 'resources')
.without('labels', 'names')
.or('resources', 'names', 'paths', 'inline', 'labels')
.required()
.options({ abortEarly: true, allowUnknown: false });
//# sourceMappingURL=GetAllActionProcessor.js.map