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 1 commit
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
Prev Previous commit
Next Next commit
Move a common method into KubernetesUtils
  • Loading branch information
doxiao committed Jul 31, 2020
commit 6ad1f23df69c25bd863f6eca185a5ae745902711
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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 @@ -68,8 +69,9 @@ public String getNamespace() {
}

@Override
public String getDomainUID(Response<V1ConfigMap> item) {
return getDomainUID(Optional.ofNullable(item.object)
.map(V1ConfigMap::getMetadata).orElse(null));
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 @@ -66,7 +66,7 @@ public String getNamespace() {
}

@Override
public String getDomainUID(Response<Domain> item) {
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 @@ -3,7 +3,6 @@

package oracle.kubernetes.operator;

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

Expand Down Expand Up @@ -71,7 +70,7 @@ public String getNamespace() {
}

@Override
public String getDomainUID(Response<V1Event> item) {
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 @@ -89,9 +90,9 @@ public String getNamespace() {
}

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public String getNamespace() {
}

@Override
public String getDomainUID(Watch.Response<V1Namespace> item) {
public String getDomainUid(Watch.Response<V1Namespace> item) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,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 @@ -134,9 +135,9 @@ public String getNamespace() {
}

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

/**
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 @@ -172,7 +173,7 @@ public NextAction apply(Packet packet) {
ClientPool helper = ClientPool.getInstance();
ApiClient client = helper.take();
try (LoggingContext stack =
LoggingContext.setThreadContext().namespace(getNamespace(pod)).domainUid(getDomainUID(pod))) {
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 @@ -217,12 +218,9 @@ private String getNamespace(@Nonnull V1Pod pod) {
return Optional.ofNullable(pod.getMetadata()).map(V1ObjectMeta::getNamespace).orElse(null);
}

public String getDomainUID(V1Pod pod) {
String key = "weblogic.domainUID";
return Optional.of(pod)
.map(V1Pod::getMetadata)
.map(V1ObjectMeta::getLabels)
.map(labels -> labels.get(key)).orElse(null);
public String getDomainUid(V1Pod pod) {
return KubernetesUtils.getDomainUidLabel(
Optional.ofNullable(item.object).map(V1Pod::getMetadata).orElse(null));
}

private String chooseStateOrLastKnownServerStatus(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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 @@ -68,8 +69,8 @@ public String getNamespace() {
}

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

try (LoggingContext stack =
LoggingContext.setThreadContext().namespace(getNamespace()).domainUid(getDomainUID(item))) {
LoggingContext.setThreadContext().namespace(getNamespace()).domainUid(getDomainUid(item))) {
if (isError(item)) {
handleErrorResponse(item);
} else {
Expand Down Expand Up @@ -201,14 +201,7 @@ private boolean hasNext(WatchI<T> watch) {
*
* @return String object or null if the watcher is not associated with a domain
*/
public abstract String getDomainUID(Watch.Response<T> item);

protected String getDomainUID(V1ObjectMeta metadata) {
return Optional.ofNullable(metadata)
.map(V1ObjectMeta::getLabels)
.map(labels -> labels.get(LabelConstants.DOMAINUID_LABEL))
.orElse(null);
}
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);
}
}