-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(operator): stream operator pod logs in remote test mode
- Loading branch information
Showing
2 changed files
with
103 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
operator/controller/src/test/java/io/apicurio/registry/operator/it/PodLogManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package io.apicurio.registry.operator.it; | ||
|
||
import io.fabric8.kubernetes.client.KubernetesClient; | ||
import io.fabric8.kubernetes.client.dsl.LogWatch; | ||
import io.javaoperatorsdk.operator.processing.event.ResourceID; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStreamReader; | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
|
||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
import static org.awaitility.Awaitility.await; | ||
|
||
public class PodLogManager { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(PodLogManager.class); | ||
|
||
private final KubernetesClient k8sClient; | ||
|
||
private final Map<ResourceID, AtomicBoolean> activePodLogMap = new ConcurrentHashMap<>(); | ||
|
||
public PodLogManager(KubernetesClient k8sClient) { | ||
this.k8sClient = k8sClient; | ||
} | ||
|
||
private String getNamespace(ResourceID id) { | ||
return id.getNamespace().orElse("default"); | ||
} | ||
|
||
public void startPodLog(ResourceID podID) { | ||
k8sClient.pods().inNamespace(getNamespace(podID)).withName(podID.getName()).waitUntilReady(60, | ||
SECONDS); | ||
new Thread(() -> { | ||
StringBuilder chunk = new StringBuilder(); | ||
try ( | ||
LogWatch logWatch = k8sClient.pods().inNamespace(getNamespace(podID)).withName(podID.getName()).watchLog(); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(logWatch.getOutput())) | ||
) { | ||
AtomicBoolean stop = new AtomicBoolean(false); | ||
log.debug("START LOG of pod {}/{}", getNamespace(podID), podID.getName()); | ||
activePodLogMap.put(podID, stop); | ||
var lastWriteAt = Instant.now(); | ||
while (!stop.get()) { | ||
var line = reader.readLine(); | ||
if (line != null) { | ||
chunk.append(getNamespace(podID)).append("/").append(podID.getName()).append(" >>> ") | ||
.append(line).append("\n"); | ||
if (lastWriteAt.plus(Duration.ofSeconds(5)).isBefore(Instant.now())) { | ||
log.debug("LOG of pod {}/{}:\n{}", getNamespace(podID), podID.getName(), chunk); | ||
chunk.setLength(0); | ||
lastWriteAt = Instant.now(); | ||
} | ||
} else { | ||
stop.set(true); | ||
} | ||
} | ||
} catch (Exception ex) { | ||
log.error("Error while reading logs of pod {}/{}", getNamespace(podID), podID.getName(), ex); | ||
} finally { | ||
if (chunk.length() > 0) { | ||
log.debug("LOG of pod {}/{}:\n{}", getNamespace(podID), podID.getName(), chunk); | ||
} | ||
log.debug("END LOG of pod {}/{}", getNamespace(podID), podID.getName()); | ||
activePodLogMap.remove(podID); | ||
} | ||
}).start(); | ||
} | ||
|
||
public void stopAndWait() { | ||
activePodLogMap.values().forEach(stop -> stop.set(true)); | ||
await().until(activePodLogMap::isEmpty); | ||
} | ||
} |