UNPKG

@atomist/sdm

Version:

Atomist Software Delivery Machine SDK

86 lines 3.58 kB
"use strict"; /* * Copyright © 2020 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. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.serviceAccountTemplate = exports.upsertServiceAccount = void 0; const logger_1 = require("@atomist/automation-client/lib/util/logger"); const _ = require("lodash"); const error_1 = require("../support/error"); const retry_1 = require("../support/retry"); const labels_1 = require("./labels"); const metadata_1 = require("./metadata"); const patch_1 = require("./patch"); const request_1 = require("./request"); const resource_1 = require("./resource"); /** * Create or patch service account. * * @param req Kuberenetes application request * @return Kubernetes resource spec used to create/patch the resource */ async function upsertServiceAccount(req) { const slug = request_1.appName(req); const spec = await serviceAccountTemplate(req); try { await req.clients.core.readNamespacedServiceAccount(spec.metadata.name, spec.metadata.namespace); } catch (e) { logger_1.logger.debug(`Failed to read service account ${slug}, creating: ${error_1.k8sErrMsg(e)}`); logger_1.logger.info(`Creating service account ${slug} using '${resource_1.logObject(spec)}'`); await retry_1.logRetry(() => req.clients.core.createNamespacedServiceAccount(spec.metadata.namespace, spec), `create service account ${slug}`); return spec; } logger_1.logger.info(`Service account ${slug} exists, patching using '${resource_1.logObject(spec)}'`); await retry_1.logRetry(() => req.clients.core.patchNamespacedServiceAccount(spec.metadata.name, spec.metadata.namespace, spec, undefined, undefined, undefined, undefined, patch_1.patchHeaders(req)), `patch service account ${slug}`); return spec; } exports.upsertServiceAccount = upsertServiceAccount; /** * Create service account spec for a Kubernetes application. The * `req.rbac.serviceAccountSpec`, if it not false, is merged into the * spec created by this function using `lodash.merge(default, * req.rbac.serviceAccountSpec)`. * * It is possible to override the service account name using the * [[KubernetesApplication.serviceAccountSpec]]. If you do this, make * sure you know what you are doing and also override it in the * [[KubernetesApplication.roleBindingSpec]]. * * @param req application request * @return service account resource specification */ async function serviceAccountTemplate(req) { const labels = labels_1.applicationLabels(req); const metadata = metadata_1.metadataTemplate({ name: req.name, namespace: req.ns, labels, }); const apiVersion = "v1"; const kind = "ServiceAccount"; const sa = { apiVersion, kind, metadata, }; if (req.serviceAccountSpec) { _.merge(sa, req.serviceAccountSpec, { apiVersion, kind }); sa.metadata.namespace = req.ns; } return sa; } exports.serviceAccountTemplate = serviceAccountTemplate; //# sourceMappingURL=serviceAccount.js.map