Skip to content

Commit 314014a

Browse files
committed
feat: status updates
Signed-off-by: Attila Mészáros <[email protected]>
1 parent 59b842b commit 314014a

File tree

3 files changed

+78
-10
lines changed

3 files changed

+78
-10
lines changed

src/main/java/io/javaoperatorsdk/operator/glue/customresource/glue/RelatedResourceSpec.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.List;
44
import java.util.Objects;
55

6+
import io.fabric8.crd.generator.annotation.PreserveUnknownFields;
67
import io.fabric8.generator.annotation.Required;
78

89
public class RelatedResourceSpec {
@@ -19,6 +20,9 @@ public class RelatedResourceSpec {
1920
private boolean clusterScoped = Boolean.FALSE;
2021
private List<String> resourceNames;
2122

23+
@PreserveUnknownFields
24+
private Object statusPatch;
25+
private String statusPatchTemplate;
2226

2327
public String getApiVersion() {
2428
return apiVersion;
@@ -73,14 +77,16 @@ public boolean equals(Object o) {
7377
return false;
7478
RelatedResourceSpec that = (RelatedResourceSpec) o;
7579
return clusterScoped == that.clusterScoped && Objects.equals(name, that.name)
76-
&& Objects.equals(apiVersion, that.apiVersion) && Objects.equals(kind, that.kind)
77-
&& Objects.equals(namespace, that.namespace)
78-
&& Objects.equals(resourceNames, that.resourceNames);
80+
&& Objects.equals(namespace, that.namespace) && Objects.equals(apiVersion, that.apiVersion)
81+
&& Objects.equals(kind, that.kind) && Objects.equals(resourceNames, that.resourceNames)
82+
&& Objects.equals(statusPatch, that.statusPatch)
83+
&& Objects.equals(statusPatchTemplate, that.statusPatchTemplate);
7984
}
8085

8186
@Override
8287
public int hashCode() {
83-
return Objects.hash(name, apiVersion, kind, clusterScoped, namespace, resourceNames);
88+
return Objects.hash(name, namespace, apiVersion, kind, clusterScoped, resourceNames,
89+
statusPatch, statusPatchTemplate);
8490
}
8591

8692
public boolean isClusterScoped() {
@@ -90,4 +96,20 @@ public boolean isClusterScoped() {
9096
public void setClusterScoped(boolean clusterScoped) {
9197
this.clusterScoped = clusterScoped;
9298
}
99+
100+
public Object getStatusPatch() {
101+
return statusPatch;
102+
}
103+
104+
public void setStatusPatch(Object statusPatch) {
105+
this.statusPatch = statusPatch;
106+
}
107+
108+
public String getStatusPatchTemplate() {
109+
return statusPatchTemplate;
110+
}
111+
112+
public void setStatusPatchTemplate(String statusPatchTemplate) {
113+
this.statusPatchTemplate = statusPatchTemplate;
114+
}
93115
}

src/main/java/io/javaoperatorsdk/operator/glue/reconciler/glue/GlueReconciler.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import io.fabric8.kubernetes.client.KubernetesClientException;
1111
import io.fabric8.kubernetes.client.dsl.base.PatchContext;
1212
import io.fabric8.kubernetes.client.dsl.base.PatchType;
13+
import io.fabric8.kubernetes.client.utils.Serialization;
1314
import io.javaoperatorsdk.operator.api.reconciler.*;
1415
import io.javaoperatorsdk.operator.glue.Utils;
1516
import io.javaoperatorsdk.operator.glue.conditions.JavaScripCondition;
@@ -47,17 +48,19 @@ public class GlueReconciler implements Reconciler<Glue>, Cleaner<Glue>, ErrorSta
4748

4849
private final ValidationAndErrorHandler validationAndErrorHandler;
4950
private final InformerRegister informerRegister;
51+
private final GenericTemplateHandler templateHandler;
5052

5153
private final KubernetesResourceDeletedCondition deletePostCondition =
5254
new KubernetesResourceDeletedCondition();
5355

5456
private final GenericTemplateHandler genericTemplateHandler;
5557

5658
public GlueReconciler(ValidationAndErrorHandler validationAndErrorHandler,
57-
InformerRegister informerRegister,
59+
InformerRegister informerRegister, GenericTemplateHandler templateHandler,
5860
GenericTemplateHandler genericTemplateHandler) {
5961
this.validationAndErrorHandler = validationAndErrorHandler;
6062
this.informerRegister = informerRegister;
63+
this.templateHandler = templateHandler;
6164
this.genericTemplateHandler = genericTemplateHandler;
6265
}
6366

@@ -89,6 +92,7 @@ public UpdateControl<Glue> reconcile(Glue primary,
8992
cleanupRemovedResourcesFromWorkflow(context, primary);
9093
informerRegister.deRegisterInformerOnResourceFlowChange(context, primary);
9194
result.throwAggregateExceptionIfErrorsPresent();
95+
patchRelatedResourcesStatus(context, primary);
9296
return UpdateControl.noUpdate();
9397
}
9498

@@ -222,6 +226,35 @@ private GenericDependentResource createDependentResource(DependentResourceSpec s
222226
}
223227
}
224228

229+
// todo add workflow result?
230+
private void patchRelatedResourcesStatus(Context<Glue> context,
231+
Glue primary) {
232+
233+
var targetRelatedResources = primary.getSpec().getRelatedResources().stream()
234+
.filter(r -> r.getStatusPatch() != null || r.getStatusPatchTemplate() != null)
235+
.toList();
236+
237+
if (!targetRelatedResources.isEmpty()) {
238+
return;
239+
}
240+
var actualData = genericTemplateHandler.createDataWithResources(primary, context);
241+
242+
targetRelatedResources.forEach(r -> {
243+
var relatedResources = Utils.getRelatedResources(primary, r, context);
244+
245+
var template = r.getStatusPatchTemplate() != null ? r.getStatusPatchTemplate()
246+
: Serialization.asYaml(r.getStatusPatch());
247+
var resultTemplate =
248+
genericTemplateHandler.processTemplate(actualData, template);
249+
var statusObjectMap = GenericTemplateHandler.parseTemplateToMapObject(resultTemplate);
250+
relatedResources.forEach((n, kr) -> {
251+
kr.setAdditionalProperty("status", statusObjectMap);
252+
context.getClient().resource(kr).patchStatus();
253+
});
254+
});
255+
256+
}
257+
225258
@SuppressWarnings({"rawtypes"})
226259
private Condition toCondition(ConditionSpec condition) {
227260
if (condition instanceof ReadyConditionSpec readyConditionSpec) {

src/main/java/io/javaoperatorsdk/operator/glue/templating/GenericTemplateHandler.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,26 @@ public String processTemplate(Map<String, Map<?, ?>> data, String template) {
2929

3030
public String processInputAndTemplate(Map<String, GenericKubernetesResource> data,
3131
String template) {
32-
Map<String, Map<?, ?>> res = new HashMap<>();
33-
data.forEach((key, value) -> res.put(key,
34-
value == null ? null : objectMapper.convertValue(value, Map.class)));
32+
Map<String, Map<?, ?>> res =
33+
genericKubernetesResourceDataToGenericData(data);
3534
return processTemplate(res, template);
3635
}
3736

3837
public String processTemplate(String template, Glue primary,
3938
Context<Glue> context) {
40-
4139
var data = createDataWithResources(primary, context);
4240
return processTemplate(data, template);
4341
}
4442

45-
private static Map<String, Map<?, ?>> createDataWithResources(Glue primary,
43+
private static Map<String, Map<?, ?>> genericKubernetesResourceDataToGenericData(
44+
Map<String, GenericKubernetesResource> data) {
45+
Map<String, Map<?, ?>> res = new HashMap<>();
46+
data.forEach((key, value) -> res.put(key,
47+
value == null ? null : objectMapper.convertValue(value, Map.class)));
48+
return res;
49+
}
50+
51+
public static Map<String, Map<?, ?>> createDataWithResources(Glue primary,
4652
Context<Glue> context) {
4753
Map<String, Map<?, ?>> res = new HashMap<>();
4854
var actualResourcesByName = Utils.getActualResourcesByNameInWorkflow(context, primary);
@@ -55,4 +61,11 @@ public String processTemplate(String template, Glue primary,
5561

5662
return res;
5763
}
64+
65+
// todo unit test
66+
@SuppressWarnings("unchecked")
67+
public static Map<String, ?> parseTemplateToMapObject(String template) {
68+
return objectMapper.convertValue(template, Map.class);
69+
}
70+
5871
}

0 commit comments

Comments
 (0)