Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Etienne Tremel committed Jan 16, 2018
0 parents commit 2945d7b
Show file tree
Hide file tree
Showing 26 changed files with 1,315 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
vendor
examples/app/vendor
_dist
helm-monitor
helm-monitor.exe
20 changes: 20 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
language: go

go:
- 1.9.2

install:
- make dep

script:
- make test-all
- make dist

deploy:
- provider: releases
# api_key: ${GITHUB_TOKEN}
file: _dist/helm-monitor*
skip_cleanup: true
file_glob: true
on:
tags: true
105 changes: 105 additions & 0 deletions Gopkg.lock

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

34 changes: 34 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

# Gopkg.toml example
#
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
# 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"


[[constraint]]
name = "github.com/kubernetes/helm"
version = "2.0.0-alpha.2"

[[constraint]]
name = "github.com/spf13/cobra"
version = "0.0.1"

[[constraint]]
branch = "master"
name = "k8s.io/helm"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 estafette

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
42 changes: 42 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
HELM_HOME ?= $(shell helm home)
HELM_PLUGIN_DIR ?= $(HELM_HOME)/plugins/helm-monitor
VERSION := $(shell sed -n -e 's/version:[ "]*\([^"]*\).*/\1/p' plugin.yaml)
DIST := $(CURDIR)/_dist
LDFLAGS := "-X main.version=${VERSION}"
BINARY := "helm-monitor"

.PHONY: dist release build install test lint vet dep

install: dep build
cp $(BINARY) $(HELM_PLUGIN_DIR)
cp plugin.yaml $(HELM_PLUGIN_DIR)

build:
go build -o $(BINARY) -ldflags $(LDFLAGS) ./cmd/...

dist:
mkdir -p $(DIST)
GOOS=linux GOARCH=amd64 go build -o $(BINARY) -ldflags $(LDFLAGS) ./cmd/...
tar -zcvf $(DIST)/helm-monitor_linux_$(VERSION).tgz $(BINARY) README.md LICENSE plugin.yaml
GOOS=darwin GOARCH=amd64 go build -o $(BINARY) -ldflags $(LDFLAGS) ./cmd/...
tar -zcvf $(DIST)/helm-monitor_macos_$(VERSION).tgz $(BINARY) README.md LICENSE plugin.yaml
GOOS=windows GOARCH=amd64 go build -o $(BINARY).exe -ldflags $(LDFLAGS) ./cmd/...
tar -zcvf $(DIST)/helm-monitor_windows_$(VERSION).tgz $(BINARY).exe README.md LICENSE plugin.yaml

test-all: vet lint test

test:
go test -v -parallel=4 ./...

lint:
@go get github.com/golang/lint/golint
go list ./... | grep -v vendor | xargs -n1 golint

vet:
go vet ./...

dep:
ifndef HAS_DEP
go get -u github.com/golang/dep/cmd/dep
endif
dep ensure
79 changes: 79 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
Helm Monitor
============

> Monitor a release, rollback to a previous version depending on the result of
a PromQL (Prometheus), Lucene or DSL query (ElasticSearch).

## Install

```bash
$ helm plugin install https://github.com/ContainerSolutions/helm-monitor
```

## Usage

A rollback happen only if the number of result from the query is greater than 0.

You can find a step-by-step example in the `./examples` directory.

### Prometheus

Monitor the **peeking-bunny** release against a Prometheus server, a rollback
is initiated if the 5xx error rate is over 0 for the last minute.

```bash
$ helm monitor peeking-bunny 'rate(http_requests_total{status=~"^5..$"}[1m]) > 0'
```

You can connect to a given Prometheus instance, by default it will connect to
*http://localhost:9091*.

```bash
$ helm monitor --prometheus http://prometheus.domain.com:9091 \
peeking-bunny \
'rate(http_requests_total{status=~"^5..$"}[1m]) > 0'
```

### ElasticSearch

Monitor the **peeking-bunny** release against an ElasticSearch server, a
rollback is initiated if the 5xx error rate is over 0 for the last minute.

```bash
$ helm monitor peeking-bunny ''
```

You can connect to a given ElasticSearch instance, by default it will connect to
*http://localhost:9200*.

```bash
$ helm monitor --prometheus http://prometheus.domain.com:9091 \
peeking-bunny \
'rate(http_requests_total{status=~"^5..$"}[1m]) > 0'
```

## Development

Clone the repo, then add a symlink to the Helm plugin directory:

```bash
$ ln -s $GOPATH/src/github.com/ContainerSolutions/helm-monitor ~/.helm/plugins/helm-monitor
```

Install dependencies using [dep](https://github.com/golang/dep):

```bash
$ dep ensure
```

Build:

```bash
$ go build -o helm-monitor ./cmd/...
```

Run:

```bash
$ helm monitor elasticsearch my-release ./examples/elasticsearch-query.json
```
62 changes: 62 additions & 0 deletions cmd/monitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"errors"
"io"
"os"

"github.com/spf13/cobra"
"google.golang.org/grpc"
"k8s.io/helm/pkg/helm"
helm_env "k8s.io/helm/pkg/helm/environment"
)

var (
settings helm_env.EnvSettings
)

const monitorDesc = `
This command monitor a release by querying Prometheus or Elasticsearch at a
given interval and take care of rolling back to the previous version if the
query return a non-empty result.
`

func setupConnection(c *cobra.Command, args []string) error {
settings.TillerHost = os.Getenv("TILLER_HOST")
return nil
}

func ensureHelmClient(h helm.Interface) helm.Interface {
if h != nil {
return h
}

return helm.NewClient(helm.Host(settings.TillerHost))
}

func prettyError(err error) error {
if err == nil {
return nil
}
return errors.New(grpc.ErrorDesc(err))
}

func newMonitorCmd(out io.Writer) *cobra.Command {
cmd := &cobra.Command{
Use: "monitor prometheus|elasticsearch",
Short: "monitor a release",
Long: monitorDesc,
}

cmd.AddCommand(newMonitorPrometheusCmd(nil, out))
cmd.AddCommand(newMonitorElasticSearchCmd(nil, out))

return cmd
}

func main() {
cmd := newMonitorCmd(os.Stdout)
if err := cmd.Execute(); err != nil {
os.Exit(1)
}
}
Loading

0 comments on commit 2945d7b

Please sign in to comment.