From 8ac39fad0303bbce88674c1c749587bb60e4783d Mon Sep 17 00:00:00 2001 From: yamatcha Date: Fri, 29 Nov 2024 01:32:49 +0000 Subject: [PATCH 1/2] Add log-rotation-size --- api/v1beta2/mysqlcluster_types.go | 10 ++++++++++ api/v1beta2/mysqlcluster_webhook_test.go | 7 +++++++ charts/moco/templates/generated/crds/moco_crds.yaml | 3 +++ config/crd/bases/moco.cybozu.com_mysqlclusters.yaml | 3 +++ ...sourcedefinition_mysqlclusters.moco.cybozu.com.yaml | 3 +++ controllers/mysql_container.go | 3 +++ controllers/mysqlcluster_controller_test.go | 2 ++ docs/crd_mysqlcluster_v1beta2.md | 1 + go.mod | 2 +- go.sum | 4 ++-- 10 files changed, 35 insertions(+), 3 deletions(-) diff --git a/api/v1beta2/mysqlcluster_types.go b/api/v1beta2/mysqlcluster_types.go index a4092330..1e2aa592 100644 --- a/api/v1beta2/mysqlcluster_types.go +++ b/api/v1beta2/mysqlcluster_types.go @@ -109,6 +109,11 @@ type MySQLClusterSpec struct { // +optional LogRotationSchedule string `json:"logRotationSchedule,omitempty"` + // LogRotationSize specifies the size to rotate MySQL logs + // If not set, size-based log rotation is disabled by default + // +optional + LogRotationSize int `json:"logRotationSize,omitempty"` + // The name of BackupPolicy custom resource in the same namespace. // If this is set, MOCO creates a CronJob to take backup of this MySQL cluster periodically. // +nullable @@ -171,6 +176,11 @@ func (s MySQLClusterSpec) validateCreate() (admission.Warnings, field.ErrorList) } } + pp = p.Child("logRotationSize") + if s.LogRotationSize < 0 { + allErrs = append(allErrs, field.Invalid(pp, s.LogRotationSize, "logRotationSize must be a positve integer or zero")) + } + pp = p.Child("replicas") if s.Replicas%2 == 0 { allErrs = append(allErrs, field.Invalid(pp, s.Replicas, "replicas must be a positive odd number")) diff --git a/api/v1beta2/mysqlcluster_webhook_test.go b/api/v1beta2/mysqlcluster_webhook_test.go index 398af769..c2964ee5 100644 --- a/api/v1beta2/mysqlcluster_webhook_test.go +++ b/api/v1beta2/mysqlcluster_webhook_test.go @@ -151,6 +151,13 @@ var _ = Describe("MySQLCluster Webhook", func() { Expect(err).NotTo(HaveOccurred()) }) + It("should allow a valid logRotationSize", func() { + r := makeMySQLCluster() + r.Spec.LogRotationSize = 1024 + err := k8sClient.Create(ctx, r) + Expect(err).NotTo(HaveOccurred()) + }) + It("should deny an invalid logRotationSchedule", func() { r := makeMySQLCluster() r.Spec.LogRotationSchedule = "hoge fuga" diff --git a/charts/moco/templates/generated/crds/moco_crds.yaml b/charts/moco/templates/generated/crds/moco_crds.yaml index ee735f16..10f29997 100644 --- a/charts/moco/templates/generated/crds/moco_crds.yaml +++ b/charts/moco/templates/generated/crds/moco_crds.yaml @@ -2256,6 +2256,9 @@ spec: logRotationSchedule: description: LogRotationSchedule specifies the schedule to rota type: string + logRotationSize: + description: LogRotationSize specifies the size to rotate MySQL + type: integer maxDelaySeconds: default: 60 description: 'MaxDelaySeconds configures the readiness probe of ' diff --git a/config/crd/bases/moco.cybozu.com_mysqlclusters.yaml b/config/crd/bases/moco.cybozu.com_mysqlclusters.yaml index b7c40754..01b1a313 100644 --- a/config/crd/bases/moco.cybozu.com_mysqlclusters.yaml +++ b/config/crd/bases/moco.cybozu.com_mysqlclusters.yaml @@ -73,6 +73,9 @@ spec: logRotationSchedule: description: LogRotationSchedule specifies the schedule to rota type: string + logRotationSize: + description: LogRotationSize specifies the size to rotate MySQL + type: integer maxDelaySeconds: default: 60 description: 'MaxDelaySeconds configures the readiness probe of ' diff --git a/config/crd/tests/apiextensions.k8s.io_v1_customresourcedefinition_mysqlclusters.moco.cybozu.com.yaml b/config/crd/tests/apiextensions.k8s.io_v1_customresourcedefinition_mysqlclusters.moco.cybozu.com.yaml index b9be3f33..c9cf24dc 100644 --- a/config/crd/tests/apiextensions.k8s.io_v1_customresourcedefinition_mysqlclusters.moco.cybozu.com.yaml +++ b/config/crd/tests/apiextensions.k8s.io_v1_customresourcedefinition_mysqlclusters.moco.cybozu.com.yaml @@ -73,6 +73,9 @@ spec: logRotationSchedule: description: LogRotationSchedule specifies the schedule to rota type: string + logRotationSize: + description: LogRotationSize specifies the size to rotate MySQL + type: integer maxDelaySeconds: default: 60 description: 'MaxDelaySeconds configures the readiness probe of ' diff --git a/controllers/mysql_container.go b/controllers/mysql_container.go index a8463b35..44e84752 100644 --- a/controllers/mysql_container.go +++ b/controllers/mysql_container.go @@ -134,6 +134,9 @@ func (r *MySQLClusterReconciler) makeV1AgentContainer(cluster *mocov1beta2.MySQL if cluster.Spec.LogRotationSchedule != "" { c.WithArgs("--log-rotation-schedule", cluster.Spec.LogRotationSchedule) } + if cluster.Spec.LogRotationSize > 0 { + c.WithArgs("--log-rotation-size", fmt.Sprintf("%d", cluster.Spec.LogRotationSize)) + } if cluster.Spec.AgentUseLocalhost { c.WithArgs(constants.MocoMySQLDLocalhostFlag, strconv.FormatBool(cluster.Spec.AgentUseLocalhost)) diff --git a/controllers/mysqlcluster_controller_test.go b/controllers/mysqlcluster_controller_test.go index ab2eb087..04ca76ca 100644 --- a/controllers/mysqlcluster_controller_test.go +++ b/controllers/mysqlcluster_controller_test.go @@ -1043,6 +1043,7 @@ var _ = Describe("MySQLCluster reconciler", func() { cluster.Spec.MaxDelaySeconds = ptr.To[int](20) cluster.Spec.StartupWaitSeconds = 3 cluster.Spec.LogRotationSchedule = "0 * * * *" + cluster.Spec.LogRotationSize = 1024 cluster.Spec.AgentUseLocalhost = true cluster.Spec.DisableSlowQueryLogContainer = true cluster.Spec.PodTemplate.OverwriteContainers = []mocov1beta2.OverwriteContainer{ @@ -1171,6 +1172,7 @@ var _ = Describe("MySQLCluster reconciler", func() { case constants.AgentContainerName: Expect(c.Args).To(ContainElement("20s")) Expect(c.Args).To(ContainElement("0 * * * *")) + Expect(c.Args).To(ContainElement("1024")) Expect(c.Args).To(ContainElements("--mysqld-localhost", "true")) Expect(c.Resources.Requests).To(Equal(corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")})) Expect(c.Resources.Limits).To(Equal(corev1.ResourceList{corev1.ResourceCPU: resource.MustParse("100m")})) diff --git a/docs/crd_mysqlcluster_v1beta2.md b/docs/crd_mysqlcluster_v1beta2.md index e89a8ebe..8e9c589b 100644 --- a/docs/crd_mysqlcluster_v1beta2.md +++ b/docs/crd_mysqlcluster_v1beta2.md @@ -81,6 +81,7 @@ MySQLClusterSpec defines the desired state of MySQLCluster | maxDelaySecondsForPodDeletion | MaxDelaySecondsForPodDeletion configures the maximum allowed replication delay before a Pod deletion is blocked. If the replication delay exceeds this threshold, deletion of the primary pod will be prevented. The default is 0 seconds. Setting this field to 0 disables the delay check for pod deletion. | int64 | false | | startupWaitSeconds | StartupWaitSeconds is the maximum duration to wait for `mysqld` container to start working. The default is 3600 seconds. | int32 | false | | logRotationSchedule | LogRotationSchedule specifies the schedule to rotate MySQL logs. If not set, the default is to rotate logs every 5 minutes. See https://pkg.go.dev/github.com/robfig/cron/v3#hdr-CRON_Expression_Format for the field format. | string | false | +| logRotationSize | LogRotationSize specifies the size to rotate MySQL logs If not set, size-based log rotation is disabled by default | int | false | | backupPolicyName | The name of BackupPolicy custom resource in the same namespace. If this is set, MOCO creates a CronJob to take backup of this MySQL cluster periodically. | *string | false | | restore | Restore is the specification to perform Point-in-Time-Recovery from existing cluster. If this field is not null, MOCO restores the data as specified and create a new cluster with the data. This field is not editable. | *[RestoreSpec](#restorespec) | false | | disableSlowQueryLogContainer | DisableSlowQueryLogContainer controls whether to add a sidecar container named \"slow-log\" to output slow logs as the containers output. If set to true, the sidecar container is not added. The default is false. | bool | false | diff --git a/go.mod b/go.mod index 90e3a296..2a0441bd 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.17.18 github.com/aws/aws-sdk-go-v2/service/s3 v1.61.2 // Do not update moco-agent until changes that will cause StatefulSet to restart are made. - github.com/cybozu-go/moco-agent v0.12.1 + github.com/cybozu-go/moco-agent v0.13.0 github.com/go-logr/logr v1.4.2 github.com/go-logr/stdr v1.2.2 github.com/go-sql-driver/mysql v1.8.1 diff --git a/go.sum b/go.sum index f533d562..17fc8f67 100644 --- a/go.sum +++ b/go.sum @@ -78,8 +78,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/creack/pty v1.1.18 h1:n56/Zwd5o6whRC5PMGretI4IdRLlmBXYNjScPaBgsbY= github.com/creack/pty v1.1.18/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= -github.com/cybozu-go/moco-agent v0.12.1 h1:Wp3S5whb9b3nD4cNvtJohJ/yBu6TtE7X3Md7WSIK/fw= -github.com/cybozu-go/moco-agent v0.12.1/go.mod h1:qea2oTno2dEpLLHHi/GiY8T0x+Aw6373aGbfk0fA5Bc= +github.com/cybozu-go/moco-agent v0.13.0 h1:74S52H9VhJ1ZnZyB4wPL+PIPRTMS1vjPBmbGtgCrapo= +github.com/cybozu-go/moco-agent v0.13.0/go.mod h1:47vPiMsxKFf0spqts4z1AWKTRIbqAwVOtPcA6q+39zI= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= From b4bc8a88ab80f9c35af6c0e8c9e28f5470ef8539 Mon Sep 17 00:00:00 2001 From: yamatcha Date: Fri, 29 Nov 2024 08:58:02 +0000 Subject: [PATCH 2/2] fix --- api/v1beta2/mysqlcluster_webhook_test.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/api/v1beta2/mysqlcluster_webhook_test.go b/api/v1beta2/mysqlcluster_webhook_test.go index c2964ee5..dfe9a73e 100644 --- a/api/v1beta2/mysqlcluster_webhook_test.go +++ b/api/v1beta2/mysqlcluster_webhook_test.go @@ -151,6 +151,13 @@ var _ = Describe("MySQLCluster Webhook", func() { Expect(err).NotTo(HaveOccurred()) }) + It("should deny an invalid logRotationSchedule", func() { + r := makeMySQLCluster() + r.Spec.LogRotationSchedule = "hoge fuga" + err := k8sClient.Create(ctx, r) + Expect(err).To(HaveOccurred()) + }) + It("should allow a valid logRotationSize", func() { r := makeMySQLCluster() r.Spec.LogRotationSize = 1024 @@ -158,9 +165,9 @@ var _ = Describe("MySQLCluster Webhook", func() { Expect(err).NotTo(HaveOccurred()) }) - It("should deny an invalid logRotationSchedule", func() { + It("should deny an invalid logRotationSize", func() { r := makeMySQLCluster() - r.Spec.LogRotationSchedule = "hoge fuga" + r.Spec.LogRotationSize = -1 err := k8sClient.Create(ctx, r) Expect(err).To(HaveOccurred()) })