Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webhook implementation #1373

Merged
merged 11 commits into from
Nov 21, 2024
92 changes: 69 additions & 23 deletions design/BulkAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ progress of the job.
"experiment_types": [
"container",
"namespace"
]
],
"webhook": {
"url" : "http://127.0.0.1:8080/webhook"
}
}
```

Expand All @@ -75,6 +78,9 @@ progress of the job.

- **experiment_types:** Specifies the type(s) of experiments to run, e.g., `"container"` or `"namespace"`.

- **webhook:** The `webhook` parameter allows the system to notify an external service or consumer about the completion status of
an experiment-processing job. Once a job is completed, this webhook will be triggered to send an HTTP request to the URL defined in the bulk request payload.

### Success Response

- **Status:** 200 OK
Expand Down Expand Up @@ -119,7 +125,10 @@ GET /bulk?job_id=123e4567-e89b-12d3-a456-426614174000
"processed_experiments": 23,
"job_id": "54905959-77d4-42ba-8e06-90bb97b823b9",
"job_start_time": "2024-10-10T06:07:09.066Z",
"job_end_time": "2024-10-10T06:07:17.471Z"
"job_end_time": "2024-10-10T06:07:17.471Z",
"webhook": {
"status": "COMPLETED"
}
dinogun marked this conversation as resolved.
Show resolved Hide resolved
}
```

Expand Down Expand Up @@ -189,7 +198,7 @@ example 1:
}
```

example 2:
example 2: Job failed

```json
{
Expand All @@ -205,11 +214,35 @@ example 2:
},
"job_id": "270fa4d9-2701-4ca0-b056-74229cc28498",
"job_start_time": "2024-11-12T15:05:46.362Z",
"job_end_time": "2024-11-12T15:06:05.301Z"
"job_end_time": "2024-11-12T15:06:05.301Z",
"webhook": {
"status": "COMPLETED"
}
}

```

example 3: Only Webhook failed

```json
{
"status": "COMPLETED",
"total_experiments": 23,
"processed_experiments": 23,
"job_id": "54905959-77d4-42ba-8e06-90bb97b823b9",
"job_start_time": "2024-10-10T06:07:09.066Z",
"job_end_time": "2024-10-10T06:07:17.471Z",
"webhook": {
"status": "FAILED",
"notifications": {
"type": "ERROR",
"message": "HttpHostConnectException: Unable to connect to the webhook. Please try again later.",
"code": 503
}
}
}
```

### Response Parameters

## API Description: Experiment and Recommendation Processing Status
Expand Down Expand Up @@ -275,6 +308,18 @@ resource optimization in Kubernetes environments. Below is a breakdown of the JS
- **Type**: `String (ISO 8601 format) or null`
- **Description**: End timestamp of the job. If the job is still in progress, this will be `null`.

- **webhook**:
- **Type**: `Object`
- **Description**: An object that provides details about the webhook status and any errors encountered during the
webhook invocation.

- The `webhook` parameter allows the system to notify an external service or consumer about the completion status of
an experiment-processing job. Once a job is completed, this webhook will be triggered to send an HTTP request to the URL defined in the bulk request payload.
This notification mechanism is essential for systems that require real-time updates about the job's processing
status, enabling consumers to take immediate follow-up actions. For example, an external analytics dashboard, a
monitoring service, or a message queue like Kafka can listen for these webhook calls to further process or log the
job completion data.

**Note: Experiment Name:**

- **Naming Pattern:** Experiment names are currently formed using the following pattern:
Expand All @@ -300,27 +345,27 @@ resource optimization in Kubernetes environments. Below is a breakdown of the JS
apiVersion: v1
kind: ConfigMap
metadata:
name: kruizeconfig
namespace: openshift-tuning
name: kruizeconfig
namespace: openshift-tuning
data:
kruizeconfigjson: |
{
"datasource": [
{
"name": "prometheus-1",
"provider": "prometheus",
"serviceName": "prometheus-k8s",
"namespace": "openshift-monitoring",
"url": "",
"authentication": {
"type": "bearer",
"credentials": {
"tokenFilePath": "/var/run/secrets/kubernetes.io/serviceaccount/token"
kruizeconfigjson: |
{
"datasource": [
{
"name": "prometheus-1",
"provider": "prometheus",
"serviceName": "prometheus-k8s",
"namespace": "openshift-monitoring",
"url": "",
"authentication": {
"type": "bearer",
"credentials": {
"tokenFilePath": "/var/run/secrets/kubernetes.io/serviceaccount/token"
}
}
}
}
]
}
]
}
```

## Limits
Expand Down Expand Up @@ -395,4 +440,5 @@ number of labels, or none at all. Here are some examples:
- "%datasource%|%clustername%|%namespace%|%workloadname%(%workloadtype%)|%containername%" -> Default
- "%label:org_id%|%label:source_id%|%label:cluster_id%|%namespace%|%workloadtype%|%workloadname%|%containername%"
- "%label:org_id%|%namespace%|%workloadtype%|%workloadname%|%containername%"
- "%label:org_id%|%label:cluster_id%|%namespace%|%workloadtype%|%workloadname%"
- "%label:org_id%|%label:cluster_id%|%namespace%|%workloadtype%|%workloadname%"

Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ data:
"local": "true",
"logAllHttpReqAndResp": "true",
"recommendationsURL" : "http://kruize.monitoring.svc.cluster.local:8080/generateRecommendations?experiment_name=%s",
"experimentsURL" : "http://kruize.monitoring.svc.cluster.local:8080/createExperiment",
"experimentsURL" : "http://kruize.monitoring.svc.cluster.local:8080/createExperiment",
"hibernate": {
"dialect": "org.hibernate.dialect.PostgreSQLDialect",
"driver": "org.postgresql.Driver",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ data:
"local": "true",
"logAllHttpReqAndResp": "true",
"recommendationsURL" : "http://kruize.monitoring.svc.cluster.local:8080/generateRecommendations?experiment_name=%s",
"experimentsURL" : "http://kruize.monitoring.svc.cluster.local:8080/createExperiment",
"experimentsURL" : "http://kruize.monitoring.svc.cluster.local:8080/createExperiment",
"experimentNameFormat" : "%datasource%|%clustername%|%namespace%|%workloadname%(%workloadtype%)|%containername%",
"bulkapilimit" : 1000,
"hibernate": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ data:
"local": "true",
"logAllHttpReqAndResp": "true",
"recommendationsURL" : "http://kruize.monitoring.svc.cluster.local:8080/generateRecommendations?experiment_name=%s",
"experimentsURL" : "http://kruize.monitoring.svc.cluster.local:8080/createExperiment",
"experimentsURL" : "http://kruize.monitoring.svc.cluster.local:8080/createExperiment",
"experimentNameFormat" : "%datasource%|%clustername%|%namespace%|%workloadname%(%workloadtype%)|%containername%",
"bulkapilimit" : 1000,
"hibernate": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ data:
"namespace": "openshift-monitoring",
"url": "",
"authentication": {
"type": "bearer",
"credentials": {
"tokenFilePath": "/var/run/secrets/kubernetes.io/serviceaccount/token"
}
"type": "bearer",
"credentials": {
"tokenFilePath": "/var/run/secrets/kubernetes.io/serviceaccount/token"
}
}
}
]
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/autotune/analyzer/serviceObjects/BulkInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class BulkInput {
private FilterWrapper filter;
private TimeRange time_range;
private String datasource;
private Webhook webhook;

// Getters and Setters

Expand Down Expand Up @@ -136,4 +137,22 @@ public void setEnd(String end) {
this.end = end;
}
}

public static class Webhook{
private String url;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}

public Webhook getWebhook() {
return webhook;
}

public void setWebhook(Webhook webhook) {
this.webhook = webhook;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class BulkJobStatus {
private String endTime; // Change to String to store formatted time
private Map<String, Notification> notifications;
private Map<String, Experiment> experiments = Collections.synchronizedMap(new HashMap<>());
private Webhook webhook;

public BulkJobStatus(String jobID, String status, Instant startTime) {
this.jobID = jobID;
Expand Down Expand Up @@ -112,6 +113,14 @@ public void setExperiments(Map<String, Experiment> experiments) {
this.experiments = experiments;
}

public Webhook getWebhook() {
return webhook;
}

public void setWebhook(Webhook webhook) {
this.webhook = webhook;
}

// Method to add a new experiment with "unprocessed" status and null notification
public synchronized Experiment addExperiment(String experimentName) {
Experiment experiment = new Experiment(experimentName);
Expand Down Expand Up @@ -188,6 +197,14 @@ public Recommendation getRecommendations() {
public void setNotification(Notification notification) {
this.notification = notification;
}

public Notification getNotification() {
return notification;
}

public void setRecommendations(Recommendation recommendations) {
this.recommendations = recommendations;
}
}

public static class Recommendation {
Expand Down Expand Up @@ -255,5 +272,29 @@ public void setCode(int code) {
}
}

public static class Webhook {
private KruizeConstants.KRUIZE_BULK_API.NotificationConstants.WebHookStatus status;
private Notification notifications; // Notifications can hold multiple entries

public Webhook(KruizeConstants.KRUIZE_BULK_API.NotificationConstants.WebHookStatus status) {
this.status = status;
}

public KruizeConstants.KRUIZE_BULK_API.NotificationConstants.WebHookStatus getStatus() {
return status;
}

public void setStatus(KruizeConstants.KRUIZE_BULK_API.NotificationConstants.WebHookStatus status) {
this.status = status;
}

public Notification getNotifications() {
return notifications;
}

public void setNotifications(Notification notifications) {
this.notifications = notifications;
}
}

}
}
9 changes: 9 additions & 0 deletions src/main/java/com/autotune/analyzer/services/BulkService.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
boolean verbose = verboseParam != null && Boolean.parseBoolean(verboseParam);
BulkJobStatus jobDetails;
LOGGER.info("Job ID: " + jobID);
if (jobStatusMap.isEmpty()) {
sendErrorResponse(
resp,
null,
HttpServletResponse.SC_NOT_FOUND,
JOB_NOT_FOUND_MSG
);
return;
}
jobDetails = jobStatusMap.get(jobID);
LOGGER.info("Job Status: " + jobDetails.getStatus());
resp.setContentType(JSON_CONTENT_TYPE);
Expand Down
Loading
Loading