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 9, 2023
1 parent 3b72bb4 commit 6c4b1d0
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 425 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,35 +105,14 @@ 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)) {
// setting resource version for SSA so only created if it doesn't exist already
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 @@ -147,20 +128,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 @@ -193,6 +178,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 @@ -290,16 +276,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 @@ -90,6 +90,15 @@ public boolean matches(R actual, R desired, Context<?> context) {
keepOnlyManagedFields(prunedActual, actualMap,
managedFieldsEntry.getFieldsV1().getAdditionalProperties(), objectMapper);

((Map<String, Object>) prunedActual.get(METADATA_KEY)).computeIfPresent("annotations", (k, v) -> {
var annotations = (Map<String, Object>)v;
annotations.remove(KubernetesDependentResource.PREVIOUS_ANNOTATION_KEY);
if (annotations.isEmpty()) {
return null;
}
return annotations;
});

removeIrrelevantValues(desiredMap);

if (LoggingUtils.isNotSensitiveResource(desired)) {
Expand Down

This file was deleted.

Loading

0 comments on commit 6c4b1d0

Please sign in to comment.