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

Commit

Permalink
feat: Reimplement service with go-sdk (#358)
Browse files Browse the repository at this point in the history
* feat: Implement prometheus-service using go-sdk

Signed-off-by: TannerGabriel <[email protected]>

* Remove old unit tests

Signed-off-by: TannerGabriel <[email protected]>

* Change error logging

Signed-off-by: TannerGabriel <[email protected]>

* Remove distributor

Signed-off-by: TannerGabriel <[email protected]>

* Change health check port

Signed-off-by: TannerGabriel <[email protected]>

* Uncomment unit tests

Signed-off-by: TannerGabriel <[email protected]>

* Rename Kubernetes API variable

Signed-off-by: TannerGabriel <[email protected]>

* Send started and finished event manually

Signed-off-by: TannerGabriel <[email protected]>

* Fix PR comments

Signed-off-by: TannerGabriel <[email protected]>

* Send project context in finished event

Signed-off-by: TannerGabriel <[email protected]>

* Upgrade integration tests to 0.18

Signed-off-by: TannerGabriel <[email protected]>

* Send alertmanager events over NATS

Signed-off-by: TannerGabriel <[email protected]>

* Move dependencies in go.mod

Signed-off-by: TannerGabriel <[email protected]>

Signed-off-by: TannerGabriel <[email protected]>
  • Loading branch information
TannerGabriel authored Aug 29, 2022
1 parent 28f167a commit dbb3576
Show file tree
Hide file tree
Showing 10 changed files with 396 additions and 342 deletions.
91 changes: 20 additions & 71 deletions chart/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,24 @@ spec:
livenessProbe:
httpGet:
path: /health
port: http
port: 8081
readinessProbe:
httpGet:
path: /health
port: http
port: 8081
resources:
{{- toYaml .Values.resources | nindent 12 }}
env:
- name: METRICS_SCRAPE_PATH
value: '/metrics'
- name: CONFIGURATION_SERVICE
value: 'http://resource-service:8080'
- name: PROMETHEUS_NS
value: '{{- include "prometheus-service.namespace" . }}'
- name: PROMETHEUS_CM
value: 'prometheus-server'
- name: PROMETHEUS_LABELS
value: 'component=server'
- name: HEALTH_ENDPOINT_PORT
value: '8081'
- name: PROMETHEUS_ENDPOINT
value: "{{ include "prometheus-service.endpoint" . }}"
- name: PROMETHEUS_CONFIG_FILENAME
Expand All @@ -82,90 +82,39 @@ spec:
value: '{{ ((.Values.prometheus).createTargets) | default "true" }}'
- name: CREATE_ALERTS
value: '{{ ((.Values.prometheus).createAlerts) | default "true" }}'
- name: K8S_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: distributor
image: "{{ .Values.distributor.image.repository }}:{{ .Values.distributor.image.tag | default .Chart.AppVersion }}"
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 0
periodSeconds: 5
readinessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
imagePullPolicy: {{ .Values.distributor.image.pullPolicy }}
ports:
- containerPort: 8080
resources:
requests:
memory: "16Mi"
cpu: "25m"
limits:
memory: "32Mi"
cpu: "100m"
env:
- name: PUBSUB_URL
value: 'nats://keptn-nats'
- name: PUBSUB_TOPIC
value: 'sh.keptn.event.monitoring.configure,sh.keptn.event.configure-monitoring.triggered,sh.keptn.event.get-sli.triggered'
- name: PUBSUB_RECIPIENT
value: '127.0.0.1'
- name: PUBSUB_RECIPIENT_PATH
value: '/events'
- name: STAGE_FILTER
value: "{{ .Values.distributor.stageFilter }}"
- name: PROJECT_FILTER
value: "{{ .Values.distributor.projectFilter }}"
- name: SERVICE_FILTER
value: "{{ .Values.distributor.serviceFilter }}"
- name: DISTRIBUTOR_VERSION
value: {{ .Values.distributor.image.tag | default .Chart.AppVersion }}
- name: VERSION
value: {{ ((.Values).subscription).pubsubTopic | default "sh.keptn.>" }}
- name: K8S_DEPLOYMENT_NAME
valueFrom:
fieldRef:
fieldPath: metadata.labels['app.kubernetes.io/version']
- name: LOCATION
apiVersion: v1
fieldPath: 'metadata.labels[''app.kubernetes.io/name'']'
- name: K8S_DEPLOYMENT_VERSION
valueFrom:
fieldRef:
fieldPath: metadata.labels['app.kubernetes.io/component']
- name: K8S_DEPLOYMENT_NAME
apiVersion: v1
fieldPath: 'metadata.labels[''app.kubernetes.io/version'']'
- name: K8S_DEPLOYMENT_COMPONENT
valueFrom:
fieldRef:
fieldPath: metadata.labels['app.kubernetes.io/name']
- name: K8S_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
apiVersion: v1
fieldPath: 'metadata.labels[''app.kubernetes.io/component'']'
- name: K8S_NAMESPACE
valueFrom:
fieldRef:
apiVersion: v1
fieldPath: metadata.namespace
- name: K8S_NODE_NAME
valueFrom:
fieldRef:
apiVersion: v1
fieldPath: spec.nodeName
{{- if .Values.remoteControlPlane.enabled }}
- name: KEPTN_API_ENDPOINT
value: "{{ .Values.remoteControlPlane.api.protocol }}://{{ .Values.remoteControlPlane.api.hostname }}/api"
- name: KEPTN_API_TOKEN
value: "{{ .Values.remoteControlPlane.api.token }}"
- name: HTTP_SSL_VERIFY
{{- $apiValidateTls := .Values.remoteControlPlane.api.apiValidateTls | ternary "true" "false" }}
value: "{{ $apiValidateTls }}"
{{- end }}
{{- if (((.Values.distributor).config).queueGroup).enabled | default true }}
- name: PUBSUB_GROUP
- name: K8S_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.labels['app.kubernetes.io/name']
{{- end }}
apiVersion: v1
fieldPath: metadata.name


{{- with .Values.nodeSelector }}
nodeSelector:
Expand Down
15 changes: 3 additions & 12 deletions chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ image:
# Overrides the image tag whose default is the chart appVersion.
tag: ""

subscription:
pubsubTopic: "sh.keptn.event.monitoring.configure,sh.keptn.event.get-sli.triggered" # Sets the events the service subscribes to

# Prometheus specific configuration
prometheus:
namespace: "" # K8s namespace where prometheus is installed
Expand All @@ -21,18 +24,6 @@ prometheus:
autodetect: true # Enable of the auto-detection of the Prometheus installation
autodetect_am: true # Enable of the auto-detection of the Prometheus Alertmanager installation

distributor:
stageFilter: "" # Sets the stage this helm service belongs to
serviceFilter: "" # Sets the service this helm service belongs to
projectFilter: "" # Sets the project this helm service belongs to
image:
repository: docker.io/keptn/distributor # Container Image Name
pullPolicy: IfNotPresent # Kubernetes Image Pull Policy
tag: "0.18.0" # Container Tag
config:
queueGroup:
enabled: true # Enable connection via Nats queue group to support exactly-once message processing

# Note: Remote Control Plane is currently not supported by prometheus-service - please keep this setting disabled
remoteControlPlane:
enabled: false # Enables remote execution plane mode
Expand Down
45 changes: 41 additions & 4 deletions eventhandling/alertEvent.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package eventhandling

import (
"context"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
keptncommons "github.com/keptn/go-utils/pkg/lib"
"github.com/nats-io/nats.go"
"log"
"net/http"
"net/url"
"strings"
Expand All @@ -13,6 +17,7 @@ import (
"github.com/keptn/go-utils/pkg/lib/keptn"
keptnv2 "github.com/keptn/go-utils/pkg/lib/v0_2_0"

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

keptnHandler, err := keptnv2.NewKeptn(&event, keptn.KeptnOpts{})
err = forwardEventToNATSServer(event)
if err != nil {
return fmt.Errorf("could not initialize Keptn Handler: %s", err.Error())
return err
}

if err := keptnHandler.SendCloudEvent(event); err != nil {
return fmt.Errorf("could not send event: %s", err.Error())
return nil
}

func forwardEventToNATSServer(event cloudevents.Event) error {
pubSubConnection, err := createPubSubConnection(event.Context.GetType())
if err != nil {
return err
}

c, err := cloudevents.NewClient(pubSubConnection)
if err != nil {
log.Printf("Failed to create cloudevents client: %v", err)
return err
}

cloudevents.WithEncodingStructured(context.Background())

if result := c.Send(context.Background(), event); cloudevents.IsUndelivered(result) {
log.Printf("Failed to send cloud event: %v", result.Error())
} else {
log.Printf("Sent: %s, accepted: %t", event.ID(), cloudevents.IsACK(result))
}
return nil
}

func createPubSubConnection(topic string) (*cenats.Sender, error) {
if topic == "" {
return nil, errors.New("no PubSub Topic defined")
}

p, err := cenats.NewSender("nats://keptn-nats", topic, cenats.NatsOptions(nats.MaxReconnects(-1)))
if err != nil {
log.Printf("Failed to create nats protocol, %v", err)
return nil, err
}

return p, nil
}

// createOrApplyKeptnContext re-uses the existing Keptn Context or creates a new one based on prometheus fingerprint
func createOrApplyKeptnContext(contextID string) string {
uuid.SetRand(nil)
Expand Down
Loading

0 comments on commit dbb3576

Please sign in to comment.