Skip to content

Commit c28f4de

Browse files
committed
Merge branch 'main' into fix-latest-version
2 parents cc50177 + 01ad655 commit c28f4de

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+994
-34
lines changed

client/events/EventClient.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@ import (
3737
)
3838

3939
type EventClientConfig struct {
40-
DestinationURL string `env:"EVENT_URL" envDefault:"http://localhost:3000/notify"`
40+
DestinationURL string `env:"EVENT_URL" envDefault:"http://localhost:3000/notify"`
41+
NotificationMedium NotificationMedium `env:"NOTIFICATION_MEDIUM" envDefault:"rest"`
4142
}
43+
type NotificationMedium string
44+
45+
const PUB_SUB NotificationMedium = "nats"
4246

4347
func GetEventClientConfig() (*EventClientConfig, error) {
4448
cfg := &EventClientConfig{}
@@ -238,6 +242,16 @@ func (impl *EventRESTClientImpl) WriteNotificationEvent(event Event) (bool, erro
238242
}
239243
return true, err
240244
}
245+
func (impl *EventRESTClientImpl) sendEventsOnNats(body []byte) error {
246+
247+
err := impl.pubsubClient.Publish(pubsub.NOTIFICATION_EVENT_TOPIC, string(body))
248+
if err != nil {
249+
impl.logger.Errorw("err while publishing msg for testing topic", "msg", body, "err", err)
250+
return err
251+
}
252+
return nil
253+
254+
}
241255

242256
// do not call this method if notification module is not installed
243257
func (impl *EventRESTClientImpl) sendEvent(event Event) (bool, error) {
@@ -247,6 +261,14 @@ func (impl *EventRESTClientImpl) sendEvent(event Event) (bool, error) {
247261
impl.logger.Errorw("error while marshaling event request ", "err", err)
248262
return false, err
249263
}
264+
if impl.config.NotificationMedium == PUB_SUB {
265+
err = impl.sendEventsOnNats(body)
266+
if err != nil {
267+
impl.logger.Errorw("error while publishing event ", "err", err)
268+
return false, err
269+
}
270+
return true, nil
271+
}
250272
var reqBody = []byte(body)
251273
req, err := http.NewRequest(http.MethodPost, impl.config.DestinationURL, bytes.NewBuffer(reqBody))
252274
if err != nil {

client/events/event_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package client
2+
3+
import (
4+
"fmt"
5+
pubsub_lib "github.com/devtron-labs/common-lib/pubsub-lib"
6+
"github.com/devtron-labs/devtron/internal/sql/repository"
7+
"github.com/devtron-labs/devtron/internal/sql/repository/pipelineConfig"
8+
"github.com/devtron-labs/devtron/internal/util"
9+
"github.com/devtron-labs/devtron/pkg/sql"
10+
"testing"
11+
)
12+
13+
func TestSendEventsOnNats(t *testing.T) {
14+
logger, err := util.NewSugardLogger()
15+
//nats, err := pubsub_lib.NewNatsClient(logger)
16+
//mockPubsubClient := NewPubSubClientServiceImpl(logger)
17+
mockPubsubClient, err := pubsub_lib.NewPubSubClientServiceImpl(logger)
18+
client := util.NewHttpClient()
19+
config := sql.Config{}
20+
db, err := sql.NewDbConnection(&config, logger)
21+
trans := sql.NewTransactionUtilImpl(db)
22+
impl := &EventRESTClientImpl{
23+
logger: logger,
24+
pubsubClient: mockPubsubClient,
25+
client: client,
26+
config: &EventClientConfig{DestinationURL: "localhost:3000/notify", NotificationMedium: PUB_SUB},
27+
ciPipelineRepository: pipelineConfig.NewCiPipelineRepositoryImpl(db, logger, trans),
28+
pipelineRepository: pipelineConfig.NewPipelineRepositoryImpl(db, logger),
29+
attributesRepository: repository.NewAttributesRepositoryImpl(db),
30+
}
31+
//xpectedTopic := "NOTIFICATION_EVENT_TOPIC"
32+
expectedMsg := "'{\"eventTypeId\":1,\"pipelineId\":123,\"payload\":{\"key\":\"value\"},\"eventTime\":\"2024-05-09T12:00:00Z\",\"appId\":456,\"envId\":789,\"teamId\":101}'"
33+
34+
err = impl.sendEventsOnNats([]byte(expectedMsg))
35+
fmt.Println(err)
36+
37+
}

docs/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@
104104
* [Overview](user-guide/jobs/overview-job.md)
105105
* [Application Groups](user-guide/application-groups.md)
106106
* [Resource Browser](user-guide/resource-browser.md)
107+
* [Resource Watcher](user-guide/resource-watcher.md)
107108
* [Charts](user-guide/deploy-chart/README.md)
108109
* [Charts Overview](user-guide/deploy-chart/overview-of-charts.md)
109110
* [Deploy & Observe](user-guide/deploy-chart/deployment-of-charts.md)

docs/user-guide/resource-watcher.md

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# Resource Watcher
2+
3+
## Introduction [![](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/elements/EnterpriseTag.svg)](https://devtron.ai/pricing)
4+
5+
An incident response if delayed can impact businesses, revenue, and waste valuable engineering time. Devtron's Resource Watcher enables you to perform automated actions upon the occurrence of events:
6+
7+
* **Create Event** - Occurs when a new Kubernetes resource is created, for e.g., a new pod spun up to handle increased traffic.
8+
* **Update Event** - Occurs when an existing Kubernetes resource is modified, for e.g., deployment configuration tweaked to increase the replica count.
9+
* **Delete Event** - Occurs when an existing Kubernetes resource is deleted, for e.g., deletion of an orphaned pod.
10+
11+
You can make the Resource Watcher listen to the above events and accordingly run a job you wish to get done, for e.g., increasing memory, executing a script, raising Jira ticket, emailing your stakeholders, sending Slack notifications, and many more. Since manual intervention is absent, the timely response of this auto-remediation system improves your operational efficiency.
12+
13+
---
14+
15+
## Creating a Watcher
16+
17+
{% hint style="warning" %}
18+
### Who Can Perform This Action?
19+
Users need to have super-admin permission to create a watcher.
20+
{% endhint %}
21+
22+
This page allows you to create a watcher to track events and run a job. It also shows the existing list of watchers (if any).
23+
24+
1. Click **+ Create Watcher**.
25+
26+
![Figure 1: Watchers - Page](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/resource-watcher/watchers-page.jpg)
27+
28+
2. Creating a watcher consists of 4 parts, fill all the sections one by one:
29+
* [Basic Details](#basic-details)
30+
* [Namespaces to Watch](#namespaces-to-watch)
31+
* [Intercept Change in Resources](#intercept-change-in-resources)
32+
* [Execute Runbook](#execute-runbook)
33+
34+
![Figure 2: Create Watcher - Window](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/resource-watcher/create-watcher-window.jpg)
35+
36+
### Basic Details
37+
38+
Here, you can give a name and description to your watcher.
39+
40+
![Figure 3: Adding Name and Description of Watcher](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/resource-watcher/basic-details.gif)
41+
42+
### Namespaces to Watch
43+
44+
Here, you can select the [namespaces](../reference/glossary.md#namespace) whose [Kubernetes resource](../reference/glossary.md#objects) you wish to monitor for changes.
45+
46+
* You can watch the namespace(s) across **All Clusters** (existing and future).
47+
48+
![Figure 4: Choosing Namespaces of all Clusters](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/resource-watcher/all-cluster.gif)
49+
50+
* Or you can watch namespace(s) of **Specific Clusters**.
51+
52+
![Figure 5: Choosing Namespaces of Specific Clusters](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/resource-watcher/specific-cluster.gif)
53+
54+
{% hint style="info" %}
55+
In both the above options, if you choose 'Specific Namespaces', you can further decide whether to track the namespaces you enter (by clicking 'Include selections') or to track the namespaces except the ones you enter (by clicking 'Exclude selections').
56+
{% endhint %}
57+
58+
59+
### Intercept Change in Resources
60+
61+
Here, you can select the exact Kubernetes resource(s) you wish to track for changes (in the namespace(s) you selected in the previous step).
62+
63+
![Figure 6: Picking Resources to Track](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/resource-watcher/intercept-changes.gif)
64+
65+
* You can choose the resource from the **Resource kind(s) to watch** dropdown. Enter the Group/Version/Kind (GVK) if it's a custom resource definition (CRD), for e.g., `install.istio.io/v1apha1/IstioOperator`
66+
67+
* Choose the event type your watcher should listen to: `Created`, `Updated`, `Deleted`.
68+
69+
| Event Type | Description |
70+
| ---------- | ----------------------------------------------------------------------- |
71+
| Created | Triggers the watcher when your Kubernetes resource is created |
72+
| Updated | Triggers the watcher when your existing Kubernetes resource is modified |
73+
| Deleted | Triggers the watcher when your existing Kubernetes resource is deleted |
74+
75+
* Enter a [CEL expression](https://github.com/google/cel-spec/blob/master/doc/langdef.md) to catch a specific change in the resource's manifest.
76+
77+
{% hint style="info" %}
78+
* **If resource is created** - Use 'DEVTRON_FINAL_MANIFEST'
79+
* **If resource is updated** - Both 'DEVTRON_INITIAL_MANIFEST' and 'DEVTRON_FINAL_MANIFEST' can exist
80+
* **If resource is deleted** - Use 'DEVTRON_INITIAL_MANIFEST'
81+
{% endhint %}
82+
83+
**Example**: `DEVTRON_FINAL_MANIFEST.status.currentReplicas == DEVTRON_FINAL_MANIFEST.spec.maxReplicas`
84+
85+
### Execute Runbook
86+
87+
Here, you can choose a job that should trigger if your watcher intercepts any changes.
88+
89+
![Figure 7: Choosing a Job to Trigger](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/resource-watcher/execute-runbook.gif)
90+
91+
* Choose a job pipeline from the **Run Devtron Job pipeline** dropdown. If a pipeline is not selected, the watcher won't intercept matching resource changes even if your defined conditions are met.
92+
93+
* Select the environment in which the job should run. It can either be `devtron-ci` or the source environment (the intercepted namespace where the event has occurred).
94+
95+
* If the job expects input parameters, you may add its key and value under **Runtime input parameters**.
96+
97+
During a job's execution, its container can access the initial and final resource manifest through special environment variables. These variables are:
98+
* `DEVTRON_INITIAL_MANIFEST`
99+
* `DEVTRON_FINAL_MANIFEST`
100+
101+
* Click **Create Watcher**.
102+
103+
Your watcher is now ready to intercept the changes to the selected resources.
104+
105+
---
106+
107+
## Viewing Intercepted Changes
108+
109+
{% hint style="warning" %}
110+
### Who Can Perform This Action?
111+
Users need to have super-admin permission to view intercepted changes.
112+
{% endhint %}
113+
114+
### Details
115+
116+
This page allows you to view the changes to Kubernetes resources that you have selected for tracking changes.
117+
118+
![Figure 8: Intercepted Changes - Page](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/resource-watcher/intercepted-changes-page.jpg)
119+
120+
It comes with the following items to help you locate the resource, where the event has been intercepted:
121+
122+
* Searchbox
123+
* Cluster filter
124+
* Namespace filter
125+
* Action filter (event type, i.e., `Created`, `Updated`, `Deleted`)
126+
* Watcher filter (to check the intercepted changes of a specific watcher)
127+
128+
You get the following details in the results shown on the page.
129+
130+
|Field | Description |
131+
|-------|-------------|
132+
|[Change In Resource](#change-in-resource)|Describes the type of change to the Kubernetes resource along with a link to its manifest|
133+
|[Cluster/Namespace](#namespaces-to-watch) |Shows the cluster and namespace where the tracked Kubernetes resource belongs to|
134+
|Intercepted By |Shows the name of the watcher that intercepted the change|
135+
|Intercepted At |Shows the date and time when the event occurred |
136+
|[Job Execution](#execute-runbook) |Shows the status of the execution of job, e.g., `In Progress`, `Succeeded`, `Failed`|
137+
|[Logs](#job-execution-log) |Links to the job log, i.e, the `Run history` page of the job|
138+
139+
### Change in Resource
140+
141+
You can check the changes in manifest by clicking **View Manifest** in `Change In Resource` column.
142+
143+
![Figure 9a: Created Resource Manifest - Final Manifest](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/resource-watcher/view-manifest-v1.gif)
144+
145+
146+
![Figure 9b: Updated Resource - Initial and Final Manifest](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/resource-watcher/view-manifest-v2.gif)
147+
148+
149+
![Figure 9c: Deleted Resource - Initial Manifest](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/resource-watcher/view-manifest.gif)
150+
151+
### Job Execution Log
152+
153+
You can check the logs of the job executed when the Resource Watcher intercepts any change by clicking **logs**.
154+
155+
![Figure 10: Job Progress](https://devtron-public-asset.s3.us-east-2.amazonaws.com/images/resource-watcher/job-exec-log.gif)
156+
157+
---
158+
159+
## Use Cases
160+
161+
### Live Stream Traffic Surge
162+
163+
A live streaming sports application experiences a surge in viewers during a major game. The Horizontal Pod Autoscaler (HPA) might not be able to handle the unexpected traffic if it's capped at a low max replica count.
164+
165+
1. Create a watcher named 'Live Stream Scaling Alert'.
166+
2. Monitor updates to HPA resource in the application's namespace.
167+
3. When `currentReplicas` count reaches `maxReplicas`, trigger a job that contains the script to increase the replica count.
168+
169+
### Pod Health Monitoring
170+
171+
A stock trading application constantly updates stock prices for its traders. If the pods become unhealthy, traders might see incorrect stock prices leading to bad investments.
172+
173+
1. Create a watcher named 'Pod Health Monitor'.
174+
2. Track the pod workload of your application, if `DEVTRON_FINAL_MANIFEST.status.phase != 'Running'`, trigger a job that sends an Email/Slack alert with pod details.
175+

env_gen.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
| NATS_MSG_MAX_AGE | 86400 | |
178178
| NATS_MSG_PROCESSING_BATCH_SIZE | 1 | |
179179
| NATS_SERVER_HOST | nats://devtron-nats.devtroncd:4222 | |
180+
| NOTIFICATION_MEDIUM | rest | |
180181
| ORCH_HOST | http://devtroncd-orchestrator-service-prod.devtroncd/webhook/msg/nats | |
181182
| ORCH_TOKEN | | |
182183
| OTEL_COLLECTOR_URL | | |

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ require (
1818
github.com/davecgh/go-spew v1.1.1
1919
github.com/deckarep/golang-set v1.8.0
2020
github.com/devtron-labs/authenticator v0.4.35-0.20240405091826-a91813c53470
21-
github.com/devtron-labs/common-lib v0.0.18-0.20240524141543-f4ed1281e694
21+
github.com/devtron-labs/common-lib v0.0.19-0.20240607054959-82c79c23b046
2222
github.com/devtron-labs/protos v0.0.3-0.20240326053929-48e42d9d4534
2323
github.com/evanphx/json-patch v5.6.0+incompatible
2424
github.com/gammazero/workerpool v1.1.3

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ github.com/denisenkom/go-mssqldb v0.0.0-20190707035753-2be1aa521ff4 h1:YcpmyvADG
207207
github.com/denisenkom/go-mssqldb v0.0.0-20190707035753-2be1aa521ff4/go.mod h1:zAg7JM8CkOJ43xKXIj7eRO9kmWm/TW578qo+oDO6tuM=
208208
github.com/devtron-labs/authenticator v0.4.35-0.20240405091826-a91813c53470 h1:AUTYcDnL6w6Ux+264VldYaOUQAP6pDZ5Tq8wCKJyiEg=
209209
github.com/devtron-labs/authenticator v0.4.35-0.20240405091826-a91813c53470/go.mod h1:JQxTCMmQisrpjzETJr0tzVadV+wW23rHEZAY7JVyK3s=
210-
github.com/devtron-labs/common-lib v0.0.18-0.20240524141543-f4ed1281e694 h1:lUcMarRvAKzsLpmuYwFgOsKLJQpHsJuvbKG+we/dI58=
211-
github.com/devtron-labs/common-lib v0.0.18-0.20240524141543-f4ed1281e694/go.mod h1:deAcJ5IjUjM6ozZQLJEgPWDUA0mKa632LBsKx8uM9TE=
210+
github.com/devtron-labs/common-lib v0.0.19-0.20240607054959-82c79c23b046 h1:hOyqkgILg+eDttLV6X7OAAo9PKEHzInUmBTVy/EY/iI=
211+
github.com/devtron-labs/common-lib v0.0.19-0.20240607054959-82c79c23b046/go.mod h1:deAcJ5IjUjM6ozZQLJEgPWDUA0mKa632LBsKx8uM9TE=
212212
github.com/devtron-labs/protos v0.0.3-0.20240326053929-48e42d9d4534 h1:TElPRU69QedW7DIQiiQxtjwSQ6cK0fCTAMGvSLhP0ac=
213213
github.com/devtron-labs/protos v0.0.3-0.20240326053929-48e42d9d4534/go.mod h1:ypUknVph8Ph4dxSlrFoouf7wLedQxHku2LQwgRrdgS4=
214214
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=

vendor/github.com/devtron-labs/common-lib/blob-storage/AwsS3Blob.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/devtron-labs/common-lib/blob-storage/AzureBlob.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/devtron-labs/common-lib/blob-storage/Bean.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/devtron-labs/common-lib/blob-storage/BlobStorageService.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/devtron-labs/common-lib/blob-storage/BlobUtils.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)