diff --git a/config/manager/manager.yaml b/config/manager/manager.yaml index a7e9d238..57944d7c 100644 --- a/config/manager/manager.yaml +++ b/config/manager/manager.yaml @@ -38,6 +38,9 @@ spec: - --leader-elect image: controller:latest name: manager + volumeMounts: + - name: varlog + mountPath: /var/log securityContext: allowPrivilegeEscalation: false # TODO(user): uncomment for common cases that do not require escalating privileges @@ -63,5 +66,24 @@ spec: requests: cpu: 10m memory: 64Mi + - name: filebeat + image: elastic/filebeat:7.16.3 + args: + - -c + - /etc/filebeat/conf.yaml + - -e + securityContext: + runAsUser: 9999 + volumeMounts: + - name: filebeat-config + mountPath: /etc/filebeat + - name: varlog + mountPath: /var/log + volumes: + - name: varlog + emptyDir: {} + - name: filebeat-config + configMap: + name: filebeat-config serviceAccountName: controller-manager terminationGracePeriodSeconds: 10 diff --git a/filebeat.cm.yaml b/filebeat.cm.yaml new file mode 100644 index 00000000..960ed48a --- /dev/null +++ b/filebeat.cm.yaml @@ -0,0 +1,22 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: filebeat-config + namespace: ndb-operator-system + labels: + component: filebeat +data: + conf.yaml: | + filebeat.inputs: + - type: log + paths: + - '/var/log/*.log' + output.elasticsearch: + hosts: ["https://quickstart-es-http.default.svc.cluster.local:9200"] + username: "elastic" + password: + valueFrom: + secretKeyRef: + name: quickstart-es-elastic-user + key: elastic + ssl.verification_mode: none \ No newline at end of file diff --git a/go.mod b/go.mod index d9ae0cc9..6b925e2d 100644 --- a/go.mod +++ b/go.mod @@ -67,6 +67,7 @@ require ( google.golang.org/appengine v1.6.7 // indirect google.golang.org/protobuf v1.30.0 // indirect gopkg.in/inf.v0 v0.9.1 // indirect + gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index 8a3fa2c3..17038d03 100644 --- a/go.sum +++ b/go.sum @@ -235,6 +235,8 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc= gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= +gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc= +gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go index 24b3daad..43dc415f 100644 --- a/main.go +++ b/main.go @@ -23,7 +23,11 @@ package main import ( "flag" + "io" "os" + "time" + + "gopkg.in/natefinch/lumberjack.v2" // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) // to ensure that exec-entrypoint and run can make use of them. @@ -64,9 +68,34 @@ func main() { flag.BoolVar(&enableLeaderElection, "leader-elect", false, "Enable leader election for controller manager. "+ "Enabling this will ensure there is only one active controller manager.") + + // Set the log file path + logFilePath := "/var/log/ndb-operator.log" + logRotateDuration := (1 * time.Minute) + + // Create a file for logging with log rotation + logFile := &lumberjack.Logger{ + Filename: logFilePath, + MaxSize: 10, // Max size in megabytes before log rolling (change as needed) + MaxBackups: 5, // Max number of old log files to keep + MaxAge: 0, // Max number of days to retain old log files + Compress: false, // Whether to compress old log files + } + + // Use a ticker to trigger log rotation every timestep of logRoationDuration + ticker := time.NewTicker(logRotateDuration) + go func() { + for range ticker.C { + logFile.Rotate() + } + }() + + mwriter := io.MultiWriter(logFile, os.Stderr) + opts := zap.Options{ Development: true, TimeEncoder: zapcore.RFC3339TimeEncoder, + DestWriter: mwriter, // Configure Zap options to write into a multiwriter } opts.BindFlags(flag.CommandLine) flag.Parse()