Skip to content

Commit 12f8122

Browse files
authored
fix: error message handling issues (#162)
Signed-off-by: Attila Mészáros <[email protected]>
1 parent c3e2526 commit 12f8122

File tree

4 files changed

+86
-23
lines changed

4 files changed

+86
-23
lines changed

Diff for: src/main/java/io/javaoperatorsdk/operator/glue/reconciler/ValidationAndErrorHandler.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
@Singleton
2323
public class ValidationAndErrorHandler {
2424

25+
public static final int MAX_MESSAGE_SIZE = 150;
26+
2527
private static final Logger log = LoggerFactory.getLogger(ValidationAndErrorHandler.class);
2628

2729
public static final String NON_UNIQUE_NAMES_FOUND_PREFIX = "Non unique names found: ";
@@ -37,7 +39,11 @@ public class ValidationAndErrorHandler {
3739
.setErrorMessage(NON_UNIQUE_NAMES_FOUND_PREFIX + String.join(",", ex.getDuplicates()));
3840
return ErrorStatusUpdateControl.updateStatus(resource).withNoRetry();
3941
} else {
40-
resource.getStatus().setErrorMessage("Error during reconciliation");
42+
var message = e.getMessage();
43+
if (message.length() > MAX_MESSAGE_SIZE) {
44+
message = message.substring(0, MAX_MESSAGE_SIZE) + "...";
45+
}
46+
resource.getStatus().setErrorMessage("Error: " + message);
4147
return ErrorStatusUpdateControl.updateStatus(resource);
4248
}
4349
}

Diff for: src/main/java/io/javaoperatorsdk/operator/glue/reconciler/glue/GlueReconciler.java

+29-21
Original file line numberDiff line numberDiff line change
@@ -93,17 +93,7 @@ public UpdateControl<Glue> reconcile(Glue primary,
9393
informerRegister.deRegisterInformerOnResourceFlowChange(context, primary);
9494
result.throwAggregateExceptionIfErrorsPresent();
9595
patchRelatedResourcesStatus(context, primary);
96-
return UpdateControl.noUpdate();
97-
}
98-
99-
private boolean deletedGlueIfParentMarkedForDeletion(Context<Glue> context, Glue primary) {
100-
var parent = getParentRelatedResource(primary, context);
101-
if (parent.map(HasMetadata::isMarkedForDeletion).orElse(false)) {
102-
context.getClient().resource(primary).delete();
103-
return true;
104-
} else {
105-
return false;
106-
}
96+
return removeErrorMessageFromGlueStatusIfPresent(primary);
10797
}
10898

10999
@Override
@@ -128,6 +118,34 @@ public DeleteControl cleanup(Glue primary, Context<Glue> context) {
128118
}
129119
}
130120

121+
@Override
122+
public ErrorStatusUpdateControl<Glue> updateErrorStatus(Glue resource, Context<Glue> context,
123+
Exception e) {
124+
if (resource.getStatus() == null) {
125+
resource.setStatus(new GlueStatus());
126+
}
127+
return validationAndErrorHandler.updateStatusErrorMessage(e, resource);
128+
}
129+
130+
private boolean deletedGlueIfParentMarkedForDeletion(Context<Glue> context, Glue primary) {
131+
var parent = getParentRelatedResource(primary, context);
132+
if (parent.map(HasMetadata::isMarkedForDeletion).orElse(false)) {
133+
context.getClient().resource(primary).delete();
134+
return true;
135+
} else {
136+
return false;
137+
}
138+
}
139+
140+
private UpdateControl<Glue> removeErrorMessageFromGlueStatusIfPresent(Glue primary) {
141+
if (primary.getStatus() != null && primary.getStatus().getErrorMessage() != null) {
142+
primary.getStatus().setErrorMessage(null);
143+
return UpdateControl.patchStatus(primary);
144+
} else {
145+
return UpdateControl.noUpdate();
146+
}
147+
}
148+
131149
private void registerRelatedResourceInformers(Context<Glue> context,
132150
Glue glue) {
133151
glue.getSpec().getRelatedResources().forEach(r -> {
@@ -234,7 +252,6 @@ private GenericDependentResource createDependentResource(DependentResourceSpec s
234252
}
235253
}
236254

237-
// todo add workflow result?
238255
private void patchRelatedResourcesStatus(Context<Glue> context,
239256
Glue primary) {
240257

@@ -345,15 +362,6 @@ private String parentFinalizer(String glueName) {
345362
return PARENT_GLUE_FINALIZER_PREFIX + glueName;
346363
}
347364

348-
@Override
349-
public ErrorStatusUpdateControl<Glue> updateErrorStatus(Glue resource, Context<Glue> context,
350-
Exception e) {
351-
if (resource.getStatus() == null) {
352-
resource.setStatus(new GlueStatus());
353-
}
354-
return validationAndErrorHandler.updateStatusErrorMessage(e, resource);
355-
}
356-
357365
public static boolean isGlueOfAGlueOperator(Glue glue) {
358366
var labelValue =
359367
glue.getMetadata().getLabels().get(GlueOperatorReconciler.FOR_GLUE_OPERATOR_LABEL_KEY);

Diff for: src/test/java/io/javaoperatorsdk/operator/glue/GlueTest.java

+27-1
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,37 @@ void simpleBulk() {
380380
});
381381

382382
delete(glue);
383-
384383
await().untilAsserted(
385384
() -> assertThat(getRelatedList(ConfigMap.class, glue.getMetadata().getName()).isEmpty()));
386385
}
387386

387+
@Test
388+
void invalidGlueMessageHandling() {
389+
var glueName = "invalid-glue";
390+
var glue = createGlue("/glue/Invalid.yaml");
391+
392+
await().pollDelay(INITIAL_RECONCILE_WAIT_TIMEOUT).untilAsserted(() -> {
393+
var g = get(Glue.class, glueName);
394+
assertThat(g.getStatus()).isNotNull();
395+
assertThat(g.getStatus().getErrorMessage()).isNotNull();
396+
});
397+
398+
// fix error
399+
glue.getSpec().getChildResources().get(1).setName("configMap2");
400+
glue.getMetadata().setResourceVersion(null);
401+
update(glue);
402+
403+
await().pollDelay(INITIAL_RECONCILE_WAIT_TIMEOUT).untilAsserted(() -> {
404+
var g = get(Glue.class, glueName);
405+
assertThat(g.getStatus().getErrorMessage()).isNull();
406+
});
407+
408+
delete(glue);
409+
await().pollDelay(INITIAL_RECONCILE_WAIT_TIMEOUT).untilAsserted(() -> {
410+
var g = get(Glue.class, glueName);
411+
assertThat(g).isNull();
412+
});
413+
}
388414

389415

390416
private List<Glue> testWorkflowList(int num) {

Diff for: src/test/resources/glue/Invalid.yaml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Invalid GLUE, presents resources with non-unique name
2+
apiVersion: io.javaoperatorsdk.operator.glue/v1beta1
3+
kind: Glue
4+
metadata:
5+
name: invalid-glue
6+
spec:
7+
childResources:
8+
- name: configMap
9+
resource:
10+
apiVersion: v1
11+
kind: ConfigMap
12+
metadata:
13+
name: simple-glue-configmap1
14+
data:
15+
key: "value1"
16+
- name: configMap # invalid: duplicate name
17+
resource:
18+
apiVersion: v1
19+
kind: ConfigMap
20+
metadata:
21+
name: simple-glue-configmap2
22+
data:
23+
key: "value2"

0 commit comments

Comments
 (0)