Merge pull request #10663 from rook/mergify/bp/release-1.9/pr-10528
core: add support to rotate log based on logfile size (backport #10528)
This commit is contained in:
8 files changed
+134
-16
No files matched your search
@@ -158,7 +158,7 @@ cephClusterSpec:
|
||||
# enable log collector, daemons will log on files and rotate
|
||||
# logCollector:
|
||||
# enabled: true
|
||||
# periodicity: 24h # SUFFIX may be 'h' for hours or 'd' for days.
|
||||
# periodicity: daily # one of: hourly, daily, weekly, monthly
|
||||
|
||||
# automate [data cleanup process](https://github.com/rook/rook/blob/master/Documentation/ceph-teardown.md#delete-the-data-on-hosts) in cluster destruction.
|
||||
cleanupPolicy:
|
||||
|
||||
@@ -1298,8 +1298,16 @@ spec:
|
||||
enabled:
|
||||
description: Enabled represents whether the log collector is enabled
|
||||
type: boolean
|
||||
maxLogSize:
|
||||
anyOf:
|
||||
- type: integer
|
||||
- type: string
|
||||
description: MaxLogSize is the maximum size of the log per ceph daemons. Must be at least 1M.
|
||||
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
|
||||
x-kubernetes-int-or-string: true
|
||||
periodicity:
|
||||
description: Periodicity is the periodicity of the log rotation
|
||||
description: Periodicity is the periodicity of the log rotation.
|
||||
pattern: ^$|^(hourly|daily|weekly|monthly|1h|24h|1d)$
|
||||
type: string
|
||||
type: object
|
||||
mgr:
|
||||
|
||||
@@ -115,7 +115,8 @@ spec:
|
||||
# enable log collector, daemons will log on files and rotate
|
||||
# logCollector:
|
||||
# enabled: true
|
||||
# periodicity: 24h # SUFFIX may be 'h' for hours or 'd' for days.
|
||||
# periodicity: daily # one of: hourly, daily, weekly, monthly
|
||||
# maxLogSize: 500M # SUFFIX may be 'M' or 'G'. Must be at least 1M.
|
||||
# automate [data cleanup process](https://github.com/rook/rook/blob/master/Documentation/Storage-Configuration/ceph-teardown.md#delete-the-data-on-hosts) in cluster destruction.
|
||||
cleanupPolicy:
|
||||
# Since cluster cleanup is destructive to data, confirmation is required.
|
||||
|
||||
@@ -1296,8 +1296,16 @@ spec:
|
||||
enabled:
|
||||
description: Enabled represents whether the log collector is enabled
|
||||
type: boolean
|
||||
maxLogSize:
|
||||
anyOf:
|
||||
- type: integer
|
||||
- type: string
|
||||
description: MaxLogSize is the maximum size of the log per ceph daemons. Must be at least 1M.
|
||||
pattern: ^(\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))(([KMGTPE]i)|[numkMGTPE]|([eE](\+|-)?(([0-9]+(\.[0-9]*)?)|(\.[0-9]+))))?$
|
||||
x-kubernetes-int-or-string: true
|
||||
periodicity:
|
||||
description: Periodicity is the periodicity of the log rotation
|
||||
description: Periodicity is the periodicity of the log rotation.
|
||||
pattern: ^$|^(hourly|daily|weekly|monthly|1h|24h|1d)$
|
||||
type: string
|
||||
type: object
|
||||
mgr:
|
||||
|
||||
@@ -227,9 +227,13 @@ type LogCollectorSpec struct {
|
||||
// Enabled represents whether the log collector is enabled
|
||||
// +optional
|
||||
Enabled bool `json:"enabled,omitempty"`
|
||||
// Periodicity is the periodicity of the log rotation
|
||||
// Periodicity is the periodicity of the log rotation.
|
||||
// +kubebuilder:validation:Pattern=`^$|^(hourly|daily|weekly|monthly|1h|24h|1d)$`
|
||||
// +optional
|
||||
Periodicity string `json:"periodicity,omitempty"`
|
||||
// MaxLogSize is the maximum size of the log per ceph daemons. Must be at least 1M.
|
||||
// +optional
|
||||
MaxLogSize *resource.Quantity `json:"maxLogSize,omitempty"`
|
||||
}
|
||||
|
||||
// SecuritySpec is security spec to include various security items such as kms
|
||||
|
||||
@@ -1759,7 +1759,7 @@ func (in *ClusterSpec) DeepCopyInto(out *ClusterSpec) {
|
||||
out.CleanupPolicy = in.CleanupPolicy
|
||||
in.HealthCheck.DeepCopyInto(&out.HealthCheck)
|
||||
in.Security.DeepCopyInto(&out.Security)
|
||||
out.LogCollector = in.LogCollector
|
||||
in.LogCollector.DeepCopyInto(&out.LogCollector)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -2525,6 +2525,11 @@ func (in LabelsSpec) DeepCopy() LabelsSpec {
|
||||
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
|
||||
func (in *LogCollectorSpec) DeepCopyInto(out *LogCollectorSpec) {
|
||||
*out = *in
|
||||
if in.MaxLogSize != nil {
|
||||
in, out := &in.MaxLogSize, &out.MaxLogSize
|
||||
x := (*in).DeepCopy()
|
||||
*out = &x
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -76,21 +76,25 @@ var (
|
||||
CEPH_CLIENT_ID=%s
|
||||
PERIODICITY=%s
|
||||
LOG_ROTATE_CEPH_FILE=/etc/logrotate.d/ceph
|
||||
|
||||
if [ -z "$PERIODICITY" ]; then
|
||||
PERIODICITY=24h
|
||||
fi
|
||||
LOG_MAX_SIZE=%s
|
||||
|
||||
# edit the logrotate file to only rotate a specific daemon log
|
||||
# otherwise we will logrotate log files without reloading certain daemons
|
||||
# this might happen when multiple daemons run on the same machine
|
||||
sed -i "s|*.log|$CEPH_CLIENT_ID.log|" "$LOG_ROTATE_CEPH_FILE"
|
||||
|
||||
# replace default daily with given user input
|
||||
sed --in-place "s/daily/$PERIODICITY/g" "$LOG_ROTATE_CEPH_FILE"
|
||||
|
||||
if [ "$LOG_MAX_SIZE" -ne 0 ]; then
|
||||
# adding maxsize $LOG_MAX_SIZE at the 4th line of the logrotate config file with 4 spaces to maintain indentation
|
||||
sed --in-place "4i \ \ \ \ maxsize $LOG_MAX_SIZE" "$LOG_ROTATE_CEPH_FILE"
|
||||
fi
|
||||
|
||||
while true; do
|
||||
sleep "$PERIODICITY"
|
||||
echo "starting log rotation"
|
||||
logrotate --verbose --force "$LOG_ROTATE_CEPH_FILE"
|
||||
echo "I am going to sleep now, see you in $PERIODICITY"
|
||||
# we don't force the logrorate but we let the logrotate binary handle the rotation based on user's input for periodicity and size
|
||||
logrotate --verbose "$LOG_ROTATE_CEPH_FILE"
|
||||
sleep 15m
|
||||
done
|
||||
`
|
||||
)
|
||||
@@ -661,8 +665,28 @@ func PrivilegedContext(runAsRoot bool) *v1.SecurityContext {
|
||||
return sec
|
||||
}
|
||||
|
||||
// LogCollectorContainer runs a cron job to rotate logs
|
||||
// LogCollectorContainer rotate logs
|
||||
func LogCollectorContainer(daemonID, ns string, c cephv1.ClusterSpec) *v1.Container {
|
||||
|
||||
var maxLogSize resource.Quantity
|
||||
if c.LogCollector.MaxLogSize != nil {
|
||||
size := c.LogCollector.MaxLogSize.Value() / 1000 / 1000
|
||||
if size == 0 {
|
||||
size = 1
|
||||
logger.Info("maxLogSize is 0M setting to minimum of 1M")
|
||||
}
|
||||
maxLogSize = resource.MustParse(fmt.Sprintf("%dM", size))
|
||||
}
|
||||
|
||||
periodicity := "daily"
|
||||
if c.LogCollector.Periodicity == "1h" {
|
||||
periodicity = "hourly"
|
||||
} else if strings.Contains(c.LogCollector.Periodicity, "h") || strings.Contains(c.LogCollector.Periodicity, "d") {
|
||||
periodicity = "daily"
|
||||
} else if c.LogCollector.Periodicity != "" {
|
||||
periodicity = c.LogCollector.Periodicity
|
||||
}
|
||||
|
||||
return &v1.Container{
|
||||
Name: logCollector,
|
||||
Command: []string{
|
||||
@@ -671,7 +695,7 @@ func LogCollectorContainer(daemonID, ns string, c cephv1.ClusterSpec) *v1.Contai
|
||||
"-e", // Exit immediately if a command exits with a non-zero status.
|
||||
"-m", // Terminal job control, allows job to be terminated by SIGTERM
|
||||
"-c", // Command to run
|
||||
fmt.Sprintf(cronLogRotate, daemonID, c.LogCollector.Periodicity),
|
||||
fmt.Sprintf(cronLogRotate, daemonID, periodicity, maxLogSize.String()),
|
||||
},
|
||||
Image: c.CephVersion.Image,
|
||||
VolumeMounts: DaemonVolumeMounts(config.NewDatalessDaemonDataPathMap(ns, c.DataDirHostPath), ""),
|
||||
|
||||
@@ -374,3 +374,71 @@ func TestConfigureExternalMetricsEndpoint(t *testing.T) {
|
||||
assert.Equal(t, "192.168.0.1", currentEndpoints.Subsets[0].Addresses[0].IP, currentEndpoints)
|
||||
})
|
||||
}
|
||||
|
||||
func TestLogCollectorContainer(t *testing.T) {
|
||||
daemonId := "ceph-mon-a"
|
||||
ns := "rook-ceph"
|
||||
|
||||
t.Run("Periodicity 1d and no MaxlogSize", func(t *testing.T) {
|
||||
c := cephv1.ClusterSpec{LogCollector: cephv1.LogCollectorSpec{Enabled: true, Periodicity: "1d"}}
|
||||
got := LogCollectorContainer(daemonId, ns, c)
|
||||
want := fmt.Sprintf(cronLogRotate, daemonId, "daily", "0")
|
||||
assert.Equal(t, want, got.Command[5])
|
||||
})
|
||||
|
||||
t.Run("Periodicity 1h and MaxlogSize 1M", func(t *testing.T) {
|
||||
maxsize, _ := resource.ParseQuantity("1M")
|
||||
c := cephv1.ClusterSpec{LogCollector: cephv1.LogCollectorSpec{Enabled: true, Periodicity: "1h", MaxLogSize: &maxsize}}
|
||||
got := LogCollectorContainer(daemonId, ns, c)
|
||||
want := fmt.Sprintf(cronLogRotate, daemonId, "hourly", "1M")
|
||||
assert.Equal(t, want, got.Command[5])
|
||||
})
|
||||
|
||||
t.Run("Periodicity weekly and 1G MaxlogSize", func(t *testing.T) {
|
||||
maxsize, _ := resource.ParseQuantity("1Gi")
|
||||
c := cephv1.ClusterSpec{LogCollector: cephv1.LogCollectorSpec{Enabled: true, Periodicity: "weekly", MaxLogSize: &maxsize}}
|
||||
got := LogCollectorContainer(daemonId, ns, c)
|
||||
want := fmt.Sprintf(cronLogRotate, daemonId, "weekly", "1073M")
|
||||
assert.Equal(t, want, got.Command[5])
|
||||
})
|
||||
|
||||
t.Run("MaxlogSize 1M and no Periodicity", func(t *testing.T) {
|
||||
maxsize, _ := resource.ParseQuantity("1Mi")
|
||||
c := cephv1.ClusterSpec{LogCollector: cephv1.LogCollectorSpec{Enabled: true, MaxLogSize: &maxsize}}
|
||||
got := LogCollectorContainer(daemonId, ns, c)
|
||||
want := fmt.Sprintf(cronLogRotate, daemonId, "daily", "1M")
|
||||
assert.Equal(t, want, got.Command[5])
|
||||
})
|
||||
|
||||
t.Run("10G MaxlogSize and Periodicity 1d", func(t *testing.T) {
|
||||
maxsize, _ := resource.ParseQuantity("10G")
|
||||
c := cephv1.ClusterSpec{LogCollector: cephv1.LogCollectorSpec{Enabled: true, Periodicity: "1d", MaxLogSize: &maxsize}}
|
||||
got := LogCollectorContainer(daemonId, ns, c)
|
||||
want := fmt.Sprintf(cronLogRotate, daemonId, "daily", "10G")
|
||||
assert.Equal(t, want, got.Command[5])
|
||||
})
|
||||
|
||||
t.Run("10M MaxlogSize and Periodicity weekly", func(t *testing.T) {
|
||||
maxsize, _ := resource.ParseQuantity("10Mi")
|
||||
c := cephv1.ClusterSpec{LogCollector: cephv1.LogCollectorSpec{Enabled: true, Periodicity: "weekly", MaxLogSize: &maxsize}}
|
||||
got := LogCollectorContainer(daemonId, ns, c)
|
||||
want := fmt.Sprintf(cronLogRotate, daemonId, "weekly", "10M")
|
||||
assert.Equal(t, want, got.Command[5])
|
||||
})
|
||||
|
||||
t.Run("1M MaxlogSize and Periodicity 1d", func(t *testing.T) {
|
||||
maxsize, _ := resource.ParseQuantity("1M")
|
||||
c := cephv1.ClusterSpec{LogCollector: cephv1.LogCollectorSpec{Enabled: true, Periodicity: "1d", MaxLogSize: &maxsize}}
|
||||
got := LogCollectorContainer(daemonId, ns, c)
|
||||
want := fmt.Sprintf(cronLogRotate, daemonId, "daily", "1M")
|
||||
assert.Equal(t, want, got.Command[5])
|
||||
})
|
||||
|
||||
t.Run("500k MaxlogSize and Periodicity 1d", func(t *testing.T) {
|
||||
maxsize, _ := resource.ParseQuantity("500K")
|
||||
c := cephv1.ClusterSpec{LogCollector: cephv1.LogCollectorSpec{Enabled: true, Periodicity: "1d", MaxLogSize: &maxsize}}
|
||||
got := LogCollectorContainer(daemonId, ns, c)
|
||||
want := fmt.Sprintf(cronLogRotate, daemonId, "daily", "1M")
|
||||
assert.Equal(t, want, got.Command[5])
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user