UNPKG

@kinvolk/headlamp-plugin

Version:

The needed infrastructure for building Headlamp plugins.

122 lines (121 loc) 4.37 kB
/* * Copyright 2025 The Kubernetes Authors * * 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. */ import { getProductName, getVersion } from '../helpers/getProductInfo'; import { isElectron } from '../helpers/isElectron'; import { setCluster } from '../lib/k8s/api/v1/clusterApi'; /** * Plugins may call Headlamp.registerPlugin(pluginId: string, pluginObj: Plugin) to register themselves. * * They will have their initialize(register) method called at plugin initialization time. * */ export class Plugin { } let currentAppMenus = null; /** * This class is a more convenient way for plugins to call registerPlugin in * order to register themselves. */ export class Headlamp { /** * Got a new plugin to add? Well, registerPlugin is your friend. * * @param pluginId - a unique id string for your plugin. * @param pluginObj - the plugin being added. * * @example * * ```javascript * const myPlugin = { * initialize: (register) => { * // do some stuff with register * // use some libraries in window.pluginLib * return true; * } * } * * Headlamp.registerPlugin("aPluginIdString", myPlugin) * ``` */ static registerPlugin(pluginId, pluginObj) { // @todo: what happens if this plugin exists? (and is already initialized?) // Should it raise an error? Silently keep going? Do we need quit() methods on plugins? window.plugins[pluginId] = pluginObj; } /** * Configure (or update) a cluster that can then be used throughout Headlamp. * If the request is successful, further calls to `K8s.useClustersConf()` * will show the newly configured cluster. * * **Important:** This is only available in the desktop version and will result in a * bad request when running in-cluster. * * @param clusterReq - the cluster to be added or updated. * @returns a promise which completes to Headlamp's configuration (showing the list of configured clusters). */ static setCluster(clusterReq) { return setCluster(clusterReq).catch(e => { console.error(e); }); } /** * Changes the app menu. * If Headlamp is not running as a desktop app, then this method prints an error and doesn't do anything. * * @param appMenuFunc A function that receives the current app menu configuration and a new one. If the function returns null, the menu is not changed. */ static setAppMenu(appMenuFunc) { if (!isElectron()) { console.error('Cannot set app menu: not running as a desktop app!'); return; } // Update our (renderer) local copy currentAppMenus = appMenuFunc(currentAppMenus); window.desktopApi.send('setMenu', currentAppMenus); } /** * Returns whether Headlamp is running as a desktop app. * * @returns true if Headlamp is running as a desktop app. */ static isRunningAsApp() { return isElectron(); } /** * Returns the version of Headlamp as an object with a VERSION (application version) and * GIT_VERSION (commit) fields. Like: * { VERSION: 'v0.0.0', GIT_VERSION: '0000000000000} * * @returns the version of Headlamp. */ getVersion() { const version = getVersion(); return { VERSION: version.VERSION || '', GIT_VERSION: version.GIT_VERSION || '' }; } /** * Returns the name of the product. * * @returns the name of the product. */ getProductName() { return getProductName() || ''; } } window.desktopApi?.receive('currentMenu', (currentMenus) => { console.debug('Received current menu config from app', currentMenus); currentAppMenus = currentMenus; }); window.registerPlugin = Headlamp.registerPlugin;