Skip to content

Commit

Permalink
adds logging prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
BugRoger committed Nov 27, 2017
1 parent 0688a9c commit ce12898
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
VERSION ?= $(shell git rev-parse --verify HEAD)
GOOS ?= $(shell go env | grep GOOS | cut -d'"' -f2)
BINARIES := apiserver kubernikus kubernikusctl wormhole
BINARIES := apiserver kubernikus kubernikusctl wormhole play

LDFLAGS := -X github.com/sapcc/kubernikus/pkg/version.GitCommit=$(VERSION)
GOFLAGS := -ldflags "$(LDFLAGS) -s -w"
Expand Down
139 changes: 139 additions & 0 deletions cmd/play/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package main

import (
"fmt"
"os"
"time"

"github.com/go-kit/kit/log"
)

type Kluster struct {
Name string
Pool []string
}

var KLUSTER = []Kluster{
Kluster{"fabus", []string{"mypool"}},
Kluster{"esther", []string{"swimmingpool"}},
Kluster{"michi", []string{"poo", "pool"}},
Kluster{"marian", []string{"schwarz", "weiss"}},
}

type Controller interface {
Run()
}

type Reconciler interface {
Reconcile(Kluster) (bool, error)
}

type controller struct {
Name string
Reconciler Reconciler
Logger log.Logger
}

func (c *controller) Run() {
c.Logger.Log("msg", "running")

for _, kluster := range KLUSTER {
requeue, _ := c.Reconciler.Reconcile(kluster)
if requeue {
c.Logger.Log("msg", "requeue requested", "kluster", kluster.Name)
c.Reconciler.Reconcile(kluster)
}
}
}

type launchReconciler struct {
Logger log.Logger
}

type groundReconciler struct {
Logger log.Logger
}

func NewLaunchControl(logger log.Logger) Controller {
logger = log.With(logger, "controller", "launch")
return &controller{
Name: "launch",
Reconciler: &launchReconciler{logger},
Logger: logger,
}
}

func NewGroundControl(logger log.Logger) Controller {
logger = log.With(logger, "controller", "ground")
return &controller{
Name: "ground",
Reconciler: &groundReconciler{logger},
Logger: logger,
}
}

func (r *launchReconciler) Reconcile(kluster Kluster) (requeue bool, err error) {
defer func(begin time.Time) {
r.Logger.Log(
"msg", "reconciled kluster",
"kluster", kluster.Name,
"reqeue", requeue,
"took", time.Since(begin),
"err", err)
}(time.Now())

if kluster.Name == "michi" {
return false, fmt.Errorf("reconciliation failed")
}

for _, pool := range kluster.Pool {
if err = r.ReconcilePool(kluster, pool); err != nil {
return
}
}

if kluster.Name == "fabus" {
return true, nil
}

return
}

func (r *launchReconciler) ReconcilePool(kluster Kluster, pool string) (err error) {
defer func(begin time.Time) {
r.Logger.Log(
"msg", "reconciled pool",
"kluster", kluster.Name,
"pool", pool,
"took", time.Since(begin),
"err", err)
}(time.Now())

return nil
}

func (r *groundReconciler) Reconcile(kluster Kluster) (requeue bool, err error) {
defer func(begin time.Time) {
r.Logger.Log(
"msg", "reconciled kluster",
"kluster", kluster.Name,
"reqeue", requeue,
"took", time.Since(begin),
"err", err)
}(time.Now())

if kluster.Name == "michi" {
return false, fmt.Errorf("reconciliation failed")
}

return false, nil
}

func main() {
w := log.NewSyncWriter(os.Stderr)
logger := log.NewLogfmtLogger(w)
logger = log.With(logger, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)

NewLaunchControl(logger).Run()
NewGroundControl(logger).Run()
}

0 comments on commit ce12898

Please sign in to comment.