Skip to content

Commit 51fadfe

Browse files
committed
Ignore kubeconfig changes where the config file is empty or deleted
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]>
1 parent f766930 commit 51fadfe

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

Diff for: src/main/java/com/redhat/devtools/intellij/common/utils/ConfigWatcher.java

+23-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import java.io.IOException;
1818
import java.nio.file.FileSystems;
19+
import java.nio.file.Files;
1920
import java.nio.file.Path;
2021
import java.nio.file.Paths;
2122
import java.nio.file.StandardWatchEventKinds;
@@ -66,7 +67,8 @@ private void runOnConfigChange(Runnable runnable) {
6667
while ((key = service.take()) != null) {
6768
key.pollEvents().stream()
6869
.filter(this::isConfigPath)
69-
.forEach((Void) -> runnable.run());
70+
.filter(this::isValid)
71+
.forEach(Void -> runnable.run());
7072
key.reset();
7173
}
7274
} catch (IOException | InterruptedException e) {
@@ -95,6 +97,26 @@ protected boolean isConfigPath(WatchEvent<?> event) {
9597
return path.equals(config);
9698
}
9799

100+
/**
101+
* Returns {@code true} if the path (to the kube config file) in the given event
102+
* <ul>
103+
* <li>exists and</li>
104+
* <li>is not empty</li>
105+
* </ul>
106+
*
107+
* @param event the WatchEvent to get the path to the kube config from
108+
* @return returns true if the kube config that the event points to exists and is not empty
109+
*/
110+
protected boolean isValid(WatchEvent<?> event) {
111+
Path path = getWatchedPath().resolve((Path) event.context());
112+
try {
113+
return Files.exists(path) && Files.size(path) > 0;
114+
} catch (IOException e) {
115+
// do nothing
116+
}
117+
return true;
118+
}
119+
98120
private Path getWatchedPath() {
99121
return config.getParent();
100122
}

0 commit comments

Comments
 (0)