Skip to content

Commit

Permalink
Ignore kubeconfig changes where the config file is empty or deleted
Browse files Browse the repository at this point in the history
I'm not convinced this is doing much to address
redhat-developer/intellij-kubernetes#640.
In practice, the first few times I attempt to switch namespaces,
the tree collapses, regardless of if I use this PR or the existing code.
After that, I can occasionally switch namespaces without the tree
collapsing.

See redhat-developer#190.

Signed-off-by: David Thompson <[email protected]>
  • Loading branch information
datho7561 committed Jul 25, 2023
1 parent f766930 commit 51fadfe
Showing 1 changed file with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardWatchEventKinds;
Expand Down Expand Up @@ -66,7 +67,8 @@ private void runOnConfigChange(Runnable runnable) {
while ((key = service.take()) != null) {
key.pollEvents().stream()
.filter(this::isConfigPath)
.forEach((Void) -> runnable.run());
.filter(this::isValid)
.forEach(Void -> runnable.run());
key.reset();
}
} catch (IOException | InterruptedException e) {
Expand Down Expand Up @@ -95,6 +97,26 @@ protected boolean isConfigPath(WatchEvent<?> event) {
return path.equals(config);
}

/**
* Returns {@code true} if the path (to the kube config file) in the given event
* <ul>
* <li>exists and</li>
* <li>is not empty</li>
* </ul>
*
* @param event the WatchEvent to get the path to the kube config from
* @return returns true if the kube config that the event points to exists and is not empty
*/
protected boolean isValid(WatchEvent<?> event) {
Path path = getWatchedPath().resolve((Path) event.context());
try {
return Files.exists(path) && Files.size(path) > 0;
} catch (IOException e) {
// do nothing
}
return true;
}

private Path getWatchedPath() {
return config.getParent();
}
Expand Down

0 comments on commit 51fadfe

Please sign in to comment.