"Fossies" - the Fresh Open Source Software Archive

Member "lens-5.3.3/src/migrations/cluster-store/3.6.0-beta.1.ts" (14 Dec 2021, 3675 Bytes) of package /linux/misc/lens-5.3.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "3.6.0-beta.1.ts": 5.2.7_vs_5.3.0.

    1 /**
    2  * Copyright (c) 2021 OpenLens Authors
    3  *
    4  * Permission is hereby granted, free of charge, to any person obtaining a copy of
    5  * this software and associated documentation files (the "Software"), to deal in
    6  * the Software without restriction, including without limitation the rights to
    7  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
    8  * the Software, and to permit persons to whom the Software is furnished to do so,
    9  * subject to the following conditions:
   10  *
   11  * The above copyright notice and this permission notice shall be included in all
   12  * copies or substantial portions of the Software.
   13  *
   14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
   16  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
   17  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
   18  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   19  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   20  */
   21 
   22 // Move embedded kubeconfig into separate file and add reference to it to cluster settings
   23 // convert file path cluster icons to their base64 encoded versions
   24 
   25 import path from "path";
   26 import fse from "fs-extra";
   27 import { loadConfigFromFileSync } from "../../common/kube-helpers";
   28 import { MigrationDeclaration, migrationLog } from "../helpers";
   29 import type { ClusterModel } from "../../common/cluster-types";
   30 import { getCustomKubeConfigPath, storedKubeConfigFolder } from "../../common/utils";
   31 import { AppPaths } from "../../common/app-paths";
   32 
   33 interface Pre360ClusterModel extends ClusterModel {
   34   kubeConfig: string;
   35 }
   36 
   37 export default {
   38   version: "3.6.0-beta.1",
   39   run(store) {
   40     const userDataPath = AppPaths.get("userData");
   41     const storedClusters: Pre360ClusterModel[] = store.get("clusters") ?? [];
   42     const migratedClusters: ClusterModel[] = [];
   43 
   44     fse.ensureDirSync(storedKubeConfigFolder());
   45 
   46     migrationLog("Number of clusters to migrate: ", storedClusters.length);
   47 
   48     for (const clusterModel of storedClusters) {
   49       /**
   50        * migrate kubeconfig
   51        */
   52       try {
   53         const absPath = getCustomKubeConfigPath(clusterModel.id);
   54 
   55         // take the embedded kubeconfig and dump it into a file
   56         fse.writeFileSync(absPath, clusterModel.kubeConfig, { encoding: "utf-8", mode: 0o600 });
   57 
   58         clusterModel.kubeConfigPath = absPath;
   59         clusterModel.contextName = loadConfigFromFileSync(clusterModel.kubeConfigPath).config.getCurrentContext();
   60         delete clusterModel.kubeConfig;
   61 
   62       } catch (error) {
   63         migrationLog(`Failed to migrate Kubeconfig for cluster "${clusterModel.id}", removing clusterModel...`, error);
   64 
   65         continue;
   66       }
   67 
   68       /**
   69        * migrate cluster icon
   70        */
   71       try {
   72         if (clusterModel.preferences?.icon) {
   73           migrationLog(`migrating ${clusterModel.preferences.icon} for ${clusterModel.preferences.clusterName}`);
   74           const iconPath = clusterModel.preferences.icon.replace("store://", "");
   75           const fileData = fse.readFileSync(path.join(userDataPath, iconPath));
   76 
   77           clusterModel.preferences.icon = `data:;base64,${fileData.toString("base64")}`;
   78         } else {
   79           delete clusterModel.preferences?.icon;
   80         }
   81       } catch (error) {
   82         migrationLog(`Failed to migrate cluster icon for cluster "${clusterModel.id}"`, error);
   83         delete clusterModel.preferences.icon;
   84       }
   85 
   86       migratedClusters.push(clusterModel);
   87     }
   88 
   89     store.set("clusters", migratedClusters);
   90   },
   91 } as MigrationDeclaration;