forked from redhat-developer/intellij-common
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: allow to compare client- and fileconfig-contexts (redhat-develop…
…er#201) Signed-off-by: Andre Dietisheim <[email protected]>
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/main/java/com/redhat/devtools/intellij/common/utils/ContextHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |