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

Commit

Permalink
Merge pull request #9 from keptn/release-0.1.x
Browse files Browse the repository at this point in the history
Release 0.1.x
  • Loading branch information
bacherfl authored May 15, 2019
2 parents fcce7c9 + d31f96c commit b35be78
Show file tree
Hide file tree
Showing 16 changed files with 591 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dynatrace-service
55 changes: 55 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
sudo: true
dist: xenial
language: go
services:
- docker
before_install:
- export TZ=Europe/Vienna
- IMAGE=keptn/dynatrace-service
- REGISTRY_USER=bacherfl
- VERSION="$(cat version | tr -d '[:space:]')"
- DATE="$(date +'%Y%m%d.%H%M')"
- GIT_SHA="$(git rev-parse --short HEAD)"
- echo "$REGISTRY_PASSWORD" | docker login --username $REGISTRY_USER --password-stdin
- REPO_URL="$(git remote get-url --all origin)"
- LAST_COMMIT="$(git log -1 --oneline)"
- ./writeManifest.sh
- cat MANIFEST
jobs:
include:
- stage: feature/bug
if: branch =~ ^feature.*$ OR branch =~ ^bug.*$ OR branch =~ ^hotfix.*$
script:
- echo $TRAVIS_BUILD_STAGE_NAME
- TYPE="$(echo $TRAVIS_BRANCH | cut -d'/' -f1)"
- NUMBER="$(echo $TRAVIS_BRANCH | cut -d'/' -f2)"
- docker build . -t "${IMAGE}:${GIT_SHA}"
- docker tag "${IMAGE}:${GIT_SHA}" "${IMAGE}:${TYPE}.${NUMBER}.${DATE}"
- docker push "${IMAGE}:${GIT_SHA}"
- docker push "${IMAGE}:${TYPE}.${NUMBER}.${DATE}"
- stage: develop
if: branch = develop AND NOT type = pull_request
script:
- echo $TRAVIS_BUILD_STAGE_NAME
- docker build . -t "${IMAGE}:${GIT_SHA}"
- docker tag "${IMAGE}:${GIT_SHA}" "${IMAGE}:${DATE}"
- docker tag "${IMAGE}:${GIT_SHA}" "${IMAGE}:latest"
- docker push "${IMAGE}:${GIT_SHA}"
- docker push "${IMAGE}:${DATE}"
- docker push "${IMAGE}:latest"
- stage: release-branch
if: branch =~ ^release.*$ AND NOT type = pull_request
script:
- echo $TRAVIS_BUILD_STAGE_NAME
- docker build . -t "${IMAGE}:${GIT_SHA}"
- docker tag "${IMAGE}:${GIT_SHA}" "${IMAGE}:${VERSION}.${DATE}"
- docker tag "${IMAGE}:${GIT_SHA}" "${IMAGE}:${VERSION}.latest"
- docker push "${IMAGE}:${GIT_SHA}"
- docker push "${IMAGE}:${VERSION}.${DATE}"
- docker push "${IMAGE}:${VERSION}.latest"
- stage: master
if: branch = master AND NOT type = pull_request
script:
- echo $TRAVIS_BUILD_STAGE_NAME
- docker build . -t "${IMAGE}:${VERSION}"
- docker push "${IMAGE}:${VERSION}"
24 changes: 24 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Use the offical Golang image to create a build artifact.
# This is based on Debian and sets the GOPATH to /go.
# https://hub.docker.com/_/golang
FROM golang:1.12 as builder

# Copy local code to the container image.
WORKDIR /go/src/github.com/keptn/dynatrace-service
COPY . .

# Build the command inside the container.
# (You may fetch or manage dependencies here,
# either manually or with a tool like "godep".)
RUN CGO_ENABLED=0 GOOS=linux go build -v -o dynatrace-service

# Use a Docker multi-stage build to create a lean production image.
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM alpine
RUN apk add --no-cache ca-certificates

# Copy the binary to the production image from the builder stage.
COPY --from=builder /go/src/github.com/keptn/dynatrace-service/dynatrace-service /dynatrace-service
ADD MANIFEST /
# Run the web service on container startup.
CMD ["sh", "-c", "cat MANIFEST && /dynatrace-service"]
9 changes: 9 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true


[prune]
go-tests = true
unused-packages = true
8 changes: 8 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
##########
branch: MANIFEST_BRANCH
repository: MANIFEST_REPOSITORY
commitlink: MANIFEST_REPOSITORY/commit/MANIFEST_COMMIT
repolink: MANIFEST_REPOSITORY/tree/MANIFEST_COMMIT
travisbuild: MANIFEST_TRAVIS_JOB_URL
timestamp: MANIFEST_DATE
##########
54 changes: 53 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,55 @@
# Dynatrace Service

TODO
This service sends information about the current state of a pipeline run for a service to Dynatrace by sending events for the correlating detected service.
The service is subscribed to the following keptn events:

- sh.keptn.events.deployment-finished
- sh.keptn.events.evaluation-done
- sh.keptn.events.tests-finished

## Installation

To use this service, you must have set up Dynatrace monitoring, as described in the [documentation](https://keptn.sh/docs/0.2.1/monitoring/dynatrace/).
Afterwards, to install the service in your keptn installation checkout or copy the `dynatrace-service.yaml`.

Then apply the `dynatrace-service.yaml` using `kubectl` to create the Dynatrace service and the subscriptions to the keptn channels.

```
kubectl apply -f dynatrace-service.yaml
```

Expected output:

```
service.serving.knative.dev/dynatrace-service created
subscription.eventing.knative.dev/dynatrace-subscription-deployment-finished created
subscription.eventing.knative.dev/dynatrace-subscription-tests-finished created
subscription.eventing.knative.dev/dynatrace-subscription-evaluation-done created
```

## Verification of installation

```
$ kubectl get ksvc dynatrace-service -n keptn
NAME DOMAIN LATESTCREATED LATESTREADY READY REASON
dynatrace-service dynatrace-service.keptn.x.x.x.x.xip.io dynatrace-service-dd9km dynatrace-service-dd9km True
```

```
$ kubectl get subscription -n keptn | grep dynatrace-subscription
dynatrace-subscription-deployment-finished True
dynatrace-subscription-evaluation-done True
dynatrace-subscription-tests-finished True
$
```

When the next event is sent to any of the keptn channels you should see an event in Dynatrace for the correlating service:
![Dynatrace events](assets/events.png?raw=true "Dynatrace Events")

## Uninstall service

To uninstall the dynatrace service and remove the subscriptions to keptn channels execute this command.

```
kubectl delete -f dynatrace-service.yaml
```
Binary file added assets/events.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions buildAndDeploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
REGISTRY_URI=$(kubectl describe svc docker-registry -n keptn | grep IP: | sed 's~IP:[ \t]*~~')

# Deploy service
rm -f config/gen/service-build.yaml

cat config/service-build.yaml | \
sed 's~REGISTRY_URI_PLACEHOLDER~'"$REGISTRY_URI"'~' >> config/gen/service-build.yaml

kubectl delete -f config/gen/service-build.yaml --ignore-not-found
kubectl apply -f config/gen/service-build.yaml
4 changes: 4 additions & 0 deletions deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

kubectl delete -f config/service.yaml --ignore-not-found
kubectl apply -f config/service.yaml
73 changes: 73 additions & 0 deletions dynatrace-service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# This is a very simple Knative Service that writes the input request to its log.

apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
name: dynatrace-service
namespace: keptn
spec:
runLatest:
configuration:
revisionTemplate:
spec:
container:
image: keptn/dynatrace-service:0.1.0
env:
- name: DT_API_TOKEN
valueFrom:
secretKeyRef:
name: dynatrace
key: DT_API_TOKEN
- name: DT_TENANT
valueFrom:
secretKeyRef:
name: dynatrace
key: DT_TENANT
---
apiVersion: eventing.knative.dev/v1alpha1
kind: Subscription
metadata:
name: dynatrace-subscription-deployment-finished
namespace: keptn
spec:
channel:
apiVersion: eventing.knative.dev/v1alpha1
kind: Channel
name: deployment-finished
subscriber:
ref:
apiVersion: serving.knative.dev/v1alpha1
kind: Service
name: dynatrace-service
---
apiVersion: eventing.knative.dev/v1alpha1
kind: Subscription
metadata:
name: dynatrace-subscription-tests-finished
namespace: keptn
spec:
channel:
apiVersion: eventing.knative.dev/v1alpha1
kind: Channel
name: tests-finished
subscriber:
ref:
apiVersion: serving.knative.dev/v1alpha1
kind: Service
name: dynatrace-service
---
apiVersion: eventing.knative.dev/v1alpha1
kind: Subscription
metadata:
name: dynatrace-subscription-evaluation-done
namespace: keptn
spec:
channel:
apiVersion: eventing.knative.dev/v1alpha1
kind: Channel
name: evaluation-done
subscriber:
ref:
apiVersion: serving.knative.dev/v1alpha1
kind: Service
name: dynatrace-service
57 changes: 57 additions & 0 deletions keptn/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package keptn

import (
"encoding/json"
"fmt"
)

type keptnLogMessage struct {
KeptnContext string `json:"keptnContext"`
Message string `json:"message"`
KeptnService string `json:"keptnService"`
LogLevel string `json:"logLevel"`
}

// Info logs an info message
func Info(keptnContext string, message string) {
var logMessage keptnLogMessage
logMessage.KeptnContext = keptnContext
logMessage.LogLevel = "INFO"
logMessage.KeptnService = "dynatrace-service"
logMessage.Message = message

printLogMessage(logMessage)
}

// Error logs an error message
func Error(keptnContext string, message string) {
var logMessage keptnLogMessage
logMessage.KeptnContext = keptnContext
logMessage.LogLevel = "ERROR"
logMessage.KeptnService = "dynatrace-service"
logMessage.Message = message

printLogMessage(logMessage)
}

// Debug logs a debug message
func Debug(keptnContext string, message string) {
var logMessage keptnLogMessage
logMessage.KeptnContext = keptnContext
logMessage.LogLevel = "DEBUG"
logMessage.KeptnService = "dynatrace-service"
logMessage.Message = message

printLogMessage(logMessage)
}

func printLogMessage(logMessage keptnLogMessage) {
logString, err := json.Marshal(logMessage)

if err != nil {
fmt.Println("Could not log keptn log message")
return
}

fmt.Println(string(logString))
}
Loading

0 comments on commit b35be78

Please sign in to comment.