Skip to content

Commit

Permalink
fix: bump kubernetes-client to 7.0.0 (#921)
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Dec 13, 2024
1 parent 20de3d6 commit c7ee649
Show file tree
Hide file tree
Showing 17 changed files with 340 additions and 264 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[versions]
# libraries
junit = "4.13.2"
openshift-client = "6.12.0"
openshift-client = "7.0.0"
devtools-common = "1.9.7-SNAPSHOT"
keycloak = "24.0.5"
jsonwebtoken = "0.12.6"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,24 @@
import org.jboss.tools.intellij.openshift.utils.odo.OdoDelegate;

import java.util.concurrent.ExecutionException;
import org.mockito.Mockito;

public class ToolFactoryTest extends BasePlatformTestCase {

public void testGetOdo() throws ExecutionException, InterruptedException {
Tool<OdoDelegate> tool = ToolFactory.getInstance().createOdo(getProject()).get();
Tool<OdoDelegate> tool = ToolFactory.getInstance().createOdo(Mockito.mock(), getProject()).get();
Odo odo = tool.get();
assertNotNull(odo);
}

public void testGetHelm() throws ExecutionException, InterruptedException {
Tool<Helm> tool = ToolFactory.getInstance().createHelm(getProject()).get();
Tool<Helm> tool = ToolFactory.getInstance().createHelm().get();
Helm helm = tool.get();
assertNotNull(helm);
}

public void testGetOc() throws ExecutionException, InterruptedException {
Tool<Oc> tool = ToolFactory.getInstance().createOc(getProject()).get();
Tool<Oc> tool = ToolFactory.getInstance().createOc(Mockito.mock()).get();
Oc oc = tool.get();
assertNotNull(oc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import org.mockito.Mockito;

import static org.awaitility.Awaitility.with;
import static org.jboss.tools.intellij.openshift.Constants.PLUGIN_FOLDER;
Expand Down Expand Up @@ -68,7 +69,7 @@ public abstract class OdoCliTest extends BasePlatformTestCase {
@Before
public void init() throws Exception {
previousTestDialog = MessagesHelper.setTestDialog(TestDialog.OK);
ToolFactory.getInstance().createOc(getProject()).whenComplete((ocTool, throwable) -> {
ToolFactory.getInstance().createOc(Mockito.mock()).whenComplete((ocTool, throwable) -> {
try {
OdoCluster.INSTANCE.login(ocTool.get());
} catch (IOException e) {
Expand All @@ -85,7 +86,7 @@ public void cleanup() {

private CompletableFuture<OdoFacade> getOdo() {
return ToolFactory.getInstance()
.createOdo(getProject())
.createOdo(Mockito.mock(), getProject())
.thenApply(tool -> new ApplicationRootNodeOdo(tool.get(), false, rootNode, processHelper));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@
import io.fabric8.openshift.api.model.ProjectList;
import io.fabric8.openshift.client.OpenShiftClient;
import io.fabric8.openshift.client.dsl.ProjectOperation;
import org.jboss.tools.intellij.openshift.utils.Cli;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
Expand All @@ -40,7 +36,9 @@
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;
import org.jboss.tools.intellij.openshift.utils.Cli;
import org.junit.Before;
import org.junit.Test;

import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
Expand Down Expand Up @@ -250,17 +248,17 @@ public void isAuthorized_should_throw_if_listing_secrets_throws_other_Kubernetes
}

private OdoCli createOdo(KubernetesClient kubernetesClient, OpenShiftClient openShiftClient) {
KubernetesClient client = mock(KubernetesClient.class);
Project project = mock(Project.class);
String command = "Star Wars";
MessageBusConnection connection = mock(MessageBusConnection.class);
MessageBus bus = mock(MessageBus.class);
doReturn(connection)
.when(bus).connect();
Supplier<KubernetesClient> kubernetesClientFactory = () -> kubernetesClient;
Function<KubernetesClient, OpenShiftClient> openShiftClientFactory = client -> openShiftClient;
Function<KubernetesClient, OpenShiftClient> openShiftClientFactory = kubeClient -> openShiftClient;
Function<String, Map<String, String>> envVarFactory = url -> new HashMap<>();
Cli.TelemetryReport telemetryReport = mock(Cli.TelemetryReport.class);
return new OdoCli(project, command, bus, kubernetesClientFactory, openShiftClientFactory, envVarFactory, telemetryReport);
return new OdoCli(command, project, client, bus, openShiftClientFactory, envVarFactory, telemetryReport);
}

private static <R extends HasMetadata> R mockResource(String name, Class<R> clazz) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void actionPerformed(AnActionEvent anActionEvent, TreePath[] path, Object

protected Helm getHelm(AnActionEvent anActionEvent) {
try {
return ActionUtils.getApplicationRootNode(anActionEvent).getHelm(true);
return ActionUtils.getApplicationRootNode(anActionEvent).getHelm();
} catch (Exception e) {
LOGGER.warn("Could not get helm: {}", e.getMessage(), e);
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@ protected OcAction(Class... filters) {
@Override
public void actionPerformed(AnActionEvent anActionEvent, TreePath path, Object selected) {
setTelemetrySender(new TelemetrySender(PREFIX_ACTION + getTelemetryActionName()));
ActionUtils.getApplicationRootNode(anActionEvent).getOcTool().whenComplete(
(ocTool, throwable) -> {
if (ocTool != null) {
Oc oc = ocTool.get();
if (oc != null) {
this.actionPerformedOnSelectedObject(anActionEvent, getElement(selected), oc);
ActionUtils.getApplicationRootNode(anActionEvent).getOcTool()
.whenComplete(
(ocTool, throwable) -> {
if (ocTool != null) {
Oc oc = ocTool.get();
if (oc != null) {
this.actionPerformedOnSelectedObject(anActionEvent, getElement(selected), oc);
}
}
}
}
);
);
}

public abstract void actionPerformedOnSelectedObject(AnActionEvent anActionEvent, Object selected, @NotNull Oc oc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
******************************************************************************/
package org.jboss.tools.intellij.openshift.actions.project;

import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProgressIndicator;
Expand Down Expand Up @@ -189,4 +190,9 @@ public void run() {
);
}
}

@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.BGT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,19 @@
import com.redhat.devtools.intellij.common.utils.ConfigHelper;
import com.redhat.devtools.intellij.common.utils.ConfigWatcher;
import com.redhat.devtools.intellij.common.utils.ExecHelper;
import io.fabric8.kubernetes.api.model.Config;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.Future;
import org.jboss.tools.intellij.openshift.actions.NotificationUtils;
import org.jboss.tools.intellij.openshift.utils.KubernetesClientFactory;
import org.jboss.tools.intellij.openshift.utils.ProjectUtils;
import org.jboss.tools.intellij.openshift.utils.ToolFactory;
import org.jboss.tools.intellij.openshift.utils.ToolFactory.Tool;
Expand All @@ -59,14 +63,13 @@ public class ApplicationsRootNode
private CompletableFuture<Tool<Helm>> helmFuture;
private CompletableFuture<Tool<Oc>> ocFuture;
private boolean logged;
private Config config;
private KubernetesClient client;
private final OdoProcessHelper processHelper;

public ApplicationsRootNode(Project project, ApplicationsTreeStructure structure, Disposable parent) {
this.project = project;
this.structure = structure;
initConfigWatcher();
this.config = loadConfig();
registerProjectListener(project);
this.processHelper = new OdoProcessHelper();
Disposer.register(parent, this);
Expand All @@ -91,7 +94,7 @@ private CompletableFuture<ApplicationRootNodeOdo> doGetOdo() {
if (odoFuture == null) {
this.odoFuture =
ReadAction.compute(() -> ToolFactory.getInstance()
.createOdo(project)
.createOdo(getClient(), project)
.thenApply(tool -> {
ApplicationRootNodeOdo odo = new ApplicationRootNodeOdo(tool.get(), tool.isDownloaded(), this, processHelper);
loadProjectModel(odo, project);
Expand All @@ -111,32 +114,45 @@ public CompletableFuture<ApplicationRootNodeOdo> getOdo() {
});
}

public void resetOdo() {
private void disposeClientAwareCLIs() {
safeCancel(odoFuture);
this.odoFuture = null;
safeCancel(ocFuture);
this.ocFuture = null;
}

private void safeCancel(Future<?> future) {
if (future != null) {
try {
future.cancel(true);
} catch (CompletionException e) {
// swallowing intentionally
}
}
}

public CompletableFuture<ToolFactory.Tool<Oc>> getOcTool() {
if (ocFuture == null) {
this.ocFuture = ToolFactory.getInstance().createOc(project);
this.ocFuture = ToolFactory.getInstance().createOc(getClient());
}
return ocFuture;
}

public CompletableFuture<ToolFactory.Tool<Helm>> getHelmTool(boolean notify) {
private CompletableFuture<ToolFactory.Tool<Helm>> getHelmTool() {
if (helmFuture == null) {
this.helmFuture = ToolFactory.getInstance()
.createHelm(project)
.createHelm()
.whenComplete((tool, err) -> {
if (notify && tool.isDownloaded()) {
if (tool.isDownloaded()) {
structure.fireModified(this);
}
});
}
return helmFuture;
}

public Helm getHelm(boolean notify) {
Tool<Helm> tool = getHelmTool(notify).getNow(null);
public Helm getHelm() {
Tool<Helm> tool = getHelmTool().getNow(null);
if (tool == null) {
return null;
}
Expand All @@ -148,11 +164,7 @@ public Project getProject() {
}

protected void initConfigWatcher() {
ExecHelper.submit(new ConfigWatcher(Paths.get(ConfigHelper.getKubeConfigPath()), this));
}

protected Config loadConfig() {
return ConfigHelper.safeLoadKubeConfig();
ExecHelper.submit(new ConfigWatcher(this));
}

public Map<String, ComponentDescriptor> getLocalComponents() {
Expand Down Expand Up @@ -247,15 +259,16 @@ protected void registerProjectListener(Project project) {
}

@Override
public void onUpdate(ConfigWatcher source, Config config) {
if (!ConfigHelper.areEqual(config, this.config)) {
this.config = config;
public void onUpdate(Config updated) {
Config current = getClient().getConfiguration();
if (!ConfigHelper.areEqual(current, updated)) {
this.client = createClient(updated);
refresh();
}
}

public synchronized void refresh() {
resetOdo();
disposeClientAwareCLIs();
doGetOdo().whenComplete((odo, err) ->
structure.fireModified(ApplicationsRootNode.this)
);
Expand Down Expand Up @@ -303,6 +316,18 @@ public ApplicationsRootNode getRoot() {

@Override
public void dispose() {
resetOdo();
disposeClientAwareCLIs();
}

protected KubernetesClient getClient() {
if (client == null) {
this.client = createClient(new ConfigBuilder().build());
}
return client;
}

protected KubernetesClient createClient(Config config) {
return new KubernetesClientFactory().apply(config);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private Object getCurrentNamespace(ApplicationsRootNode element) {
}

private Object[] createHelmRepositoriesChildren(HelmRepositoriesNode parent) {
Helm helm = root.getHelm(true);
Helm helm = root.getHelm();
if (helm == null) {
return new Object[]{new MessageNode<>(root, parent, "Could not list repositories: Helm binary missing.")};
}
Expand Down Expand Up @@ -229,7 +229,7 @@ private List<BaseNode<?>> getServices(NamespaceNode namespaceNode, Odo odo) {
}

private List<BaseNode<?>> getHelmReleases(NamespaceNode namespaceNode) {
Helm helm = namespaceNode.getRoot().getHelm(true);
Helm helm = namespaceNode.getRoot().getHelm();
if (helm == null) {
return List.of(new MessageNode<>(root, namespaceNode, "Could not get chart releases"));
}
Expand Down
Loading

0 comments on commit c7ee649

Please sign in to comment.