diff --git a/.gitignore b/.gitignore index af574d1..d71234d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ *.test *.out -slack-duty-bot config.yaml +slack-duty-bot +!/slack-duty-bot .kubernetes/deploy.yaml diff --git a/.helm/slack-duty-bot/.helmignore b/.helm/slack-duty-bot/.helmignore new file mode 100644 index 0000000..f0c1319 --- /dev/null +++ b/.helm/slack-duty-bot/.helmignore @@ -0,0 +1,21 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*~ +# Various IDEs +.project +.idea/ +*.tmproj diff --git a/.helm/slack-duty-bot/Chart.yaml b/.helm/slack-duty-bot/Chart.yaml new file mode 100644 index 0000000..b77a17e --- /dev/null +++ b/.helm/slack-duty-bot/Chart.yaml @@ -0,0 +1,5 @@ +apiVersion: v1 +appVersion: "1.0" +description: A Helm chart for Kubernetes +name: slack-duty-bot +version: 0.1.0 diff --git a/.helm/slack-duty-bot/templates/_helpers.tpl b/.helm/slack-duty-bot/templates/_helpers.tpl new file mode 100644 index 0000000..b83eb27 --- /dev/null +++ b/.helm/slack-duty-bot/templates/_helpers.tpl @@ -0,0 +1,32 @@ +{{/* vim: set filetype=mustache: */}} +{{/* +Expand the name of the chart. +*/}} +{{- define "slack-duty-bot.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}} +{{- end -}} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "slack-duty-bot.fullname" -}} +{{- if .Values.fullnameOverride -}} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- $name := default .Chart.Name .Values.nameOverride -}} +{{- if contains $name .Release.Name -}} +{{- .Release.Name | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}} +{{- end -}} +{{- end -}} +{{- end -}} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "slack-duty-bot.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}} +{{- end -}} diff --git a/.helm/slack-duty-bot/templates/configmap.yaml b/.helm/slack-duty-bot/templates/configmap.yaml new file mode 100644 index 0000000..e0e6964 --- /dev/null +++ b/.helm/slack-duty-bot/templates/configmap.yaml @@ -0,0 +1,19 @@ +kind: ConfigMap +apiVersion: v1 +metadata: + name: "{{ include "slack-duty-bot.name" . }}-config-map" +data: + config.yaml: |- + slack: + keyword: + {{- range $keyword := .Values.configuration.keywords }} + - {{ $keyword | quote}} + {{- end }} + duties: + - [ {{ .Values.dutyUsers.sunday | quote }} ] + - [ {{ .Values.dutyUsers.monday | quote }} ] + - [ {{ .Values.dutyUsers.tuesday | quote }} ] + - [ {{ .Values.dutyUsers.wednesday | quote }} ] + - [ {{ .Values.dutyUsers.thursday | quote }} ] + - [ {{ .Values.dutyUsers.friday | quote }} ] + - [ {{ .Values.dutyUsers.saturday | quote }} ] diff --git a/.helm/slack-duty-bot/templates/deployment.yaml b/.helm/slack-duty-bot/templates/deployment.yaml new file mode 100644 index 0000000..3fe038f --- /dev/null +++ b/.helm/slack-duty-bot/templates/deployment.yaml @@ -0,0 +1,43 @@ +apiVersion: apps/v1beta2 +kind: Deployment +metadata: + name: {{ include "slack-duty-bot.fullname" . }} + labels: + app.kubernetes.io/name: {{ include "slack-duty-bot.name" . }} + helm.sh/chart: {{ include "slack-duty-bot.chart" . }} + app.kubernetes.io/instance: {{ .Release.Name }} + app.kubernetes.io/managed-by: {{ .Release.Service }} +spec: + replicas: {{ .Values.replicaCount }} + selector: + matchLabels: + app.kubernetes.io/name: {{ include "slack-duty-bot.name" . }} + app.kubernetes.io/instance: {{ .Release.Name }} + template: + metadata: + labels: + app.kubernetes.io/name: {{ include "slack-duty-bot.name" . }} + app.kubernetes.io/instance: {{ .Release.Name }} + spec: + containers: + - name: {{ .Chart.Name }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + args: ["--config.path=/etc/slack-duty-bot"] + env: + - name: SDB_SLACK_TOKEN + valueFrom: + secretKeyRef: + name: "{{ include "slack-duty-bot.name" . }}-secret" + key: token + volumeMounts: + - name: "{{ include "slack-duty-bot.name" . }}-config-volume" + mountPath: /etc/slack-duty-bot + resources: +{{ toYaml .Values.resources | indent 12 }} + volumes: + - name: "{{ include "slack-duty-bot.name" . }}-config-volume" + configMap: + name: "{{ include "slack-duty-bot.name" . }}-config-map" + restartPolicy: Always + terminationGracePeriodSeconds: 30 diff --git a/.helm/slack-duty-bot/templates/secret.yaml b/.helm/slack-duty-bot/templates/secret.yaml new file mode 100644 index 0000000..2597957 --- /dev/null +++ b/.helm/slack-duty-bot/templates/secret.yaml @@ -0,0 +1,8 @@ +--- +apiVersion: v1 +kind: Secret +metadata: + name: "{{ include "slack-duty-bot.name" . }}-secret" +type: Opaque +data: + token: {{ .Values.configuration.slackToken | b64enc }} diff --git a/.helm/slack-duty-bot/values.yaml b/.helm/slack-duty-bot/values.yaml new file mode 100644 index 0000000..7078a03 --- /dev/null +++ b/.helm/slack-duty-bot/values.yaml @@ -0,0 +1,35 @@ +# Default values for slack-duty-bot. +# This is a YAML-formatted file. +# Declare variables to be passed into your templates. + +nameOverride: "slack-duty-bot" +fullnameOverride: "slack-duty-bot" + +replicaCount: 1 + +image: + repository: iqoption/slack-duty-bot + tag: 1.1.0 + pullPolicy: IfNotPresent + +configuration: + slackToken: "" + keywords: + - duty + +dutyUsers: + sunday: user.one, user.two + monday: user.one + tuesday: user.two + wednesday: user.one + thursday: user.two + friday: user.one + saturday: user.one, user.two + +resources: {} + # limits: + # cpu: 100m + # memory: 128Mi + # requests: + # cpu: 100m + # memory: 128Mi diff --git a/CHANGELOG.md b/CHANGELOG.md index 7888a8c..ab65ee2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ CHANGELOG * Update Makefile * Fix bug with setting "Debug" logger level * Add utils: bash script for preparing release artifacts + * Add helm support [#9 - Use helm for install in kubernetes](//github.com/iqoption/slack-duty-bot/issues/9) 1.1.0 ----- diff --git a/README.md b/README.md index 315be8a..78fad0e 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,11 @@ # Slack duty bot + [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![Go Report Card](https://goreportcard.com/badge/github.com/iqoption/slack-duty-bot)](https://goreportcard.com/report/github.com/iqoption/slack-duty-bot) [![Build Status](https://travis-ci.org/iqoption/slack-duty-bot.svg?branch=master)](https://travis-ci.org/iqoption/slack-duty-bot) -### Usage +## Usage + 1. Create new custom integration `Bots` (e.g https://{team}.slack.com/apps/manage/custom-integrations) 2. Add bot to channels you want to listen 3. Build for your environment or download [tarball with binary](https://github.com/iqoption/slack-duty-bot/releases) for your OS and arch @@ -18,7 +20,8 @@ SDB_SLACK_TOKEN=your-token-here ./slack-duty-bot \ --slack.group.name your-group-name ``` -You also can run in application in docker +You also can run in application in docker: + ```bash docker run \ --name slack-duty-bot \ @@ -35,17 +38,23 @@ docker run \ ``` ### Build package + ** Golang:1.11 is required ** Build + ```bash env GOOS=linux GOARCH=amd64 GO111MODULE=on go build -o slack-duty-bot -v ``` + Build via makefile + ```bash make GOOS=linux GOARCH=amd64 ``` + Build in docker + ```bash docker run \ --rm \ @@ -57,12 +66,12 @@ docker run \ golang:1.11 go build -o slack-duty-bot -v ``` -### Configuration +### Configuration flags, environment variables -#### Configuration flags, environment variables Environment variables are prefixed with `SDB_` and **MUST** be uppercase with `_` delimiter Available variables: + * `SDB_SLACK_TOKEN` * `SDB_SLACK_GROUP_ID` * `SDB_SLACK_GROUP_NAME` @@ -72,18 +81,21 @@ Available variables: Every environment variable can be overwritten by startup flags Available flags: + * `--slack.token` - Slack API client token (** Required **) * `--slack.keyword` - Case insensitive keywords slice to search in message text, can be set multiple times (default: []) (** Required **) * `--config.path` - Path to config.yaml file (default: . and $HOME/.slack-duty-bot) * `--slack.group.name` - Slack user group name, to mention in channel if duty list is empty * `--slack.group.id` - Slack user group ID, to mention in channel if duty list is empty -* `--slack.threads` - Use threads as reply target or push message direct to channel (default: true) +* `--slack.threads` - Use threads as reply target or push message direct to channel (default: true) * `--logger.level` - Log level (default: "info") You can get IDS from api or just use [testing page](https://api.slack.com/methods/usergroups.list/test) -#### Configuration file +### Configuration file + Configuration file **MUST** contain `duties` key with **7** slices of Slack user names + ```yaml duties: - [username.one, username.two] # Sunday @@ -95,19 +107,44 @@ duties: - [username.one, username.two] # Saturday ``` -#### Configuration priority +### Configuration priority + * Flags * Environment variables * Config file -### Deploy to Kubernetes +## Deploy to Kubernetes + +### Deploy with Helm + +#### Configuration + +The following table lists the configurable parameters of the slack-duty-bot chart and their default values. + +| Parameter | Description | Default | +| --------------------------------- | ------------------------------------------ | --------------------------------------------------------- | +| `image.repository` | SDB image registry | `iqoption/slack-duty-bot` | +| `image.tag` | SDB Image tag | `{VERSION}` | +| `image.pullPolicy` | SDB image pull policy | `IfNotPresent` | +| `configuration.slackToken` | Slack token | `nil` | +| `configuration.keywords` | Trigger words | array `duty` | + +#### Run deploy + +```bash +helm upgrade --install slack-duty-bot-my-app-name .helm/slack-duty-bot/ --set configuration.slackToken=secret-token,configuration.keywords[0]="duty",configuration.keywords[1]="autobot",dutyUsers.monday=user.one,dutyUsers.tuesday=user.two,dutyUsers.wednesday=user.one +``` + +## Manual deploy to Kubernetes + +### Create namespace -#### Create namespace ```bash kubectl create namespace slack-duty-bot ``` -#### Create namespace quota +### Create namespace quota + ```yaml #namespace-quota.yaml apiVersion: v1 @@ -121,11 +158,13 @@ spec: limits.cpu: "4" limits.memory: 2Gi ``` + ```bash kubectl create -f namespace-quota.yaml --namespace=slack-duty-bot ``` -#### Create limit range +### Create limit range + ```yaml #namespace-limit-range.yaml apiVersion: v1 @@ -142,11 +181,13 @@ limits: memory: 64Mi type: Container ``` + ```bash kubectl create -f namespace-limit-range.yaml --namespace=slack-duty-bot ``` -#### Prepare your deployment file +### Prepare your deployment file + ```bash (docker run \ --rm \ @@ -160,9 +201,10 @@ kubectl create -f namespace-limit-range.yaml --namespace=slack-duty-bot -e SDB_SLACK_GROUP_ID=group-id \ -e SDB_SLACK_GROUP_NAME=group-name \ supinf/envsubst /tmp/.kubernetes/deploy.yaml.tpl) > $(pwd)/.kubernetes/deploy.yaml - ``` + or use native `envsubst` + ```bash (SDB_SLACK_TOKEN_BASE64=your-token-hash \ SDB_NAME=your-deployment-name \ @@ -176,7 +218,8 @@ or use native `envsubst` After that you can change configuration with `kubect` or edit config map directly from Kubernetes dashboard -#### Deploy! +### Deploy + ```bash kubectl apply -f $(pwd)/.kubernetes/deploy.yaml --namespace slack-duty-bot ``` @@ -184,22 +227,26 @@ kubectl apply -f $(pwd)/.kubernetes/deploy.yaml --namespace slack-duty-bot ## Contributing ### Travis-CI and tests + To enable tests for your fork repository you **MUST**: * Create your project in [TravisCI](http://travis-ci.com) for your fork repository * Add environment variables to Travis-CI project: - * `DOCKER_NAMESPACE` - * `DOCKER_USER` - * `DOCKER_PASSWORD` + * `DOCKER_NAMESPACE` + * `DOCKER_USER` + * `DOCKER_PASSWORD` Travis-CI will run test on every push for every ref and build docker image and push to [docker hub](http://hub.docker.io) *ONLY FOR TAGS* ## Changelog + [Changelog for project](CHANGELOG.md) ## Roadmap + [Roadmap for project](ROADMAP.md) ## Authors + * [Konstantin Perminov](https://github.com/SpiLLeR) -* [Ageev Pavel](https://github.com/insidieux) +* [Ageev Pavel](https://github.com/insidieux) \ No newline at end of file