Skip to content

Commit

Permalink
fix: allow to compare client- and fileconfig-contexts (redhat-develop…
Browse files Browse the repository at this point in the history
…er#201)

Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Nov 13, 2023
1 parent 621f743 commit 07d9ad5
Showing 1 changed file with 58 additions and 0 deletions.
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);
}
}

0 comments on commit 07d9ad5

Please sign in to comment.