Skip to content

Make sure domainUid is in operator log messages - part 1 #1844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@

package oracle.kubernetes.operator;

import java.util.Optional;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicBoolean;

import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.models.V1ConfigMap;
import io.kubernetes.client.util.Watch.Response;
import oracle.kubernetes.operator.TuningParameters.WatchTuning;
import oracle.kubernetes.operator.builders.WatchBuilder;
import oracle.kubernetes.operator.builders.WatchI;
import oracle.kubernetes.operator.helpers.KubernetesUtils;
import oracle.kubernetes.operator.watcher.WatchListener;

/**
Expand Down Expand Up @@ -64,4 +67,11 @@ public WatchI<V1ConfigMap> initiateWatch(WatchBuilder watchBuilder) throws ApiEx
public String getNamespace() {
return ns;
}

@Override
public String getDomainUid(Response<V1ConfigMap> item) {
return KubernetesUtils.getDomainUidLabel(
Optional.ofNullable(item.object).map(V1ConfigMap::getMetadata).orElse(null));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ public void execute() {
if (!delegate.isNamespaceRunning(getNamespace())) {
return;
}

if (isShouldContinue()) {
internalMakeRightDomainPresence();
} else {
Expand Down Expand Up @@ -752,7 +753,8 @@ public void onThrowable(Packet packet, Throwable throwable) {
() -> {
DomainPresenceInfo existing = getExistingDomainPresenceInfo(ns, domainUid);
if (existing != null) {
try (LoggingContext stack = LoggingContext.setThreadContext().namespace(ns)) {
try (LoggingContext stack =
LoggingContext.setThreadContext().namespace(ns).domainUid(domainUid)) {
existing.setPopulated(false);
// proceed only if we have not already retried max number of times
int retryCount = existing.incrementAndGetFailureCount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

package oracle.kubernetes.operator;

import java.util.Optional;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicBoolean;

import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.util.Watch.Response;
import oracle.kubernetes.operator.TuningParameters.WatchTuning;
import oracle.kubernetes.operator.builders.WatchBuilder;
import oracle.kubernetes.operator.builders.WatchI;
Expand Down Expand Up @@ -62,4 +64,9 @@ public WatchI<Domain> initiateWatch(WatchBuilder watchBuilder) throws ApiExcepti
public String getNamespace() {
return ns;
}

@Override
public String getDomainUid(Response<Domain> item) {
return Optional.ofNullable(item.object).map(Domain::getDomainUid).orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.models.V1Event;
import io.kubernetes.client.util.Watch.Response;
import oracle.kubernetes.operator.TuningParameters.WatchTuning;
import oracle.kubernetes.operator.builders.WatchBuilder;
import oracle.kubernetes.operator.builders.WatchI;
Expand Down Expand Up @@ -67,4 +68,9 @@ public WatchI<V1Event> initiateWatch(WatchBuilder watchBuilder) throws ApiExcept
public String getNamespace() {
return ns;
}

@Override
public String getDomainUid(Response<V1Event> item) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import oracle.kubernetes.operator.builders.WatchBuilder;
import oracle.kubernetes.operator.builders.WatchI;
import oracle.kubernetes.operator.helpers.CallBuilder;
import oracle.kubernetes.operator.helpers.KubernetesUtils;
import oracle.kubernetes.operator.helpers.ResponseStep;
import oracle.kubernetes.operator.logging.LoggingFacade;
import oracle.kubernetes.operator.logging.LoggingFactory;
Expand Down Expand Up @@ -88,6 +89,12 @@ public String getNamespace() {
return namespace;
}

@Override
public String getDomainUid(Watch.Response<V1Job> item) {
return KubernetesUtils.getDomainUidLabel(
Optional.ofNullable(item.object).map(V1Job::getMetadata).orElse(null));
}

/**
* Creates a new JobWatcher and caches it by namespace.
*
Expand Down
4 changes: 3 additions & 1 deletion operator/src/main/java/oracle/kubernetes/operator/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,9 @@ public NextAction onSuccess(Packet packet, CallResponse<V1NamespaceList> callRes
Set<String> namespacesToStart = new TreeSet<>(targetNamespaces);
for (String ns : targetNamespaces) {
if (!nsList.contains(ns)) {
LOGGER.warning(MessageKeys.NAMESPACE_IS_MISSING, ns);
try (LoggingContext stack = LoggingContext.setThreadContext().namespace(ns)) {
LOGGER.warning(MessageKeys.NAMESPACE_IS_MISSING, ns);
}
namespacesToStart.remove(ns);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.models.V1Namespace;
import io.kubernetes.client.util.Watch;
import oracle.kubernetes.operator.TuningParameters.WatchTuning;
import oracle.kubernetes.operator.builders.WatchBuilder;
import oracle.kubernetes.operator.builders.WatchI;
Expand Down Expand Up @@ -59,4 +60,9 @@ public WatchI<V1Namespace> initiateWatch(WatchBuilder watchBuilder) throws ApiEx
public String getNamespace() {
return null;
}

@Override
public String getDomainUid(Watch.Response<V1Namespace> item) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import oracle.kubernetes.operator.builders.WatchBuilder;
import oracle.kubernetes.operator.builders.WatchI;
import oracle.kubernetes.operator.helpers.CallBuilder;
import oracle.kubernetes.operator.helpers.KubernetesUtils;
import oracle.kubernetes.operator.helpers.PodHelper;
import oracle.kubernetes.operator.helpers.ResponseStep;
import oracle.kubernetes.operator.logging.LoggingFacade;
Expand Down Expand Up @@ -143,6 +144,12 @@ public String getNamespace() {
return namespace;
}

@Override
public String getDomainUid(Watch.Response<V1Pod> item) {
return KubernetesUtils.getDomainUidLabel(
Optional.ofNullable(item.object).map(V1Pod::getMetadata).orElse(null));
}

/**
* Receive response.
* @param item item
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.kubernetes.client.openapi.models.V1Pod;
import oracle.kubernetes.operator.helpers.ClientPool;
import oracle.kubernetes.operator.helpers.DomainPresenceInfo;
import oracle.kubernetes.operator.helpers.KubernetesUtils;
import oracle.kubernetes.operator.helpers.LastKnownStatus;
import oracle.kubernetes.operator.helpers.PodHelper;
import oracle.kubernetes.operator.logging.LoggingContext;
Expand Down Expand Up @@ -171,7 +172,8 @@ public NextAction apply(Packet packet) {
String state = null;
ClientPool helper = ClientPool.getInstance();
ApiClient client = helper.take();
try (LoggingContext stack = LoggingContext.setThreadContext().namespace(getNamespace(pod))) {
try (LoggingContext stack =
LoggingContext.setThreadContext().namespace(getNamespace(pod)).domainUid(getDomainUid(pod))) {
try {
KubernetesExec kubernetesExec = EXEC_FACTORY.create(client, pod, CONTAINER_NAME);
kubernetesExec.setStdin(stdin);
Expand Down Expand Up @@ -216,6 +218,11 @@ private String getNamespace(@Nonnull V1Pod pod) {
return Optional.ofNullable(pod.getMetadata()).map(V1ObjectMeta::getNamespace).orElse(null);
}

public String getDomainUid(V1Pod pod) {
return KubernetesUtils.getDomainUidLabel(
Optional.ofNullable(pod).map(V1Pod::getMetadata).orElse(null));
}

private String chooseStateOrLastKnownServerStatus(
LastKnownStatus lastKnownStatus, String state) {
if (state != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@

package oracle.kubernetes.operator;

import java.util.Optional;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicBoolean;

import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.models.V1Service;
import io.kubernetes.client.util.Watch.Response;
import oracle.kubernetes.operator.TuningParameters.WatchTuning;
import oracle.kubernetes.operator.builders.WatchBuilder;
import oracle.kubernetes.operator.builders.WatchI;
import oracle.kubernetes.operator.helpers.KubernetesUtils;
import oracle.kubernetes.operator.watcher.WatchListener;

/**
Expand Down Expand Up @@ -64,4 +67,10 @@ public WatchI<V1Service> initiateWatch(WatchBuilder watchBuilder) throws ApiExce
public String getNamespace() {
return ns;
}

@Override
public String getDomainUid(Response<V1Service> item) {
return KubernetesUtils.getDomainUidLabel(
Optional.ofNullable(item.object).map(V1Service::getMetadata).orElse(null));
}
}
10 changes: 9 additions & 1 deletion operator/src/main/java/oracle/kubernetes/operator/Watcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ private void watchForEvents() {
continue;
}

try (LoggingContext stack = LoggingContext.setThreadContext().namespace(getNamespace())) {
try (LoggingContext stack =
LoggingContext.setThreadContext().namespace(getNamespace()).domainUid(getDomainUid(item))) {
if (isError(item)) {
handleErrorResponse(item);
} else {
Expand Down Expand Up @@ -195,6 +196,13 @@ private boolean hasNext(WatchI<T> watch) {
*/
public abstract String getNamespace();

/**
* Gets the domainUID associated with a watch response.
*
* @return String object or null if the watch response is not associated with a domain
*/
public abstract String getDomainUid(Watch.Response<T> item);

private boolean isError(Watch.Response<T> item) {
return item.type.equalsIgnoreCase("ERROR");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static com.google.common.base.Strings.isNullOrEmpty;
import static oracle.kubernetes.operator.LabelConstants.CREATEDBYOPERATOR_LABEL;
import static oracle.kubernetes.operator.LabelConstants.DOMAINUID_LABEL;

public class KubernetesUtils {

Expand Down Expand Up @@ -188,4 +189,16 @@ private static String getOperatorCreatedLabel(V1ObjectMeta metadata) {
.orElse("false");
}

/**
* Returns the value of the domainUID label in the given Kubernetes resource metadata.
*
* @param metadata the Kubernetes Metadata object
* @return value of the domainUID label
*/
public static String getDomainUidLabel(V1ObjectMeta metadata) {
return Optional.ofNullable(metadata)
.map(V1ObjectMeta::getLabels)
.map(labels -> labels.get(DOMAINUID_LABEL))
.orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,20 @@ private String getDomainUid(Fiber fiber) {
.map(Fiber::getPacket)
.map(this::getDomainPresenceInfo)
.map(DomainPresenceInfo::getDomainUid)
.orElse(getDomainUidFromThreadContext());
.orElse(getDomainUidFromLoggingContext(fiber));
}

private DomainPresenceInfo getDomainPresenceInfo(Packet packet) {
return packet.getSpi(DomainPresenceInfo.class);
private String getDomainUidFromLoggingContext(Fiber fiber) {
return Optional.ofNullable(fiber)
.map(Fiber::getPacket)
.map(p -> p.getSpi(LoggingContext.class))
.or(LoggingContext::optionalContext)
.map(LoggingContext::domainUid)
.orElse("");
}

private String getDomainUidFromThreadContext() {
return LoggingContext.optionalContext().map(LoggingContext::domainUid).orElse("");
private DomainPresenceInfo getDomainPresenceInfo(Packet packet) {
return packet.getSpi(DomainPresenceInfo.class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ public void whenPacketContainsDomainPresence_retrieveDomainUid() {
assertThat(getFormattedMessageInFiber().get("domainUID"), equalTo("test-uid"));
}

@Test
public void whenPacketContainsLoggingContext_retrieveDomainUid() {
testSupport.addLoggingContext(new LoggingContext().domainUid("test-lc-uid"));

assertThat(getFormattedMessageInFiber().get("domainUID"), equalTo("test-lc-uid"));
}

@Test
public void whenNotInFiber_retrieveDomainUidFromThread() throws JsonProcessingException {
Expand All @@ -96,6 +102,29 @@ public void whenNotInFiber_retrieveDomainUidFromThread() throws JsonProcessingEx
}
}

@Test
public void whenPacketContainsDomainPresenceAndLoggingContext_retrieveDomainUidFromDomainPresence() {
testSupport.addDomainPresenceInfo(new DomainPresenceInfo("test-ns", "test-uid"));
testSupport.addLoggingContext(new LoggingContext().namespace("test-lc-ns").domainUid("test-lc-uid"));

assertThat(getFormattedMessageInFiber().get("domainUID"), equalTo("test-uid"));
}

@Test
public void whenPacketContainsLoggingContextAndThreadLocalIsDefined_retrieveDomainUidFromLoggingContext() {
testSupport.addLoggingContext(new LoggingContext().domainUid("test-lc-uid1"));
try (LoggingContext stack = LoggingContext.setThreadContext().namespace("test-lc-tl-uid")) {
assertThat(getFormattedMessageInFiber().get("domainUID"), equalTo("test-lc-uid1"));
}
}

@Test
public void whenThreadLocalDefinedAndPacketContainsNoDomainPresenceOrLoggingContext_retrieveDomainUidFromThread() {
try (LoggingContext stack = LoggingContext.setThreadContext().domainUid("test-lc-tl-uid1")) {
assertThat(getFormattedMessageInFiber().get("domainUID"), equalTo("test-lc-tl-uid1"));
}
}

@Test
public void whenPacketLacksDomainPresence_domainNamespaceIsEmpty() {
assertThat(getFormattedMessageInFiber().get("namespace"), equalTo(""));
Expand Down