-
Notifications
You must be signed in to change notification settings - Fork 462
/
logrotate.go
68 lines (61 loc) · 1.86 KB
/
logrotate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package logrotate
import (
"k8s.io/utils/ptr"
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1"
)
// Config returns the content for logrotate units and files.
// Whenever logrotate is ran, this config will:
// - keep only 14 old (rotated) logs, and will discard older logs.
//
// Prefix carries the target container runtime (such as containerd, docker).
// When containerd is used the log rotation based on size is performed by kubelet.
func Config(pathConfig, pathLogFiles, prefix string) ([]extensionsv1alpha1.Unit, []extensionsv1alpha1.File) {
serviceFile := extensionsv1alpha1.File{
Path: pathConfig,
Permissions: ptr.To[int32](0644),
Content: extensionsv1alpha1.FileContent{
Inline: &extensionsv1alpha1.FileContentInline{
Data: pathLogFiles + ` {
rotate 14
copytruncate
missingok
notifempty
compress
daily
dateext
dateformat -%Y%m%d-%s
create 0644 root root
}
`,
},
},
}
serviceUnit := extensionsv1alpha1.Unit{
Name: prefix + "-logrotate.service",
Enable: ptr.To(true),
Content: ptr.To(`[Unit]
Description=Rotate and Compress System Logs
[Service]
ExecStart=/usr/sbin/logrotate -s /var/lib/` + prefix + `-logrotate.status ` + pathConfig + `
[Install]
WantedBy=multi-user.target`),
FilePaths: []string{serviceFile.Path},
}
timerUnit := extensionsv1alpha1.Unit{
Name: prefix + "-logrotate.timer",
Command: ptr.To(extensionsv1alpha1.CommandStart),
Enable: ptr.To(true),
Content: ptr.To(`[Unit]
Description=Log Rotation at each 10 minutes
[Timer]
OnCalendar=*:0/10
AccuracySec=1min
Persistent=true
[Install]
WantedBy=multi-user.target`),
}
return []extensionsv1alpha1.Unit{serviceUnit, timerUnit}, []extensionsv1alpha1.File{serviceFile}
}