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

icq support #147

Open
wants to merge 2 commits 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
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.

5 changes: 5 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
name = "github.com/tbruyelle/hipchat-go"
version = "1.2.0"

[[constraint]]
name = "gopkg.in/icq.v1"
version = "1.0.0"
source = "https://github.com/go-icq/icq"

[[constraint]]
branch = "v2"
name = "gopkg.in/yaml.v2"
Expand Down
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(icqConfigCmd)
}
62 changes: 62 additions & 0 deletions cmd/icq.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright azalio.net

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"
)

// icqConfigCmd represents the icq subcommand
var icqConfigCmd = &cobra.Command{
Use: "icq FLAG",
Short: "specific icq configuration",
Long: `specific icq 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.Icq.Token = token
}
} else {
logrus.Fatal(err)
}
channel, err := cmd.Flags().GetString("uid")
if err == nil {
if len(channel) > 0 {
conf.Handler.Icq.Uid = channel
}
} else {
logrus.Fatal(err)
}

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

func init() {
icqConfigCmd.Flags().StringP("uid", "u", "", "Specify icq uid")
icqConfigCmd.Flags().StringP("token", "t", "", "Specify icq token")
}
7 changes: 7 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"`
Icq Icq `json:"icq"`
}

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

// Icq contains icq configuration
type Icq struct {
Token string `json:"token"`
Uid string `json:"uid"`
}

// New creates new config object
func New() (*Config, error) {
c := &Config{}
Expand Down
14 changes: 14 additions & 0 deletions examples/conf/kubewatch.conf.icq.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
handler:
icq:
token: 000.0000000.00000000:00000000 # Get token from megabot bot.
uid: [email protected] # Change UID.
resource:
deployment: true
replicationcontroller: false
replicaset: false
daemonset: false
services: false
pod: false
job: false
persistentvolume: false
ingress: false
10 changes: 7 additions & 3 deletions pkg/client/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ 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/icq"
"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/webhook"
)

Expand All @@ -43,6 +45,8 @@ func Run(conf *config.Config) {
eventHandler = new(flock.Flock)
case len(conf.Handler.Webhook.Url) > 0:
eventHandler = new(webhook.Webhook)
case len(conf.Handler.Icq.Token) > 0 || len(conf.Handler.Icq.Uid) > 0:
eventHandler = new(icq.Icq)
default:
eventHandler = new(handlers.Default)
}
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/icq"
"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/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{},
"icq": &icq.Icq{},
}

// Default handler implements Handler interface,
Expand Down
106 changes: 106 additions & 0 deletions pkg/handlers/icq/icq.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
Copyright azalio.net

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 icq

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

"github.com/bitnami-labs/kubewatch/config"
kbEvent "github.com/bitnami-labs/kubewatch/pkg/event"
icq "gopkg.in/icq.v1"
)

var icqErrMsg = `
%s

You need to set both icq token and uid for icq notify,
using "--token/-t" and "--uid/-u", or using environment variables:

export KW_ICQ_TOKEN=icq_token
export KW_ICQ_UID=icq_uid

Command line flags will override environment variables

`

// Icq handler implements handler.Handler interface,
// Notify event to icq uid
type Icq struct {
Token string
Uid string
}

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

// Init prepares Icq configuration
func (s *Icq) Init(c *config.Config) error {
token := c.Handler.Icq.Token
uid := c.Handler.Icq.Uid

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

if uid == "" {
uid = os.Getenv("KW_ICQ_UID")
}

s.Token = token
s.Uid = uid

return checkMissingIcqVars(s)
}

func (s *Icq) ObjectCreated(obj interface{}) {
notifyIcq(s, obj, "created")
}

func (s *Icq) ObjectDeleted(obj interface{}) {
notifyIcq(s, obj, "deleted")
}

func (s *Icq) ObjectUpdated(oldObj, newObj interface{}) {
notifyIcq(s, newObj, "updated")
}

func notifyIcq(s *Icq, obj interface{}, action string) {
e := kbEvent.New(obj, action)
api := icq.NewAPI(s.Token)

icqMessage := e.Message()

r, err := api.SendMessage(s.Uid, icqMessage)
if err != nil {
log.Printf("%s\n", err)
return
}

log.Printf("Message successfully sent to channel %s with state %s at %s", s.Uid, r.State, time.Now())
}

func checkMissingIcqVars(s *Icq) error {
if s.Token == "" || s.Uid == "" {
return fmt.Errorf(icqErrMsg, "Missing Icq token or uid")
}

return nil
}
48 changes: 48 additions & 0 deletions pkg/handlers/icq/icq_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright azalio.net

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 icq

import (
"fmt"
"reflect"
"testing"

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

func TestIcqInit(t *testing.T) {
s := &Icq{}
expectedError := fmt.Errorf(icqErrMsg, "Missing Icq token or uid")

var Tests = []struct {
icq config.Icq
err error
}{
{config.Icq{Token: "foo", Uid: "bar"}, nil},
{config.Icq{Token: "foo"}, expectedError},
{config.Icq{Uid: "bar"}, expectedError},
{config.Icq{}, expectedError},
}

for _, tt := range Tests {
c := &config.Config{}
c.Handler.Icq = tt.icq
if err := s.Init(c); !reflect.DeepEqual(err, tt.err) {
t.Fatalf("Init(): %v", err)
}
}
}
14 changes: 14 additions & 0 deletions vendor/gopkg.in/icq.v1/.gitignore

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

21 changes: 21 additions & 0 deletions vendor/gopkg.in/icq.v1/LICENSE

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

Loading