Skip to content
This repository has been archived by the owner on Jul 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #29 from keptn-contrib/feature/1663/prepare-release
Browse files Browse the repository at this point in the history
#1663 prepare release 0.2.2
  • Loading branch information
bacherfl authored May 4, 2020
2 parents 2e4d918 + 8768286 commit 2883cda
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 231 deletions.
2 changes: 1 addition & 1 deletion deploy/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ spec:
spec:
containers:
- name: prometheus-sli-service
image: keptncontrib/prometheus-sli-service:latest
image: keptncontrib/prometheus-sli-service:0.2.2
ports:
- containerPort: 8080
resources:
Expand Down
16 changes: 13 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@ go 1.12

require (
github.com/cloudevents/sdk-go v0.10.0
github.com/gogo/protobuf v1.3.1 // indirect
github.com/google/uuid v1.1.1
github.com/googleapis/gnostic v0.3.1 // indirect
github.com/json-iterator/go v1.1.7 // indirect
github.com/kelseyhightower/envconfig v1.4.0
github.com/keptn/go-utils v0.6.1
github.com/keptn/go-utils v0.6.2
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.4.0
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
golang.org/x/net v0.0.0-20191021144547-ec77196f6094
golang.org/x/sys v0.0.0-20190726091711-fc99dfbffb4e // indirect
golang.org/x/time v0.0.0-20191023065245-6d3f0bb11be5 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.2.4
k8s.io/api v0.0.0-20190313235455-40a48860b5ab // indirect
k8s.io/apimachinery v0.0.0-20190313205120-d7deff9243b1
k8s.io/client-go v11.0.0+incompatible
k8s.io/klog v1.0.0 // indirect
k8s.io/utils v0.0.0-20191010214722-8d271d903fe4 // indirect
sigs.k8s.io/yaml v1.1.0 // indirect
)

replace github.com/cloudevents/sdk-go => github.com/cloudevents/sdk-go v0.0.0-20190509003705-56931988abe3
210 changes: 63 additions & 147 deletions go.sum

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions lib/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"strings"
"time"

keptnevents "github.com/keptn/go-utils/pkg/events"
"github.com/keptn/go-utils/pkg/lib"
)

const Throughput = "throughput"
Expand Down Expand Up @@ -43,12 +43,12 @@ type Handler struct {
Stage string
Service string
HTTPClient *http.Client
CustomFilters []*keptnevents.SLIFilter
CustomFilters []*keptn.SLIFilter
CustomQueries map[string]string
}

// NewPrometheusHandler returns a new prometheus handler that interacts with the Prometheus REST API
func NewPrometheusHandler(apiURL string, project string, stage string, service string, customFilters []*keptnevents.SLIFilter) *Handler {
func NewPrometheusHandler(apiURL string, project string, stage string, service string, customFilters []*keptn.SLIFilter) *Handler {
ph := &Handler{
ApiURL: apiURL,
Project: project,
Expand Down
2 changes: 1 addition & 1 deletion lib/prometheus/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"testing"
"time"

keptnevents "github.com/keptn/go-utils/pkg/events"
keptnevents "github.com/keptn/go-utils/pkg/lib"
)

func testingHTTPClient(handler http.Handler) (*http.Client, func()) {
Expand Down
113 changes: 37 additions & 76 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package main
import (
"context"
"errors"
"fmt"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"log"
"math"
"net/url"
Expand All @@ -22,9 +23,7 @@ import (
"github.com/google/uuid"
"github.com/kelseyhightower/envconfig"

configutils "github.com/keptn/go-utils/pkg/configuration-service/utils"
keptnevents "github.com/keptn/go-utils/pkg/events"
keptnutils "github.com/keptn/go-utils/pkg/utils"
keptn "github.com/keptn/go-utils/pkg/lib"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/client-go/kubernetes/typed/core/v1"
)
Expand Down Expand Up @@ -78,7 +77,7 @@ func _main(args []string, env envConfig) int {
func gotEvent(ctx context.Context, event cloudevents.Event) error {

switch event.Type() {
case keptnevents.InternalGetSLIEventType:
case keptn.InternalGetSLIEventType:
return retrieveMetrics(event) // backwards compatibility to Keptn versions <= 0.5.x
default:
return errors.New("received unknown event type")
Expand All @@ -88,7 +87,7 @@ func gotEvent(ctx context.Context, event cloudevents.Event) error {
func retrieveMetrics(event cloudevents.Event) error {
var shkeptncontext string
event.Context.ExtensionAs("shkeptncontext", &shkeptncontext)
eventData := &keptnevents.InternalGetSLIEventData{}
eventData := &keptn.InternalGetSLIEventData{}
err := event.DataAs(eventData)
if err != nil {
return err
Expand All @@ -99,22 +98,34 @@ func retrieveMetrics(event cloudevents.Event) error {
return nil
}

stdLogger := keptnutils.NewLogger(shkeptncontext, event.Context.GetID(), "prometheus-sli-service")
stdLogger := keptn.NewLogger(shkeptncontext, event.Context.GetID(), "prometheus-sli-service")
stdLogger.Info("Retrieving Prometheus metrics")

kubeClient, err := keptnutils.GetKubeAPI(true)
clusterConfig, err := rest.InClusterConfig()
if err != nil {
stdLogger.Error("could not create Kubernetes client")
return errors.New("could not create Kubernetes client")
}

prometheusApiURL, err := getPrometheusApiURL(eventData.Project, kubeClient, stdLogger)
kubeClient, err := kubernetes.NewForConfig(clusterConfig)
if err != nil {
stdLogger.Error("could not create Kubernetes client")
return errors.New("could not create Kubernetes client")
}

prometheusApiURL, err := getPrometheusApiURL(eventData.Project, kubeClient.CoreV1(), stdLogger)
if err != nil {
return err
}

keptnHandler, err := keptn.NewKeptn(&event, keptn.KeptnOpts{})
if err != nil {
stdLogger.Error("Failed to get custom queries for project " + eventData.Project)
stdLogger.Error(err.Error())
return err
}
// retrieve custom metrics for project
projectCustomQueries, err := getCustomQueries(eventData.Project, eventData.Stage, eventData.Service, kubeClient, stdLogger)
projectCustomQueries, err := getCustomQueries(keptnHandler, eventData.Project, eventData.Stage, eventData.Service, stdLogger)
if err != nil {
stdLogger.Error("Failed to get custom queries for project " + eventData.Project)
stdLogger.Error(err.Error())
Expand All @@ -127,57 +138,51 @@ func retrieveMetrics(event cloudevents.Event) error {
prometheusHandler.CustomQueries = projectCustomQueries
}

var sliResults []*keptnevents.SLIResult
var sliResults []*keptn.SLIResult

for _, indicator := range eventData.Indicators {
stdLogger.Info("Fetching indicator: " + indicator)
sliValue, err := prometheusHandler.GetSLIValue(indicator, eventData.Start, eventData.End)
if err != nil {
sliResults = append(sliResults, &keptnevents.SLIResult{
sliResults = append(sliResults, &keptn.SLIResult{
Metric: indicator,
Value: 0,
Success: false,
Message: err.Error(),
})
} else if math.IsNaN(sliValue) {
sliResults = append(sliResults, &keptnevents.SLIResult{
sliResults = append(sliResults, &keptn.SLIResult{
Metric: indicator,
Value: 0,
Success: false,
Message: "SLI value is NaN",
})
} else {
sliResults = append(sliResults, &keptnevents.SLIResult{
sliResults = append(sliResults, &keptn.SLIResult{
Metric: indicator,
Value: sliValue,
Success: true,
})
}
}

return sendInternalGetSLIDoneEvent(shkeptncontext, eventData.Project, eventData.Service, eventData.Stage,
return sendInternalGetSLIDoneEvent(keptnHandler,
sliResults, eventData.Start, eventData.End, eventData.TestStrategy, eventData.DeploymentStrategy, eventData.Labels)
}

// getCustomQueries returns custom queries as stored in configuration store
func getCustomQueries(project string, stage string, service string, kubeClient v1.CoreV1Interface, logger *keptnutils.Logger) (map[string]string, error) {
func getCustomQueries(keptnHandler *keptn.Keptn, project string, stage string, service string, logger *keptn.Logger) (map[string]string, error) {
logger.Info("Checking for custom SLI queries")

endPoint, err := getServiceEndpoint(configservice)
if err != nil {
return nil, errors.New("Failed to retrieve endpoint of configuration-service. %s" + err.Error())
}

resourceHandler := configutils.NewResourceHandler(endPoint.String())
customQueries, err := resourceHandler.GetSLIConfiguration(project, stage, service, sliResourceURI)
customQueries, err := keptnHandler.GetSLIConfiguration(project, stage, service, sliResourceURI)
if err != nil {
return nil, err
}

return customQueries, nil
}

func getPrometheusApiURL(project string, kubeClient v1.CoreV1Interface, logger *keptnutils.Logger) (string, error) {
func getPrometheusApiURL(project string, kubeClient v1.CoreV1Interface, logger *keptn.Logger) (string, error) {
logger.Info("Checking if external prometheus instance has been defined for project " + project)
// check if secret 'prometheus-credentials-<project> exists

Expand Down Expand Up @@ -229,16 +234,15 @@ func generatePrometheusURL(pc *prometheusCredentials) string {
return strings.Replace(prometheusURL, " ", "", -1)
}

func sendInternalGetSLIDoneEvent(shkeptncontext string, project string,
service string, stage string, indicatorValues []*keptnevents.SLIResult, start string, end string, testStrategy string, deploymentStrategy string, labels map[string]string) error {
func sendInternalGetSLIDoneEvent(keptnHandler *keptn.Keptn, indicatorValues []*keptn.SLIResult, start string, end string, testStrategy string, deploymentStrategy string, labels map[string]string) error {

source, _ := url.Parse("prometheus-sli-service")
contentType := "application/json"

getSLIEvent := keptnevents.InternalGetSLIDoneEventData{
Project: project,
Service: service,
Stage: stage,
getSLIEvent := keptn.InternalGetSLIDoneEventData{
Project: keptnHandler.KeptnBase.Project,
Service: keptnHandler.KeptnBase.Service,
Stage: keptnHandler.KeptnBase.Stage,
IndicatorValues: indicatorValues,
Start: start,
End: end,
Expand All @@ -250,56 +254,13 @@ func sendInternalGetSLIDoneEvent(shkeptncontext string, project string,
Context: cloudevents.EventContextV02{
ID: uuid.New().String(),
Time: &types.Timestamp{Time: time.Now()},
Type: keptnevents.InternalGetSLIDoneEventType,
Type: keptn.InternalGetSLIDoneEventType,
Source: types.URLRef{URL: *source},
ContentType: &contentType,
Extensions: map[string]interface{}{"shkeptncontext": shkeptncontext},
Extensions: map[string]interface{}{"shkeptncontext": keptnHandler.KeptnContext},
}.AsV02(),
Data: getSLIEvent,
}

return sendEvent(event)
}

func sendEvent(event cloudevents.Event) error {
endPoint, err := getServiceEndpoint(eventbroker)
if err != nil {
return errors.New("Failed to retrieve endpoint of eventbroker. %s" + err.Error())
}

if endPoint.Host == "" {
return errors.New("Host of eventbroker not set")
}

transport, err := cloudeventshttp.New(
cloudeventshttp.WithTarget(endPoint.String()),
cloudeventshttp.WithEncoding(cloudeventshttp.StructuredV02),
)
if err != nil {
return errors.New("Failed to create transport:" + err.Error())
}

c, err := client.New(transport)
if err != nil {
return errors.New("Failed to create HTTP client:" + err.Error())
}

if _, err := c.Send(context.Background(), event); err != nil {
return errors.New("Failed to send cloudevent:, " + err.Error())
}
return nil
}

// getServiceEndpoint gets an endpoint stored in an environment variable and sets http as default scheme
func getServiceEndpoint(service string) (url.URL, error) {
url, err := url.Parse(os.Getenv(service))
if err != nil {
return *url, fmt.Errorf("Failed to retrieve value from ENVIRONMENT_VARIABLE: %s", service)
}

if url.Scheme == "" {
url.Scheme = "http"
}

return *url, nil
return keptnHandler.SendCloudEvent(event)
}
8 changes: 8 additions & 0 deletions releasenotes/releasenotes_V0.2.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Release Notes 0.2.2

## New Features

## Fixed Issues
- Include received labels in outgoing CloudEvents [#27](https://github.com/keptn-contrib/prometheus-sli-service/issues/27)

## Known Limitations

0 comments on commit 2883cda

Please sign in to comment.