"Fossies" - the Fresh Open Source Software Archive

Member "lens-5.3.3/src/migrations/cluster-store/5.0.0-beta.13.ts" (14 Dec 2021, 4954 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 "5.0.0-beta.13.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 import type { ClusterModel, ClusterPreferences, ClusterPrometheusPreferences } from "../../common/cluster-types";
   23 import { MigrationDeclaration, migrationLog } from "../helpers";
   24 import { generateNewIdFor } from "../utils";
   25 import path from "path";
   26 import { moveSync, removeSync } from "fs-extra";
   27 import { AppPaths } from "../../common/app-paths";
   28 
   29 function mergePrometheusPreferences(left: ClusterPrometheusPreferences, right: ClusterPrometheusPreferences): ClusterPrometheusPreferences {
   30   if (left.prometheus && left.prometheusProvider) {
   31     return {
   32       prometheus: left.prometheus,
   33       prometheusProvider: left.prometheusProvider,
   34     };
   35   }
   36 
   37   if (right.prometheus && right.prometheusProvider) {
   38     return {
   39       prometheus: right.prometheus,
   40       prometheusProvider: right.prometheusProvider,
   41     };
   42   }
   43 
   44   return {};
   45 }
   46 
   47 function mergePreferences(left: ClusterPreferences, right: ClusterPreferences): ClusterPreferences {
   48   return {
   49     terminalCWD: left.terminalCWD || right.terminalCWD || undefined,
   50     clusterName: left.clusterName || right.clusterName || undefined,
   51     iconOrder: left.iconOrder || right.iconOrder || undefined,
   52     icon: left.icon || right.icon || undefined,
   53     httpsProxy: left.httpsProxy || right.httpsProxy || undefined,
   54     hiddenMetrics: mergeSet(left.hiddenMetrics ?? [], right.hiddenMetrics ?? []),
   55     ...mergePrometheusPreferences(left, right),
   56   };
   57 }
   58 
   59 function mergeLabels(left: Record<string, string>, right: Record<string, string>): Record<string, string> {
   60   return {
   61     ...right,
   62     ...left,
   63   };
   64 }
   65 
   66 function mergeSet(...iterables: Iterable<string>[]): string[] {
   67   const res = new Set<string>();
   68 
   69   for (const iterable of iterables) {
   70     for (const val of iterable) {
   71       res.add(val);
   72     }
   73   }
   74 
   75   return [...res];
   76 }
   77 
   78 function mergeClusterModel(prev: ClusterModel, right: Omit<ClusterModel, "id">): ClusterModel {
   79   return {
   80     id: prev.id,
   81     kubeConfigPath: prev.kubeConfigPath,
   82     contextName: prev.contextName,
   83     preferences: mergePreferences(prev.preferences ?? {}, right.preferences ?? {}),
   84     metadata: prev.metadata,
   85     labels: mergeLabels(prev.labels ?? {}, right.labels ?? {}),
   86     accessibleNamespaces: mergeSet(prev.accessibleNamespaces ?? [], right.accessibleNamespaces ?? []),
   87     workspace: prev.workspace || right.workspace,
   88     workspaces: mergeSet([prev.workspace, right.workspace], prev.workspaces ?? [], right.workspaces ?? []),
   89   };
   90 }
   91 
   92 function moveStorageFolder({ folder, newId, oldId }: { folder: string, newId: string, oldId: string }): void {
   93   const oldPath = path.resolve(folder, `${oldId}.json`);
   94   const newPath = path.resolve(folder, `${newId}.json`);
   95 
   96   try {
   97     moveSync(oldPath, newPath);
   98   } catch (error) {
   99     if (String(error).includes("dest already exists")) {
  100       migrationLog(`Multiple old lens-local-storage files for newId=${newId}. Removing ${oldId}.json`);
  101       removeSync(oldPath);
  102     }
  103   }
  104 }
  105 
  106 export default {
  107   version: "5.0.0-beta.13",
  108   run(store) {
  109     const folder = path.resolve(AppPaths.get("userData"), "lens-local-storage");
  110 
  111     const oldClusters: ClusterModel[] = store.get("clusters") ?? [];
  112     const clusters = new Map<string, ClusterModel>();
  113 
  114     for (const { id: oldId, ...cluster } of oldClusters) {
  115       const newId = generateNewIdFor(cluster);
  116 
  117       if (clusters.has(newId)) {
  118         migrationLog(`Duplicate entries for ${newId}`, { oldId });
  119         clusters.set(newId, mergeClusterModel(clusters.get(newId), cluster));
  120       } else {
  121         migrationLog(`First entry for ${newId}`, { oldId });
  122         clusters.set(newId, {
  123           ...cluster,
  124           id: newId,
  125           workspaces: [cluster.workspace].filter(Boolean),
  126         });
  127         moveStorageFolder({ folder, newId, oldId });
  128       }
  129     }
  130 
  131     store.set("clusters", [...clusters.values()]);
  132   },
  133 } as MigrationDeclaration;