Skip to content
This repository was archived by the owner on Jan 19, 2024. It is now read-only.

Commit dbb3576

Browse files
feat: Reimplement service with go-sdk (#358)
* feat: Implement prometheus-service using go-sdk Signed-off-by: TannerGabriel <gabrieltanner.code@gmail.com> * Remove old unit tests Signed-off-by: TannerGabriel <gabrieltanner.code@gmail.com> * Change error logging Signed-off-by: TannerGabriel <gabrieltanner.code@gmail.com> * Remove distributor Signed-off-by: TannerGabriel <gabrieltanner.code@gmail.com> * Change health check port Signed-off-by: TannerGabriel <gabrieltanner.code@gmail.com> * Uncomment unit tests Signed-off-by: TannerGabriel <gabrieltanner.code@gmail.com> * Rename Kubernetes API variable Signed-off-by: TannerGabriel <gabrieltanner.code@gmail.com> * Send started and finished event manually Signed-off-by: TannerGabriel <gabrieltanner.code@gmail.com> * Fix PR comments Signed-off-by: TannerGabriel <gabrieltanner.code@gmail.com> * Send project context in finished event Signed-off-by: TannerGabriel <gabrieltanner.code@gmail.com> * Upgrade integration tests to 0.18 Signed-off-by: TannerGabriel <gabrieltanner.code@gmail.com> * Send alertmanager events over NATS Signed-off-by: TannerGabriel <gabrieltanner.code@gmail.com> * Move dependencies in go.mod Signed-off-by: TannerGabriel <gabrieltanner.code@gmail.com> Signed-off-by: TannerGabriel <gabrieltanner.code@gmail.com>
1 parent 28f167a commit dbb3576

10 files changed

+396
-342
lines changed

chart/templates/deployment.yaml

+20-71
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,24 @@ spec:
4040
livenessProbe:
4141
httpGet:
4242
path: /health
43-
port: http
43+
port: 8081
4444
readinessProbe:
4545
httpGet:
4646
path: /health
47-
port: http
47+
port: 8081
4848
resources:
4949
{{- toYaml .Values.resources | nindent 12 }}
5050
env:
5151
- name: METRICS_SCRAPE_PATH
5252
value: '/metrics'
53-
- name: CONFIGURATION_SERVICE
54-
value: 'http://resource-service:8080'
5553
- name: PROMETHEUS_NS
5654
value: '{{- include "prometheus-service.namespace" . }}'
5755
- name: PROMETHEUS_CM
5856
value: 'prometheus-server'
5957
- name: PROMETHEUS_LABELS
6058
value: 'component=server'
59+
- name: HEALTH_ENDPOINT_PORT
60+
value: '8081'
6161
- name: PROMETHEUS_ENDPOINT
6262
value: "{{ include "prometheus-service.endpoint" . }}"
6363
- name: PROMETHEUS_CONFIG_FILENAME
@@ -82,90 +82,39 @@ spec:
8282
value: '{{ ((.Values.prometheus).createTargets) | default "true" }}'
8383
- name: CREATE_ALERTS
8484
value: '{{ ((.Values.prometheus).createAlerts) | default "true" }}'
85-
- name: K8S_NAMESPACE
86-
valueFrom:
87-
fieldRef:
88-
fieldPath: metadata.namespace
89-
- name: distributor
90-
image: "{{ .Values.distributor.image.repository }}:{{ .Values.distributor.image.tag | default .Chart.AppVersion }}"
91-
livenessProbe:
92-
httpGet:
93-
path: /health
94-
port: 8080
95-
initialDelaySeconds: 0
96-
periodSeconds: 5
97-
readinessProbe:
98-
httpGet:
99-
path: /health
100-
port: 8080
101-
initialDelaySeconds: 5
102-
periodSeconds: 5
103-
imagePullPolicy: {{ .Values.distributor.image.pullPolicy }}
104-
ports:
105-
- containerPort: 8080
106-
resources:
107-
requests:
108-
memory: "16Mi"
109-
cpu: "25m"
110-
limits:
111-
memory: "32Mi"
112-
cpu: "100m"
113-
env:
114-
- name: PUBSUB_URL
115-
value: 'nats://keptn-nats'
11685
- name: PUBSUB_TOPIC
117-
value: 'sh.keptn.event.monitoring.configure,sh.keptn.event.configure-monitoring.triggered,sh.keptn.event.get-sli.triggered'
118-
- name: PUBSUB_RECIPIENT
119-
value: '127.0.0.1'
120-
- name: PUBSUB_RECIPIENT_PATH
121-
value: '/events'
122-
- name: STAGE_FILTER
123-
value: "{{ .Values.distributor.stageFilter }}"
124-
- name: PROJECT_FILTER
125-
value: "{{ .Values.distributor.projectFilter }}"
126-
- name: SERVICE_FILTER
127-
value: "{{ .Values.distributor.serviceFilter }}"
128-
- name: DISTRIBUTOR_VERSION
129-
value: {{ .Values.distributor.image.tag | default .Chart.AppVersion }}
130-
- name: VERSION
86+
value: {{ ((.Values).subscription).pubsubTopic | default "sh.keptn.>" }}
87+
- name: K8S_DEPLOYMENT_NAME
13188
valueFrom:
13289
fieldRef:
133-
fieldPath: metadata.labels['app.kubernetes.io/version']
134-
- name: LOCATION
90+
apiVersion: v1
91+
fieldPath: 'metadata.labels[''app.kubernetes.io/name'']'
92+
- name: K8S_DEPLOYMENT_VERSION
13593
valueFrom:
13694
fieldRef:
137-
fieldPath: metadata.labels['app.kubernetes.io/component']
138-
- name: K8S_DEPLOYMENT_NAME
95+
apiVersion: v1
96+
fieldPath: 'metadata.labels[''app.kubernetes.io/version'']'
97+
- name: K8S_DEPLOYMENT_COMPONENT
13998
valueFrom:
14099
fieldRef:
141-
fieldPath: metadata.labels['app.kubernetes.io/name']
142-
- name: K8S_POD_NAME
143-
valueFrom:
144-
fieldRef:
145-
fieldPath: metadata.name
100+
apiVersion: v1
101+
fieldPath: 'metadata.labels[''app.kubernetes.io/component'']'
146102
- name: K8S_NAMESPACE
147103
valueFrom:
148104
fieldRef:
105+
apiVersion: v1
149106
fieldPath: metadata.namespace
150107
- name: K8S_NODE_NAME
151108
valueFrom:
152109
fieldRef:
110+
apiVersion: v1
153111
fieldPath: spec.nodeName
154-
{{- if .Values.remoteControlPlane.enabled }}
155-
- name: KEPTN_API_ENDPOINT
156-
value: "{{ .Values.remoteControlPlane.api.protocol }}://{{ .Values.remoteControlPlane.api.hostname }}/api"
157-
- name: KEPTN_API_TOKEN
158-
value: "{{ .Values.remoteControlPlane.api.token }}"
159-
- name: HTTP_SSL_VERIFY
160-
{{- $apiValidateTls := .Values.remoteControlPlane.api.apiValidateTls | ternary "true" "false" }}
161-
value: "{{ $apiValidateTls }}"
162-
{{- end }}
163-
{{- if (((.Values.distributor).config).queueGroup).enabled | default true }}
164-
- name: PUBSUB_GROUP
112+
- name: K8S_POD_NAME
165113
valueFrom:
166114
fieldRef:
167-
fieldPath: metadata.labels['app.kubernetes.io/name']
168-
{{- end }}
115+
apiVersion: v1
116+
fieldPath: metadata.name
117+
169118

170119
{{- with .Values.nodeSelector }}
171120
nodeSelector:

chart/values.yaml

+3-12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ image:
1010
# Overrides the image tag whose default is the chart appVersion.
1111
tag: ""
1212

13+
subscription:
14+
pubsubTopic: "sh.keptn.event.monitoring.configure,sh.keptn.event.get-sli.triggered" # Sets the events the service subscribes to
15+
1316
# Prometheus specific configuration
1417
prometheus:
1518
namespace: "" # K8s namespace where prometheus is installed
@@ -21,18 +24,6 @@ prometheus:
2124
autodetect: true # Enable of the auto-detection of the Prometheus installation
2225
autodetect_am: true # Enable of the auto-detection of the Prometheus Alertmanager installation
2326

24-
distributor:
25-
stageFilter: "" # Sets the stage this helm service belongs to
26-
serviceFilter: "" # Sets the service this helm service belongs to
27-
projectFilter: "" # Sets the project this helm service belongs to
28-
image:
29-
repository: docker.io/keptn/distributor # Container Image Name
30-
pullPolicy: IfNotPresent # Kubernetes Image Pull Policy
31-
tag: "0.18.0" # Container Tag
32-
config:
33-
queueGroup:
34-
enabled: true # Enable connection via Nats queue group to support exactly-once message processing
35-
3627
# Note: Remote Control Plane is currently not supported by prometheus-service - please keep this setting disabled
3728
remoteControlPlane:
3829
enabled: false # Enables remote execution plane mode

eventhandling/alertEvent.go

+41-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package eventhandling
22

33
import (
4+
"context"
45
"crypto/sha256"
56
"encoding/json"
7+
"errors"
68
"fmt"
79
keptncommons "github.com/keptn/go-utils/pkg/lib"
10+
"github.com/nats-io/nats.go"
11+
"log"
812
"net/http"
913
"net/url"
1014
"strings"
@@ -13,6 +17,7 @@ import (
1317
"github.com/keptn/go-utils/pkg/lib/keptn"
1418
keptnv2 "github.com/keptn/go-utils/pkg/lib/v0_2_0"
1519

20+
cenats "github.com/cloudevents/sdk-go/protocol/nats/v2"
1621
cloudevents "github.com/cloudevents/sdk-go/v2"
1722
"github.com/google/uuid"
1823
)
@@ -145,18 +150,50 @@ func createAndSendCE(problemData remediationTriggeredEventData, shkeptncontext s
145150
return fmt.Errorf("unable to set cloud event data: %w", err)
146151
}
147152

148-
keptnHandler, err := keptnv2.NewKeptn(&event, keptn.KeptnOpts{})
153+
err = forwardEventToNATSServer(event)
149154
if err != nil {
150-
return fmt.Errorf("could not initialize Keptn Handler: %s", err.Error())
155+
return err
151156
}
152157

153-
if err := keptnHandler.SendCloudEvent(event); err != nil {
154-
return fmt.Errorf("could not send event: %s", err.Error())
158+
return nil
159+
}
160+
161+
func forwardEventToNATSServer(event cloudevents.Event) error {
162+
pubSubConnection, err := createPubSubConnection(event.Context.GetType())
163+
if err != nil {
164+
return err
155165
}
156166

167+
c, err := cloudevents.NewClient(pubSubConnection)
168+
if err != nil {
169+
log.Printf("Failed to create cloudevents client: %v", err)
170+
return err
171+
}
172+
173+
cloudevents.WithEncodingStructured(context.Background())
174+
175+
if result := c.Send(context.Background(), event); cloudevents.IsUndelivered(result) {
176+
log.Printf("Failed to send cloud event: %v", result.Error())
177+
} else {
178+
log.Printf("Sent: %s, accepted: %t", event.ID(), cloudevents.IsACK(result))
179+
}
157180
return nil
158181
}
159182

183+
func createPubSubConnection(topic string) (*cenats.Sender, error) {
184+
if topic == "" {
185+
return nil, errors.New("no PubSub Topic defined")
186+
}
187+
188+
p, err := cenats.NewSender("nats://keptn-nats", topic, cenats.NatsOptions(nats.MaxReconnects(-1)))
189+
if err != nil {
190+
log.Printf("Failed to create nats protocol, %v", err)
191+
return nil, err
192+
}
193+
194+
return p, nil
195+
}
196+
160197
// createOrApplyKeptnContext re-uses the existing Keptn Context or creates a new one based on prometheus fingerprint
161198
func createOrApplyKeptnContext(contextID string) string {
162199
uuid.SetRand(nil)

0 commit comments

Comments
 (0)