Skip to content

feat: desired method aware of actual resource #1482

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

Closed
wants to merge 5 commits into from
Closed
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 @@ -56,7 +56,7 @@ protected ReconcileResult<R> reconcile(P primary, R actualResource, Context<P> c
if (creatable || updatable) {
if (actualResource == null) {
if (creatable) {
var desired = desired(primary, context);
var desired = desired(primary, null, context);
throwIfNull(desired, primary, "Desired");
logForOperation("Creating", primary, desired);
var createdResource = handleCreate(desired, primary, context);
Expand All @@ -66,7 +66,8 @@ protected ReconcileResult<R> reconcile(P primary, R actualResource, Context<P> c
if (updatable) {
final Matcher.Result<R> match = match(actualResource, primary, context);
if (!match.matched()) {
final var desired = match.computedDesired().orElse(desired(primary, context));
final var desired =
match.computedDesired().orElse(desired(primary, actualResource, context));
throwIfNull(desired, primary, "Desired");
logForOperation("Updating", primary, desired);
var updatedResource = handleUpdate(actualResource, desired, primary, context);
Expand Down Expand Up @@ -147,7 +148,7 @@ protected R handleUpdate(R actual, R desired, P primary, Context<P> context) {
return updated;
}

protected R desired(P primary, Context<P> context) {
protected R desired(P primary, R actual, Context<P> context) {
throw new IllegalStateException(
"desired method must be implemented if this DependentResource can be created and/or updated");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private AbstractDependentResource<R, P> asAbstractDependentResource() {
}

@Override
protected R desired(P primary, Context<P> context) {
protected R desired(P primary, R actual, Context<P> context) {
return desired;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public DesiredEqualsMatcher(AbstractDependentResource<R, P> abstractDependentRes

@Override
public Result<R> match(R actualResource, P primary, Context<P> context) {
var desired = abstractDependentResource.desired(primary, context);
var desired = abstractDependentResource.desired(primary, actualResource, context);
return Result.computed(actualResource.equals(desired), desired);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static <R extends HasMetadata, P extends HasMetadata> Matcher<R, P> matcherFor(

@Override
public Result<R> match(R actualResource, P primary, Context<P> context) {
var desired = dependentResource.desired(primary, context);
var desired = dependentResource.desired(primary, actualResource, context);
return match(desired, actualResource, false);
}

Expand Down Expand Up @@ -93,7 +93,7 @@ public static <R extends HasMetadata> Result<R> match(R desired, R actualResourc
public static <R extends HasMetadata, P extends HasMetadata> Result<R> match(
KubernetesDependentResource<R, P> dependentResource, R actualResource, P primary,
Context<P> context, boolean considerMetadata) {
final var desired = dependentResource.desired(primary, context);
final var desired = dependentResource.desired(primary, actualResource, context);
return match(desired, actualResource, considerMetadata);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ public KubernetesClient getKubernetesClient() {
}

@Override
protected R desired(P primary, Context<P> context) {
return super.desired(primary, context);
protected R desired(P primary, R actual, Context<P> context) {
return super.desired(primary, actual, context);
}

private void prepareEventFiltering(R desired, ResourceID resourceID) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ protected void onUpdated(TestCustomResource primary, ConfigMap updated, ConfigMa
Context<TestCustomResource> context) {}

@Override
protected ConfigMap desired(TestCustomResource primary, Context<TestCustomResource> context) {
protected ConfigMap desired(TestCustomResource primary, ConfigMap actual,
Context<TestCustomResource> context) {
return desired;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public TestDependentResource(Deployment desired) {
}

@Override
protected Deployment desired(HasMetadata primary, Context context) {
protected Deployment desired(HasMetadata primary, Deployment actual, Context context) {
final var currentCase = Optional.ofNullable(primary)
.map(p -> p.getMetadata().getLabels().get("case"))
.orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ConfigMapDependentResource() {

@Override
protected ConfigMap desired(CleanerForManagedDependentCustomResource primary,
Context<CleanerForManagedDependentCustomResource> context) {
ConfigMap actual, Context<CleanerForManagedDependentCustomResource> context) {

ConfigMap configMap = new ConfigMap();
configMap.setMetadata(new ObjectMeta());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public BaseService(String component) {
}

@Override
protected Service desired(ComplexDependentCustomResource primary,
protected Service desired(ComplexDependentCustomResource primary, Service actual,
Context<ComplexDependentCustomResource> context) {
var template = ReconcilerUtils.loadYaml(Service.class, getClass(), "service.yaml");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public BaseStatefulSet(String component) {
}

@Override
protected StatefulSet desired(ComplexDependentCustomResource primary,
protected StatefulSet desired(ComplexDependentCustomResource primary, StatefulSet actual,
Context<ComplexDependentCustomResource> context) {
var template = ReconcilerUtils.loadYaml(StatefulSet.class, getClass(), "statefulset.yaml");
var name = name(primary);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public ConfigMapDependentResource() {

@Override
protected ConfigMap desired(DependentAnnotationSecondaryMapperResource primary,
Context<DependentAnnotationSecondaryMapperResource> context) {
ConfigMap actual, Context<DependentAnnotationSecondaryMapperResource> context) {
ConfigMap configMap = new ConfigMap();
configMap.setMetadata(new ObjectMetaBuilder()
.withName(primary.getMetadata().getName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public FilteredDependentConfigMap() {

@Override
protected ConfigMap desired(DependentFilterTestCustomResource primary,
Context<DependentFilterTestCustomResource> context) {
ConfigMap actual, Context<DependentFilterTestCustomResource> context) {
ConfigMap configMap = new ConfigMap();
configMap.setMetadata(new ObjectMetaBuilder()
.withName(primary.getMetadata().getName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public ConfigMapDependentResource() {

@Override
protected ConfigMap desired(DependentOperationEventFilterCustomResource primary,
Context<DependentOperationEventFilterCustomResource> context) {
ConfigMap actual, Context<DependentOperationEventFilterCustomResource> context) {

ConfigMap configMap = new ConfigMap();
configMap.setMetadata(new ObjectMeta());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public SecretDependentResource() {

@Override
protected Secret desired(DependentResourceCrossRefResource primary,
Context<DependentResourceCrossRefResource> context) {
Secret actual, Context<DependentResourceCrossRefResource> context) {
Secret secret = new Secret();
secret.setMetadata(new ObjectMetaBuilder()
.withName(primary.getMetadata().getName())
Expand All @@ -80,7 +80,7 @@ public ConfigMapDependentResource() {

@Override
protected ConfigMap desired(DependentResourceCrossRefResource primary,
Context<DependentResourceCrossRefResource> context) {
ConfigMap actual, Context<DependentResourceCrossRefResource> context) {
var secret = context.getSecondaryResource(Secret.class);
if (secret.isEmpty()) {
throw new IllegalStateException("Secret is empty");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Set<ExternalResource> fetchResources(
}

@Override
protected ExternalResource desired(ExternalStateCustomResource primary,
protected ExternalResource desired(ExternalStateCustomResource primary, ExternalResource actual,
Context<ExternalStateCustomResource> context) {
return new ExternalResource(primary.getSpec().getData());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public IndexDiscriminatorTestDRConfigMap(String value) {
}

@Override
protected ConfigMap desired(IndexDiscriminatorTestCustomResource primary,
protected ConfigMap desired(IndexDiscriminatorTestCustomResource primary, ConfigMap actual,
Context<IndexDiscriminatorTestCustomResource> context) {
Map<String, String> data = new HashMap<>();
data.put(DATA_KEY, primary.getSpec().getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public ConfigMapDependentResource() {
}

@Override
protected ConfigMap desired(InformerRelatedBehaviorTestCustomResource primary,
protected ConfigMap desired(InformerRelatedBehaviorTestCustomResource primary, ConfigMap actual,
Context<InformerRelatedBehaviorTestCustomResource> context) {
return new ConfigMapBuilder()
.withMetadata(new ObjectMetaBuilder()
Expand All @@ -30,6 +30,5 @@ protected ConfigMap desired(InformerRelatedBehaviorTestCustomResource primary,
.build())
.withData(Map.of(DATA_KEY, primary.getMetadata().getName()))
.build();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public ConfigMapDependentResource() {

@Override
protected ConfigMap desired(DependentGarbageCollectionTestCustomResource primary,
Context<DependentGarbageCollectionTestCustomResource> context) {
ConfigMap actual, Context<DependentGarbageCollectionTestCustomResource> context) {
ConfigMap configMap = new ConfigMap();
configMap.setMetadata(new ObjectMetaBuilder()
.withName(primary.getMetadata().getName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public MultipleDependentResourceConfigMap(int value) {

@Override
protected ConfigMap desired(MultipleDependentResourceCustomResource primary,
Context<MultipleDependentResourceCustomResource> context) {
ConfigMap actual, Context<MultipleDependentResourceCustomResource> context) {
Map<String, String> data = new HashMap<>();
data.put(DATA_KEY, String.valueOf(value));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public MultipleManagedDependentResourceMultiInformerConfigMap1() {

@Override
protected ConfigMap desired(MultipleManagedDependentResourceMultiInformerCustomResource primary,
ConfigMap actual,
Context<MultipleManagedDependentResourceMultiInformerCustomResource> context) {
Map<String, String> data = new HashMap<>();
data.put(MultipleManagedDependentResourceReconciler.DATA_KEY, primary.getSpec().getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public MultipleManagedDependentResourceMultiInformerConfigMap2() {

@Override
protected ConfigMap desired(MultipleManagedDependentResourceMultiInformerCustomResource primary,
ConfigMap actual,
Context<MultipleManagedDependentResourceMultiInformerCustomResource> context) {
Map<String, String> data = new HashMap<>();
data.put(DATA_KEY, primary.getSpec().getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public MultipleManagedDependentResourceConfigMap1() {

@Override
protected ConfigMap desired(MultipleManagedDependentResourceCustomResource primary,
ConfigMap actual,
Context<MultipleManagedDependentResourceCustomResource> context) {
Map<String, String> data = new HashMap<>();
data.put(MultipleManagedDependentResourceReconciler.DATA_KEY, primary.getSpec().getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public MultipleManagedDependentResourceConfigMap2() {

@Override
protected ConfigMap desired(MultipleManagedDependentResourceCustomResource primary,
ConfigMap actual,
Context<MultipleManagedDependentResourceCustomResource> context) {
Map<String, String> data = new HashMap<>();
data.put(DATA_KEY, primary.getSpec().getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public ExternalResource update(ExternalResource actual,
public Matcher.Result<ExternalResource> match(ExternalResource actualResource,
MultipleManagedExternalDependentResourceCustomResource primary,
Context<MultipleManagedExternalDependentResourceCustomResource> context) {
var desired = desired(primary, context);
var desired = desired(primary, actualResource, context);
return Matcher.Result.computed(actualResource.equals(desired), desired);
}

Expand All @@ -59,7 +59,9 @@ public void delete(MultipleManagedExternalDependentResourceCustomResource primar
externalServiceMock.delete(toExternalResourceID(primary));
}

@Override
protected ExternalResource desired(MultipleManagedExternalDependentResourceCustomResource primary,
ExternalResource externalResource,
Context<MultipleManagedExternalDependentResourceCustomResource> context) {
return new ExternalResource(toExternalResourceID(primary),
primary.getSpec().getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ReconcileResult<ConfigMap> reconcile(OrderedManagedDependentCustomResourc

@Override
protected ConfigMap desired(OrderedManagedDependentCustomResource primary,
Context<OrderedManagedDependentCustomResource> context) {
ConfigMap actual, Context<OrderedManagedDependentCustomResource> context) {

ConfigMap configMap = new ConfigMap();
configMap.setMetadata(new ObjectMeta());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ReconcileResult<ConfigMap> reconcile(OrderedManagedDependentCustomResourc

@Override
protected ConfigMap desired(OrderedManagedDependentCustomResource primary,
Context<OrderedManagedDependentCustomResource> context) {
ConfigMap actual, Context<OrderedManagedDependentCustomResource> context) {

ConfigMap configMap = new ConfigMap();
configMap.setMetadata(new ObjectMeta());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public ConfigMapDependentResource() {
}

@Override
protected ConfigMap desired(RestartTestCustomResource primary,
protected ConfigMap desired(RestartTestCustomResource primary, ConfigMap actual,
Context<RestartTestCustomResource> context) {
return new ConfigMapBuilder()
.withMetadata(new ObjectMetaBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public DeploymentDependentResource() {

@Override
protected Deployment desired(StandaloneDependentTestCustomResource primary,
Context<StandaloneDependentTestCustomResource> context) {
Deployment actual, Context<StandaloneDependentTestCustomResource> context) {
Deployment deployment =
ReconcilerUtils.loadYaml(Deployment.class, StandaloneDependentResourceIT.class,
"nginx-deployment.yaml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public ConfigMapDependentResource() {

@Override
protected ConfigMap desired(WorkflowAllFeatureCustomResource primary,
Context<WorkflowAllFeatureCustomResource> context) {
ConfigMap actual, Context<WorkflowAllFeatureCustomResource> context) {
ConfigMap configMap = new ConfigMap();
configMap.setMetadata(new ObjectMetaBuilder()
.withName(primary.getMetadata().getName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public DeploymentDependentResource() {

@Override
protected Deployment desired(WorkflowAllFeatureCustomResource primary,
Context<WorkflowAllFeatureCustomResource> context) {
Deployment actual, Context<WorkflowAllFeatureCustomResource> context) {
Deployment deployment =
ReconcilerUtils.loadYaml(Deployment.class, WorkflowAllFeatureIT.class,
"nginx-deployment.yaml");
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<fabric8-client.version>6.3.1</fabric8-client.version>
<slf4j.version>1.7.36</slf4j.version>
<log4j.version>2.19.0</log4j.version>
<mokito.version>5.1.0</mokito.version>
<mokito.version>5.1.1</mokito.version>
<commons-lang3.version>3.12.0</commons-lang3.version>
<compile-testing.version>0.19</compile-testing.version>
<javapoet.version>1.13.0</javapoet.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void configureWith(ResourcePollerConfig config) {
}

@Override
public Schema desired(MySQLSchema primary, Context<MySQLSchema> context) {
public Schema desired(MySQLSchema primary, Schema actual, Context<MySQLSchema> context) {
return new Schema(primary.getMetadata().getName(), primary.getSpec().getEncoding());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private static String encode(String value) {
}

@Override
protected Secret desired(MySQLSchema schema, Context<MySQLSchema> context) {
protected Secret desired(MySQLSchema schema, Secret actual, Context<MySQLSchema> context) {
final var password = RandomStringUtils
.randomAlphanumeric(16); // NOSONAR: we don't need cryptographically-strong randomness here
final var name = schema.getMetadata().getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private static String tomcatImage(Tomcat tomcat) {
}

@Override
protected Deployment desired(Tomcat tomcat, Context<Tomcat> context) {
protected Deployment desired(Tomcat tomcat, Deployment actual, Context<Tomcat> context) {
Deployment deployment =
ReconcilerUtils.loadYaml(Deployment.class, getClass(), "deployment.yaml");
final ObjectMeta tomcatMetadata = tomcat.getMetadata();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public ServiceDependentResource() {
}

@Override
protected Service desired(Tomcat tomcat, Context<Tomcat> context) {
protected Service desired(Tomcat tomcat, Service actual, Context<Tomcat> context) {
final ObjectMeta tomcatMetadata = tomcat.getMetadata();
return new ServiceBuilder(ReconcilerUtils.loadYaml(Service.class, getClass(), "service.yaml"))
.editMetadata()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public ConfigMapDependentResource() {
}

@Override
protected ConfigMap desired(WebPage webPage, Context<WebPage> context) {
protected ConfigMap desired(WebPage webPage, ConfigMap actual, Context<WebPage> context) {
Map<String, String> data = new HashMap<>();
data.put("index.html", webPage.getSpec().getHtml());
Map<String, String> labels = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public DeploymentDependentResource() {
}

@Override
protected Deployment desired(WebPage webPage, Context<WebPage> context) {
protected Deployment desired(WebPage webPage, Deployment actual, Context<WebPage> context) {
Map<String, String> labels = new HashMap<>();
labels.put(SELECTOR, "true");
var deploymentName = deploymentName(webPage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public IngressDependentResource() {
}

@Override
protected Ingress desired(WebPage webPage, Context<WebPage> context) {
protected Ingress desired(WebPage webPage, Ingress actual, Context<WebPage> context) {
return makeDesiredIngress(webPage);
}

Expand Down
Loading