Skip to content
This repository was archived by the owner on Apr 8, 2022. It is now read-only.

add telegram integration #133

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion Gopkg.lock

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

3 changes: 2 additions & 1 deletion cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
var configCmd = &cobra.Command{
Use: "config SUBCOMMAND",
Short: "config modifies kubewatch configuration",
Long: `config command allows admin setup his own configuration for running kubewatch`,
Long: `config command allows admin setup his own configuration for running kubewatch`,
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
Expand All @@ -38,4 +38,5 @@ func init() {
configCmd.AddCommand(resourceConfigCmd)
configCmd.AddCommand(flockConfigCmd)
configCmd.AddCommand(webhookConfigCmd)
configCmd.AddCommand(telegramCmd)
}
60 changes: 60 additions & 0 deletions cmd/telegram.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright © 2018 NAME HERE <EMAIL ADDRESS>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cmd

import (
"github.com/Sirupsen/logrus"
"github.com/bitnami-labs/kubewatch/config"
"github.com/spf13/cobra"
)

// telegramCmd represents the telegram command
var telegramCmd = &cobra.Command{
Use: "telegram",
Short: "specific telegram configuration",
Long: `specific telegram configuration`,
Run: func(cmd *cobra.Command, args []string) {
conf, err := config.New()
if err != nil {
logrus.Fatal(err)
}

token, err := cmd.Flags().GetString("token")
if err == nil {
if len(token) > 0 {
conf.Handler.Telegram.Token = token
}
} else {
logrus.Fatal(err)
}
channel, err := cmd.Flags().GetString("channel")
if err == nil {
if len(channel) > 0 {
conf.Handler.Telegram.Channel = channel
}
} else {
logrus.Fatal(err)
}

if err = conf.Write(); err != nil {
logrus.Fatal(err)
}
},
}

func init() {
telegramCmd.Flags().StringP("channel", "c", "", "Specify telegram channel")
telegramCmd.Flags().StringP("token", "t", "", "Specify telegram token")
}
14 changes: 14 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Handler struct {
Mattermost Mattermost `json:"mattermost"`
Flock Flock `json:"flock"`
Webhook Webhook `json:"webhook"`
Telegram Telegram `json:"telegram"`
}

// Resource contains resource configuration
Expand Down Expand Up @@ -91,6 +92,12 @@ type Webhook struct {
Url string `json:"url"`
}

// Telegram contains slack configuration
type Telegram struct {
Token string `json:"token"`
Channel string `json:"channel"`
}

// New creates new config object
func New() (*Config, error) {
c := &Config{}
Expand Down Expand Up @@ -186,6 +193,13 @@ func (c *Config) CheckMissingResourceEnvvars() {
if (c.Handler.Slack.Token == "") && (os.Getenv("SLACK_TOKEN") != "") {
c.Handler.Slack.Token = os.Getenv("SLACK_TOKEN")
}

if (c.Handler.Telegram.Channel == "") && (os.Getenv("TELEGRAM_CHANNEL") != "") {
c.Handler.Telegram.Channel = os.Getenv("TELEGRAM_CHANNEL")
}
if (c.Handler.Telegram.Token == "") && (os.Getenv("TELEGRAM_TOKEN") != "") {
c.Handler.Telegram.Token = os.Getenv("TELEGRAM_TOKEN")
}
}

func (c *Config) Write() error {
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ limitations under the License.

package main

import "github.com/bitnami-labs/kubewatch/cmd"
import (
"github.com/bitnami-labs/kubewatch/cmd"
)

func main() {
cmd.Execute()
Expand Down
9 changes: 6 additions & 3 deletions pkg/client/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ import (
"log"

"github.com/bitnami-labs/kubewatch/config"
"github.com/bitnami-labs/kubewatch/pkg/handlers"
"github.com/bitnami-labs/kubewatch/pkg/handlers/slack"
"github.com/bitnami-labs/kubewatch/pkg/controller"
"github.com/bitnami-labs/kubewatch/pkg/handlers"
"github.com/bitnami-labs/kubewatch/pkg/handlers/flock"
"github.com/bitnami-labs/kubewatch/pkg/handlers/hipchat"
"github.com/bitnami-labs/kubewatch/pkg/handlers/mattermost"
"github.com/bitnami-labs/kubewatch/pkg/handlers/flock"
"github.com/bitnami-labs/kubewatch/pkg/handlers/slack"
"github.com/bitnami-labs/kubewatch/pkg/handlers/telegram"
"github.com/bitnami-labs/kubewatch/pkg/handlers/webhook"
)

Expand All @@ -35,6 +36,8 @@ func Run(conf *config.Config) {
switch {
case len(conf.Handler.Slack.Channel) > 0 || len(conf.Handler.Slack.Token) > 0:
eventHandler = new(slack.Slack)
case len(conf.Handler.Telegram.Channel) > 0 || len(conf.Handler.Telegram.Token) > 0:
eventHandler = new(telegram.Telegram)
case len(conf.Handler.Hipchat.Room) > 0 || len(conf.Handler.Hipchat.Token) > 0:
eventHandler = new(hipchat.Hipchat)
case len(conf.Handler.Mattermost.Channel) > 0 || len(conf.Handler.Mattermost.Url) > 0:
Expand Down
16 changes: 9 additions & 7 deletions pkg/handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ package handlers

import (
"github.com/bitnami-labs/kubewatch/config"
"github.com/bitnami-labs/kubewatch/pkg/handlers/slack"
"github.com/bitnami-labs/kubewatch/pkg/handlers/flock"
"github.com/bitnami-labs/kubewatch/pkg/handlers/hipchat"
"github.com/bitnami-labs/kubewatch/pkg/handlers/mattermost"
"github.com/bitnami-labs/kubewatch/pkg/handlers/flock"
"github.com/bitnami-labs/kubewatch/pkg/handlers/slack"
"github.com/bitnami-labs/kubewatch/pkg/handlers/telegram"
"github.com/bitnami-labs/kubewatch/pkg/handlers/webhook"
)

Expand All @@ -36,12 +37,13 @@ type Handler interface {

// Map maps each event handler function to a name for easily lookup
var Map = map[string]interface{}{
"default": &Default{},
"slack": &slack.Slack{},
"hipchat": &hipchat.Hipchat{},
"default": &Default{},
"slack": &slack.Slack{},
"hipchat": &hipchat.Hipchat{},
"mattermost": &mattermost.Mattermost{},
"flock": &flock.Flock{},
"webhook": &webhook.Webhook{},
"flock": &flock.Flock{},
"webhook": &webhook.Webhook{},
"telegram": &telegram.Telegram{},
}

// Default handler implements Handler interface,
Expand Down
116 changes: 116 additions & 0 deletions pkg/handlers/telegram/telegram.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
Copyright 2016 Skippbox, Ltd.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package telegram

import (
"fmt"
"log"
"os"
"time"

// "gopkg.in/telegram-bot-api.v4"
tb "gopkg.in/tucnak/telebot.v2"

"github.com/bitnami-labs/kubewatch/config"
kbEvent "github.com/bitnami-labs/kubewatch/pkg/event"
)

var telegramErrMsg = `
%s

You need to set both token and channel for slack notify,
using "--token/-t" and "--channel/-c", or using environment variables:

export KW_TELEGRAM_TOKEN=telegram_token
export KW_TELEGRAM_CHANNEL=telegram_channel

Command line flags will override environment variables

`

// Telegram handler implements handler.Handler interface,
// Notify event to telegram channel
type Telegram struct {
Token string
Channel string
}

type TelegramMessage struct {
Text string `json:"text"`
}

// Init prepares slack configuration
func (s *Telegram) Init(c *config.Config) error {
token := c.Handler.Telegram.Token
channel := c.Handler.Telegram.Channel

if token == "" {
token = os.Getenv("KW_TELEGRAM_TOKEN")
}

if channel == "" {
channel = os.Getenv("KW_TELEGRAM_CHANNEL")
}

s.Token = token
s.Channel = channel

return checkMissingTelegramVars(s)
}

func (s *Telegram) ObjectCreated(obj interface{}) {
notifyTelegram(s, obj, "created")
}

func (s *Telegram) ObjectDeleted(obj interface{}) {
notifyTelegram(s, obj, "deleted")
}

func (s *Telegram) ObjectUpdated(oldObj, newObj interface{}) {
notifyTelegram(s, newObj, "updated")
}

func notifyTelegram(t *Telegram, obj interface{}, action string) {
e := kbEvent.New(obj, action)
b, err := tb.NewBot(tb.Settings{
Token: t.Token,
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
})

if err != nil {
log.Fatal(err)
return
}

stChannel := &tb.Chat{
Type: tb.ChatChannel,
Username: t.Channel,
}
var opts tb.SendOptions
opts.ParseMode = tb.ModeMarkdown

b.Send(stChannel, e.Message(), &opts)
log.Printf("Message successfully sent to channel %s", t.Channel)
}

func checkMissingTelegramVars(t *Telegram) error {
if t.Token == "" || t.Channel == "" {
return fmt.Errorf(telegramErrMsg, "Missing telegram token or channel")
}

return nil
}
48 changes: 48 additions & 0 deletions pkg/handlers/telegram/telegram_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright 2016 Skippbox, Ltd.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package telegram

import (
"fmt"
"reflect"
"testing"

"github.com/bitnami-labs/kubewatch/config"
)

func TestTelegramInit(t *testing.T) {
s := &Telegram{}
expectedError := fmt.Errorf(telegramErrMsg, "Missing telegram token or channel")

var Tests = []struct {
telegram config.Telegram
err error
}{
{config.Telegram{Token: "foo", Channel: "bar"}, nil},
{config.Telegram{Token: "foo"}, expectedError},
{config.Telegram{Channel: "bar"}, expectedError},
{config.Telegram{}, expectedError},
}

for _, tt := range Tests {
c := &config.Config{}
c.Handler.Telegram = tt.telegram
if err := s.Init(c); !reflect.DeepEqual(err, tt.err) {
t.Fatalf("Init(): %v, tt.err: %v", err, tt.err)
}
}
}
Loading