Skip to content

Commit

Permalink
fix: Log service works for specific Pods
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa committed Feb 25, 2022
1 parent c60062a commit 1f313ef
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Usage:
./scripts/extract-changelog-for-version.sh 1.3.37 5
```
### 1.7.0-SNAPSHOT
* Fix #1315: Pod Log Service works for Jobs with no selector
* Fix #1126: Liveness and readiness TCP ports are not serialized as numbers when defined as numbers
* Fix #1211: Port `java-options-env-merge` integration test and ContainerEnvJavaOptionsMergeEnricher documentation to gradle
* Fix #1214: Add integration test to verify `jkube.debug.enabled` flag works as expected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void runTask_withNoK8sManifests_shouldLogCantWatchPods() {

// Then
verify(taskEnvironment.logger).lifecycle("k8s: Running in Kubernetes mode");
verify(taskEnvironment.logger).warn("k8s: No selector in deployment so cannot watch pods!");
verify(taskEnvironment.logger).warn("k8s: No selector detected and no Pod name specified, cannot watch Pods!");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public class PodLogService {

private Watch podWatcher;
private LogWatch logWatcher;
private Map<String, Pod> addedPods = new ConcurrentHashMap<>();
private CountDownLatch terminateLatch = new CountDownLatch(1);
private final Map<String, Pod> addedPods = new ConcurrentHashMap<>();
private final CountDownLatch terminateLatch = new CountDownLatch(1);
private String watchingPodName;
private CountDownLatch logWatchTerminateLatch;

Expand All @@ -79,7 +79,7 @@ public void tailAppPodsLogs(final KubernetesClient kubernetes, final String name

LabelSelector selector = KubernetesHelper.extractPodLabelSelector(entities);

if (selector != null) {
if (selector != null || StringUtils.isNotBlank(context.getPodName())) {
String ctrlCMessage = "stop tailing the log";
if (StringUtils.isNotBlank(onExitOperation)) {
final String onExitOperationLower = onExitOperation.toLowerCase().trim();
Expand Down Expand Up @@ -113,18 +113,19 @@ public void run() {
}
waitAndLogPods(kubernetes, namespace, selector, watchAddedPodsOnly, ctrlCMessage, followLog, ignorePodsOlderThan, waitInCurrentThread);
} else {
log.warn("No selector in deployment so cannot watch pods!");
log.warn("No selector detected and no Pod name specified, cannot watch Pods!");
}
}

private void waitAndLogPods(final KubernetesClient kubernetes, final String namespace, LabelSelector selector, final boolean watchAddedPodsOnly, final String ctrlCMessage, final boolean
followLog, Date ignorePodsOlderThan, boolean waitInCurrentThread) {
FilterWatchListDeletable<Pod, PodList> pods = withSelector(kubernetes.pods().inNamespace(namespace), selector, log);
if (context.getPodName() != null) {
final FilterWatchListDeletable<Pod, PodList> pods;
if (StringUtils.isNotBlank(context.getPodName())) {
log.info("Watching pod with selector %s, and name %s waiting for a running pod...", selector, context.getPodName());
pods = pods.withField("metadata.name", context.getPodName());
pods = kubernetes.pods().inNamespace(namespace).withField("metadata.name", context.getPodName());
} else {
log.info("Watching pods with selector %s waiting for a running pod...", selector);
pods = withSelector(kubernetes.pods().inNamespace(namespace), selector, log);
}
Pod latestPod = null;
boolean runningPod = false;
Expand Down

0 comments on commit 1f313ef

Please sign in to comment.