Skip to content

Commit

Permalink
Set up simplified logging for in-cluster event sender
Browse files Browse the repository at this point in the history
This prevents it from failing with (in some setups):

```
mkdir /.cache: permission denied
```
  • Loading branch information
cardil committed Sep 25, 2024
1 parent ceb59ae commit eb5f95b
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 20 deletions.
4 changes: 1 addition & 3 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,4 @@ linters-settings:
- knative.dev/client/pkg/*
gomoddirectives:
# List of allowed `replace` directives. Default is empty.
replace-allow-list:
# TODO: Remove when https://github.com/knative/client/pull/1968 is merged
- knative.dev/client/pkg
replace-allow-list: []
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ require (
k8s.io/api v0.30.3
k8s.io/apimachinery v0.30.3
k8s.io/client-go v0.30.3
knative.dev/client/pkg v0.0.0-20240903134911-f09e7164ceaf
knative.dev/client/pkg v0.0.0-20240925104631-c9f128423b58
knative.dev/eventing v0.42.1-0.20240828134450-34f9cd384dea
knative.dev/hack v0.0.0-20240814130635-06f7aff93954
knative.dev/pkg v0.0.0-20240815051656-89743d9bbf7c
Expand All @@ -32,9 +32,6 @@ require (
sigs.k8s.io/yaml v1.4.0
)

// TODO: Remove when https://github.com/knative/client/pull/1968 is merged
replace knative.dev/client/pkg => github.com/cardil/knative-client/pkg v0.0.0-20240923095307-3a2bcba04752

require (
contrib.go.opencensus.io/exporter/ocagent v0.7.1-0.20200907061046-05415f1de66d // indirect
contrib.go.opencensus.io/exporter/prometheus v0.4.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,8 @@ k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 h1:pUdcCO1Lk/tbT5ztQWOBi5HBgbBP1
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
knative.dev/client-pkg v0.0.0-20240808015000-22f598931483 h1:jBfmxcR0H5Z9IzamelZtmmg9jfeOXfssllUVX5M4Xzs=
knative.dev/client-pkg v0.0.0-20240808015000-22f598931483/go.mod h1:Y56KfZx3gJJpju88l86jQ9csxywLiopR0GkxCWW3+Kg=
knative.dev/client/pkg v0.0.0-20240925104631-c9f128423b58 h1:EXXrHvJNGuWakfy27sJz0GvVSNuf5uzH6BW+m2dBq3w=
knative.dev/client/pkg v0.0.0-20240925104631-c9f128423b58/go.mod h1:JR3XomuVf2cBqgvXFONkX6Ebf1/gJwUnl/1OH47U18g=
knative.dev/eventing v0.42.1-0.20240828134450-34f9cd384dea h1:j3bFBE797vD6IZJsECQ5lEENumLp817rkQxANrbKxHs=
knative.dev/eventing v0.42.1-0.20240828134450-34f9cd384dea/go.mod h1:Clx8z37Nwg321H9+vGNxp5C6bVdo4l4XM5g6T5CgZVI=
knative.dev/hack v0.0.0-20240814130635-06f7aff93954 h1:dGMK5VoL75szvrYQTL9NqhPYHu1f5dGaXx1hJI8fAFM=
Expand Down
3 changes: 2 additions & 1 deletion go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ github.com/buildkite/interpolate v0.0.0-20200526001904-07f35b4ae251 h1:k6UDF1uPY
github.com/buildkite/interpolate v0.0.0-20200526001904-07f35b4ae251/go.mod h1:gbPR1gPu9dB96mucYIR7T3B7p/78hRVSOuzIWLHK2Y4=
github.com/c2h5oh/datasize v0.0.0-20231215233829-aa82cc1e6500 h1:6lhrsTEnloDPXyeZBvSYvQf8u86jbKehZPVDDlkgDl4=
github.com/c2h5oh/datasize v0.0.0-20231215233829-aa82cc1e6500/go.mod h1:S/7n9copUssQ56c7aAgHqftWO4LTf4xY6CGWt8Bc+3M=
github.com/cardil/knative-client/pkg v0.0.0-20240923095307-3a2bcba04752/go.mod h1:JR3XomuVf2cBqgvXFONkX6Ebf1/gJwUnl/1OH47U18g=
github.com/cavaliercoder/badio v0.0.0-20160213150051-ce5280129e9e h1:YYUjy5BRwO5zPtfk+aa2gw255FIIoi93zMmuy19o0bc=
github.com/cavaliercoder/badio v0.0.0-20160213150051-ce5280129e9e/go.mod h1:V284PjgVwSk4ETmz84rpu9ehpGg7swlIH8npP9k2bGw=
github.com/cavaliercoder/go-rpm v0.0.0-20200122174316-8cb9fd9c31a8 h1:jP7ki8Tzx9ThnFPLDhBYAhEpI2+jOURnHQNURgsMvnY=
Expand Down Expand Up @@ -676,6 +675,8 @@ k8s.io/kms v0.30.3 h1:NLg+oN45S2Y3U0WiLRzbS61AY/XrS5JBMZp531Z+Pho=
k8s.io/kms v0.30.3/go.mod h1:GrMurD0qk3G4yNgGcsCEmepqf9KyyIrTXYR2lyUOJC4=
knative.dev/caching v0.0.0-20240812133420-93e6a0a5b46d h1:pj+XufdayIdxV/PUFpuIB1Y/pFoiWPJT6VrAXja4OcE=
knative.dev/caching v0.0.0-20240812133420-93e6a0a5b46d/go.mod h1:vxgDv5XOAYFa2LrreP8quBiyKB2HB2fqWG6nc6LmDR4=
knative.dev/client/pkg v0.0.0-20240903134911-f09e7164ceaf h1:QPGwYLkkMwssyEw0ek8T1u4Q3+FSeVPSH4IIKThdngc=
knative.dev/client/pkg v0.0.0-20240903134911-f09e7164ceaf/go.mod h1:JR3XomuVf2cBqgvXFONkX6Ebf1/gJwUnl/1OH47U18g=
knative.dev/hack/schema v0.0.0-20240814130635-06f7aff93954 h1:0yjDplGHUnZ8NpcfgmH0thXSzG28VSM16hb3Vz171l8=
knative.dev/hack/schema v0.0.0-20240814130635-06f7aff93954/go.mod h1:jRH/sx6mwwuMVhvJgnzSaoYA1N4qaIkJa+zxEGtVA5I=
mvdan.cc/sh/v3 v3.7.0 h1:lSTjdP/1xsddtaKfGg7Myu7DnlHItd3/M2tomOcNNBg=
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (a *App) Command() *cobra.Command {
if a.Verbose {
lvl = zapcore.DebugLevel

Check warning on line 59 in internal/cli/root.go

View check run for this annotation

Codecov / codecov/patch

internal/cli/root.go#L59

Added line #L59 was not covered by tests
}
cli.SetupContext(cmd, lvl)
cli.SetupContext(cmd, cli.DefaultLoggingSetup(lvl))
}
c.PersistentPostRunE = func(cmd *cobra.Command, _ []string) error {
closer := outlogging.LogFileCloserFrom(cmd.Context())
Expand Down
8 changes: 1 addition & 7 deletions internal/ics/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/wavesoftware/go-commandline"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
outlogging "knative.dev/client/pkg/output/logging"
"knative.dev/kn-plugin-event/pkg/binding"
"knative.dev/kn-plugin-event/pkg/cli"
"knative.dev/kn-plugin-event/pkg/k8s"
Expand All @@ -28,12 +27,7 @@ func (a App) Command() *cobra.Command {
}
c.SetContext(cli.InitialContext())
c.PersistentPreRun = func(cmd *cobra.Command, _ []string) {
cli.SetupContext(cmd, zapcore.DebugLevel)
}
c.PersistentPostRunE = func(cmd *cobra.Command, _ []string) error {
closer := outlogging.LogFileCloserFrom(cmd.Context())
// ensure to close the log file
return closer()
cli.SetupContext(cmd, cli.SimplifiedLoggingSetup(zapcore.DebugLevel))
}
a.SetGlobalFlags(c.PersistentFlags())
return c
Expand Down
37 changes: 34 additions & 3 deletions pkg/cli/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ package cli
import (
"context"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"knative.dev/client/pkg/output"
outlogging "knative.dev/client/pkg/output/logging"
"knative.dev/pkg/logging"
"knative.dev/pkg/signals"
)

Expand All @@ -39,17 +41,46 @@ func InitialContext() context.Context {
return initialCtx
}

// LoggingSetup is a func that sets the logging into the context.
type LoggingSetup func(ctx context.Context) context.Context

// DefaultLoggingSetup is the default logging setup.
func DefaultLoggingSetup(logLevel zapcore.Level) func(ctx context.Context) context.Context {
return func(ctx context.Context) context.Context {
ctx = outlogging.WithLogLevel(ctx, logLevel)
return outlogging.EnsureLogger(ctx)
}
}

// SimplifiedLoggingSetup is just a production logger to avoid creating
// additional log files.
//
// TODO: Remove this after simplified logging is supported in
// knative.dev/client/pkg/output/logging package.
func SimplifiedLoggingSetup(logLevel zapcore.Level) func(ctx context.Context) context.Context {
return func(ctx context.Context) context.Context {
prtr := output.PrinterFrom(ctx)
errout := prtr.ErrOrStderr()
ec := zap.NewProductionEncoderConfig()
logger := zap.New(zapcore.NewCore(
zapcore.NewJSONEncoder(ec),
zapcore.AddSync(errout),
logLevel,
))
return logging.WithLogger(ctx, logger.Sugar())
}
}

// SetupContext will set the context commonly for all CLIs.
func SetupContext(ctxual Contextual, defaultLogLevel zapcore.Level) {
func SetupContext(ctxual Contextual, loggingSetup LoggingSetup) {
ctx := ctxual.Context()
if ctx == initialCtx {
// TODO: knative.dev/pkg/signals should allow for resetting the
// context for testing purposes.
ctx = signals.NewContext()
}
ctx = output.WithContext(ctx, ctxual)
ctx = outlogging.WithLogLevel(ctx, defaultLogLevel)
ctx = outlogging.EnsureLogger(ctx)
ctx = loggingSetup(ctx)
ctxual.SetContext(ctx)
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/cli/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import (
func TestSetupContext(t *testing.T) {
cmd := &cobra.Command{}
cmd.SetContext(cli.InitialContext())
cli.SetupContext(cmd, zapcore.InvalidLevel)
cli.SetupContext(cmd, cli.SimplifiedLoggingSetup(zapcore.InvalidLevel))
cli.SetupContext(cmd, cli.DefaultLoggingSetup(zapcore.InvalidLevel))
ctx := cmd.Context()
assert.Equal(t, zapcore.InvalidLevel, outlogging.LogLevelFromContext(ctx))
}

0 comments on commit eb5f95b

Please sign in to comment.