Skip to content

Commit 025aed0

Browse files
authored
Fix exception when reconciling object deletion (#147)
1 parent 076a544 commit 025aed0

File tree

5 files changed

+36
-24
lines changed

5 files changed

+36
-24
lines changed

hoptimator-k8s/src/main/java/com/linkedin/hoptimator/k8s/K8sUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ static void checkResponse(Supplier<String> msgSupplier, KubernetesApiResponse<?>
103103
case 409: // conflict
104104
case 410: // gone
105105
case 412: // precondition failed
106-
throw new SQLTransientException(msgSupplier.get(), e);
106+
throw new SQLTransientException(msgSupplier.get(), null, resp.getHttpStatusCode(), e);
107107
default:
108-
throw new SQLNonTransientException(msgSupplier.get(), e);
108+
throw new SQLNonTransientException(msgSupplier.get(), null, resp.getHttpStatusCode(), e);
109109
}
110110
}
111111
}

hoptimator-k8s/src/testFixtures/java/com/linkedin/hoptimator/k8s/FakeK8sApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public Collection<T> list() throws SQLException {
2626
@Override
2727
public T get(String name) throws SQLException {
2828
return objects.stream().filter(x -> x.getMetadata().getName().equals(name)).findFirst()
29-
.orElseThrow(() -> new SQLException("No object named " + name));
29+
.orElseThrow(() -> new SQLException("No object named " + name, null, 404));
3030
}
3131

3232
@Override

hoptimator-operator/src/main/java/com/linkedin/hoptimator/operator/pipeline/PipelineReconciler.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,15 @@ public Result reconcile(Request request) {
4646

4747
Result result = new Result(true, pendingRetryDuration());
4848
try {
49-
V1alpha1Pipeline object = pipelineApi.get(namespace, name);
50-
51-
if (object == null) {
52-
log.info("Object {} deleted. Skipping.", name);
53-
return new Result(false);
49+
V1alpha1Pipeline object;
50+
try {
51+
object = pipelineApi.get(namespace, name);
52+
} catch (SQLException e) {
53+
if (e.getErrorCode() == 404) {
54+
log.info("Object {} deleted. Skipping.", name);
55+
return new Result(false);
56+
}
57+
throw e;
5458
}
5559

5660
V1alpha1PipelineStatus status = object.getStatus();
@@ -79,13 +83,6 @@ public Result reconcile(Request request) {
7983

8084
pipelineApi.updateStatus(object, status);
8185
} catch (Exception e) {
82-
if (e instanceof SQLException) {
83-
SQLException sqlException = (SQLException) e;
84-
if (sqlException.getErrorCode() == 404) {
85-
log.info("Object {} deleted. Skipping.", name);
86-
return new Result(false);
87-
}
88-
}
8986
log.error("Encountered exception while reconciling Pipeline {}.", name, e);
9087
return new Result(true, failureRetryDuration());
9188
}

hoptimator-operator/src/main/java/com/linkedin/hoptimator/operator/trigger/TableTriggerReconciler.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private TableTriggerReconciler(K8sContext context) {
8282
this.tableTriggerApi = tableTriggerApi;
8383
this.jobApi = jobApi;
8484
this.yamlApi = yamlApi;
85-
}
85+
}
8686

8787
@Override
8888
public Result reconcile(Request request) {
@@ -91,11 +91,15 @@ public Result reconcile(Request request) {
9191
String namespace = request.getNamespace();
9292

9393
try {
94-
V1alpha1TableTrigger object = tableTriggerApi.get(namespace, name);
95-
96-
if (object == null) {
97-
log.info("Object {} deleted. Skipping.", name);
98-
return new Result(false);
94+
V1alpha1TableTrigger object;
95+
try {
96+
object = tableTriggerApi.get(namespace, name);
97+
} catch (SQLException e) {
98+
if (e.getErrorCode() == 404) {
99+
log.info("Object {} deleted. Skipping.", name);
100+
return new Result(false);
101+
}
102+
throw e;
99103
}
100104

101105
V1alpha1TableTriggerStatus status = object.getStatus();

hoptimator-operator/src/test/java/com/linkedin/hoptimator/operator/trigger/TestTableTriggerReconciler.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.linkedin.hoptimator.operator.trigger;
22

3+
import io.kubernetes.client.extended.controller.reconciler.Result;
34
import java.time.OffsetDateTime;
45
import java.util.ArrayList;
56
import java.util.HashMap;
@@ -43,6 +44,13 @@ void beforeEach() {
4344
yamls.clear();
4445
}
4546

47+
@Test
48+
void deletedJob() {
49+
Result result = reconciler.reconcile(new Request("namespace", "table-trigger"));
50+
Assertions.assertFalse(result.isRequeue());
51+
Assertions.assertTrue(yamls.isEmpty(), "Job should not exist");
52+
}
53+
4654
@Test
4755
void createsNewJob() {
4856
V1Job job = new V1Job().apiVersion("v1/batch").kind("Job")
@@ -51,7 +59,8 @@ void createsNewJob() {
5159
.metadata(new V1ObjectMeta().name("table-trigger"))
5260
.spec(new V1alpha1TableTriggerSpec().yaml(Yaml.dump(job)))
5361
.status(new V1alpha1TableTriggerStatus().timestamp(OffsetDateTime.now())));
54-
reconciler.reconcile(new Request("namespace", "table-trigger"));
62+
Result result = reconciler.reconcile(new Request("namespace", "table-trigger"));
63+
Assertions.assertTrue(result.isRequeue());
5564
Assertions.assertFalse(yamls.isEmpty(), "Job was not created");
5665
}
5766

@@ -66,7 +75,8 @@ void deletesCompletedJob() {
6675
.status(new V1alpha1TableTriggerStatus().timestamp(OffsetDateTime.now()));
6776
triggers.add(trigger);
6877
jobs.add(job);
69-
reconciler.reconcile(new Request("namespace", "table-trigger"));
78+
Result result = reconciler.reconcile(new Request("namespace", "table-trigger"));
79+
Assertions.assertTrue(result.isRequeue());
7080
Assertions.assertTrue(jobs.isEmpty(), "Job was not deleted");
7181
}
7282

@@ -83,7 +93,8 @@ void updatesWatermark() {
8393
.status(new V1alpha1TableTriggerStatus().timestamp(OffsetDateTime.now()));
8494
triggers.add(trigger);
8595
jobs.add(job);
86-
reconciler.reconcile(new Request("namespace", "table-trigger"));
96+
Result result = reconciler.reconcile(new Request("namespace", "table-trigger"));
97+
Assertions.assertTrue(result.isRequeue());
8798
Assertions.assertNotNull(trigger.getStatus().getWatermark(), "Watermark was not set");
8899
}
89100
}

0 commit comments

Comments
 (0)