From 092349cf8f0a2a30d40b1bb091e97c313f857583 Mon Sep 17 00:00:00 2001 From: Victor Rubezhny Date: Mon, 20 Jan 2025 15:32:40 +0100 Subject: [PATCH] Cannot login to existing cluster #3935 The `openshiftToolkit.failOnBrokenKubeConfigEntry` (boolean) option is added with the default set to `false` in order to make Kube config loading more robust. If it's required to fully check the correctness of the Kube config on startup, users can set its value to `true`, so Kubernetes Client will throw errors if any. Fixes: #3935 Signed-off-by: Victor Rubezhny --- package.json | 5 +++++ src/util/kubeUtils.ts | 11 +++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index e45427f3f..bf8ca736a 100644 --- a/package.json +++ b/package.json @@ -2226,6 +2226,11 @@ "openshiftToolkit.disable-namespace-info-status-bar": { "type": "boolean", "description": "Disable displaying the active namespace in VS Code's status bar." + }, + "openshiftToolkit.failOnBrokenKubeConfigEntry": { + "type": "boolean", + "default": false, + "description": "Fail on broken Kube config entry" } } }, diff --git a/src/util/kubeUtils.ts b/src/util/kubeUtils.ts index 66de8056f..7482fbb63 100644 --- a/src/util/kubeUtils.ts +++ b/src/util/kubeUtils.ts @@ -4,10 +4,10 @@ *-----------------------------------------------------------------------------------------------*/ import { KubeConfig, findHomeDir, loadYaml } from '@kubernetes/client-node'; -import { Cluster, Context, User } from '@kubernetes/client-node/dist/config_types'; +import { ActionOnInvalid, Cluster, Context, User } from '@kubernetes/client-node/dist/config_types'; import * as fs from 'fs'; import * as path from 'path'; -import { QuickPickItem, window } from 'vscode'; +import { QuickPickItem, window, workspace } from 'vscode'; import { CommandText } from '../base/command'; import { CliChannel, ExecutionContext } from '../cli'; import { Platform } from './platform'; @@ -27,8 +27,11 @@ export class KubeConfigUtils extends KubeConfig { constructor() { super(); try { - this.loadFromDefault(); - } catch { + const failOnBrokenEntry: boolean = workspace.getConfiguration('openshiftToolkit') + .get('failOnBrokenKubeConfigEntry'); + const onInvalidEntry = failOnBrokenEntry ? ActionOnInvalid.THROW : ActionOnInvalid.FILTER; + this.loadFromDefault({onInvalidEntry}); + } catch { throw new Error('Kubernetes configuration cannot be loaded. Please check configuration files for errors and fix them to continue.'); } // k8s nodejs-client ignores all unknown properties,