|
16 | 16 |
|
17 | 17 | import java.io.IOException;
|
18 | 18 | import java.nio.file.FileSystems;
|
| 19 | +import java.nio.file.Files; |
19 | 20 | import java.nio.file.Path;
|
20 | 21 | import java.nio.file.Paths;
|
21 | 22 | import java.nio.file.StandardWatchEventKinds;
|
@@ -66,7 +67,8 @@ private void runOnConfigChange(Runnable runnable) {
|
66 | 67 | while ((key = service.take()) != null) {
|
67 | 68 | key.pollEvents().stream()
|
68 | 69 | .filter(this::isConfigPath)
|
69 |
| - .forEach((Void) -> runnable.run()); |
| 70 | + .filter(this::isValid) |
| 71 | + .forEach(Void -> runnable.run()); |
70 | 72 | key.reset();
|
71 | 73 | }
|
72 | 74 | } catch (IOException | InterruptedException e) {
|
@@ -95,6 +97,26 @@ protected boolean isConfigPath(WatchEvent<?> event) {
|
95 | 97 | return path.equals(config);
|
96 | 98 | }
|
97 | 99 |
|
| 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 | + |
98 | 120 | private Path getWatchedPath() {
|
99 | 121 | return config.getParent();
|
100 | 122 | }
|
|
0 commit comments