Skip to content

Commit

Permalink
server: redirect all controller-runtime logs to klog (#4357)
Browse files Browse the repository at this point in the history
Unlike most other K8s libraries/projects, controller-runtime uses
a logr abstract interface internally. Unfortunately, it doesn't
_really_ make use of levels internally very much, so redirecting
all its logs to the Tilt debug log adds a bunch of noise (see #4350).

As an alternative approach, we can use `klogr` which is a logr impl
using klog under the hood and redirect it to a higher verbosity
klog instance. Given that apiserver internal logs use klog already,
this gives a single, consistent way to get internal logging across
these components. In the future, we might want to re-introduce a
Tilt-specific facade for these and have more granular handling by
each component to avoid noisy libraries from flooding the output,
but this is a good interim step.
  • Loading branch information
milas committed Mar 23, 2021
1 parent 37855e4 commit 99821c7
Show file tree
Hide file tree
Showing 21 changed files with 428 additions and 1,120 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ require (
k8s.io/cli-runtime v0.20.2
k8s.io/client-go v0.20.2
k8s.io/code-generator v0.20.2
k8s.io/klog/v2 v2.4.0
k8s.io/klog/v2 v2.8.0
k8s.io/kube-openapi v0.0.0-20201113171705-d219536bb9fd
k8s.io/kubectl v0.20.2
sigs.k8s.io/controller-runtime v0.8.2
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,8 @@ github.com/go-logr/logr v0.2.1 h1:fV3MLmabKIZ383XifUjFSwcoGee0v9qgPp8wy5svibE=
github.com/go-logr/logr v0.2.1/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
github.com/go-logr/logr v0.3.0 h1:q4c+kbcR0d5rSurhBR8dIgieOaYpXtsdTYfx22Cu6rs=
github.com/go-logr/logr v0.3.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
github.com/go-logr/logr v0.4.0 h1:K7/B1jt6fIBQVd4Owv2MqGQClcgf0R266+7C/QjRcLc=
github.com/go-logr/logr v0.4.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU=
github.com/go-logr/zapr v0.1.0/go.mod h1:tabnROwaDl0UNxkVeFRbY8bwB37GwRv0P8lg6aAiEnk=
github.com/go-logr/zapr v0.2.0 h1:v6Ji8yBW77pva6NkJKQdHLAJKrIJKRHz0RXwPqCHSR4=
github.com/go-logr/zapr v0.2.0/go.mod h1:qhKdvif7YF5GI9NWEpyxTSSBdGmzkNguibrdCNVPunU=
Expand Down Expand Up @@ -1834,6 +1836,8 @@ k8s.io/klog/v2 v2.2.0 h1:XRvcwJozkgZ1UQJmfMGpvRthQHOvihEhYtDfAaxMz/A=
k8s.io/klog/v2 v2.2.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y=
k8s.io/klog/v2 v2.4.0 h1:7+X0fUguPyrKEC4WjH8iGDg3laWgMo5tMnRTIGTTxGQ=
k8s.io/klog/v2 v2.4.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y=
k8s.io/klog/v2 v2.8.0 h1:Q3gmuM9hKEjefWFFYF0Mat+YyFJvsUyYuwyNNJ5C9Ts=
k8s.io/klog/v2 v2.8.0/go.mod h1:hy9LJ/NvuK+iVyP4Ehqva4HxZG/oXyIS3n3Jmire4Ec=
k8s.io/kube-openapi v0.0.0-20180731170545-e3762e86a74c/go.mod h1:BXM9ceUBTj2QnfH2MK1odQs778ajze1RxcmP6S8RVVc=
k8s.io/kube-openapi v0.0.0-20191107075043-30be4d16710a/go.mod h1:1TqjTSzOxsLGIKfj0lK8EeCP7K1iUG65v09OM0/WG5E=
k8s.io/kube-openapi v0.0.0-20200121204235-bf4fb3bd569c/go.mod h1:GRQhZsXIAJ1xR0C9bd8UpWHZ5plfAS9fzPjJuQ6JL3E=
Expand Down
11 changes: 6 additions & 5 deletions internal/controllers/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import (

"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"k8s.io/klog/v2/klogr"
ctrl "sigs.k8s.io/controller-runtime"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/cluster"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

"github.com/tilt-dev/tilt/internal/hud/server"
"github.com/tilt-dev/tilt/internal/store"
"github.com/tilt-dev/tilt/pkg/logger"
)

type TiltServerControllerManager struct {
Expand Down Expand Up @@ -51,9 +50,11 @@ func (m *TiltServerControllerManager) GetClient() ctrlclient.Client {
func (m *TiltServerControllerManager) SetUp(ctx context.Context, st store.RStore) error {
ctx, m.cancel = context.WithCancel(ctx)

// TODO(milas): we should provide a logr.Logger facade for our logger rather than using zap
w := logger.Get(ctx).Writer(logger.DebugLvl)
ctrl.SetLogger(zap.New(zap.UseDevMode(true), zap.WriteTo(w)))
// controller-runtime internals don't really make use of verbosity levels, so in lieu of a better
// mechanism, all its logs are redirected to klog, for which there is already special handling
// V(3) was picked because while controller-runtime is a bit chatty at startup, once steady state
// is reached, most of the logging is generally useful (e.g. reconciler errors)
ctrl.SetLogger(klogr.New().V(3).WithName("tilt"))

mgr, err := ctrl.NewManager(m.config, ctrl.Options{
Scheme: m.scheme,
Expand Down
36 changes: 26 additions & 10 deletions vendor/github.com/go-logr/logr/discard.go

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

82 changes: 63 additions & 19 deletions vendor/github.com/go-logr/logr/logr.go

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

3 changes: 0 additions & 3 deletions vendor/github.com/go-logr/zapr/.gitignore

This file was deleted.

52 changes: 0 additions & 52 deletions vendor/github.com/go-logr/zapr/Gopkg.lock

This file was deleted.

38 changes: 0 additions & 38 deletions vendor/github.com/go-logr/zapr/Gopkg.toml

This file was deleted.

0 comments on commit 99821c7

Please sign in to comment.