From 07d9ad54b71c3d611bf4769e33248866d110129d Mon Sep 17 00:00:00 2001 From: Andre Dietisheim Date: Mon, 13 Nov 2023 16:47:43 +0100 Subject: [PATCH] fix: allow to compare client- and fileconfig-contexts (#201) Signed-off-by: Andre Dietisheim --- .../intellij/common/utils/ContextHelper.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/main/java/com/redhat/devtools/intellij/common/utils/ContextHelper.java diff --git a/src/main/java/com/redhat/devtools/intellij/common/utils/ContextHelper.java b/src/main/java/com/redhat/devtools/intellij/common/utils/ContextHelper.java new file mode 100644 index 0000000..b51e720 --- /dev/null +++ b/src/main/java/com/redhat/devtools/intellij/common/utils/ContextHelper.java @@ -0,0 +1,58 @@ +/******************************************************************************* + * Copyright (c) 2023 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package com.redhat.devtools.intellij.common.utils; + +import io.fabric8.kubernetes.api.model.Config; +import io.fabric8.kubernetes.api.model.NamedContext; +import io.fabric8.kubernetes.client.internal.KubeConfigUtils; +import org.apache.commons.codec.binary.StringUtils; + +public class ContextHelper { + + private ContextHelper() { + } + + public static boolean hasChanged(Config newConfig, Config currentConfig) { + NamedContext currentContext = currentConfig != null? + KubeConfigUtils.getCurrentContext(currentConfig) : null; + NamedContext newContext = newConfig != null ? + KubeConfigUtils.getCurrentContext(newConfig) : null; + return hasChanged(newContext, currentContext) + || hasNewToken(newContext, newConfig, currentContext, currentConfig); + } + + private static boolean hasChanged(NamedContext newContext, NamedContext currentContext) { + return newContext == null + || currentContext == null + || !StringUtils.equals(currentContext.getContext().getCluster(), + newContext.getContext().getCluster()) + || !StringUtils.equals(currentContext.getContext().getUser(), + newContext.getContext().getUser()) + || !StringUtils.equals(currentContext.getContext().getNamespace(), + newContext.getContext().getNamespace()); + } + + private static boolean hasNewToken(NamedContext newContext, Config newConfig, NamedContext currentContext, Config currentConfig) { + if (newContext == null) { + return false; + } + if (currentContext == null) { + return true; + } + String newToken = KubeConfigUtils.getUserToken(newConfig, newContext.getContext()); + if (newToken == null) { + // logout, do not refresh, LogoutAction already refreshes + return false; + } + String currentToken = KubeConfigUtils.getUserToken(currentConfig, currentContext.getContext()); + return !StringUtils.equals(newToken, currentToken); + } +}