Skip to content

Commit

Permalink
Merge pull request #36 from jelmersnoeck/use-logrus
Browse files Browse the repository at this point in the history
Use logrus instead of stdlib log.
  • Loading branch information
jelmersnoeck authored Mar 24, 2019
2 parents 1778538 + 1c74e4c commit aa9efbe
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 52 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## v0.3.1 - 2019-03-24

### Changed

- We're now using logrus instead of the stdlib log package.

## v0.3.0 - 2019-01-18

### Added

- Added functionality to ensure that SSL checks are enabled with StatusCake

## v0.2.0 - 2018-10-31

### Added
Expand Down
17 changes: 17 additions & 0 deletions Gopkg.lock

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

7 changes: 2 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,10 @@ test: vendor
CGO_ENABLED=0 go test -v ./...

linters:
go get -u github.com/alecthomas/gometalinter
gometalinter --install
go get -u github.com/golangci/golangci-lint/cmd/golangci-lint

lint:
gometalinter --tests --disable-all --vendor --deadline=5m -e ".*generated.*" \
-E gofmt -E golint -E gosimple -E vet -E misspell -E ineffassign -E deadcode \
$(LINTER_PKGS)
golangci-lint run -D errcheck -D staticcheck ./...

cover: vendor
CGO_ENABLED=0 go test -v -coverprofile=coverage.txt -covermode=atomic ./...
Expand Down
16 changes: 16 additions & 0 deletions cmd/ingress-monitor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,28 @@
package main

import (
"os"
"strconv"

"github.com/jelmersnoeck/ingress-monitor/internal/ingressmonitor/cmd"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)

func main() {
viper.SetEnvPrefix("im")

debugString, ok := os.LookupEnv("DEBUG")
if ok {
debug, err := strconv.ParseBool(debugString)
if err != nil {
panic(err)
}

if debug {
logrus.SetLevel(logrus.DebugLevel)
}
}

cmd.Execute()
}
18 changes: 9 additions & 9 deletions internal/ingressmonitor/cmd/operator.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"log"
"os"
"time"

Expand All @@ -15,9 +14,10 @@ import (
"github.com/jelmersnoeck/ingress-monitor/pkg/client/generated/clientset/versioned"

"github.com/prometheus/client_golang/prometheus"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"k8s.io/api/core/v1"

v1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
Expand All @@ -44,22 +44,22 @@ func runOperator(cmd *cobra.Command, args []string) {

resync, err := time.ParseDuration(operatorFlags.ResyncPeriod)
if err != nil {
log.Fatalf("Error parsing ResyncPeriod: %s", err)
logrus.WithError(err).Fatal("Error parsing ResyncPeriod")
}

cfg, err := clientcmd.BuildConfigFromFlags(operatorFlags.MasterURL, operatorFlags.KubeConfig)
if err != nil {
log.Fatalf("Error building kubeconfig: %s", err.Error())
logrus.WithError(err).Fatal("Error building kubeconfig")
}

kubeClient, err := kubernetes.NewForConfig(cfg)
if err != nil {
log.Fatalf("Error building Kubernetes clientset: %s", err)
logrus.WithError(err).Fatal("Error building Kubernetes clientset")
}

imClient, err := versioned.NewForConfig(cfg)
if err != nil {
log.Fatalf("Error building IngressMonitor clientset: %s", err)
logrus.WithError(err).Fatal("Error building IngressMonitor clientset")
}

// register the available providers
Expand Down Expand Up @@ -88,11 +88,11 @@ func runOperator(cmd *cobra.Command, args []string) {
resync, fact, mtrc,
)
if err != nil {
log.Fatalf("Error building IngressMonitor Operator: %s", err)
logrus.WithError(err).Fatalf("Error building IngressMonitor Operator")
}

if err := op.Run(stopCh); err != nil {
log.Fatalf("Error running the operator: %s", err)
logrus.WithError(err).Fatalf("Error running the operator")
}
}

Expand Down
75 changes: 51 additions & 24 deletions internal/ingressmonitor/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"html/template"
"log"
"strings"
"time"

Expand All @@ -33,6 +32,7 @@ import (
"k8s.io/kubernetes/pkg/apis/extensions"

"github.com/dchest/blake2b"
"github.com/sirupsen/logrus"
)

const (
Expand Down Expand Up @@ -130,24 +130,24 @@ func (o *Operator) Run(stopCh <-chan struct{}) error {
defer o.monitorQueue.ShutDown()
defer o.ingressMonitorQueue.ShutDown()

log.Printf("Starting IngressMonitor Operator")
logrus.Infof("Starting IngressMonitor Operator")
if err := o.connectToCluster(stopCh); err != nil {
return err
}

log.Printf("Starting the informers")
logrus.Infof("Starting the informers")
if err := o.startInformers(stopCh); err != nil {
return err
}

log.Printf("Starting the workers")
logrus.Infof("Starting the workers")
for i := 0; i < 4; i++ {
go wait.Until(runWorker(o.processNextIngressMonitor), time.Second, stopCh)
go wait.Until(runWorker(o.processNextMonitor), time.Second, stopCh)
}

<-stopCh
log.Printf("Stopping IngressMonitor Operator")
logrus.Infof("Stopping IngressMonitor Operator")

return nil
}
Expand All @@ -161,7 +161,9 @@ func (o *Operator) connectToCluster(stopCh <-chan struct{}) error {
return
}

log.Printf("Connected to the cluster (version %s)", v)
logrus.WithFields(logrus.Fields{
"cluster_version": v,
}).Info("Connected to the cluster")
errCh <- nil
}()

Expand All @@ -179,27 +181,28 @@ func (o *Operator) connectToCluster(stopCh <-chan struct{}) error {

func (o *Operator) startInformers(stopCh <-chan struct{}) error {
for _, inf := range o.informers {
log.Printf("Starting informer %s", inf.name)
logrus.WithFields(logrus.Fields{"name": inf.name}).Info("Starting informer")
go inf.informer.Run(stopCh)
}

if err := o.waitForCaches(stopCh); err != nil {
return err
}

log.Printf("Synced all caches")
logrus.Infof("Synced all caches")
return nil
}

func (o *Operator) waitForCaches(stopCh <-chan struct{}) error {
var syncFailed bool
for _, inf := range o.informers {
log.Printf("Waiting for cache sync for %s", inf.name)
log := logrus.WithFields(logrus.Fields{"name": inf.name})
log.Info("Waiting for cache sync for")
if !cache.WaitForCacheSync(stopCh, inf.informer.HasSynced) {
log.Printf("Could not sync cache for %s", inf.name)
log.Infof("Could not sync cache for")
syncFailed = true
} else {
log.Printf("Synced cache for %s", inf.name)
log.Infof("Synced cache for")
}
}

Expand All @@ -226,6 +229,9 @@ func (o *Operator) processNextMonitor() bool {
}

func (o *Operator) handleNextItem(name string, queue workqueue.RateLimitingInterface, handlerFunc func(string) error) bool {
log := logrus.WithFields(logrus.Fields{
"queue_name": name,
})
obj, shutdown := queue.Get()

if shutdown {
Expand All @@ -241,7 +247,7 @@ func (o *Operator) handleNextItem(name string, queue workqueue.RateLimitingInter
if key, ok = obj.(string); !ok {
queue.Forget(obj)

log.Printf("Expected object name in %s workqueue, got %#v", name, obj)
log.Infof("Expected object name workqueue, got %#v", obj)
return nil
}

Expand All @@ -250,12 +256,14 @@ func (o *Operator) handleNextItem(name string, queue workqueue.RateLimitingInter
}

queue.Forget(obj)
log.Printf("Synced '%s' in %s workqueue", key, name)
log.WithFields(logrus.Fields{
"key": key,
}).Debug("Synced key in workqueue")
return nil
}(obj)

if err != nil {
log.Printf(err.Error())
log.WithError(err).Error("Error handling the queue")
}

return true
Expand Down Expand Up @@ -312,32 +320,45 @@ func (o *Operator) OnDelete(obj interface{}) {

cl, err := o.providerFactory.From(obj.Spec.Provider)
if err != nil {
log.Printf("Could not get provider for IngressMonitor %s:%s: %s", obj.Namespace, obj.Name, err)
logDeleteErr("ingress_monitor", obj.Namespace, obj.Name, err, "could not get provider for IngressMonitor")
return
}

if err := cl.Delete(obj.Status.ID); err != nil {
log.Printf("Could not delete IngressMonitor %s:%s: %s", obj.Namespace, obj.Name, err)
logDeleteErr("ingress_monitor", obj.Namespace, obj.Name, err, "could not delete IngressMonitor")
return
}
case *v1alpha1.Monitor:
imList, err := o.imClient.IngressMonitors(obj.Namespace).
List(listOptions(map[string]string{monitorLabel: obj.Name}))
if err != nil {
log.Printf("Could not list IngressMonitors for Monitors %s:%s: %s", obj.Namespace, obj.Name, err)
logDeleteErr("monitor", obj.Namespace, obj.Name, err, "could not list IngressMonitors for Monitor")
return
}

for _, im := range imList.Items {
log.Printf("Deleting IngressMonitor `%s:%s` associated with deleted Monitor `%s:%s`", im.Namespace, im.Name, obj.Namespace, obj.Name)
ll := logrus.WithFields(logrus.Fields{
"ingress_monitor_namespace": im.Namespace,
"ingress_monitor_name": im.Name,
"monitor_namespace": obj.Namespace,
"monitor_name": obj.Name,
})
ll.Debug("Deleting IngressMonitor")
if err := o.imClient.IngressMonitors(obj.Namespace).
Delete(im.Name, &metav1.DeleteOptions{}); err != nil {
log.Printf("Could not delete IngressMonitor %s for Monitors %s:%s: %s", im.Name, obj.Namespace, obj.Name, err)
ll.WithError(err).Error("could not delete IngressMonitor for Monitor")
}
}
}
}

func logDeleteErr(prefix, ns, name string, err error, msg string) {
logrus.WithFields(logrus.Fields{
fmt.Sprintf("%s_namespace", prefix): ns,
fmt.Sprintf("%s_name", prefix): name,
}).WithError(err).Error(msg)
}

// handleIngressMonitor handles IngressMonitors in a way that it knows how to
// deal with creating and updating resources.
func (o *Operator) handleIngressMonitor(key string) (err error) {
Expand Down Expand Up @@ -435,11 +456,14 @@ func (o *Operator) garbageCollectMonitors(obj *v1alpha1.Monitor) error {
// reconciliation to take care of actually removing the monitor with the
// provider.
if !isActive {
log.Printf("Deleting IngressMonitor %s:%s with GC", im.Namespace, im.Name)
ll := logrus.WithFields(logrus.Fields{
"ingress_monitor_namespace": im.Namespace,
"ingress_monitor_name": im.Name,
})
ll.Debug("Deleting IngressMonitor with GC")
if err := o.imClient.IngressMonitors(im.Namespace).
Delete(im.Name, &metav1.DeleteOptions{}); err != nil {

log.Printf("Could not delete IngressMonitor %s:%s: %s", im.Namespace, im.Name, err)
ll.WithError(err).Error("Could not delete IngressMonitor")
}
}
})
Expand Down Expand Up @@ -474,7 +498,7 @@ func (o *Operator) handleMonitor(key string) error {
}

if len(ingressList) == 0 {
log.Printf("No ingresses selected for %s:%s", obj.Namespace, obj.Name)
logrus.Infof("No ingresses selected for %s:%s", obj.Namespace, obj.Name)
return nil
}

Expand Down Expand Up @@ -578,7 +602,10 @@ func (o *Operator) handleMonitor(key string) error {
return fmt.Errorf("Could not ensure IngressMonitor: %s", err)
}

log.Printf("Successfully synced IngressMonitor %s:%s", im.Namespace, im.Name)
logrus.WithFields(logrus.Fields{
"ingress_monitor_namespace": im.Namespace,
"ingress_monitor_name": im.Name,
}).Debug("successfully synced IngressMonitor")
}
}

Expand Down
Loading

0 comments on commit aa9efbe

Please sign in to comment.