Skip to content

Commit 76a4108

Browse files
committed
Set up simplified logging for in-cluster event sender
This prevents it from failing with (in some setups): ``` mkdir /.cache: permission denied ```
1 parent ceb59ae commit 76a4108

File tree

8 files changed

+44
-20
lines changed

8 files changed

+44
-20
lines changed

.golangci.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,4 @@ linters-settings:
4444
- knative.dev/client/pkg/*
4545
gomoddirectives:
4646
# List of allowed `replace` directives. Default is empty.
47-
replace-allow-list:
48-
# TODO: Remove when https://github.com/knative/client/pull/1968 is merged
49-
- knative.dev/client/pkg
47+
replace-allow-list: []

go.mod

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ require (
2323
k8s.io/api v0.30.3
2424
k8s.io/apimachinery v0.30.3
2525
k8s.io/client-go v0.30.3
26-
knative.dev/client/pkg v0.0.0-20240903134911-f09e7164ceaf
26+
knative.dev/client/pkg v0.0.0-20240925104631-c9f128423b58
2727
knative.dev/eventing v0.42.1-0.20240828134450-34f9cd384dea
2828
knative.dev/hack v0.0.0-20240814130635-06f7aff93954
2929
knative.dev/pkg v0.0.0-20240815051656-89743d9bbf7c
@@ -32,9 +32,6 @@ require (
3232
sigs.k8s.io/yaml v1.4.0
3333
)
3434

35-
// TODO: Remove when https://github.com/knative/client/pull/1968 is merged
36-
replace knative.dev/client/pkg => github.com/cardil/knative-client/pkg v0.0.0-20240923095307-3a2bcba04752
37-
3835
require (
3936
contrib.go.opencensus.io/exporter/ocagent v0.7.1-0.20200907061046-05415f1de66d // indirect
4037
contrib.go.opencensus.io/exporter/prometheus v0.4.2 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,8 @@ k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 h1:pUdcCO1Lk/tbT5ztQWOBi5HBgbBP1
900900
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
901901
knative.dev/client-pkg v0.0.0-20240808015000-22f598931483 h1:jBfmxcR0H5Z9IzamelZtmmg9jfeOXfssllUVX5M4Xzs=
902902
knative.dev/client-pkg v0.0.0-20240808015000-22f598931483/go.mod h1:Y56KfZx3gJJpju88l86jQ9csxywLiopR0GkxCWW3+Kg=
903+
knative.dev/client/pkg v0.0.0-20240925104631-c9f128423b58 h1:EXXrHvJNGuWakfy27sJz0GvVSNuf5uzH6BW+m2dBq3w=
904+
knative.dev/client/pkg v0.0.0-20240925104631-c9f128423b58/go.mod h1:JR3XomuVf2cBqgvXFONkX6Ebf1/gJwUnl/1OH47U18g=
903905
knative.dev/eventing v0.42.1-0.20240828134450-34f9cd384dea h1:j3bFBE797vD6IZJsECQ5lEENumLp817rkQxANrbKxHs=
904906
knative.dev/eventing v0.42.1-0.20240828134450-34f9cd384dea/go.mod h1:Clx8z37Nwg321H9+vGNxp5C6bVdo4l4XM5g6T5CgZVI=
905907
knative.dev/hack v0.0.0-20240814130635-06f7aff93954 h1:dGMK5VoL75szvrYQTL9NqhPYHu1f5dGaXx1hJI8fAFM=

go.work.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ github.com/buildkite/interpolate v0.0.0-20200526001904-07f35b4ae251 h1:k6UDF1uPY
136136
github.com/buildkite/interpolate v0.0.0-20200526001904-07f35b4ae251/go.mod h1:gbPR1gPu9dB96mucYIR7T3B7p/78hRVSOuzIWLHK2Y4=
137137
github.com/c2h5oh/datasize v0.0.0-20231215233829-aa82cc1e6500 h1:6lhrsTEnloDPXyeZBvSYvQf8u86jbKehZPVDDlkgDl4=
138138
github.com/c2h5oh/datasize v0.0.0-20231215233829-aa82cc1e6500/go.mod h1:S/7n9copUssQ56c7aAgHqftWO4LTf4xY6CGWt8Bc+3M=
139-
github.com/cardil/knative-client/pkg v0.0.0-20240923095307-3a2bcba04752/go.mod h1:JR3XomuVf2cBqgvXFONkX6Ebf1/gJwUnl/1OH47U18g=
140139
github.com/cavaliercoder/badio v0.0.0-20160213150051-ce5280129e9e h1:YYUjy5BRwO5zPtfk+aa2gw255FIIoi93zMmuy19o0bc=
141140
github.com/cavaliercoder/badio v0.0.0-20160213150051-ce5280129e9e/go.mod h1:V284PjgVwSk4ETmz84rpu9ehpGg7swlIH8npP9k2bGw=
142141
github.com/cavaliercoder/go-rpm v0.0.0-20200122174316-8cb9fd9c31a8 h1:jP7ki8Tzx9ThnFPLDhBYAhEpI2+jOURnHQNURgsMvnY=
@@ -676,6 +675,8 @@ k8s.io/kms v0.30.3 h1:NLg+oN45S2Y3U0WiLRzbS61AY/XrS5JBMZp531Z+Pho=
676675
k8s.io/kms v0.30.3/go.mod h1:GrMurD0qk3G4yNgGcsCEmepqf9KyyIrTXYR2lyUOJC4=
677676
knative.dev/caching v0.0.0-20240812133420-93e6a0a5b46d h1:pj+XufdayIdxV/PUFpuIB1Y/pFoiWPJT6VrAXja4OcE=
678677
knative.dev/caching v0.0.0-20240812133420-93e6a0a5b46d/go.mod h1:vxgDv5XOAYFa2LrreP8quBiyKB2HB2fqWG6nc6LmDR4=
678+
knative.dev/client/pkg v0.0.0-20240903134911-f09e7164ceaf h1:QPGwYLkkMwssyEw0ek8T1u4Q3+FSeVPSH4IIKThdngc=
679+
knative.dev/client/pkg v0.0.0-20240903134911-f09e7164ceaf/go.mod h1:JR3XomuVf2cBqgvXFONkX6Ebf1/gJwUnl/1OH47U18g=
679680
knative.dev/hack/schema v0.0.0-20240814130635-06f7aff93954 h1:0yjDplGHUnZ8NpcfgmH0thXSzG28VSM16hb3Vz171l8=
680681
knative.dev/hack/schema v0.0.0-20240814130635-06f7aff93954/go.mod h1:jRH/sx6mwwuMVhvJgnzSaoYA1N4qaIkJa+zxEGtVA5I=
681682
mvdan.cc/sh/v3 v3.7.0 h1:lSTjdP/1xsddtaKfGg7Myu7DnlHItd3/M2tomOcNNBg=

internal/cli/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (a *App) Command() *cobra.Command {
5858
if a.Verbose {
5959
lvl = zapcore.DebugLevel
6060
}
61-
cli.SetupContext(cmd, lvl)
61+
cli.SetupContext(cmd, cli.DefaultLoggingSetup(lvl))
6262
}
6363
c.PersistentPostRunE = func(cmd *cobra.Command, _ []string) error {
6464
closer := outlogging.LogFileCloserFrom(cmd.Context())

internal/ics/app.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"github.com/wavesoftware/go-commandline"
66
"go.uber.org/zap"
77
"go.uber.org/zap/zapcore"
8-
outlogging "knative.dev/client/pkg/output/logging"
98
"knative.dev/kn-plugin-event/pkg/binding"
109
"knative.dev/kn-plugin-event/pkg/cli"
1110
"knative.dev/kn-plugin-event/pkg/k8s"
@@ -28,12 +27,7 @@ func (a App) Command() *cobra.Command {
2827
}
2928
c.SetContext(cli.InitialContext())
3029
c.PersistentPreRun = func(cmd *cobra.Command, _ []string) {
31-
cli.SetupContext(cmd, zapcore.DebugLevel)
32-
}
33-
c.PersistentPostRunE = func(cmd *cobra.Command, _ []string) error {
34-
closer := outlogging.LogFileCloserFrom(cmd.Context())
35-
// ensure to close the log file
36-
return closer()
30+
cli.SetupContext(cmd, cli.SimplifiedLoggingSetup(zapcore.DebugLevel))
3731
}
3832
a.SetGlobalFlags(c.PersistentFlags())
3933
return c

pkg/cli/context.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ package cli
1919
import (
2020
"context"
2121

22+
"go.uber.org/zap"
2223
"go.uber.org/zap/zapcore"
2324
"knative.dev/client/pkg/output"
2425
outlogging "knative.dev/client/pkg/output/logging"
26+
"knative.dev/pkg/logging"
2527
"knative.dev/pkg/signals"
2628
)
2729

@@ -39,17 +41,46 @@ func InitialContext() context.Context {
3941
return initialCtx
4042
}
4143

44+
// LoggingSetup is a func that sets the logging into the context.
45+
type LoggingSetup func(ctx context.Context) context.Context
46+
47+
// DefaultLoggingSetup is the default logging setup.
48+
func DefaultLoggingSetup(logLevel zapcore.Level) func(ctx context.Context) context.Context {
49+
return func(ctx context.Context) context.Context {
50+
ctx = outlogging.WithLogLevel(ctx, logLevel)
51+
return outlogging.EnsureLogger(ctx)
52+
}
53+
}
54+
55+
// SimplifiedLoggingSetup is just a production logger to avoid creating
56+
// additional log files.
57+
//
58+
// TODO: Remove this after simplified logging is supported in
59+
// knative.dev/client/pkg/output/logging package.
60+
func SimplifiedLoggingSetup(logLevel zapcore.Level) func(ctx context.Context) context.Context {
61+
return func(ctx context.Context) context.Context {
62+
prtr := output.PrinterFrom(ctx)
63+
errout := prtr.ErrOrStderr()
64+
ec := zap.NewProductionEncoderConfig()
65+
logger := zap.New(zapcore.NewCore(
66+
zapcore.NewJSONEncoder(ec),
67+
zapcore.AddSync(errout),
68+
logLevel,
69+
))
70+
return logging.WithLogger(ctx, logger.Sugar())
71+
}
72+
}
73+
4274
// SetupContext will set the context commonly for all CLIs.
43-
func SetupContext(ctxual Contextual, defaultLogLevel zapcore.Level) {
75+
func SetupContext(ctxual Contextual, loggingSetup LoggingSetup) {
4476
ctx := ctxual.Context()
4577
if ctx == initialCtx {
4678
// TODO: knative.dev/pkg/signals should allow for resetting the
4779
// context for testing purposes.
4880
ctx = signals.NewContext()
4981
}
5082
ctx = output.WithContext(ctx, ctxual)
51-
ctx = outlogging.WithLogLevel(ctx, defaultLogLevel)
52-
ctx = outlogging.EnsureLogger(ctx)
83+
ctx = loggingSetup(ctx)
5384
ctxual.SetContext(ctx)
5485
}
5586

pkg/cli/context_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ import (
2929
func TestSetupContext(t *testing.T) {
3030
cmd := &cobra.Command{}
3131
cmd.SetContext(cli.InitialContext())
32-
cli.SetupContext(cmd, zapcore.InvalidLevel)
32+
cli.SetupContext(cmd, cli.SimplifiedLoggingSetup(zapcore.InvalidLevel))
33+
cli.SetupContext(cmd, cli.DefaultLoggingSetup(zapcore.InvalidLevel))
3334
ctx := cmd.Context()
3435
assert.Equal(t, zapcore.InvalidLevel, outlogging.LogLevelFromContext(ctx))
3536
}

0 commit comments

Comments
 (0)