Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stimlog #9

Merged
merged 7 commits into from
Apr 19, 2019
Merged
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
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

func main() {
stim := stim.New()
// stim.AddStimpack(discover.New())
// stim.AddStimpack(discover.New())
stim.AddStimpack(aws.New())
stim.AddStimpack(kubernetes.New())
stim.AddStimpack(pagerduty.New())
Expand Down
17 changes: 14 additions & 3 deletions pkg/aws/aws.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package aws

import (
"github.com/readytalk/stim/pkg/log"
"github.com/readytalk/stim/pkg/stimlog"
// "github.com/aws/aws-sdk-go/aws"
// "github.com/aws/aws-sdk-go/aws/awserr"
// "github.com/aws/aws-sdk-go/aws/session"
Expand All @@ -13,11 +13,18 @@ import (
type Aws struct {
// client *slack.Client
config *Config
log log.Logger
log Logger
}

type Config struct {
Token string
Log Logger
}

type Logger interface {
Debug(...interface{})
Warn(...interface{})
Fatal(...interface{})
}

// New builds a client from the provided config
Expand All @@ -26,7 +33,11 @@ func New(config *Config) (*Aws, error) {
// client := slack.New(config.Token)

s := &Aws{config: config}

if config.Log != nil {
s.log = config.Log
} else {
s.log = stimlog.GetLogger()
}
return s, nil
}

Expand Down
16 changes: 8 additions & 8 deletions pkg/kubernetes/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (k *Kubernetes) modifyKubeconfig(o *KubeConfigOptions) error {
return err
}

k.Debug("Kubernetes config modified...")
k.log.Debug("Kubernetes config modified...")

return nil

Expand All @@ -53,10 +53,10 @@ func (k *Kubernetes) modifyKubeconfigCluster(kubeConfig *clientcmdapi.Config, o

stanza, exists := kubeConfig.Clusters[o.ClusterName]
if !exists {
k.Debug("Kubernetes cluster `" + o.ClusterName + "` not found in config, creating...")
k.log.Debug("Kubernetes cluster `" + o.ClusterName + "` not found in config, creating...")
stanza = clientcmdapi.NewCluster()
} else {
k.Debug("Kubernetes cluster `" + o.ClusterName + "` found, modifying...")
k.log.Debug("Kubernetes cluster `" + o.ClusterName + "` found, modifying...")
}

stanza.Server = o.ClusterServer
Expand All @@ -71,10 +71,10 @@ func (k *Kubernetes) modifyKubeconfigAuth(kubeConfig *clientcmdapi.Config, o *Ku

stanza, exists := kubeConfig.AuthInfos[o.AuthName]
if !exists {
k.Debug("Kubernetes auth `" + o.AuthName + "` not found in config, creating...")
k.log.Debug("Kubernetes auth `" + o.AuthName + "` not found in config, creating...")
stanza = clientcmdapi.NewAuthInfo()
} else {
k.Debug("Kubernetes auth `" + o.AuthName + "` found, modifying...")
k.log.Debug("Kubernetes auth `" + o.AuthName + "` found, modifying...")
}

stanza.Token = o.AuthToken
Expand All @@ -86,10 +86,10 @@ func (k *Kubernetes) modifyKubeconfigContext(kubeConfig *clientcmdapi.Config, o

stanza, exists := kubeConfig.Contexts[o.ContextName]
if !exists {
k.Debug("Kubernetes context `" + o.ContextName + "` not found in config, creating...")
k.log.Debug("Kubernetes context `" + o.ContextName + "` not found in config, creating...")
stanza = clientcmdapi.NewContext()
} else {
k.Debug("Kubernetes context `" + o.ContextName + "` found, modifying...")
k.log.Debug("Kubernetes context `" + o.ContextName + "` found, modifying...")
}

stanza.Cluster = o.ClusterName
Expand All @@ -99,7 +99,7 @@ func (k *Kubernetes) modifyKubeconfigContext(kubeConfig *clientcmdapi.Config, o

if o.ContextSetCurrent {
kubeConfig.CurrentContext = o.ContextName
k.Debug("Kubernetes current-context set to `" + o.ContextName + "`")
k.log.Debug("Kubernetes current-context set to `" + o.ContextName + "`")
}

}
34 changes: 13 additions & 21 deletions pkg/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,38 @@
package kubernetes

import (
"fmt"
"github.com/readytalk/stim/pkg/stimlog"
"k8s.io/client-go/tools/clientcmd"
)

type Kubernetes struct {
// client *api.Client
config *Config

log Logger
// This allows us to read/write the kube config
// It takes into account KUBECONFIG env var for setting the location
configAccess clientcmd.ConfigAccess
}

type Config struct {
// Address string
Logger
Log Logger
}

type Logger interface {
Debug(args ...interface{})
Info(args ...interface{})
Debug(...interface{})
Warn(...interface{})
Fatal(...interface{})
}

func (k *Kubernetes) Debug(message string) {
if k.config.Logger != nil {
k.config.Debug(message)
}
}
func New(kconf *Config) (*Kubernetes, error) {

k := &Kubernetes{config: kconf}

func (k *Kubernetes) Info(message string) {
if k.config.Logger != nil {
k.config.Info(message)
if kconf.Log != nil {
k.log = kconf.Log
} else {
fmt.Println(message)
k.log = stimlog.GetLogger()
}
}

func New(config *Config) (*Kubernetes, error) {

k := &Kubernetes{config: config}

return k, nil
}
Expand All @@ -49,7 +41,7 @@ func (k *Kubernetes) SetKubeconfig(kubeConfigOptions *KubeConfigOptions) error {

// configAccess is used by subcommands and methods in this package to load and modify the appropriate config files
k.configAccess = clientcmd.NewDefaultPathOptions()
k.Debug("Using kubeconfig file: " + k.configAccess.GetDefaultFilename())
k.log.Debug("Using kubeconfig file: " + k.configAccess.GetDefaultFilename())

err := k.modifyKubeconfig(kubeConfigOptions)
if err != nil {
Expand Down
37 changes: 0 additions & 37 deletions pkg/log/logger.go

This file was deleted.

19 changes: 12 additions & 7 deletions pkg/pagerduty/pagerduty.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ package pagerduty

import (
"errors"
pdApi "github.com/PagerDuty/go-pagerduty"
"github.com/readytalk/stim/pkg/log"
"github.com/readytalk/stim/pkg/utils"
"os"
"strings"

pdApi "github.com/PagerDuty/go-pagerduty"
"github.com/readytalk/stim/pkg/utils"
)

// Pagerduty is the main object
type Pagerduty struct {
client *pdApi.Client
log log.Logger
log Logger
}

// Event contains the required and optional fields to sent an event
Expand All @@ -29,13 +29,18 @@ type Event struct {
DedupKey string
}

type Logger interface {
Debug(...interface{})
Warn(...interface{})
Fatal(...interface{})
}

// New returns a new Pagerduty "instance"
func New(apiKey string, givenLog log.Logger) *Pagerduty {
func New(apiKey string, log Logger) *Pagerduty {

// Initialize client
client := pdApi.NewClient(apiKey)
p := &Pagerduty{client: client}
log.SetLogger(givenLog)
p := &Pagerduty{client: client, log: log}

return p
}
Expand Down
33 changes: 13 additions & 20 deletions pkg/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,30 @@ package prometheus

import (
"context"
"fmt"
"time"

"github.com/prometheus/client_golang/api"
"github.com/prometheus/client_golang/api/prometheus/v1"
"time"
"github.com/readytalk/stim/pkg/stimlog"
)

type Prometheus struct {
client *api.Client
config *Config
context context.Context
API v1.API
log Logger
}

type Config struct {
Address string
Logger
Log Logger
}

type Logger interface {
Debug(args ...interface{})
Info(args ...interface{})
}

func (p *Prometheus) Debug(message string) {
if p.config.Logger != nil {
p.config.Debug(message)
}
}

func (p *Prometheus) Info(message string) {
if p.config.Logger != nil {
p.config.Info(message)
} else {
fmt.Println(message)
}
Debug(...interface{})
Warn(...interface{})
Fatal(...interface{})
}

func New(config *Config) (*Prometheus, error) {
Expand All @@ -50,7 +39,11 @@ func New(config *Config) (*Prometheus, error) {
api := v1.NewAPI(client)

p := &Prometheus{client: &client, API: api, context: context.Background()}

if config.Log != nil {
p.log = config.Log
} else {
p.log = stimlog.GetLogger()
}
return p, nil
}

Expand Down
33 changes: 13 additions & 20 deletions pkg/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ package slack

import (
"errors"
"fmt"

"github.com/nlopes/slack"
"github.com/readytalk/stim/pkg/stimlog"
)

// Slack is the main object
type Slack struct {
client *slack.Client
log Logger
config *Config
}

// Config contains information about setting up a new slack client
type Config struct {
Token string
Logger
Log Logger
}

// Message contains information about a slack message
Expand All @@ -26,24 +28,10 @@ type Message struct {
IconUrl string
}

// Logger is an interface for passing a custom logger to be used by this package
type Logger interface {
Debug(args ...interface{})
Info(args ...interface{})
}

func (s *Slack) debug(message string) {
if s.config.Logger != nil {
s.config.Debug(message)
}
}

func (s *Slack) info(message string) {
if s.config.Logger != nil {
s.config.Info(message)
} else {
fmt.Println(message)
}
Debug(...interface{})
Warn(...interface{})
Fatal(...interface{})
}

// New builds a slack client from the provided config
Expand All @@ -52,6 +40,11 @@ func New(config *Config) (*Slack, error) {
client := slack.New(config.Token)

s := &Slack{config: config, client: client}
if config.Log != nil {
s.log = config.Log
} else {
s.log = stimlog.GetLogger()
}

return s, nil
}
Expand Down Expand Up @@ -112,7 +105,7 @@ func (s *Slack) PostMessage(msg *Message) error {
return err
}

s.debug("Slack message successfully sent at " + timestamp + " to channel " + msg.Channel + " (" + channelId + ")")
s.log.Debug("Slack message successfully sent at " + timestamp + " to channel " + msg.Channel + " (" + channelId + ")")

return nil
}
Loading