-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
logrotate.go
153 lines (124 loc) · 4.37 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package model
import (
"strings"
"k8s.io/kops/pkg/apis/kops/model"
"k8s.io/kops/pkg/systemd"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/nodeup/nodetasks"
"k8s.io/kops/util/pkg/distributions"
"k8s.io/klog/v2"
)
// LogrotateBuilder installs logrotate.d and configures log rotation for kubernetes logs
type LogrotateBuilder struct {
*NodeupModelContext
}
var _ fi.ModelBuilder = &LogrotateBuilder{}
// Build is responsible for configuring logrotate
func (b *LogrotateBuilder) Build(c *fi.ModelBuilderContext) error {
switch b.Distribution {
case distributions.DistributionContainerOS:
klog.Infof("Detected ContainerOS; won't install logrotate")
return nil
case distributions.DistributionFlatcar:
klog.Infof("Detected Flatcar; won't install logrotate")
default:
c.AddTask(&nodetasks.Package{Name: "logrotate"})
}
b.addLogRotate(c, "docker", "/var/log/docker.log", logRotateOptions{})
b.addLogRotate(c, "kube-addons", "/var/log/kube-addons.log", logRotateOptions{})
b.addLogRotate(c, "kube-apiserver", "/var/log/kube-apiserver.log", logRotateOptions{})
b.addLogRotate(c, "kube-controller-manager", "/var/log/kube-controller-manager.log", logRotateOptions{})
b.addLogRotate(c, "kube-proxy", "/var/log/kube-proxy.log", logRotateOptions{})
b.addLogRotate(c, "kube-scheduler", "/var/log/kube-scheduler.log", logRotateOptions{})
b.addLogRotate(c, "kubelet", "/var/log/kubelet.log", logRotateOptions{})
b.addLogRotate(c, "etcd", "/var/log/etcd.log", logRotateOptions{})
b.addLogRotate(c, "etcd-events", "/var/log/etcd-events.log", logRotateOptions{})
if model.UseCiliumEtcd(b.Cluster) {
b.addLogRotate(c, "etcd-cilium", "/var/log/etcd-cilium.log", logRotateOptions{})
}
if err := b.addLogrotateService(c); err != nil {
return err
}
// Add timer to run hourly.
{
unit := &systemd.Manifest{}
unit.Set("Unit", "Description", "Hourly Log Rotation")
unit.Set("Timer", "OnCalendar", "hourly")
service := &nodetasks.Service{
Name: "logrotate.timer", // Override (by name) any existing timer
Definition: s(unit.Render()),
}
service.InitDefaults()
c.AddTask(service)
}
return nil
}
// addLogrotateService creates a logrotate systemd task to act as target for the timer, if one is needed
func (b *LogrotateBuilder) addLogrotateService(c *fi.ModelBuilderContext) error {
switch b.Distribution {
case distributions.DistributionFlatcar, distributions.DistributionContainerOS:
// logrotate service already exists
return nil
}
manifest := &systemd.Manifest{}
manifest.Set("Unit", "Description", "Rotate and Compress System Logs")
manifest.Set("Service", "ExecStart", "/usr/sbin/logrotate /etc/logrotate.conf")
service := &nodetasks.Service{
Name: "logrotate.service",
Definition: s(manifest.Render()),
}
service.InitDefaults()
c.AddTask(service)
return nil
}
type logRotateOptions struct {
MaxSize string
DateFormat string
}
func (b *LogrotateBuilder) addLogRotate(c *fi.ModelBuilderContext, name, path string, options logRotateOptions) {
if options.MaxSize == "" {
options.MaxSize = "100M"
}
// Flatcar sets "dateext" options, and maxsize-based rotation will fail if
// the file has been previously rotated on the same calendar date.
if b.Distribution == distributions.DistributionFlatcar {
options.DateFormat = "-%Y%m%d-%s"
}
lines := []string{
path + "{",
" rotate 5",
" copytruncate",
" missingok",
" notifempty",
" delaycompress",
" maxsize " + options.MaxSize,
}
if options.DateFormat != "" {
lines = append(lines, " dateformat "+options.DateFormat)
}
lines = append(
lines,
" daily",
" create 0644 root root",
"}",
)
contents := strings.Join(lines, "\n") + "\n"
c.AddTask(&nodetasks.File{
Path: "/etc/logrotate.d/" + name,
Contents: fi.NewStringResource(contents),
Type: nodetasks.FileType_File,
Mode: s("0644"),
})
}