Skip to content

Commit

Permalink
draft of changes to simplify the recording mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Aug 10, 2023
1 parent 70f0eae commit 16fcfe1
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 427 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public abstract class KubernetesDependentResource<R extends HasMetadata, P exten
implements KubernetesClientAware,
DependentResourceConfigurator<KubernetesDependentResourceConfig<R>> {

public static String PREVIOUS_ANNOTATION_KEY = "javaoperatorsdk.io/previous";

private static final Logger log = LoggerFactory.getLogger(KubernetesDependentResource.class);

protected KubernetesClient client;
Expand Down Expand Up @@ -103,29 +105,6 @@ public void configureWith(InformerEventSource<R, P> informerEventSource) {
setEventSource(informerEventSource);
}


protected R handleCreate(R desired, P primary, Context<P> context) {
ResourceID resourceID = ResourceID.fromResource(desired);
try {
prepareEventFiltering(desired, resourceID);
return super.handleCreate(desired, primary, context);
} catch (RuntimeException e) {
cleanupAfterEventFiltering(resourceID);
throw e;
}
}

protected R handleUpdate(R actual, R desired, P primary, Context<P> context) {
ResourceID resourceID = ResourceID.fromResource(desired);
try {
prepareEventFiltering(desired, resourceID);
return super.handleUpdate(actual, desired, primary, context);
} catch (RuntimeException e) {
cleanupAfterEventFiltering(resourceID);
throw e;
}
}

@SuppressWarnings("unused")
public R create(R target, P primary, Context<P> context) {
if (useSSA(context)) {
Expand All @@ -137,6 +116,8 @@ public R create(R target, P primary, Context<P> context) {
target.getMetadata().setResourceVersion("1");
}
}
String id = ((InformerEventSource<R, P>) eventSource().orElseThrow()).getId();
target.getMetadata().getAnnotations().put(PREVIOUS_ANNOTATION_KEY, id);
final var resource = prepare(target, primary, "Creating");
return useSSA(context)
? resource
Expand All @@ -152,20 +133,24 @@ public R update(R actual, R target, P primary, Context<P> context) {
actual.getMetadata().getResourceVersion());
}
R updatedResource;
String id = ((InformerEventSource<R, P>) eventSource().orElseThrow()).getId();
target.getMetadata().getAnnotations().put(PREVIOUS_ANNOTATION_KEY,
id + "," + actual.getMetadata().getResourceVersion());
if (useSSA(context)) {
target.getMetadata().setResourceVersion(actual.getMetadata().getResourceVersion());
updatedResource = prepare(target, primary, "Updating")
.fieldManager(context.getControllerConfiguration().fieldManager())
.forceConflicts().serverSideApply();
} else {
var updatedActual = updaterMatcher.updateResource(actual, target, context);
updatedResource = prepare(updatedActual, primary, "Updating").replace();
updatedResource = prepare(updatedActual, primary, "Updating").update();
}
log.debug("Resource version after update: {}",
updatedResource.getMetadata().getResourceVersion());
return updatedResource;
}

@Override
public Result<R> match(R actualResource, P primary, Context<P> context) {
final var desired = desired(primary, context);
final boolean matches;
Expand Down Expand Up @@ -198,6 +183,7 @@ private boolean useSSA(Context<P> context) {
.ssaBasedCreateUpdateMatchForDependentResources();
}

@Override
protected void handleDelete(P primary, R secondary, Context<P> context) {
if (secondary != null) {
client.resource(secondary).delete();
Expand Down Expand Up @@ -295,16 +281,6 @@ protected R desired(P primary, Context<P> context) {
return super.desired(primary, context);
}

private void prepareEventFiltering(R desired, ResourceID resourceID) {
((InformerEventSource<R, P>) eventSource().orElseThrow())
.prepareForCreateOrUpdateEventFiltering(resourceID, desired);
}

private void cleanupAfterEventFiltering(ResourceID resourceID) {
((InformerEventSource<R, P>) eventSource().orElseThrow())
.cleanupOnCreateOrUpdateEventFiltering(resourceID);
}

@Override
public Optional<KubernetesDependentResourceConfig<R>> configuration() {
return Optional.ofNullable(kubernetesDependentResourceConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,28 @@ public boolean matches(R actual, R desired, Context<?> context) {
keepOnlyManagedFields(prunedActual, actualMap,
managedFieldsEntry.getFieldsV1().getAdditionalProperties(), objectMapper);

removeSDKAnnotations(prunedActual);

removeIrrelevantValues(desiredMap);

log.debug("Pruned actual: \n {} \n desired: \n {} ", prunedActual, desiredMap);

return prunedActual.equals(desiredMap);
}

private void removeSDKAnnotations(HashMap<String, Object> prunedActual) {
Optional.ofNullable(((Map<String, Object>) prunedActual.get(METADATA_KEY)))
.ifPresent(m -> m.computeIfPresent("annotations",
(k, v) -> {
var annotations = (Map<String, Object>) v;
annotations.remove(KubernetesDependentResource.PREVIOUS_ANNOTATION_KEY);
if (annotations.isEmpty()) {
return null;
}
return annotations;
}));
}

@SuppressWarnings("unchecked")
private static void removeIrrelevantValues(Map<String, Object> desiredMap) {
var metadata = (Map<String, Object>) desiredMap.get(METADATA_KEY);
Expand Down

This file was deleted.

Loading

0 comments on commit 16fcfe1

Please sign in to comment.