UNPKG

@controlplane/cli

Version:

Control Plane Corporation CLI

171 lines 7.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HelmBase = void 0; const functions_1 = require("./functions"); const resolver_1 = require("../commands/resolver"); const resultFetcher_1 = require("../rest/resultFetcher"); const constants_1 = require("./constants"); class HelmBase { constructor(client, session, args, ensureDeletion) { this.client = client; this.session = session; this.args = args; this.ensureDeletion = ensureDeletion; // Parameters dependant this.secretParentLink = (0, resolver_1.kindResolver)('secret').parentLink(this.session.context); this.org = this.session.context.org; this.gvc = this.session.context.gvc || ''; this.releaseName = this.args.release || ''; } // Public Methods // async createReleaseSecret(release, tags) { // Construct the secret object for the release const secret = { name: (0, functions_1.getReleaseSecretName)(release.name, release.version), kind: 'secret', description: constants_1.CPLN_RELEASE_SECRET_DESCRIPTION, tags: { ...tags, schemaVersion: release.schemaVersion, owner: constants_1.CPLN_RELEASE_SECRET_OWNER, name: release.name, version: release.version, status: release.info.status, }, type: 'opaque', data: { encoding: 'base64', payload: await (0, functions_1.encodeRelease)(release), }, }; // Add special tags secret.tags[constants_1.CPLN_RELEASE_SECRET_GENERATED_BY] = true; secret.tags[constants_1.CPLN_HELM_SECRET_SPECIAL_TAG] = true; // Create the secret await this.client.axios.put(this.secretParentLink, secret); } async loadReleaseRevision(secretName) { const { HelmReleaseRevisionManager } = require('./helm-release-revision-manager'); // Construct the self link for this secret const selfLink = (0, resolver_1.resolveToLink)('secret', secretName, this.session.context); // Reveal the secret const revealedSecret = await this.revealSecret(selfLink); // Return a new HelmReleaseRevisionManager instance based on the created secret return new HelmReleaseRevisionManager(this.client, this.session, this.args, this.ensureDeletion, revealedSecret); } async revealSecret(secretSelfLink) { // Fetch and reveal secret using its self link const secret = await this.client.get(`${secretSelfLink}/-reveal`); // Prepare secret before return this.prepareResource(secret); // Return the revealed secret return secret; } async fetchSecret(secretSelfLink) { // Fetch the release using its self link without reveal const secret = await this.client.get(secretSelfLink); // Prepare secret before return this.prepareResource(secret); // Return the fetched secret return secret; } async fetchResource(kind, selfLink) { // Reveal and return the secret if specified if (kind === 'secret') { return this.revealSecret(selfLink); } // Fetch the resource using its self link const resource = await this.client.get(selfLink); // Prepare resource before return this.prepareResource(resource); // Return the resource return resource; } async migrateReleaseSecretIfNecessary(releaseName) { var _a; const { HelmReleaseMigrator } = require('./helm-release-migrator'); // Construct the self link for the legacy release secret const selfLink = (0, resolver_1.resolveToLink)('secret', (0, functions_1.getReleaseSecretNameLegacy)(releaseName), this.session.context); // Attempt to fetch and migrate a legacy release with the given release name try { // Fetch the legacy release secret const secret = await this.client.get(`${selfLink}/-reveal`); // If we got here, then that legacy release exists and we need to migrate it const helmMigrator = new HelmReleaseMigrator(this.client, this.session, this.args, this.ensureDeletion, secret); // Migrate the release await helmMigrator.migrate(); } catch (e) { if (((_a = e.response) === null || _a === void 0 ? void 0 : _a.status) !== 404) { // We might get here, for example, if the user does not have enough permission to reveal a secret this.session.err(`ERROR: A regular check failed, please try again after resolving the error below.`); this.session.abort({ error: e }); } } // Attempt to find release secrets that are using schemaVersion larger or equal to 3 try { // Fetch all secrets for the specified release const secretList = await this.client.post(`${this.secretParentLink}/-query`, { kind: 'secret', spec: { match: 'all', terms: [ { op: '=', tag: 'owner', value: constants_1.CPLN_RELEASE_SECRET_OWNER, }, { op: '=', tag: constants_1.CPLN_HELM_SECRET_SPECIAL_TAG, value: 'true', }, { op: '=', tag: 'name', value: releaseName, }, { op: '!=', tag: constants_1.CPLN_HELM_SCHEMA_LATEST_VERSION_TAG_KEY, value: constants_1.CPLN_HELM_SCHEMA_LATEST_VERSION, }, ], }, }); // Continue fetching secrets await (0, resultFetcher_1.fetchPages)(this.client, this.session.format.max, secretList); // Treat the secret list items as an array of secrets const secrets = secretList.items; // Skip migration if no secrets were found if (secrets.length === 0) { return; } // Migrate release revision secrets if found for (const secret of secrets) { // Construct the self link for the release secret const selfLink = (0, resolver_1.resolveToLink)('secret', secret.name, this.session.context); // Reveal the release secret const revealedSecret = await this.client.get(`${selfLink}/-reveal`); // If we got here, then that legacy release exists and we need to migrate it const helmMigrator = new HelmReleaseMigrator(this.client, this.session, this.args, this.ensureDeletion, revealedSecret); // Migrate to the latest version await helmMigrator.migrate(); } } catch (e) { // We might get here, for example, if the user does not have enough permission to reveal a secret this.session.err(`ERROR: A regular check failed, please try again after resolving the error below.`); this.session.abort({ error: e }); } } // Private Methods // prepareResource(resource) { // Remove unnecessary properties to avoid a 409 on update delete resource.version; delete resource.created; delete resource.lastModified; } } exports.HelmBase = HelmBase; //# sourceMappingURL=helm-base.js.map