Skip to content

Commit

Permalink
setup logging as modern kubebuilder does it with zap
Browse files Browse the repository at this point in the history
  • Loading branch information
jabdoa2 committed Apr 27, 2024
1 parent de65b16 commit 8ac6d27
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 43 deletions.
48 changes: 22 additions & 26 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,21 @@ limitations under the License.
package main

import (
goflag "flag"
"flag"
"fmt"
"os"
"runtime"
"time"

"sigs.k8s.io/controller-runtime/pkg/cache"

"github.com/go-logr/glogr"
flag "github.com/spf13/pflag"
"github.com/wave-k8s/wave/pkg/apis"
"github.com/wave-k8s/wave/pkg/controller"
"github.com/wave-k8s/wave/pkg/webhook"
_ "k8s.io/client-go/plugin/pkg/client/auth"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client/config"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
)
Expand All @@ -43,36 +42,33 @@ var (
leaderElectionNamespace = flag.String("leader-election-namespace", "", "Namespace for the configmap used by the leader election system")
syncPeriod = flag.Duration("sync-period", 5*time.Minute, "Reconcile sync period")
showVersion = flag.Bool("version", false, "Show version and exit")
setupLog = ctrl.Log.WithName("setup")
)

func main() {
// Setup flags
err := goflag.Lookup("logtostderr").Value.Set("true")
if err != nil {
fmt.Printf("unable to set logtostderr %v", err)
os.Exit(1)
opts := zap.Options{
Development: true,
}
flag.CommandLine.AddGoFlagSet(goflag.CommandLine)
opts.BindFlags(flag.CommandLine)
flag.Parse()

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

if *showVersion {
fmt.Printf("wave %s (built with %s)\n", VERSION, runtime.Version())
return
}

logf.SetLogger(glogr.New())
log := logf.Log.WithName("entrypoint")

// Get a config to talk to the apiserver
log.Info("setting up client for manager")
setupLog.Info("setting up client for manager")
cfg, err := config.GetConfig()
if err != nil {
log.Error(err, "unable to set up client config")
setupLog.Error(err, "unable to set up client config")
os.Exit(1)
}

// Create a new Cmd to provide shared dependencies and start components
log.Info("setting up manager")
setupLog.Info("setting up manager")
mgr, err := manager.New(cfg, manager.Options{
LeaderElection: *leaderElection,
LeaderElectionID: *leaderElectionID,
Expand All @@ -82,36 +78,36 @@ func main() {
},
})
if err != nil {
log.Error(err, "unable to set up overall controller manager")
setupLog.Error(err, "unable to set up overall controller manager")
os.Exit(1)
}

log.Info("Registering Components.")
setupLog.Info("Registering Components.")

// Setup Scheme for all resources
log.Info("setting up scheme")
setupLog.Info("setting up scheme")
if err := apis.AddToScheme(mgr.GetScheme()); err != nil {
log.Error(err, "unable add APIs to scheme")
setupLog.Error(err, "unable add APIs to scheme")
os.Exit(1)
}

// Setup all Controllers
log.Info("Setting up controller")
setupLog.Info("Setting up controller")
if err := controller.AddToManager(mgr); err != nil {
log.Error(err, "unable to register controllers to the manager")
setupLog.Error(err, "unable to register controllers to the manager")
os.Exit(1)
}

log.Info("setting up webhooks")
setupLog.Info("setting up webhooks")
if err := webhook.AddToManager(mgr); err != nil {
log.Error(err, "unable to register webhooks to the manager")
setupLog.Error(err, "unable to register webhooks to the manager")
os.Exit(1)
}

// Start the Cmd
log.Info("Starting the Cmd.")
setupLog.Info("Starting the Cmd.")
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
log.Error(err, "unable to run the manager")
setupLog.Error(err, "unable to run the manager")
os.Exit(1)
}
}
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ module github.com/wave-k8s/wave
go 1.22.1

require (
github.com/go-logr/glogr v1.2.2
github.com/onsi/ginkgo v1.12.1
github.com/onsi/gomega v1.32.0
github.com/prometheus/client_golang v1.19.0
github.com/spf13/pflag v1.0.5
k8s.io/api v0.29.0
k8s.io/apimachinery v0.29.0
k8s.io/client-go v0.29.0
Expand All @@ -23,11 +21,11 @@ require (
github.com/evanphx/json-patch/v5 v5.8.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.1.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
Expand All @@ -46,6 +44,9 @@ require (
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.26.0 // indirect
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.20.0 // indirect
Expand Down
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/go-logr/glogr v1.2.2 h1:Jpc1ppf1yWnU+aRGFjIZeE2KfUnc1W4LV9XmndmP0QY=
github.com/go-logr/glogr v1.2.2/go.mod h1:u/37V9lMYDEmbMcbNNpRKnAB5Nof5FgtxhteHXbD3xY=
github.com/go-logr/logr v0.2.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
Expand All @@ -35,9 +32,6 @@ github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEe
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/glog v1.1.0 h1:/d3pCKDPWNnvIWe0vVUpNP32qc8U3PDVxySP/y360qE=
github.com/golang/glog v1.1.0/go.mod h1:pfYeQZ3JWZoXTV5sFc986z3HTpwQs9At6P4ImfuP3NQ=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/daemonset/daemonset_controller_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ import (

"github.com/wave-k8s/wave/test/reporters"

"github.com/go-logr/glogr"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
ctrl "sigs.k8s.io/controller-runtime"

"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

Expand All @@ -51,7 +51,7 @@ var _ = BeforeSuite(func() {
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crds")},
}

logf.SetLogger(glogr.New())
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

var err error
if cfg, err = t.Start(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/deployment/deployment_controller_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

ctrl "sigs.k8s.io/controller-runtime"

"github.com/go-logr/glogr"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/wave-k8s/wave/pkg/apis"
Expand All @@ -33,6 +32,7 @@ import (
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

Expand All @@ -53,7 +53,7 @@ var _ = BeforeSuite(func() {
}
apis.AddToScheme(scheme.Scheme)

logf.SetLogger(glogr.New())
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

var err error
if cfg, err = t.Start(); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

"github.com/wave-k8s/wave/test/reporters"

"github.com/go-logr/glogr"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -34,6 +33,7 @@ import (
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

Expand All @@ -54,7 +54,7 @@ var _ = BeforeSuite(func() {
}
apis.AddToScheme(scheme.Scheme)

logf.SetLogger(glogr.New())
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

var err error
if cfg, err = t.Start(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/core/wave_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"sync"
"testing"

"github.com/go-logr/glogr"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/wave-k8s/wave/pkg/apis"
Expand All @@ -32,6 +31,7 @@ import (
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
Expand All @@ -51,7 +51,7 @@ var _ = BeforeSuite(func() {
}
apis.AddToScheme(scheme.Scheme)

logf.SetLogger(glogr.New())
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

var err error
if cfg, err = t.Start(); err != nil {
Expand Down

0 comments on commit 8ac6d27

Please sign in to comment.