@atomist/sdm-core
Version:
Atomist Software Delivery Machine - Implementation
88 lines • 4.45 kB
JavaScript
/*
* Copyright © 2019 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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 });
const sdm_1 = require("@atomist/sdm");
const SdmGoalMessage_1 = require("@atomist/sdm/lib/api/goal/SdmGoalMessage");
const _ = require("lodash");
const container_1 = require("../../goal/container/container");
exports.DefaultKubernetesFulfillmentOptions = {
registration: "@atomist/k8s-sdm",
name: "kubernetes-container-fulfill",
};
/**
* GoalScheduler implementation that redirects goals to a registered k8s-sdm for
* fulfillment.
*
* This is useful in runtimes that don't support launching container goals such as
* FaaS environments like Google Cloud Functions.
*/
class KubernetesFulfillmentGoalScheduler {
constructor(options = exports.DefaultKubernetesFulfillmentOptions) {
this.options = options;
}
schedule(gi) {
return __awaiter(this, void 0, void 0, function* () {
const { goalEvent, goal, configuration } = gi;
const containerGoal = goal;
let registration = _.cloneDeep(containerGoal.registrations[0]);
const fulfillment = _.get(registration, "fulfillment");
if (!!fulfillment) {
goalEvent.fulfillment.registration = fulfillment.registration;
goalEvent.fulfillment.name = fulfillment.name;
}
else {
goalEvent.fulfillment.registration =
typeof this.options.registration === "string" ? this.options.registration : yield this.options.registration(gi);
goalEvent.fulfillment.name =
typeof this.options.name === "string" ? this.options.name : yield this.options.name(gi);
}
goalEvent.fulfillment.method = SdmGoalMessage_1.SdmGoalFulfillmentMethod.Sdm;
if (registration.callback) {
registration = yield configuration.sdm.projectLoader.doWithProject(Object.assign(Object.assign({}, gi), { readOnly: true, cloneOptions: sdm_1.minimalClone(goalEvent.push, { detachHead: true }) }), (p) => __awaiter(this, void 0, void 0, function* () {
return Object.assign(Object.assign({}, registration), (yield registration.callback(_.cloneDeep(registration), p, containerGoal, goalEvent, gi)) || {});
}));
}
const data = JSON.parse(goalEvent.data || "{}");
const newData = {};
delete registration.callback;
_.set(newData, container_1.ContainerRegistrationGoalDataKey, registration);
goalEvent.data = JSON.stringify(_.merge(data, newData));
return {
state: sdm_1.SdmGoalState.requested,
description: sdm_1.descriptionFromState(goal, sdm_1.SdmGoalState.requested),
phase: "scheduled",
};
});
}
supports(gi) {
return __awaiter(this, void 0, void 0, function* () {
const { goal } = gi;
return goal instanceof container_1.Container;
});
}
}
exports.KubernetesFulfillmentGoalScheduler = KubernetesFulfillmentGoalScheduler;
//# sourceMappingURL=KubernetesFulfillmentGoalScheduler.js.map
;