Skip to content

Backport OWLS-94649 fix to 3.3 release #2681

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 2 commits into from
Dec 23, 2021
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 @@ -23,6 +23,7 @@
import io.kubernetes.client.openapi.models.CoreV1Event;
import io.kubernetes.client.openapi.models.V1ConfigMap;
import io.kubernetes.client.openapi.models.V1ContainerState;
import io.kubernetes.client.openapi.models.V1ContainerStateWaiting;
import io.kubernetes.client.openapi.models.V1ContainerStatus;
import io.kubernetes.client.openapi.models.V1ObjectMeta;
import io.kubernetes.client.openapi.models.V1ObjectReference;
Expand Down Expand Up @@ -1380,6 +1381,21 @@ private void invoke() {
DomainStatusUpdater.createFailureRelatedSteps(
info, waiting.getReason(), waiting.getMessage(), null)));
break;
case INIT_CONTAINERS_NOT_READY:
List<String> waitingReasons = new ArrayList<>();
List<String> waitingMessages = new ArrayList<>();

Optional.ofNullable(getInitContainerStatuses(introspectorPod))
.orElseGet(Collections::emptyList).stream()
.forEach(status -> {
waitingMessages.add(getWaitingMessageFromStatus(status));
waitingReasons.add(getWaitingReason(status));
});
if (!waitingReasons.isEmpty()) {
delegate.runSteps(DomainStatusUpdater.createFailureRelatedSteps(
info, onSeparateLines(waitingReasons), onSeparateLines(waitingMessages), null));
}
break;
case TERMINATED_ERROR_REASON:
Optional.ofNullable(getMatchingContainerStatus())
.map(V1ContainerStatus::getState)
Expand Down Expand Up @@ -1408,6 +1424,30 @@ private void invoke() {
}
}

private String getWaitingReason(V1ContainerStatus status) {
return Optional.ofNullable(status)
.map(V1ContainerStatus::getState)
.map(V1ContainerState::getWaiting)
.map(V1ContainerStateWaiting::getReason)
.orElse(null);
}

private String getWaitingMessageFromStatus(V1ContainerStatus status) {
return Optional.ofNullable(status)
.map(V1ContainerStatus::getState)
.map(V1ContainerState::getWaiting)
.map(V1ContainerStateWaiting::getMessage)
.orElse(null);
}

private List<V1ContainerStatus> getInitContainerStatuses(V1Pod pod) {
return Optional.ofNullable(pod.getStatus()).map(V1PodStatus::getInitContainerStatuses).orElse(null);
}

private String onSeparateLines(List<String> waitingReasons) {
return String.join(System.lineSeparator(), waitingReasons);
}

private boolean isNotTerminatedByOperator() {
return notNullOrEmpty(getPodStatusReason()) || notNullOrEmpty(getPodStatusMessage()) || !isJobPodTerminated();
}
Expand Down
13 changes: 13 additions & 0 deletions operator/src/main/java/oracle/kubernetes/operator/PodWatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class PodWatcher extends Watcher<V1Pod> implements WatchListener<V1Pod>,
public enum PodStatus {
PHASE_FAILED,
WAITING_NON_NULL_MESSAGE,
INIT_CONTAINERS_NOT_READY,
TERMINATED_ERROR_REASON,
UNSCHEDULABLE,
SUCCESS
Expand Down Expand Up @@ -214,6 +215,8 @@ static PodStatus getPodStatus(@Nonnull V1Pod pod) {
return PodStatus.PHASE_FAILED;
} else if (notReady(conStatus) && getContainerStateWaitingMessage(conStatus) != null) {
return PodStatus.WAITING_NON_NULL_MESSAGE;
} else if (initContainersNotReady(pod)) {
return PodStatus.INIT_CONTAINERS_NOT_READY;
} else if (notReady(conStatus) && getContainerStateTerminatedReason(conStatus).contains("Error")) {
return PodStatus.TERMINATED_ERROR_REASON;
} else if (isUnschedulable(pod)) {
Expand All @@ -222,6 +225,11 @@ static PodStatus getPodStatus(@Nonnull V1Pod pod) {
return PodStatus.SUCCESS;
}

private static boolean initContainersNotReady(@Nonnull V1Pod pod) {
return notReady(Optional.ofNullable(pod.getStatus()).map(s -> s.getInitContainerStatuses())
.orElseGet(Collections::emptyList));
}

static V1ContainerStatus getContainerStatus(@Nonnull V1Pod pod) {
return getContainerStatuses(pod)
.stream()
Expand Down Expand Up @@ -257,6 +265,11 @@ private static String getReason(V1PodCondition podCondition) {
return Optional.ofNullable(podCondition).map(V1PodCondition::getReason).orElse("");
}

private static boolean notReady(List<V1ContainerStatus> initContainerStatuses) {
return Optional.ofNullable(initContainerStatuses)
.orElseGet(Collections::emptyList).stream().anyMatch(status -> notReady(status));
}

private static boolean notReady(V1ContainerStatus conStatus) {
return !Optional.ofNullable(conStatus).map(V1ContainerStatus::getReady).orElse(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public interface ProcessingConstants {
String DOMAIN_TOPOLOGY = "domainTopology";
String JOB_POD_NAME = "jobPodName";
String JOB_POD_CONTAINER_WAITING_REASON = "jobPodContainerWaitingReason";
String JOB_POD_INIT_CONTAINER_WAITING_REASON = "jobPodInitContainerWaitingReason";
String DOMAIN_INTROSPECTOR_JOB = "domainIntrospectorJob";
String DOMAIN_INTROSPECTOR_LOG_RESULT = "domainIntrospectorLogResult";
String DOMAIN_INTROSPECT_REQUESTED = "domainIntrospectRequested";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,10 @@ private static boolean isImagePullError(String jobPodContainerWaitingReason) {
.orElse(false);
}

private List<V1ContainerStatus> getInitContainerStatuses(V1Pod pod) {
return Optional.ofNullable(pod.getStatus()).map(V1PodStatus::getInitContainerStatuses).orElse(null);
}

private static boolean isJobTimedout(DomainPresenceInfo info) {
return Objects.equals(getReason(info), "DeadlineExceeded") || getMessage(info).contains("DeadlineExceeded");
}
Expand Down Expand Up @@ -564,22 +568,37 @@ private void recordJobPodNameAndStatus(Packet packet, V1Pod pod) {
.map(V1PodStatus::getContainerStatuses).map(statuses -> statuses.get(0))
.map(V1ContainerStatus::getState).map(V1ContainerState::getWaiting)
.map(V1ContainerStateWaiting::getReason).orElse(null));
packet.put(ProcessingConstants.JOB_POD_INIT_CONTAINER_WAITING_REASON, getInitContainerWaitingMessages(pod));
}
}

private Boolean getInitContainerWaitingMessages(V1Pod pod) {
return Optional.ofNullable(getInitContainerStatuses(pod)).orElseGet(Collections::emptyList).stream()
.anyMatch(status -> isImagePullError(getWaitingReason(status)));
}

private String getWaitingReason(V1ContainerStatus status) {
return Optional.ofNullable(status)
.map(V1ContainerStatus::getState)
.map(V1ContainerState::getWaiting)
.map(V1ContainerStateWaiting::getReason)
.orElse(null);
}
}

static OffsetDateTime createNextSteps(List<Step> nextSteps, Packet packet, V1Job job, Step next) {
OffsetDateTime jobStartTime;
DomainPresenceInfo info = packet.getSpi(DomainPresenceInfo.class);
String namespace = info.getNamespace();
String jobPodContainerWaitingReason = (String) packet.get(ProcessingConstants.JOB_POD_CONTAINER_WAITING_REASON);
String jobPodContainerWaitingReason = packet.getValue(ProcessingConstants.JOB_POD_CONTAINER_WAITING_REASON);
Boolean jobInitContainerImagePullError = getjobInitContainerImagePullError(packet);

if (job != null) {
jobStartTime = Optional.ofNullable(job.getMetadata())
.map(V1ObjectMeta::getCreationTimestamp).orElse(OffsetDateTime.now());
String lastIntrospectJobProcessedId = getLastIntrospectJobProcessedId(info);

if (isJobTimedout(info) || (isImagePullError(jobPodContainerWaitingReason))) {
if (isJobTimedout(info) || (isImagePullError(jobPodContainerWaitingReason)) || jobInitContainerImagePullError) {
jobStartTime = OffsetDateTime.now();
packet.put(DOMAIN_INTROSPECT_REQUESTED, ReadDomainIntrospectorPodLogResponseStep.INTROSPECTION_FAILED);
nextSteps.add(Step.chain(deleteDomainIntrospectorJobStep(null),
Expand All @@ -602,6 +621,11 @@ static OffsetDateTime createNextSteps(List<Step> nextSteps, Packet packet, V1Job
}
return jobStartTime;
}

private static Boolean getjobInitContainerImagePullError(Packet packet) {
return Optional.ofNullable(packet.<Boolean>getValue(ProcessingConstants.JOB_POD_INIT_CONTAINER_WAITING_REASON))
.orElse(Boolean.FALSE);
}
}

static ReadDomainIntrospectorPodLogStep readDomainIntrospectorPodLog(Step next) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@
import com.meterware.simplestub.StaticStubSupport;
import io.kubernetes.client.openapi.models.CoreV1Event;
import io.kubernetes.client.openapi.models.V1ConfigMap;
import io.kubernetes.client.openapi.models.V1ContainerState;
import io.kubernetes.client.openapi.models.V1ContainerStateWaiting;
import io.kubernetes.client.openapi.models.V1ContainerStatus;
import io.kubernetes.client.openapi.models.V1Job;
import io.kubernetes.client.openapi.models.V1JobCondition;
import io.kubernetes.client.openapi.models.V1JobStatus;
import io.kubernetes.client.openapi.models.V1LabelSelector;
import io.kubernetes.client.openapi.models.V1ObjectMeta;
import io.kubernetes.client.openapi.models.V1Pod;
import io.kubernetes.client.openapi.models.V1PodStatus;
import io.kubernetes.client.openapi.models.V1Secret;
import io.kubernetes.client.openapi.models.V1Service;
import io.kubernetes.client.openapi.models.V1ServicePort;
Expand All @@ -52,6 +56,7 @@
import oracle.kubernetes.operator.helpers.ServiceHelper;
import oracle.kubernetes.operator.helpers.TuningParametersStub;
import oracle.kubernetes.operator.helpers.UnitTestHash;
import oracle.kubernetes.operator.logging.MessageKeys;
import oracle.kubernetes.operator.rest.ScanCacheStub;
import oracle.kubernetes.operator.utils.InMemoryCertificates;
import oracle.kubernetes.operator.wlsconfig.WlsClusterConfig;
Expand Down Expand Up @@ -87,16 +92,19 @@
import static oracle.kubernetes.operator.LabelConstants.DOMAINUID_LABEL;
import static oracle.kubernetes.operator.LabelConstants.INTROSPECTION_STATE_LABEL;
import static oracle.kubernetes.operator.LabelConstants.SERVERNAME_LABEL;
import static oracle.kubernetes.operator.ProcessingConstants.DOMAIN_INTROSPECTOR_JOB;
import static oracle.kubernetes.operator.WebLogicConstants.RUNNING_STATE;
import static oracle.kubernetes.operator.WebLogicConstants.SHUTDOWN_STATE;
import static oracle.kubernetes.operator.helpers.KubernetesTestSupport.CONFIG_MAP;
import static oracle.kubernetes.operator.helpers.KubernetesTestSupport.DOMAIN;
import static oracle.kubernetes.operator.helpers.KubernetesTestSupport.JOB;
import static oracle.kubernetes.operator.helpers.KubernetesTestSupport.POD;
import static oracle.kubernetes.operator.helpers.KubernetesTestSupport.SERVICE;
import static oracle.kubernetes.operator.logging.MessageKeys.NOT_STARTING_DOMAINUID_THREAD;
import static oracle.kubernetes.utils.LogMatcher.containsFine;
import static oracle.kubernetes.weblogic.domain.model.ConfigurationConstants.START_ALWAYS;
import static oracle.kubernetes.weblogic.domain.model.ConfigurationConstants.START_NEVER;
import static oracle.kubernetes.weblogic.domain.model.DomainConditionType.Failed;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
Expand All @@ -120,6 +128,7 @@ class DomainProcessorTest {
private static final String[] MANAGED_SERVER_NAMES =
IntStream.rangeClosed(1, MAX_SERVERS).mapToObj(DomainProcessorTest::getManagedServerName).toArray(String[]::new);
public static final String DOMAIN_NAME = "base_domain";
private TestUtils.ConsoleHandlerMemento consoleHandlerMemento;

@Nonnull
private static String getManagedServerName(int n) {
Expand Down Expand Up @@ -165,6 +174,9 @@ private static WlsDomainConfig createDomainConfig() {

@BeforeEach
public void setUp() throws Exception {
consoleHandlerMemento = TestUtils.silenceOperatorLogger()
.collectLogMessages(logRecords, NOT_STARTING_DOMAINUID_THREAD).withLogLevel(Level.FINE);
mementos.add(consoleHandlerMemento);
mementos.add(TestUtils.silenceOperatorLogger()
.collectLogMessages(logRecords, NOT_STARTING_DOMAINUID_THREAD).withLogLevel(Level.FINE));
mementos.add(testSupport.install());
Expand Down Expand Up @@ -1337,4 +1349,71 @@ private void defineDuplicateServerNames() {
domain.getSpec().getManagedServers().add(new ManagedServer().withServerName("ms1"));
domain.getSpec().getManagedServers().add(new ManagedServer().withServerName("ms1"));
}

@Test
void whenIntrospectionJobInitContainerHasImagePullFailure_jobRecreatedAndFailedConditionCleared() throws Exception {
consoleHandlerMemento.ignoringLoggedExceptions(RuntimeException.class);
consoleHandlerMemento.ignoreMessage(MessageKeys.NOT_STARTING_DOMAINUID_THREAD);
jobStatus = createBackoffStatus();
establishPreviousIntrospection(null);
defineIntrospectionWithInitContainerImagePullError();
testSupport.doOnDelete(JOB, j -> deletePod());
testSupport.doOnCreate(JOB, j -> createJobPodAndSetCompletedStatus(job));
domainConfigurator.withIntrospectVersion(NEW_INTROSPECTION_STATE);
processor.createMakeRightOperation(new DomainPresenceInfo(newDomain)).interrupt().execute();

assertThat(isDomainConditionFailed(), is(false));
}

V1JobStatus createBackoffStatus() {
return new V1JobStatus().addConditionsItem(new V1JobCondition().status("True").type("Failed")
.reason("BackoffLimitExceeded"));
}

private void defineIntrospectionWithInitContainerImagePullError() {
V1Job job = asFailedJob(createIntrospectorJob("IMAGE_PULL_FAILURE_JOB"));
testSupport.defineResources(job);
testSupport.addToPacket(DOMAIN_INTROSPECTOR_JOB, job);
setJobPodInitContainerStatusImagePullError();
}

private void setJobPodInitContainerStatusImagePullError() {
testSupport.<V1Pod>getResourceWithName(POD, getJobName()).status(new V1PodStatus().initContainerStatuses(
Arrays.asList(new V1ContainerStatus().state(new V1ContainerState().waiting(
new V1ContainerStateWaiting().reason("ImagePullBackOff").message("Back-off pulling image"))))));
}

private V1Job asFailedJob(V1Job job) {
job.setStatus(new V1JobStatus().addConditionsItem(new V1JobCondition().status("True").type("Failed")
.reason("BackoffLimitExceeded")));
return job;
}

private V1Job createIntrospectorJob(String uid) {
return new V1Job().metadata(createJobMetadata(uid)).status(new V1JobStatus());
}

private V1ObjectMeta createJobMetadata(String uid) {
return new V1ObjectMeta().name(getJobName()).namespace(NS).creationTimestamp(SystemClock.now()).uid(uid);
}

private static String getJobName() {
return LegalNames.toJobIntrospectorName(UID);
}

private void deletePod() {
testSupport.deleteResources(new V1Pod().metadata(new V1ObjectMeta().name(getJobName()).namespace(NS)));
}

private void createJobPodAndSetCompletedStatus(V1Job job) {
Map<String, String> labels = new HashMap<>();
labels.put(LabelConstants.JOBNAME_LABEL, getJobName());
testSupport.defineResources(POD,
new V1Pod().metadata(new V1ObjectMeta().name(getJobName()).labels(labels).namespace(NS)));
job.setStatus(createCompletedStatus());
}

private boolean isDomainConditionFailed() {
return newDomain.getStatus().getConditions().stream().anyMatch(c -> c.getType() == Failed);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class IntrospectionStatusTest {
private static final String UNSCHEDULABLE = "Unschedulable";
private static final String IMAGE_PULL_BACKOFF = "ImagePullBackoff";
private static final String DEADLINE_EXCEEDED = "DeadlineExceeded";
private static final int MESSAGE_LENGTH = 10;
private final List<Memento> mementos = new ArrayList<>();
private final KubernetesTestSupport testSupport = new KubernetesTestSupport();
private final Map<String, Map<String, DomainPresenceInfo>> presenceInfoMap = new HashMap<>();
Expand Down Expand Up @@ -208,6 +209,21 @@ void whenNewIntrospectorJobPodStatusReasonNullAfterImagePullFailure_patchDomain(
assertThat(updatedDomain.getStatus().getMessage(), emptyOrNullString());
}

@Test
void whenPodHasInitContainerImagePullErrorWaitingMessage_updateDomainStatus() {
processor.dispatchPodWatch(
WatchEvent.createAddedEvent(
createIntrospectorJobPodWithInitContainerStatus(createWaitingState(IMAGE_PULL_FAILURE, MESSAGE)))
.toWatchResponse());

assertThat(getDomain().getStatus().getReason(), equalTo("ErrImagePull"));
assertThat(getDomain().getStatus().getMessage(), equalTo(MESSAGE));
}

private Domain getDomain() {
return testSupport.getResourceWithName(KubernetesTestSupport.DOMAIN, UID);
}

private V1Pod createIntrospectorJobPod(V1ContainerState waitingState) {
return createIntrospectorJobPod(UID)
.status(
Expand All @@ -224,14 +240,27 @@ private V1Pod createIntrospectorJobPod(V1ContainerState waitingState) {
@SuppressWarnings("SameParameterValue")
private V1Pod createIntrospectorJobPod(String domainUid) {
return AnnotationHelper.withSha256Hash(
new V1Pod()
.metadata(
withIntrospectorJobLabels(
new V1ObjectMeta()
.name(toJobIntrospectorName(domainUid) + getPodSuffix())
.namespace(NS),
domainUid))
.spec(new V1PodSpec()));
new V1Pod()
.metadata(
withIntrospectorJobLabels(
new V1ObjectMeta()
.name(toJobIntrospectorName(domainUid) + getPodSuffix())
.namespace(NS),
domainUid))
.spec(new V1PodSpec()));
}

private V1Pod createIntrospectorJobPodWithInitContainerStatus(V1ContainerState waitingState) {
return createIntrospectorJobPod(UID)
.status(
new V1PodStatusBuilder()
.addNewInitContainerStatus()
.withImage(IMAGE_NAME)
.withName(toJobIntrospectorName(UID))
.withReady(false)
.withState(waitingState)
.endInitContainerStatus()
.build());
}

private V1Pod createIntrospectorJobPodWithConditions(V1PodCondition condition) {
Expand Down