Skip to content

Commit 215e8fd

Browse files
committed
Add support to write logs to a file
This will allow a sidecar to process the logs, either for indexing or any other usecase.
1 parent 43e11a6 commit 215e8fd

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

cmd/main.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,18 @@ package main
1919
import (
2020
"flag"
2121
"fmt"
22+
"io"
2223
"os"
2324
"strings"
2425
"time"
2526

2627
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2728
// to ensure that exec-entrypoint and run can make use of them.
29+
30+
"go.uber.org/zap/zapcore"
2831
_ "k8s.io/client-go/plugin/pkg/client/auth"
2932

33+
"gopkg.in/natefinch/lumberjack.v2"
3034
"k8s.io/apimachinery/pkg/runtime"
3135
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3236
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
@@ -63,6 +67,8 @@ func main() {
6367
var healthHookTimeout time.Duration
6468
var nodeDisruptionTypesRaw string
6569
var defaultNodeDisruptionTypesRaw string
70+
var logConsole bool
71+
var logPath string
6672
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
6773
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
6874
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
@@ -74,13 +80,16 @@ func main() {
7480
flag.DurationVar(&healthHookTimeout, "healthhook-timeout", controller.DefaultHealthHookTimeout, "HTTP client timeout for calling HealthHook resolved from ADB")
7581
flag.StringVar(&nodeDisruptionTypesRaw, "node-disruption-types", "", "The list of types allowed for a node disruption separated by a comma.")
7682
flag.StringVar(&defaultNodeDisruptionTypesRaw, "default-node-disruption-types", "", "The default list of node disruption types for ADBs that don't specify supportedNodeDisruptionTypes. Must be a subset of --node-disruption-types.")
83+
flag.BoolVar(&logConsole, "log-console", true, "Write logs to the console (stderr)")
84+
flag.StringVar(&logPath, "log-path", "", "Write logs to file at path")
7785

7886
opts := zap.Options{
7987
Development: true,
8088
}
8189
opts.BindFlags(flag.CommandLine)
8290
flag.Parse()
8391

92+
opts.DestWriter = logOutput(logPath, logConsole)
8493
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
8594

8695
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
@@ -174,3 +183,22 @@ func main() {
174183
os.Exit(1)
175184
}
176185
}
186+
187+
func logOutput(logPath string, logConsole bool) io.Writer {
188+
if logPath == "" {
189+
return nil // When not set, defaults to console (stderr) anyway.
190+
}
191+
192+
w := &lumberjack.Logger{
193+
Filename: logPath,
194+
MaxSize: 10,
195+
MaxBackups: 1,
196+
Compress: false, // disabled by default
197+
}
198+
if logConsole {
199+
return zapcore.NewMultiWriteSyncer(
200+
zapcore.AddSync(w),
201+
zapcore.AddSync(os.Stderr))
202+
}
203+
return w
204+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/onsi/ginkgo/v2 v2.22.0
1111
github.com/onsi/gomega v1.36.1
1212
github.com/stretchr/testify v1.11.1
13+
gopkg.in/natefinch/lumberjack.v2 v2.2.1
1314
k8s.io/api v0.34.2
1415
k8s.io/apimachinery v0.34.2
1516
k8s.io/client-go v0.34.2

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ gopkg.in/evanphx/json-patch.v4 v4.13.0 h1:czT3CmqEaQ1aanPc5SdlgQrrEIb8w/wwCvWWnf
217217
gopkg.in/evanphx/json-patch.v4 v4.13.0/go.mod h1:p8EYWUEYMpynmqDbY58zCKCFZw8pRWMG4EsWvDvM72M=
218218
gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc=
219219
gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw=
220+
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
221+
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
220222
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
221223
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
222224
k8s.io/api v0.34.2 h1:fsSUNZhV+bnL6Aqrp6O7lMTy6o5x2C4XLjnh//8SLYY=

0 commit comments

Comments
 (0)