Skip to content

Commit e422f83

Browse files
Handle AlreadyExists error on EventType create (#4527)
Co-authored-by: Christoph Stäbler <cstabler@redhat.com>
1 parent 01e7e31 commit e422f83

1 file changed

Lines changed: 26 additions & 1 deletion

File tree

‎data-plane/core/src/main/java/dev/knative/eventing/kafka/broker/core/eventtype/EventTypeCreatorImpl.java‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,32 @@ public Future<EventType> create(
9292
.withSchema(event.getDataSchema())
9393
.withDescription("Event Type auto-created by controller");
9494

95-
return this.eventTypeClient.resource(et.build()).create();
95+
try {
96+
return this.eventTypeClient.resource(et.build()).create();
97+
} catch (io.fabric8.kubernetes.client.KubernetesClientException e) {
98+
// Handle race condition where EventType was created between cache check and creation attempt
99+
// Check for HTTP 409 (Conflict) status code or "AlreadyExists" reason
100+
final boolean isAlreadyExists = (e.getCode() == 409)
101+
|| (e.getStatus() != null
102+
&& "AlreadyExists".equals(e.getStatus().getReason()));
103+
104+
if (isAlreadyExists) {
105+
logger.debug("EventType {} already exists (race condition), fetching existing resource", name);
106+
// Fetch the existing EventType instead of failing
107+
final var existing = this.eventTypeClient
108+
.inNamespace(ownerReference.getNamespace())
109+
.withName(name)
110+
.get();
111+
if (existing != null) {
112+
return existing;
113+
}
114+
// If we can't fetch it, fall back to the original exception
115+
logger.warn(
116+
"EventType {} already exists but could not fetch it, falling back to original error", name);
117+
}
118+
// Re-throw the original exception if it's not an AlreadyExists error or we couldn't handle it
119+
throw e;
120+
}
96121
});
97122
}
98123
}

0 commit comments

Comments
 (0)