Skip to content

Commit 700d155

Browse files
authored
owls-106707 adjust domain Created and Changed events in recheck (#4040)
* Adjust domain Created and Changed events in recheck and treat domain with null status as new
1 parent a5a9f19 commit 700d155

File tree

6 files changed

+105
-29
lines changed

6 files changed

+105
-29
lines changed

operator/src/main/java/oracle/kubernetes/operator/DomainProcessorImpl.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -375,17 +375,11 @@ public void runMakeRight(MakeRightClusterOperation operation) {
375375
private boolean shouldContinue(MakeRightDomainOperation operation, DomainPresenceInfo liveInfo) {
376376
final DomainPresenceInfo cachedInfo = getExistingDomainPresenceInfo(liveInfo);
377377
if (isNewDomain(cachedInfo)) {
378-
if (!operation.hasEventData()) {
379-
operation.withEventData(new EventData(DOMAIN_CREATED));
380-
}
381378
return true;
382379
} else if (liveInfo.isFromOutOfDateEvent(operation, cachedInfo)
383380
|| liveInfo.isDomainProcessingHalted(cachedInfo)) {
384381
return false;
385382
} else if (operation.isExplicitRecheck() || liveInfo.isDomainGenerationChanged(cachedInfo)) {
386-
if (!operation.hasEventData() && liveInfo.isDomainGenerationChanged(cachedInfo)) {
387-
operation.withEventData(new EventData(DOMAIN_CHANGED));
388-
}
389383
return true;
390384
} else {
391385
cachedInfo.setDomain(liveInfo.getDomain());

operator/src/main/java/oracle/kubernetes/operator/DomainResourcesValidation.java

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import java.util.ArrayList;
77
import java.util.Collection;
88
import java.util.Collections;
9+
import java.util.HashSet;
910
import java.util.List;
1011
import java.util.Map;
12+
import java.util.Objects;
1113
import java.util.Optional;
1214
import java.util.Set;
1315
import java.util.concurrent.ConcurrentHashMap;
@@ -28,6 +30,7 @@
2830
import oracle.kubernetes.operator.helpers.DomainPresenceInfo;
2931
import oracle.kubernetes.operator.helpers.EventHelper;
3032
import oracle.kubernetes.operator.helpers.EventHelper.EventData;
33+
import oracle.kubernetes.operator.helpers.EventHelper.EventItem;
3134
import oracle.kubernetes.operator.helpers.PodDisruptionBudgetHelper;
3235
import oracle.kubernetes.operator.helpers.PodHelper;
3336
import oracle.kubernetes.operator.helpers.ServiceHelper;
@@ -36,7 +39,9 @@
3639
import oracle.kubernetes.weblogic.domain.model.ClusterResource;
3740
import oracle.kubernetes.weblogic.domain.model.DomainList;
3841
import oracle.kubernetes.weblogic.domain.model.DomainResource;
42+
import org.jetbrains.annotations.Nullable;
3943

44+
import static oracle.kubernetes.operator.helpers.EventHelper.EventItem.DOMAIN_CHANGED;
4045
import static oracle.kubernetes.operator.helpers.EventHelper.EventItem.DOMAIN_CREATED;
4146

4247
/**
@@ -48,6 +53,8 @@ class DomainResourcesValidation {
4853
private final String namespace;
4954
private final DomainProcessor processor;
5055
private ClusterList activeClusterResources;
56+
private final Set<String> modifiedDomainNames = new HashSet<>();
57+
private final Set<String> newDomainNames = new HashSet<>();
5158

5259
DomainResourcesValidation(String namespace, DomainProcessor processor) {
5360
this.namespace = namespace;
@@ -212,6 +219,12 @@ private boolean isNotBeingProcessed(String namespace, String domainUid) {
212219
}
213220

214221
private void addDomain(DomainResource domain) {
222+
DomainPresenceInfo cachedInfo = getDomainPresenceInfoMap().get(domain.getDomainUid());
223+
if (cachedInfo == null) {
224+
newDomainNames.add(domain.getDomainUid());
225+
} else if (generationChanged(cachedInfo, domain)) {
226+
modifiedDomainNames.add(domain.getDomainUid());
227+
}
215228
getOrComputeDomainPresenceInfo(domain.getDomainUid()).setDomain(domain);
216229
}
217230

@@ -244,7 +257,7 @@ private static void removeStrandedDomainPresenceInfo(DomainProcessor dp, DomainP
244257
info.setDeleting(true);
245258
info.setPopulated(true);
246259
dp.createMakeRightOperation(info).withExplicitRecheck().forDeletion().withEventData(new EventData(
247-
EventHelper.EventItem.DOMAIN_DELETED)).execute();
260+
EventItem.DOMAIN_DELETED)).execute();
248261
}
249262

250263
private Stream<DomainPresenceInfo> getActiveDomainPresenceInfos() {
@@ -255,15 +268,39 @@ private boolean isActive(DomainPresenceInfo dpi) {
255268
return dpi.getDomain() != null;
256269
}
257270

258-
private static void activateDomain(DomainProcessor dp, DomainPresenceInfo info) {
271+
private void activateDomain(DomainProcessor dp, DomainPresenceInfo info) {
259272
info.setPopulated(true);
273+
EventItem eventItem = getEventItem(info);
260274
MakeRightDomainOperation makeRight = dp.createMakeRightOperation(info).withExplicitRecheck();
261-
if (info.getDomain().getStatus() == null) {
262-
makeRight.withEventData(new EventData(DOMAIN_CREATED)).interrupt();
275+
if (eventItem != null) {
276+
makeRight.withEventData(new EventData(eventItem)).interrupt();
263277
}
264278
makeRight.execute();
265279
}
266280

281+
private EventItem getEventItem(DomainPresenceInfo info) {
282+
if (newDomainNames.contains(info.getDomainUid()) || info.getDomain().getStatus() == null) {
283+
return DOMAIN_CREATED;
284+
}
285+
if (modifiedDomainNames.contains(info.getDomainUid())) {
286+
return DOMAIN_CHANGED;
287+
}
288+
return null;
289+
}
290+
291+
private boolean generationChanged(DomainPresenceInfo cachedInfo, DomainResource domain) {
292+
return !Objects.equals(getGeneration(cachedInfo), domain.getMetadata().getGeneration());
293+
}
294+
295+
@Nullable
296+
private Long getGeneration(DomainPresenceInfo cachedInfo) {
297+
return Optional.ofNullable(cachedInfo)
298+
.map(DomainPresenceInfo::getDomain)
299+
.map(DomainResource::getMetadata)
300+
.map(V1ObjectMeta::getGeneration)
301+
.orElse(null);
302+
}
303+
267304
private void deActivateCluster(DomainProcessor dp, ClusterPresenceInfo info) {
268305
dp.createMakeRightOperationForClusterEvent(EventHelper.EventItem.CLUSTER_DELETED, info.getCluster()).execute();
269306
}

operator/src/main/java/oracle/kubernetes/operator/Namespaces.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ private static class NamespaceValidationContext {
347347
}
348348

349349
private boolean isNotManaged(String ns) {
350-
return !allDomainNamespaces.contains(ns) || domainNamespaces.isStopping(ns).get();
350+
return isNoLongerActiveDomainNamespace(ns) || domainNamespaces.isStopping(ns).get();
351351
}
352352

353353
private boolean isNoLongerActiveDomainNamespace(String ns) {

operator/src/test/java/oracle/kubernetes/operator/DomainPresenceTest.java

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,13 @@
5252
import static com.meterware.simplestub.Stub.createStrictStub;
5353
import static com.meterware.simplestub.Stub.createStub;
5454
import static oracle.kubernetes.operator.DomainProcessorTest.getInitContainerStatusWithImagePullError;
55+
import static oracle.kubernetes.operator.EventMatcher.hasEvent;
5556
import static oracle.kubernetes.operator.LabelConstants.DOMAINUID_LABEL;
5657
import static oracle.kubernetes.operator.LabelConstants.JOBNAME_LABEL;
5758
import static oracle.kubernetes.operator.LabelConstants.SERVERNAME_LABEL;
59+
import static oracle.kubernetes.operator.helpers.EventHelper.EventItem.CLUSTER_DELETED;
60+
import static oracle.kubernetes.operator.helpers.EventHelper.EventItem.DOMAIN_CHANGED;
61+
import static oracle.kubernetes.operator.helpers.EventHelper.EventItem.DOMAIN_CREATED;
5862
import static oracle.kubernetes.operator.helpers.KubernetesTestSupport.CLUSTER;
5963
import static oracle.kubernetes.operator.helpers.KubernetesTestSupport.DOMAIN;
6064
import static org.hamcrest.Matchers.anEmptyMap;
@@ -87,6 +91,8 @@ class DomainPresenceTest extends ThreadFactoryTestBase {
8791
private final DomainNamespaces domainNamespaces = new DomainNamespaces(null);
8892
final DomainResource domain = createDomain(UID1, NS);
8993
final DomainPresenceInfo info = new DomainPresenceInfo(domain);
94+
private final DomainProcessorDelegateStub processorDelegate = DomainProcessorDelegateStub.createDelegate(testSupport);
95+
private final DomainProcessorImpl processor = new DomainProcessorImpl(processorDelegate);
9096

9197
@BeforeEach
9298
public void setUp() throws Exception {
@@ -246,6 +252,7 @@ void whenUnreferencedClusterResourceDeleted_triggerClusterMakeRightToGenerateClu
246252
MatcherAssert.assertThat(getClusterPresenceInfo(dp, CLUSTER_1), notNullValue());
247253
MatcherAssert.assertThat(getClusterPresenceInfo(dp, CLUSTER_2), nullValue());
248254
MatcherAssert.assertThat(getClusterPresenceInfo(dp, CLUSTER_3), notNullValue());
255+
assertThat(testSupport, not(hasEvent(CLUSTER_DELETED.getReason())));
249256
}
250257

251258
@Test
@@ -347,6 +354,45 @@ void whenK8sHasOneDomain_recordAdminServerService() {
347354
assertThat(getDomainPresenceInfo(dp, UID1).getServerService("admin"), equalTo(service));
348355
}
349356

357+
@Test
358+
void whenNewDomainAdded_generateDomainCreatedEvent() {
359+
processor.getDomainPresenceInfoMap().clear();
360+
addDomainResource(UID1, NS);
361+
362+
testSupport.runSteps(domainNamespaces.readExistingResources(NS, processor));
363+
364+
assertThat(testSupport, hasEvent(DOMAIN_CREATED.getReason()));
365+
}
366+
367+
@Test
368+
void whenDomainChanged_generateDomainChangedEvent() {
369+
DomainResource domain1 = createDomain(UID1, NS);
370+
domain1.getMetadata().setGeneration(1234L);
371+
processor.getDomainPresenceInfoMap()
372+
.computeIfAbsent(NS, k -> new ConcurrentHashMap<>()).put(UID1, new DomainPresenceInfo(domain1));
373+
DomainResource domain2 = createDomain(UID1, NS);
374+
domain2.getMetadata().setGeneration(2345L);
375+
testSupport.defineResources(domain2);
376+
377+
testSupport.runSteps(domainNamespaces.readExistingResources(NS, processor));
378+
379+
assertThat(testSupport, hasEvent(DOMAIN_CHANGED.getReason()));
380+
}
381+
382+
383+
@Test
384+
void whenDomainStatusBecameNull_generateDomainCreatedEvent() {
385+
DomainResource domain1 = createDomain(UID1, NS);
386+
processor.getDomainPresenceInfoMap()
387+
.computeIfAbsent(NS, k -> new ConcurrentHashMap<>()).put(UID1, new DomainPresenceInfo(domain1));
388+
domain1.setStatus(null);
389+
testSupport.defineResources(domain1);
390+
391+
testSupport.runSteps(domainNamespaces.readExistingResources(NS, processor));
392+
393+
assertThat(testSupport, hasEvent(DOMAIN_CREATED.getReason()));
394+
}
395+
350396
@Test
351397
void whenK8sHasOneDomainWithMissingInfo_dontRecordAdminServerService() {
352398
addDomainResource(UID1, NS);
@@ -676,6 +722,11 @@ public MakeRightDomainOperation withExplicitRecheck() {
676722
return this;
677723
}
678724

725+
@Override
726+
public MakeRightDomainOperation interrupt() {
727+
return this;
728+
}
729+
679730
@Override
680731
public MakeRightDomainOperation forDeletion() {
681732
deleting = true;
@@ -718,7 +769,7 @@ abstract static class MakeRightClusterOperationStub implements MakeRightClusterO
718769

719770
@Override
720771
public void execute() {
721-
if (eventItem == EventHelper.EventItem.CLUSTER_DELETED) {
772+
if (eventItem == CLUSTER_DELETED) {
722773
clusterResourceInfos.remove(info.getResourceName());
723774
} else {
724775
clusterResourceInfos.put(info.getResourceName(), info);

operator/src/test/java/oracle/kubernetes/operator/DomainProcessorTest.java

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -304,14 +304,6 @@ void tearDown() throws Exception {
304304
mementos.forEach(Memento::revert);
305305
}
306306

307-
@Test
308-
void whenDomainAdded_runMakeRightAndGenerateDomainCreatedEvent() {
309-
processor.createMakeRightOperation(newInfo).execute();
310-
311-
assertThat(logRecords, not(containsFine(NOT_STARTING_DOMAINUID_THREAD)));
312-
assertThat(testSupport, hasEvent(DOMAIN_CREATED.getReason()));
313-
}
314-
315307
@Test
316308
void whenDomainAddedWithChangedEventData_runMakeRightButDontGenerateDomainCreatedEvent() {
317309
processor.createMakeRightOperation(newInfo).withEventData(new EventData(DOMAIN_CHANGED)).execute();
@@ -414,15 +406,6 @@ void whenDomainChangedSpec_runMakeRight() {
414406
assertThat(logRecords, not(containsFine(NOT_STARTING_DOMAINUID_THREAD)));
415407
}
416408

417-
@Test
418-
void whenDomainChangedSpec_generateDomainChangedEvent() {
419-
processor.registerDomainPresenceInfo(originalInfo);
420-
421-
processor.createMakeRightOperation(newInfo).execute();
422-
423-
assertThat(testSupport, hasEvent(DOMAIN_CHANGED.getReason()));
424-
}
425-
426409
@Test
427410
void whenDomainChangedSpecWithDeletedEventData_dontGenerateDomainChangedEvent() {
428411
processor.registerDomainPresenceInfo(originalInfo);

operator/src/test/java/oracle/kubernetes/operator/StuckPodTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import io.kubernetes.client.openapi.models.V1ObjectMeta;
1717
import io.kubernetes.client.openapi.models.V1Pod;
1818
import oracle.kubernetes.operator.helpers.DomainPresenceInfo;
19+
import oracle.kubernetes.operator.helpers.EventHelper;
1920
import oracle.kubernetes.operator.helpers.KubernetesTestSupport;
2021
import oracle.kubernetes.operator.tuning.TuningParametersStub;
2122
import oracle.kubernetes.operator.work.Component;
@@ -252,6 +253,16 @@ public MakeRightDomainOperation withExplicitRecheck() {
252253
return this;
253254
}
254255

256+
@Override
257+
public MakeRightDomainOperation withEventData(EventHelper.EventData eventData) {
258+
return this;
259+
}
260+
261+
@Override
262+
public MakeRightDomainOperation interrupt() {
263+
return this;
264+
}
265+
255266
@Override
256267
public void execute() {
257268

0 commit comments

Comments
 (0)