Skip to content

Commit a109e21

Browse files
committed
inverted the logic to the informereventsource
moving where the annotation is handled for matching
1 parent 96c6a35 commit a109e21

File tree

4 files changed

+42
-45
lines changed

4 files changed

+42
-45
lines changed

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/KubernetesDependentResource.java

+23-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.javaoperatorsdk.operator.processing.dependent.kubernetes;
22

3-
import java.util.HashMap;
3+
import java.util.LinkedHashMap;
4+
import java.util.Map;
45
import java.util.Optional;
56
import java.util.Set;
67

@@ -37,8 +38,6 @@ public abstract class KubernetesDependentResource<R extends HasMetadata, P exten
3738
implements KubernetesClientAware,
3839
DependentResourceConfigurator<KubernetesDependentResourceConfig<R>> {
3940

40-
public static String PREVIOUS_ANNOTATION_KEY = "javaoperatorsdk.io/previous";
41-
4241
private static final Logger log = LoggerFactory.getLogger(KubernetesDependentResource.class);
4342

4443
protected KubernetesClient client;
@@ -147,17 +146,16 @@ public R update(R actual, R target, P primary, Context<P> context) {
147146
return updatedResource;
148147
}
149148

150-
void addPreviousAnnotation(String resourceVersion, HasMetadata target) {
151-
String id =
152-
((InformerEventSource<HasMetadata, HasMetadata>) eventSource().orElseThrow()).getId();
153-
target.getMetadata().getAnnotations().put(PREVIOUS_ANNOTATION_KEY,
154-
id + Optional.ofNullable(resourceVersion).map(rv -> "," + rv).orElse(""));
149+
private void addPreviousAnnotation(String resourceVersion, HasMetadata target) {
150+
((InformerEventSource<HasMetadata, HasMetadata>) eventSource().orElseThrow())
151+
.addPreviousAnnotation(resourceVersion, target);
155152
}
156153

157154
@Override
158155
public Result<R> match(R actualResource, P primary, Context<P> context) {
159156
final var desired = desired(primary, context);
160157
final boolean matches;
158+
copySDKAnnotations(actualResource, desired);
161159
if (useSSA(context)) {
162160
addReferenceHandlingMetadata(desired, primary);
163161
matches = SSABasedGenericKubernetesResourceMatcher.getInstance()
@@ -171,6 +169,7 @@ public Result<R> match(R actualResource, P primary, Context<P> context) {
171169
@SuppressWarnings("unused")
172170
public Result<R> match(R actualResource, R desired, P primary, Context<P> context) {
173171
if (useSSA(context)) {
172+
copySDKAnnotations(actualResource, desired);
174173
addReferenceHandlingMetadata(desired, primary);
175174
var matches = SSABasedGenericKubernetesResourceMatcher.getInstance()
176175
.matches(actualResource, desired, context);
@@ -182,6 +181,21 @@ public Result<R> match(R actualResource, R desired, P primary, Context<P> contex
182181
}
183182
}
184183

184+
private void copySDKAnnotations(R actualResource, final R desired) {
185+
String actual = actualResource.getMetadata().getAnnotations()
186+
.get(InformerEventSource.PREVIOUS_ANNOTATION_KEY);
187+
Map<String, String> annotations = desired.getMetadata().getAnnotations();
188+
if (actual != null) {
189+
if (annotations == null) {
190+
annotations = new LinkedHashMap<>();
191+
desired.getMetadata().setAnnotations(annotations);
192+
}
193+
annotations.put(InformerEventSource.PREVIOUS_ANNOTATION_KEY, actual);
194+
} else if (annotations != null) {
195+
annotations.remove(InformerEventSource.PREVIOUS_ANNOTATION_KEY);
196+
}
197+
}
198+
185199
private boolean useSSA(Context<P> context) {
186200
return context.getControllerConfiguration().getConfigurationService()
187201
.ssaBasedCreateUpdateMatchForDependentResources();
@@ -255,7 +269,7 @@ private boolean useDefaultAnnotationsToIdentifyPrimary() {
255269
private void addDefaultSecondaryToPrimaryMapperAnnotations(R desired, P primary) {
256270
var annotations = desired.getMetadata().getAnnotations();
257271
if (annotations == null) {
258-
annotations = new HashMap<>();
272+
annotations = new LinkedHashMap<>();
259273
desired.getMetadata().setAnnotations(annotations);
260274
}
261275
annotations.put(Mappers.DEFAULT_ANNOTATION_FOR_NAME, primary.getMetadata().getName());

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/dependent/kubernetes/SSABasedGenericKubernetesResourceMatcher.java

-17
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
// see also: https://kubernetes.slack.com/archives/C0123CNN8F3/p1686141087220719
4141
public class SSABasedGenericKubernetesResourceMatcher<R extends HasMetadata> {
4242

43-
private static final String ANNOTATIONS_KEY = "annotations";
4443
@SuppressWarnings("rawtypes")
4544
private static final SSABasedGenericKubernetesResourceMatcher INSTANCE =
4645
new SSABasedGenericKubernetesResourceMatcher<>();
@@ -91,8 +90,6 @@ public boolean matches(R actual, R desired, Context<?> context) {
9190
keepOnlyManagedFields(prunedActual, actualMap,
9291
managedFieldsEntry.getFieldsV1().getAdditionalProperties(), objectMapper);
9392

94-
removeSDKAnnotations(prunedActual);
95-
9693
removeIrrelevantValues(desiredMap);
9794

9895
if (LoggingUtils.isNotSensitiveResource(desired)) {
@@ -102,20 +99,6 @@ public boolean matches(R actual, R desired, Context<?> context) {
10299
return prunedActual.equals(desiredMap);
103100
}
104101

105-
@SuppressWarnings("unchecked")
106-
private static void removeSDKAnnotations(HashMap<String, Object> prunedActual) {
107-
Optional.ofNullable(((Map<String, Object>) prunedActual.get(METADATA_KEY)))
108-
.ifPresent(m -> m.computeIfPresent(ANNOTATIONS_KEY,
109-
(k, v) -> {
110-
var annotations = (Map<String, Object>) v;
111-
annotations.remove(KubernetesDependentResource.PREVIOUS_ANNOTATION_KEY);
112-
if (annotations.isEmpty()) {
113-
return null;
114-
}
115-
return annotations;
116-
}));
117-
}
118-
119102
@SuppressWarnings("unchecked")
120103
private static void removeIrrelevantValues(Map<String, Object> desiredMap) {
121104
var metadata = (Map<String, Object>) desiredMap.get(METADATA_KEY);

Diff for: operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/source/informer/InformerEventSource.java

+16-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
2020
import io.javaoperatorsdk.operator.api.config.informer.InformerConfiguration;
2121
import io.javaoperatorsdk.operator.api.reconciler.EventSourceContext;
22-
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResource;
2322
import io.javaoperatorsdk.operator.processing.event.Event;
2423
import io.javaoperatorsdk.operator.processing.event.EventHandler;
2524
import io.javaoperatorsdk.operator.processing.event.ResourceID;
@@ -75,14 +74,16 @@ public class InformerEventSource<R extends HasMetadata, P extends HasMetadata>
7574
extends ManagedInformerEventSource<R, P, InformerConfiguration<R>>
7675
implements ResourceEventHandler<R> {
7776

77+
public static String PREVIOUS_ANNOTATION_KEY = "javaoperatorsdk.io/previous";
78+
7879
private static final Logger log = LoggerFactory.getLogger(InformerEventSource.class);
7980

8081
private final InformerConfiguration<R> configuration;
8182
// we need direct control for the indexer to propagate the just update resource also to the index
8283
private final PrimaryToSecondaryIndex<R> primaryToSecondaryIndex;
8384
private final PrimaryToSecondaryMapper<P> primaryToSecondaryMapper;
8485
private Map<String, Function<R, List<String>>> indexerBuffer = new HashMap<>();
85-
private String id = UUID.randomUUID().toString();
86+
private final String id = UUID.randomUUID().toString();
8687

8788
public InformerEventSource(
8889
InformerConfiguration<R> configuration, EventSourceContext<P> context) {
@@ -110,10 +111,6 @@ public InformerEventSource(InformerConfiguration<R> configuration, KubernetesCli
110111
genericFilter = configuration.genericFilter().orElse(null);
111112
}
112113

113-
public String getId() {
114-
return id;
115-
}
116-
117114
@Override
118115
public void onAdd(R newResource) {
119116
if (log.isDebugEnabled()) {
@@ -192,8 +189,7 @@ private boolean canSkipEvent(R newObject, R oldObject, ResourceID resourceID) {
192189
}
193190

194191
private boolean isEventKnownFromAnnotation(R newObject, R oldObject) {
195-
String previous = newObject.getMetadata().getAnnotations()
196-
.get(KubernetesDependentResource.PREVIOUS_ANNOTATION_KEY);
192+
String previous = newObject.getMetadata().getAnnotations().get(PREVIOUS_ANNOTATION_KEY);
197193
boolean known = false;
198194
if (previous != null) {
199195
String[] parts = previous.split(",");
@@ -320,4 +316,16 @@ public void addIndexers(Map<String, Function<R, List<String>>> indexers) {
320316
indexerBuffer.putAll(indexers);
321317
}
322318

319+
/**
320+
* Add an annotation to the resource so that the subsequent will be omitted
321+
*
322+
* @param resourceVersion null if there is no prior version
323+
* @param target mutable resource that will be returned
324+
*/
325+
public R addPreviousAnnotation(String resourceVersion, R target) {
326+
target.getMetadata().getAnnotations().put(PREVIOUS_ANNOTATION_KEY,
327+
id + Optional.ofNullable(resourceVersion).map(rv -> "," + rv).orElse(""));
328+
return target;
329+
}
330+
323331
}

Diff for: operator-framework-core/src/test/java/io/javaoperatorsdk/operator/processing/event/source/informer/InformerEventSourceTest.java

+3-11
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import io.javaoperatorsdk.operator.api.config.ConfigurationService;
1717
import io.javaoperatorsdk.operator.api.config.InformerStoppedHandler;
1818
import io.javaoperatorsdk.operator.api.config.informer.InformerConfiguration;
19-
import io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResource;
2019
import io.javaoperatorsdk.operator.processing.event.EventHandler;
2120
import io.javaoperatorsdk.operator.processing.event.ResourceID;
2221
import io.javaoperatorsdk.operator.processing.event.source.SecondaryToPrimaryMapper;
@@ -38,7 +37,6 @@ class InformerEventSourceTest {
3837

3938
private static final String PREV_RESOURCE_VERSION = "0";
4039
private static final String DEFAULT_RESOURCE_VERSION = "1";
41-
private static final String NEXT_RESOURCE_VERSION = "2";
4240

4341
private InformerEventSource<Deployment, TestCustomResource> informerEventSource;
4442
private final KubernetesClient clientMock = MockKubernetesClient.client(Deployment.class);
@@ -81,21 +79,15 @@ void skipsEventPropagationIfResourceWithSameVersionInResourceCache() {
8179

8280
@Test
8381
void skipsAddEventPropagationViaAnnotation() {
84-
informerEventSource.onAdd(new DeploymentBuilder(testDeployment()).editMetadata()
85-
.addToAnnotations(KubernetesDependentResource.PREVIOUS_ANNOTATION_KEY,
86-
informerEventSource.getId())
87-
.endMetadata().build());
82+
informerEventSource.onAdd(informerEventSource.addPreviousAnnotation(null, testDeployment()));
8883

8984
verify(eventHandlerMock, never()).handleEvent(any());
9085
}
9186

9287
@Test
9388
void skipsUpdateEventPropagationViaAnnotation() {
9489
informerEventSource.onUpdate(testDeployment(),
95-
new DeploymentBuilder(testDeployment()).editMetadata()
96-
.addToAnnotations(KubernetesDependentResource.PREVIOUS_ANNOTATION_KEY,
97-
informerEventSource.getId() + ",1")
98-
.endMetadata().build());
90+
informerEventSource.addPreviousAnnotation("1", testDeployment()));
9991

10092
verify(eventHandlerMock, never()).handleEvent(any());
10193
}
@@ -110,7 +102,7 @@ void processEventPropagationWithoutAnnotation() {
110102
@Test
111103
void processEventPropagationWithIncorrectAnnotation() {
112104
informerEventSource.onAdd(new DeploymentBuilder(testDeployment()).editMetadata()
113-
.addToAnnotations(KubernetesDependentResource.PREVIOUS_ANNOTATION_KEY, "invalid")
105+
.addToAnnotations(InformerEventSource.PREVIOUS_ANNOTATION_KEY, "invalid")
114106
.endMetadata().build());
115107

116108
verify(eventHandlerMock, times(1)).handleEvent(any());

0 commit comments

Comments
 (0)