Skip to content

Commit

Permalink
fix: allow to compare fileconfig-contexts (redhat-developer#201)
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Nov 13, 2023
1 parent 621f743 commit 5914aa0
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,31 @@ && areEqual(clientConfig.getContexts(), kubeConfig.getContexts())
&& areEqualToken(kubeConfig, clientConfig);
}

/**
* Returns {@code true} if the given {@link io.fabric8.kubernetes.api.model.Config} and
* (client runtime) {@link io.fabric8.kubernetes.client.Config} are equal. They are considered equal if they're
* equal in
* <ul>
* <li>current context (cluster, user, current namespace, extensions)</li>
* <li>(existing) contexts</li>
* <li>(authentication) token</li>
* </ul>
*
* @param thisConfig the first (file) config to compare
* @param thatConfig the second (file) config to compare
* @return true if both configs are equal in context, contexts and token
*/
public static boolean areEqual(Config thisConfig, Config thatConfig) {
if (thisConfig == null) {
return thatConfig == null;
} else if (thatConfig == null) {
return false;
}
return areEqual(KubeConfigUtils.getCurrentContext(thisConfig), KubeConfigUtils.getCurrentContext(thatConfig))
&& areEqual(thisConfig.getContexts(), thatConfig.getContexts())
&& areEqualToken(getAuthInfo(thisConfig), getAuthInfo(thatConfig));
}

/**
* Returns {@code true} if both given contexts are equal. They are considered equal if they're equal in
* <ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
package com.redhat.devtools.intellij.common.utils;

import io.fabric8.kubernetes.api.model.AuthInfo;
import io.fabric8.kubernetes.api.model.AuthInfoBuilder;
import io.fabric8.kubernetes.api.model.AuthProviderConfig;
import io.fabric8.kubernetes.api.model.Context;
import io.fabric8.kubernetes.api.model.NamedAuthInfo;
import io.fabric8.kubernetes.api.model.NamedAuthInfoBuilder;
import io.fabric8.kubernetes.api.model.NamedContext;
import io.fabric8.kubernetes.client.Config;
import org.junit.Test;
Expand Down Expand Up @@ -284,6 +286,104 @@ public void two_authInfos_with_same_provider_token_in_access_token_should_be_equ
assertThat(equal).isTrue();
}

@Test
public void kubeConfig1_and_kubeConfig2_are_equal_if_same_in_currentContext_contexts_and_provider_token() {
// given
io.fabric8.kubernetes.api.model.Config kubeConfig1 = kubeConfig(
ctx2,
allContexts,
allUsers);
io.fabric8.kubernetes.api.model.Config kubeConfig2 = kubeConfig(
clone(ctx2),
clone(allContexts),
clone(allUsers));
// when
boolean equal = ConfigHelper.areEqual(kubeConfig1, kubeConfig2);
// then
assertThat(equal).isTrue();
}

@Test
public void kubeConfig1_and_kubeConfig2_are_NOT_equal_if_NOT_same_in_currentContext() {
// given
io.fabric8.kubernetes.api.model.Config kubeConfig1 = kubeConfig(
ctx2,
allContexts,
allUsers);
io.fabric8.kubernetes.api.model.Config kubeConfig2 = kubeConfig(
clone(ctx3),
clone(allContexts),
clone(allUsers));
// when
boolean equal = ConfigHelper.areEqual(kubeConfig1, kubeConfig2);
// then
assertThat(equal).isFalse();
}

@Test
public void kubeConfig1_and_kubeConfig2_are_NOT_equal_if_contexts_has_additional_member() {
// given
List<NamedContext> allContextsWithAddition = new ArrayList<>(allContexts);
allContextsWithAddition.add(ctx4);
io.fabric8.kubernetes.api.model.Config kubeConfig1 = kubeConfig(
ctx2,
allContextsWithAddition,
allUsers);
io.fabric8.kubernetes.api.model.Config kubeConfig2 = kubeConfig(
clone(ctx2),
clone(allContexts),
clone(allUsers));
// when
boolean equal = ConfigHelper.areEqual(kubeConfig1, kubeConfig2);
// then
assertThat(equal).isFalse();
}

@Test
public void kubeConfig1_and_kubeConfig2_are_NOT_equal_if_token_is_different() {
// given
io.fabric8.kubernetes.api.model.Config kubeConfig1 = kubeConfig(
ctx2,
allContexts,
allUsers);

String currentUserName = ctx2.getContext().getUser();
NamedAuthInfo currentUser = allUsers.stream()
.filter(user -> user.getName().equals(currentUserName))
.findFirst()
.get();
NamedAuthInfo currentUserClone = clone(currentUser);
List<NamedAuthInfo> allUsersClone = clone(allUsers);
int index = allUsersClone.indexOf(currentUserClone);
allUsersClone.set(index, currentUserClone);
currentUserClone.getUser().setToken("token 42");
io.fabric8.kubernetes.api.model.Config kubeConfig2 = kubeConfig(
clone(ctx2),
clone(allContexts),
allUsersClone);
// when
boolean equal = ConfigHelper.areEqual(kubeConfig1, kubeConfig2);
// then
assertThat(equal).isFalse();
}

@Test
public void kubeConfig1_and_kubeConfig2_are_NOT_equal_if_kubeConfig2_has_no_current_context() {
// given
io.fabric8.kubernetes.api.model.Config kubeConfig1 = kubeConfig(
ctx2,
allContexts,
allUsers);
io.fabric8.kubernetes.api.model.Config kubeConfig2 = kubeConfig(
null, // no current context
allContexts,
allUsers);
// when
boolean equal = ConfigHelper.areEqual(kubeConfig1, kubeConfig2);
// then
assertThat(equal).isFalse();
}

@Test
public void kubeConfig_and_clientConfig_are_equal_if_same_in_currentContext_contexts_and_provider_token() {
// given
Expand Down Expand Up @@ -456,6 +556,20 @@ private static NamedContext clone(NamedContext namedContext) {
namedContext.getName());
}

private static NamedAuthInfo clone(NamedAuthInfo user) {
return new NamedAuthInfoBuilder(user).build();
}

private static List<NamedAuthInfo> clone(Collection<NamedAuthInfo> users) {
return users.stream()
.map(user -> new NamedAuthInfoBuilder()
.withUser(new AuthInfoBuilder(user.getUser()).build())
.withName(user.getName())
.withAdditionalProperties(user.getAdditionalProperties())
.build())
.collect(Collectors.toList());
}

private static NamedAuthInfo getUser(NamedContext context, Collection<NamedAuthInfo> users) {
return users.stream()
.filter(user -> user.getName() == context.getContext().getUser())
Expand Down

0 comments on commit 5914aa0

Please sign in to comment.