Skip to content

Commit

Permalink
Merge pull request #35 from robusta-dev/control_log_level
Browse files Browse the repository at this point in the history
Add msteams to readme
  • Loading branch information
arikalon1 authored Mar 8, 2023
2 parents 4d6eaa0 + 9fe11cb commit f21ee87
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 39 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# Latest image

```
us-central1-docker.pkg.dev/genuine-flight-317411/devel/kubewatch:v2.0
us-central1-docker.pkg.dev/genuine-flight-317411/devel/kubewatch:v2.2
```

# Usage
Expand All @@ -34,6 +34,7 @@ for resource changes and notifies them through webhooks.
supported webhooks:
- slack
- slackwebhook
- msteams
- hipchat
- mattermost
- flock
Expand Down Expand Up @@ -509,6 +510,16 @@ $ kubewatch resource add --rc --po --svc
$ kubewatch resource remove --rc --po --svc
```

### Changing log level

In case you change the default log level, add an environment variable named `LOG_LEVEL` with value from `trace/debug/info/warning/error`

```yaml
env:
- name: LOG_LEVEL
value: debug
```
# Build
### Using go
Expand Down
16 changes: 16 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ func Execute() {
func init() {
cobra.OnInitialize(initConfig)

initLogger()

// Disable Help subcommand
RootCmd.SetHelpCommand(&cobra.Command{
Use: "no-help",
Expand All @@ -90,6 +92,20 @@ func init() {
}
}

func initLogger() {
logLevel := os.Getenv("LOG_LEVEL")
if logLevel != "" {
logrus.Printf("Custom log level: %s", logLevel)
parsedLevel, err := logrus.ParseLevel(logLevel)
if err == nil {
logrus.Printf("Setting custom log level to: %s", logLevel)
logrus.SetLevel(parsedLevel)
} else {
logrus.Errorf("Illegal custom log level: %s. Ignoring custom log level", logLevel)
}
}
}

// initConfig reads in config file and ENV variables if set.
func initConfig() {
if cfgFile != "" { // enable ability to specify config file via flag
Expand Down
2 changes: 1 addition & 1 deletion helm/kubewatch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ In case you want to add extra environment variables (useful for advanced operati
```yaml
extraEnvVars:
- name: LOG_LEVEL
value: DEBUG
value: debug
```
Alternatively, you can use a ConfigMap or a Secret with the environment variables. To do so, use the `extraEnvVarsCM` or the `extraEnvVarsSecret` values.
Expand Down
2 changes: 1 addition & 1 deletion helm/kubewatch/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ extraDeploy: []
image:
registry: us-central1-docker.pkg.dev
repository: genuine-flight-317411/devel/kubewatch
tag: v2.1
tag: v2.2
## Specify a imagePullPolicy
## Defaults to 'Always' if image tag is 'latest', else set to 'IfNotPresent'
## ref: https://kubernetes.io/docs/user-guide/images/#pre-pulling-images
Expand Down
5 changes: 2 additions & 3 deletions pkg/client/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ limitations under the License.
package client

import (
"log"

"github.com/bitnami-labs/kubewatch/config"
"github.com/bitnami-labs/kubewatch/pkg/controller"
"github.com/bitnami-labs/kubewatch/pkg/handlers"
Expand All @@ -31,6 +29,7 @@ import (
"github.com/bitnami-labs/kubewatch/pkg/handlers/slackwebhook"
"github.com/bitnami-labs/kubewatch/pkg/handlers/smtp"
"github.com/bitnami-labs/kubewatch/pkg/handlers/webhook"
"github.com/sirupsen/logrus"
)

// Run runs the event loop processing with given handler
Expand Down Expand Up @@ -67,7 +66,7 @@ func ParseEventHandler(conf *config.Config) handlers.Handler {
eventHandler = new(handlers.Default)
}
if err := eventHandler.Init(conf); err != nil {
log.Fatal(err)
logrus.Fatal(err)
}
return eventHandler
}
6 changes: 3 additions & 3 deletions pkg/handlers/cloudevent/cloudevent.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package cloudevent

import (
"fmt"
"log"
"github.com/sirupsen/logrus"
"os"

"bytes"
Expand Down Expand Up @@ -95,11 +95,11 @@ func (m *CloudEvent) Handle(e event.Event) {

err := m.postMessage(message)
if err != nil {
log.Printf("%s\n", err)
logrus.Printf("%s\n", err)
return
}

log.Printf("Message successfully sent to %s at %s ", m.Url, time.Now())
logrus.Printf("Message successfully sent to %s at %s ", m.Url, time.Now())
}

func (m *CloudEvent) prepareMessage(e event.Event) *CloudEventMessage {
Expand Down
6 changes: 3 additions & 3 deletions pkg/handlers/flock/flock.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package flock

import (
"fmt"
"log"
"github.com/sirupsen/logrus"
"os"

"bytes"
Expand Down Expand Up @@ -92,11 +92,11 @@ func (f *Flock) Handle(e event.Event) {

err := postMessage(f.Url, flockMessage)
if err != nil {
log.Printf("%s\n", err)
logrus.Printf("%s\n", err)
return
}

log.Printf("Message successfully sent to channel %s at %s", f.Url, time.Now())
logrus.Printf("Message successfully sent to channel %s at %s", f.Url, time.Now())
}

func checkMissingFlockVars(s *Flock) error {
Expand Down
6 changes: 3 additions & 3 deletions pkg/handlers/hipchat/hipchat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package hipchat

import (
"fmt"
"log"
"github.com/sirupsen/logrus"
"os"

hipchat "github.com/tbruyelle/hipchat-go/hipchat"
Expand Down Expand Up @@ -97,11 +97,11 @@ func (s *Hipchat) Handle(e event.Event) {
_, err := client.Room.Notification(s.Room, &notificationRequest)

if err != nil {
log.Printf("%s\n", err)
logrus.Printf("%s\n", err)
return
}

log.Printf("Message successfully sent to room %s", s.Room)
logrus.Printf("Message successfully sent to room %s", s.Room)
}

func checkMissingHipchatVars(s *Hipchat) error {
Expand Down
6 changes: 3 additions & 3 deletions pkg/handlers/mattermost/mattermost.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package mattermost

import (
"fmt"
"log"
"github.com/sirupsen/logrus"
"os"

"bytes"
Expand Down Expand Up @@ -104,11 +104,11 @@ func (m *Mattermost) Handle(e event.Event) {

err := postMessage(m.Url, mattermostMessage)
if err != nil {
log.Printf("%s\n", err)
logrus.Printf("%s\n", err)
return
}

log.Printf("Message successfully sent to channel %s at %s", m.Channel, time.Now())
logrus.Printf("Message successfully sent to channel %s at %s", m.Channel, time.Now())
}

func checkMissingMattermostVars(s *Mattermost) error {
Expand Down
6 changes: 3 additions & 3 deletions pkg/handlers/msteam/msteam.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"bytes"
"encoding/json"
"fmt"
"github.com/sirupsen/logrus"
"io/ioutil"
"log"
"net/http"
"os"

Expand Down Expand Up @@ -145,9 +145,9 @@ func (ms *MSTeams) Handle(e event.Event) {
card.Sections = append(card.Sections, s)

if _, err := sendCard(ms, card); err != nil {
log.Printf("%s\n", err)
logrus.Printf("%s\n", err)
return
}

log.Printf("Message successfully sent to MS Teams")
logrus.Printf("Message successfully sent to MS Teams")
}
6 changes: 3 additions & 3 deletions pkg/handlers/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package slack

import (
"fmt"
"log"
"github.com/sirupsen/logrus"
"os"

"github.com/slack-go/slack"
Expand Down Expand Up @@ -91,11 +91,11 @@ func (s *Slack) Handle(e event.Event) {
slack.MsgOptionAttachments(attachment),
slack.MsgOptionAsUser(true))
if err != nil {
log.Printf("%s\n", err)
logrus.Printf("%s\n", err)
return
}

log.Printf("Message successfully sent to channel %s at %s", channelID, timestamp)
logrus.Printf("Message successfully sent to channel %s at %s", channelID, timestamp)
}

func checkMissingSlackVars(s *Slack) error {
Expand Down
8 changes: 4 additions & 4 deletions pkg/handlers/slackwebhook/slackwebhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ package slackwebhook

import (
"fmt"
"log"
"github.com/sirupsen/logrus"
"os"
"time"

Expand Down Expand Up @@ -111,16 +111,16 @@ func (m *SlackWebhook) Handle(e event.Event) {
IconEmoji: m.Emoji,
}

log.Printf("slackwebhook-handle():Slackwebhook WebHookMessage: %s", webhookMessage.Text)
logrus.Printf("slackwebhook-handle():Slackwebhook WebHookMessage: %s", webhookMessage.Text)

err := slack.PostWebhook(m.Slackwebhookurl, &webhookMessage)

if err != nil {
log.Printf("slackwebhook-handle() Error: %s\n", err)
logrus.Printf("slackwebhook-handle() Error: %s\n", err)
return
}

log.Printf("Message successfully sent to %s at %s. Message: %s", m.Slackwebhookurl, time.Now(), webhookMessage.Text)
logrus.Printf("Message successfully sent to %s at %s. Message: %s", m.Slackwebhookurl, time.Now(), webhookMessage.Text)
}

func checkMissingWebhookVars(s *SlackWebhook) error {
Expand Down
3 changes: 1 addition & 2 deletions pkg/handlers/smtp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"context"
"crypto/tls"
"fmt"
"log"
"math/rand"
"mime"
"mime/multipart"
Expand Down Expand Up @@ -220,7 +219,7 @@ func sendEmail(conf config.SMTP, msg string) error {
return fmt.Errorf("write body buffer: %w", err)
}

log.Printf("sending via %s:%s, to: %q, from: %q : %s ", host, port, conf.To, conf.From, msg)
logrus.Printf("sending via %s:%s, to: %q, from: %q : %s ", host, port, conf.To, conf.From, msg)
return nil
}

Expand Down
3 changes: 1 addition & 2 deletions pkg/handlers/smtp/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package smtp

import (
"fmt"
"log"
"time"

"github.com/bitnami-labs/kubewatch/config"
Expand Down Expand Up @@ -73,7 +72,7 @@ func (s *SMTP) Init(c *config.Config) error {
// Handle handles the notification.
func (s *SMTP) Handle(e event.Event) {
send(s.cfg, e.Message())
log.Printf("Message successfully sent to %s at %s ", s.cfg.To, time.Now())
logrus.Printf("Message successfully sent to %s at %s ", s.cfg.To, time.Now())
}

func formatEmail(e event.Event) (string, error) {
Expand Down
10 changes: 5 additions & 5 deletions pkg/handlers/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"github.com/sirupsen/logrus"
"io/ioutil"
"log"
"os"

"bytes"
Expand Down Expand Up @@ -86,11 +86,11 @@ func (m *Webhook) Init(c *config.Config) error {
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
} else {
if cert == "" {
log.Printf("No webhook cert is given")
logrus.Printf("No webhook cert is given")
} else {
caCert, err := ioutil.ReadFile(cert)
if err != nil {
log.Printf("%s\n", err)
logrus.Printf("%s\n", err)
return err
}
caCertPool := x509.NewCertPool()
Expand All @@ -109,11 +109,11 @@ func (m *Webhook) Handle(e event.Event) {

err := postMessage(m.Url, webhookMessage)
if err != nil {
log.Printf("%s\n", err)
logrus.Printf("%s\n", err)
return
}

log.Printf("Message successfully sent to %s at %s ", m.Url, time.Now())
logrus.Printf("Message successfully sent to %s at %s ", m.Url, time.Now())
}

func checkMissingWebhookVars(s *Webhook) error {
Expand Down
4 changes: 2 additions & 2 deletions tools/yannotated/yannotated.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ package main
import (
"flag"
"fmt"
"github.com/sirupsen/logrus"
"go/ast"
"go/parser"
"go/token"
"io"
"log"
"os"
"strings"

Expand Down Expand Up @@ -191,6 +191,6 @@ func main() {
flag.Parse()

if err := mainE(flags); err != nil {
log.Fatal(err)
logrus.Fatal(err)
}
}

0 comments on commit f21ee87

Please sign in to comment.