UNPKG

@kinvolk/headlamp-plugin

Version:

The needed infrastructure for building Headlamp plugins.

164 lines (163 loc) 7.27 kB
import { KubeconfigObject } from '../lib/k8s/kubeconfig'; import { ConfigState } from '../redux/configSlice'; import { deleteClusterKubeconfig } from './deleteClusterKubeconfig'; import { findKubeconfigByClusterName } from './findKubeconfigByClusterName'; import { getUserIdFromLocalStorage } from './getUserIdFromLocalStorage'; import { updateStatelessClusterKubeconfig } from './updateStatelessClusterKubeconfig'; /** * DatabaseEvent is the event that is fired when the IndexedDB is upgraded. * It is used to create the object store. It is also used to get the result of the * IndexedDB open request. The result is the IndexedDB database. It is used to create * the transaction and the object store. It is also used to get the result of the * IndexedDB add request. The result is the key of the added object. * @see storeStatelessClusterKubeconfig * @see getStatelessClusterKubeConfigs * @see findKubeconfigByClusterName */ export interface DatabaseEvent extends Event { target: IDBOpenDBRequest & { result: IDBDatabase; }; } /** * DatabaseErrorEvent is the event that is fired when the IndexedDB is upgraded. * It is used to get the error of the IndexedDB open request. It is also used to get * the error of the IndexedDB add request. * @see storeStatelessClusterKubeconfig * @see getStatelessClusterKubeConfigs * @see findKubeconfigByClusterName */ export interface DatabaseErrorEvent extends Event { target: IDBOpenDBRequest & { error: DOMException | null; }; } /** * CursorSuccessEvent is the event that is fired when the IndexedDB is upgraded. * It is used to get the result of the IndexedDB open request. The result is the * cursor. It is used to iterate through the object store. It is also used to get * the result of the IndexedDB add request. The result is the cursor. It is used to * iterate through the object store. * @see getStatelessClusterKubeConfigs * @see findKubeconfigByClusterName * */ export interface CursorSuccessEvent extends Event { target: EventTarget & { result: IDBCursorWithValue | null; }; } /** handleDatabaseUpgrade creates the object store if it doesn't exist. * this upgrade is only called when the database is created. * It is not called when the database is opened. * @param event - The event that is fired when the IndexedDB is upgraded. * @see storeStatelessClusterKubeconfig * @see getStatelessClusterKubeConfigs * @see findKubeconfigByClusterName * **/ export declare function handleDatabaseUpgrade(event: DatabaseEvent): void; /** handleDataBaseError handles errors when opening IndexedDB. * @param event - The event that is fired when the IndexedDB is upgraded. * @see storeStatelessClusterKubeconfig * @see getStatelessClusterKubeConfigs * @see findKubeconfigByClusterName * */ export declare function handleDataBaseError(event: DatabaseErrorEvent, reject: (reason?: any) => void): void; /** * Store the kubeconfig for a stateless cluster in IndexedDB. * @param kubeconfig - The kubeconfig to store. * @returns promise that resolves when the kubeconfig is successfully added. * @throws Error if IndexedDB is not supported. * @throws Error if the kubeconfig is invalid. */ export declare function storeStatelessClusterKubeconfig(kubeconfig: string): Promise<void>; /** * Gets stateless cluster kubeconfigs from IndexedDB. * @returns A promise that resolves with the kubeconfigs. * @throws Error if IndexedDB is not supported. * @throws Error if the kubeconfig is invalid. */ export declare function getStatelessClusterKubeConfigs(): Promise<string[]>; /** * Finds the kubeconfig and context with the matching cluster name or custom name in headlamp_info. * @param clusterID The ID for a cluster, composed of the kubeconfig path and cluster name * @param clusterName The name of the cluster to find. * @param parsedKubeconfig The parsed kubeconfig object. * @returns An object containing the matching kubeconfig and context. */ export declare function findMatchingContexts(clusterName: string, parsedKubeconfig: KubeconfigObject, clusterID?: string): { matchingKubeconfig: { name: string; context: { cluster: string; user: string; namespace?: string; clusterID?: string; source?: string; extensions?: Array<{ name: string; extension: { customName?: string; }; }>; }; } | undefined; matchingContext: { name: string; context: { cluster: string; user: string; namespace?: string; clusterID?: string; source?: string; extensions?: Array<{ name: string; extension: { customName?: string; }; }>; }; } | undefined; }; /** * Finds and replaces a kubeconfig by cluster name. * @param clusterName - The name of the cluster to find and replace. * @param kubeconfig - The base64 encoded kubeconfig to replace the existing one with. * @param create - If true, create a new kubeconfig if it doesn't exist. If false, only replace existing kubeconfigs. * @returns A promise that resolves when the kubeconfig is successfully replaced. * @throws Error if the kubeconfig replacement fails at any step. * * Note: If deletion of the existing kubeconfig fails, the operation will still proceed * to store the new kubeconfig. This ensures the new configuration is applied even if * cleanup of the old one encounters issues. */ export declare function findAndReplaceKubeconfig(clusterName: string, kubeconfig: string, create?: boolean): Promise<void>; /** * Generates a cryptographically secure random token using the browser's crypto API. * @param {number} length - The length of the token. * @returns {string} - The generated token. */ export declare function generateSecureToken(length?: number): string; /** * Compares the cluster config from the backend and the redux store * @param clusters * @param clustersToConfig * @returns true if the present stored config is different from the fetched one. */ export declare function isEqualClusterConfigs(currentConfig: ConfigState['clusters'], newConfig: ConfigState['clusters']): boolean; /** * Parses the cluster config from the backend and updates the redux store * if the present stored config is different from the fetched one. */ export declare function fetchStatelessClusterKubeConfigs(dispatch: any): Promise<void>; declare const exportFunctions: { storeStatelessClusterKubeconfig: typeof storeStatelessClusterKubeconfig; getStatelessClusterKubeConfigs: typeof getStatelessClusterKubeConfigs; findKubeconfigByClusterName: typeof findKubeconfigByClusterName; getUserIdFromLocalStorage: typeof getUserIdFromLocalStorage; isEqualClusterConfigs: typeof isEqualClusterConfigs; fetchStatelessClusterKubeConfigs: typeof fetchStatelessClusterKubeConfigs; deleteClusterKubeconfig: typeof deleteClusterKubeconfig; updateStatelessClusterKubeconfig: typeof updateStatelessClusterKubeconfig; processClusterComparison: typeof isEqualClusterConfigs; }; export default exportFunctions;