Skip to content

Commit

Permalink
Add leveled logging (#129)
Browse files Browse the repository at this point in the history
* Add leveled logging

I reused the `—v` flag for glog to set the desiered log level. By default we only log messages with no level or level 0.
For the flag processing to work I had to move the logger creation into the cobra commands `Run` functions because otherwise I can’t access the flag without hussle.

I moved the logging setup to logutil.NewLogger() to that we can reuse that function in all binaries.

* Remove unused functions

* Refactor wormhole server

use one flat Options struct for both the controller and the tunnel.
They are not meant to be run independently and share some things (e.g. logger)

Also consolidated the server in a single package because
  • Loading branch information
databus23 committed Dec 20, 2017
1 parent 857118a commit 6d03605
Show file tree
Hide file tree
Showing 20 changed files with 188 additions and 153 deletions.
21 changes: 5 additions & 16 deletions cmd/apiserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"fmt"
"os"

kitLog "github.com/go-kit/kit/log"
"github.com/go-stack/stack"
"github.com/spf13/pflag"

apipkg "github.com/sapcc/kubernikus/pkg/api"
Expand All @@ -24,16 +22,13 @@ func init() {
}

func main() {
var logger kitLog.Logger
logger = kitLog.NewLogfmtLogger(kitLog.NewSyncWriter(os.Stderr))
logger = logutil.NewTrailingNilFilter(logger)
logger = kitLog.With(logger, "ts", kitLog.DefaultTimestampUTC, "caller", Caller(3))
if f := goflag.Lookup("logtostderr"); f != nil {
f.Value.Set("true") // log to stderr by default
}

swaggerSpec, err := spec.Spec()
if err != nil {
logger.Log(
"msg", "failed to spec swagger spec",
"err", err)
fmt.Printf(`failed to parse swagger spec: %s`, err)
os.Exit(1)
}

Expand All @@ -52,12 +47,10 @@ func main() {
fmt.Fprintln(os.Stderr, pflag.CommandLine.FlagUsages())
}
// parse the CLI flags
if f := goflag.Lookup("logtostderr"); f != nil {
f.Value.Set("true") // log to stderr by default
}
pflag.CommandLine.AddGoFlagSet(goflag.CommandLine) //slurp in glog flags
pflag.Parse()
//goflag.CommandLine.Parse([]string{}) //https://github.com/kubernetes/kubernetes/issues/17162
logger := logutil.NewLogger(pflag.CommandLine)

api := operations.NewKubernikusAPI(swaggerSpec)

Expand Down Expand Up @@ -98,7 +91,3 @@ func main() {
}

}

func Caller(depth int) kitLog.Valuer {
return func() interface{} { return fmt.Sprintf("%+v", stack.Caller(depth)) }
}
7 changes: 4 additions & 3 deletions cmd/kubernikus/main.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package main

import (
goflag "flag"
"os"
"path/filepath"

"github.com/golang/glog"

"github.com/sapcc/kubernikus/pkg/cmd"
"github.com/sapcc/kubernikus/pkg/cmd/kubernikus"
)

func main() {
defer glog.Flush()
if f := goflag.Lookup("logtostderr"); f != nil {
f.Value.Set("true") // log to stderr by default
}

baseName := filepath.Base(os.Args[0])

Expand Down
11 changes: 4 additions & 7 deletions cmd/kubernikusctl/main.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
package main

import (
"flag"
goflag "flag"
"os"
"path/filepath"

"github.com/golang/glog"

"github.com/sapcc/kubernikus/pkg/cmd"
"github.com/sapcc/kubernikus/pkg/cmd/kubernikusctl"
)

func main() {
defer glog.Flush()

baseName := filepath.Base(os.Args[0])
if f := flag.Lookup("logtostderr"); f != nil {
if f := goflag.Lookup("logtostderr"); f != nil {
f.Value.Set("true")
}

baseName := filepath.Base(os.Args[0])

err := kubernikusctl.NewCommand(baseName).Execute()
cmd.CheckError(err)
}
3 changes: 0 additions & 3 deletions cmd/nanny/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ import (
"os"
"path/filepath"

"github.com/golang/glog"

"github.com/sapcc/kubernikus/pkg/cmd"
"github.com/sapcc/kubernikus/pkg/cmd/nanny"
)

func main() {
defer glog.Flush()
if f := goflag.Lookup("logtostderr"); f != nil {
f.Value.Set("true") // log to stderr by default
}
Expand Down
7 changes: 2 additions & 5 deletions cmd/wormhole/main.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package main

import (
"flag"
goflag "flag"
"os"
"path/filepath"

"github.com/golang/glog"

"github.com/sapcc/kubernikus/pkg/cmd"
"github.com/sapcc/kubernikus/pkg/cmd/wormhole"
)

func main() {
defer glog.Flush()
if f := flag.Lookup("logtostderr"); f != nil {
if f := goflag.Lookup("logtostderr"); f != nil {
f.Value.Set("true") // log to stderr by default
}

Expand Down
10 changes: 1 addition & 9 deletions pkg/cmd/kubernikus/certificates.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
package kubernikus

import (
"os"

"github.com/go-kit/kit/log"
"github.com/spf13/cobra"

"github.com/sapcc/kubernikus/pkg/cmd/kubernikus/certificates"
logutil "github.com/sapcc/kubernikus/pkg/util/log"
)

func NewCertificatesCommand() *cobra.Command {
var logger log.Logger
logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
logger = logutil.NewTrailingNilFilter(logger)
logger = log.With(logger, "ts", log.DefaultTimestampUTC, "caller", Caller(3))

c := &cobra.Command{
Use: "certificates",
Expand All @@ -24,7 +16,7 @@ func NewCertificatesCommand() *cobra.Command {
c.AddCommand(
certificates.NewFilesCommand(),
certificates.NewPlainCommand(),
certificates.NewSignCommand(logger),
certificates.NewSignCommand(),
)

return c
Expand Down
14 changes: 6 additions & 8 deletions pkg/cmd/kubernikus/certificates/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"

"github.com/ghodss/yaml"
"github.com/go-kit/kit/log"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -16,10 +15,11 @@ import (
"github.com/sapcc/kubernikus/pkg/client/kubernetes"
"github.com/sapcc/kubernikus/pkg/cmd"
"github.com/sapcc/kubernikus/pkg/util"
logutil "github.com/sapcc/kubernikus/pkg/util/log"
)

func NewSignCommand(logger log.Logger) *cobra.Command {
o := NewSignOptions(logger)
func NewSignCommand() *cobra.Command {
o := NewSignOptions()

c := &cobra.Command{
Use: "sign KLUSTER",
Expand All @@ -42,17 +42,14 @@ type SignOptions struct {
CA string
Organization string
ApiURL string

Logger log.Logger
}

func NewSignOptions(logger log.Logger) *SignOptions {
func NewSignOptions() *SignOptions {
return &SignOptions{
Namespace: "kubernikus",
CA: "apiserver-clients-ca",
CN: os.Getenv("USER"),
Organization: "system:masters",
Logger: logger,
}
}

Expand Down Expand Up @@ -85,7 +82,8 @@ func (o *SignOptions) Complete(args []string) error {
}

func (o *SignOptions) Run(c *cobra.Command) error {
client, err := kubernetes.NewClient(o.KubeConfig, "", o.Logger)
logger := logutil.NewLogger(c.Flags())
client, err := kubernetes.NewClient(o.KubeConfig, "", logger)
if err != nil {
return err
}
Expand Down
18 changes: 2 additions & 16 deletions pkg/cmd/kubernikus/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ package kubernikus

import (
"errors"
goflag "flag"
"fmt"
"os"
"os/signal"
"sync"
"syscall"

"github.com/go-kit/kit/log"
"github.com/go-stack/stack"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

Expand All @@ -23,10 +19,6 @@ import (
func NewOperatorCommand() *cobra.Command {
o := NewOperatorOptions()

if f := goflag.Lookup("logtostderr"); f != nil {
f.Value.Set("true") // log to stderr by default
}

c := &cobra.Command{
Use: "operator",
Short: "Starts an operator that operates things. Beware of magic!",
Expand Down Expand Up @@ -91,10 +83,8 @@ func (o *Options) Complete(args []string) error {
}

func (o *Options) Run(c *cobra.Command) error {
var logger log.Logger
logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
logger = logutil.NewTrailingNilFilter(logger)
logger = log.With(logger, "ts", log.DefaultTimestampUTC, "caller", Caller(3))

logger := logutil.NewLogger(c.Flags())

sigs := make(chan os.Signal, 1)
stop := make(chan struct{})
Expand All @@ -116,7 +106,3 @@ func (o *Options) Run(c *cobra.Command) error {

return nil
}

func Caller(depth int) log.Valuer {
return func() interface{} { return fmt.Sprintf("%+v", stack.Caller(depth)) }
}
10 changes: 1 addition & 9 deletions pkg/cmd/kubernikus/seed.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
package kubernikus

import (
"os"

"github.com/go-kit/kit/log"
"github.com/spf13/cobra"

"github.com/sapcc/kubernikus/pkg/cmd/kubernikus/seed"
logutil "github.com/sapcc/kubernikus/pkg/util/log"
)

func NewSeedCommand() *cobra.Command {
var logger log.Logger
logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
logger = logutil.NewTrailingNilFilter(logger)
logger = log.With(logger, "ts", log.DefaultTimestampUTC, "caller", Caller(3))

c := &cobra.Command{
Use: "seed",
Short: "Seed stuff",
}

c.AddCommand(
seed.NewKubeDNSCommand(logger),
seed.NewKubeDNSCommand(),
)

return c
Expand Down
14 changes: 6 additions & 8 deletions pkg/cmd/kubernikus/seed/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package seed
import (
"errors"

"github.com/go-kit/kit/log"
"github.com/spf13/cobra"
"github.com/spf13/pflag"

"github.com/sapcc/kubernikus/pkg/client/kubernetes"
"github.com/sapcc/kubernikus/pkg/cmd"
"github.com/sapcc/kubernikus/pkg/controller/ground/bootstrap/dns"
logutil "github.com/sapcc/kubernikus/pkg/util/log"
)

func NewKubeDNSCommand(logger log.Logger) *cobra.Command {
o := NewKubeDNSOptions(logger)
func NewKubeDNSCommand() *cobra.Command {
o := NewKubeDNSOptions()

c := &cobra.Command{
Use: "dns",
Expand All @@ -37,16 +37,13 @@ type KubeDNSOptions struct {
version string
domain string
clusterIP string

Logger log.Logger
}

func NewKubeDNSOptions(logger log.Logger) *KubeDNSOptions {
func NewKubeDNSOptions() *KubeDNSOptions {
return &KubeDNSOptions{
repository: dns.DEFAULT_REPOSITORY,
version: dns.DEFAULT_VERSION,
domain: dns.DEFAULT_DOMAIN,
Logger: logger,
}
}

Expand All @@ -71,7 +68,8 @@ func (o *KubeDNSOptions) Complete(args []string) error {
}

func (o *KubeDNSOptions) Run(c *cobra.Command) error {
client, err := kubernetes.NewClient(o.kubeConfig, o.context, o.Logger)
logger := logutil.NewLogger(c.Flags())
client, err := kubernetes.NewClient(o.kubeConfig, o.context, logger)
if err != nil {
return err
}
Expand Down
11 changes: 3 additions & 8 deletions pkg/cmd/nanny/nanny.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package nanny
import (
"flag"
"net"
"os"
"time"

"github.com/go-kit/kit/log"
"github.com/gophercloud/gophercloud/openstack/identity/v3/tokens"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand All @@ -26,7 +24,7 @@ func NewCommand(name string) *cobra.Command {
if err := cmd.Validate(o, c, args); err != nil {
return err
}
return run(o)
return o.Run(c)
},
}
o.BindFlags(c.Flags())
Expand Down Expand Up @@ -66,11 +64,8 @@ func (o *Options) BindFlags(flags *pflag.FlagSet) {
flags.DurationVar(&o.SyncPeriod, "sync-period", o.SyncPeriod, "How often should the sync handler run.")
}

func run(o *Options) error {
var logger log.Logger
logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
logger = logutil.NewTrailingNilFilter(logger)
logger = log.With(logger, "ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
func (o *Options) Run(c *cobra.Command) error {
logger := logutil.NewLogger(c.Flags())

group := cmd.Runner()
authOpts := tokens.AuthOptions{
Expand Down
Loading

0 comments on commit 6d03605

Please sign in to comment.