Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into implement-problem-det…
Browse files Browse the repository at this point in the history
…ails
  • Loading branch information
olevitt committed Oct 8, 2024
2 parents 3f8246b + f61c0b3 commit 631e4b7
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 11 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,13 @@ Configurable properties :
| `http.proxyPassword` | | Password if the proxy requires authentication |

### Events
| Key | Default | Description |
| --------------------- |---------|----------------------------------------|
| `event.logging.enabled` | `true` | whether events should be logged or not |
| Key | Default | Description |
|--------------------------|--|--------------------------------------------------------------------|
| `event.logging.enabled` | `true` | whether events should be logged or not |
| `event.webhook.enabled` | `false` | whether events should be sent to an external webhook via HTTP POST |
| `event.webhook.url` | | URL of the webhook to send the events to |
| `event.webhook.includes` | | List of events types to send the webhook for (empty = all events). e.g `service.uninstall,service.install` |
| `event.webhook.excludes` | | List of events types to ignore for the webhook. e.g `service.uninstall,service.install` |

### Other configurations
| Key | Default | Description |
Expand Down
8 changes: 6 additions & 2 deletions onyxia-api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
FROM eclipse-temurin:21.0.4_7-jre as extract
FROM eclipse-temurin:21.0.4_7-jre AS extract
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract

FROM eclipse-temurin:21.0.4_7-jre
WORKDIR /app
# Install helm
RUN curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | DESIRED_VERSION=v3.15.3 bash
RUN wget https://get.helm.sh/helm-v3.16.1-linux-amd64.tar.gz
RUN tar -zxvf helm-v3.16.1-linux-amd64.tar.gz
RUN mv linux-amd64/helm /usr/local/bin/helm
RUN rm helm-v3.16.1-linux-amd64.tar.gz
RUN rm -rf linux-amd64
RUN groupadd --gid 101 --system onyxia && \
useradd --system --uid 101 --create-home --home-dir /var/cache/onyxia --shell /sbin/nologin --gid onyxia --comment onyxia onyxia
# Allow adding CA certificates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,20 @@ public void eventReceived(Action action, Event event) {

@Override
public void onClose() {
emitter.complete();
try {
emitter.complete();
} catch (Exception ignored) {

}
}

@Override
public void onClose(WatcherException e) {
emitter.complete();
try {
emitter.complete();
} catch (Exception ignored) {

}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package fr.insee.onyxia.api.events;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

@EnableAsync
@Component
@ConditionalOnProperty(name = "event.webhook.enabled", havingValue = "true")
public class WebhookEventListener {

private final ObjectMapper objectMapper;

private static final MediaType JSON = MediaType.get("application/json");

private static final Logger LOGGER = LoggerFactory.getLogger(WebhookEventListener.class);

@Value("${event.webhook.url}")
private String url;

@Value("${event.webhook.includes}")
private List<String> includes = new ArrayList<>();

@Value("${event.webhook.excludes}")
private List<String> excludes = new ArrayList<>();

private final OkHttpClient httpClient;

@Autowired
public WebhookEventListener(ObjectMapper objectMapper, OkHttpClient okHttpClient) {
this.objectMapper = objectMapper;
this.httpClient = okHttpClient;
}

@Async
@EventListener
public void onOnyxiaEvent(OnyxiaEvent onyxiaEvent) throws JsonProcessingException {
if (!includes.isEmpty() && !includes.contains(onyxiaEvent.getType())) {
return;
}
if (!excludes.isEmpty() && excludes.contains(onyxiaEvent.getType())) {
return;
}
RequestBody body = RequestBody.create(objectMapper.writeValueAsString(onyxiaEvent), JSON);
Request request = new Request.Builder().url(url).post(body).build();
try {
httpClient.newCall(request).execute();
} catch (IOException e) {
LOGGER.warn("Failure while sending event to webhook, will not be retried", e);
}
}

public ObjectMapper getObjectMapper() {
return objectMapper;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public List<String> getIncludes() {
return includes;
}

public void setIncludes(List<String> includes) {
this.includes = includes;
}

public List<String> getExcludes() {
return excludes;
}

public void setExcludes(List<String> excludes) {
this.excludes = excludes;
}

public OkHttpClient getHttpClient() {
return httpClient;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,14 @@ private static List<HealthCheckResult> checkHealth(
.inNamespace(namespace)
.withName(name)
.get();
if (deployment == null) continue;
details.setDesired(deployment.getSpec().getReplicas());
details.setReady(deployment.getStatus().getReadyReplicas());
// If replicas is 0 then readyReplicas is not defined (and can't be
// different from 0 anyway)
if (deployment.getStatus().getReplicas() > 0
&& deployment.getStatus().getReadyReplicas() != null) {
details.setReady(deployment.getStatus().getReadyReplicas());
}
break;
case "StatefulSet":
StatefulSet statefulset =
Expand All @@ -64,8 +70,14 @@ private static List<HealthCheckResult> checkHealth(
.inNamespace(namespace)
.withName(name)
.get();
if (statefulset == null) continue;
details.setDesired(statefulset.getSpec().getReplicas());
details.setReady(statefulset.getStatus().getReadyReplicas());
// If replicas is 0 then readyReplicas is not defined (and can't be
// different from 0 anyway)
if (statefulset.getStatus().getReplicas() > 0
&& statefulset.getStatus().getReadyReplicas() != null) {
details.setReady(statefulset.getStatus().getReadyReplicas());
}
break;
case "DaemonSet":
DaemonSet daemonSet =
Expand All @@ -75,8 +87,14 @@ private static List<HealthCheckResult> checkHealth(
.inNamespace(namespace)
.withName(name)
.get();
if (daemonSet == null) continue;
details.setDesired(daemonSet.getStatus().getDesiredNumberScheduled());
details.setReady(daemonSet.getStatus().getNumberReady());
// If replicas is 0 then readyReplicas is not defined (and can't be
// different from 0 anyway)
if (daemonSet.getStatus().getNumberAvailable() > 0
&& daemonSet.getStatus().getNumberReady() != null) {
details.setReady(daemonSet.getStatus().getNumberReady());
}
break;
default:
continue;
Expand Down
8 changes: 8 additions & 0 deletions onyxia-api/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,13 @@ springdoc.swagger-ui.oauth.clientId=
springdoc.swagger-ui.oauth.clientSecret=
# Enable events logging
event.logging.enabled=true
# Enable events webhook
event.webhook.enabled=false
# URL to send the events to
event.webhook.url=
# List of events types to send the webhook for (empty = all events). e.g service.uninstall,service.install
event.webhook.includes=
# List of events types to ignore for the webhook. e.g service.uninstall,service.install
event.webhook.excludes=
# Response stream configuration
spring.mvc.async.request-timeout=600000
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

</distributionManagement>
<properties>
<fabric8io.version>6.13.3</fabric8io.version>
<fabric8io.version>6.13.4</fabric8io.version>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
</properties>
Expand Down

0 comments on commit 631e4b7

Please sign in to comment.