Skip to content

Commit b236456

Browse files
authored
Merge pull request #472 from acekingke/FixMemLeak
*: Fix memory Leak of Operator. #469
2 parents 40e4c61 + 8a0adf4 commit b236456

File tree

9 files changed

+88
-66
lines changed

9 files changed

+88
-66
lines changed

backup/backup.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,24 @@ package backup
1919
import (
2020
"fmt"
2121

22+
"github.com/go-logr/logr"
2223
"github.com/radondb/radondb-mysql-kubernetes/utils"
2324
logf "sigs.k8s.io/controller-runtime/pkg/log"
2425

2526
v1alhpa1 "github.com/radondb/radondb-mysql-kubernetes/api/v1alpha1"
2627
)
2728

28-
var log = logf.Log.WithName("backup")
29-
3029
// Backup is a type wrapper over Backup that contains the Business logic
3130
type Backup struct {
3231
*v1alhpa1.Backup
32+
Log logr.Logger
3333
}
3434

3535
// New returns a wraper object over Backup
3636
func New(backup *v1alhpa1.Backup) *Backup {
3737
return &Backup{
3838
Backup: backup,
39+
Log: logf.Log.WithName("backup"),
3940
}
4041
}
4142

backup/status.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,26 +40,26 @@ func (c *Backup) UpdateStatusCondition(condType apiv1alpha1.BackupConditionType,
4040
t := time.Now()
4141

4242
if len(c.Status.Conditions) == 0 {
43-
log.V(4).Info(fmt.Sprintf("Setting lastTransitionTime for mysql backup "+
43+
c.Log.V(4).Info(fmt.Sprintf("Setting lastTransitionTime for mysql backup "+
4444
"%q condition %q to %v", c.Name, condType, t))
4545
newCondition.LastTransitionTime = metav1.NewTime(t)
4646
c.Status.Conditions = []apiv1alpha1.BackupCondition{newCondition}
4747
} else {
4848
if i, exist := c.condExists(condType); exist {
4949
cond := c.Status.Conditions[i]
5050
if cond.Status != newCondition.Status {
51-
log.V(3).Info(fmt.Sprintf("Found status change for mysql backup "+
51+
c.Log.V(3).Info(fmt.Sprintf("Found status change for mysql backup "+
5252
"%q condition %q: %q -> %q; setting lastTransitionTime to %v",
5353
c.Name, condType, cond.Status, status, t))
5454
newCondition.LastTransitionTime = metav1.NewTime(t)
5555
} else {
5656
newCondition.LastTransitionTime = cond.LastTransitionTime
5757
}
58-
log.V(4).Info(fmt.Sprintf("Setting lastTransitionTime for mysql backup "+
58+
c.Log.V(4).Info(fmt.Sprintf("Setting lastTransitionTime for mysql backup "+
5959
"%q condition %q to %q", c.Name, condType, status))
6060
c.Status.Conditions[i] = newCondition
6161
} else {
62-
log.V(4).Info(fmt.Sprintf("Setting new condition for mysql backup %q, condition %q to %q",
62+
c.Log.V(4).Info(fmt.Sprintf("Setting new condition for mysql backup %q, condition %q to %q",
6363
c.Name, condType, status))
6464
newCondition.LastTransitionTime = metav1.NewTime(t)
6565
c.Status.Conditions = append(c.Status.Conditions, newCondition)

backup/syncer/job.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,13 @@ import (
2525
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2626
"k8s.io/apimachinery/pkg/runtime"
2727
"sigs.k8s.io/controller-runtime/pkg/client"
28-
logf "sigs.k8s.io/controller-runtime/pkg/log"
2928

3029
v1alpha1 "github.com/radondb/radondb-mysql-kubernetes/api/v1alpha1"
3130
"github.com/radondb/radondb-mysql-kubernetes/backup"
3231
"github.com/radondb/radondb-mysql-kubernetes/mysqlcluster"
3332
"github.com/radondb/radondb-mysql-kubernetes/utils"
3433
)
3534

36-
var log = logf.Log.WithName("backup.syncer.job")
37-
3835
type jobSyncer struct {
3936
job *batchv1.Job
4037
backup *backup.Backup
@@ -59,7 +56,7 @@ func NewJobSyncer(c client.Client, s *runtime.Scheme, backup *backup.Backup) syn
5956

6057
func (s *jobSyncer) SyncFn() error {
6158
if s.backup.Status.Completed {
62-
log.V(1).Info("backup already completed", "backup", s.backup)
59+
s.backup.Log.V(1).Info("backup already completed", "backup", s.backup)
6360
// skip doing anything
6461
return syncer.ErrIgnore
6562
}

mysqlcluster/mysqlcluster.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import (
3535
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
3636
logf "sigs.k8s.io/controller-runtime/pkg/log"
3737

38+
"github.com/go-logr/logr"
3839
apiv1alpha1 "github.com/radondb/radondb-mysql-kubernetes/api/v1alpha1"
3940
"github.com/radondb/radondb-mysql-kubernetes/utils"
4041
)
@@ -47,17 +48,17 @@ const (
4748
gb
4849
)
4950

50-
var log = logf.Log.WithName("mysqlcluster")
51-
5251
// MysqlCluster is the wrapper for apiv1alpha1.MysqlCluster type.
5352
type MysqlCluster struct {
5453
*apiv1alpha1.MysqlCluster
54+
log logr.Logger
5555
}
5656

5757
// New returns a pointer to MysqlCluster.
5858
func New(mc *apiv1alpha1.MysqlCluster) *MysqlCluster {
5959
return &MysqlCluster{
6060
MysqlCluster: mc,
61+
log: logf.Log.WithName("mysqlcluster"),
6162
}
6263
}
6364

@@ -73,7 +74,7 @@ func (c *MysqlCluster) Validate() error {
7374
// MySQL8 nerver support TokuDB
7475
// https://www.percona.com/blog/2021/05/21/tokudb-support-changes-and-future-removal-from-percona-server-for-mysql-8-0/
7576
if c.Spec.MysqlVersion == "8.0" && c.Spec.MysqlOpts.InitTokuDB {
76-
log.Info("TokuDB is not supported in MySQL 8.0 any more, the value in Cluster.spec.mysqlOpts.initTokuDB should be set false")
77+
c.log.Info("TokuDB is not supported in MySQL 8.0 any more, the value in Cluster.spec.mysqlOpts.initTokuDB should be set false")
7778
return nil
7879
}
7980
// https://github.com/percona/percona-docker/blob/main/percona-server-5.7/ps-entry.sh#L159
@@ -135,7 +136,7 @@ func (c *MysqlCluster) GetMySQLVersion() string {
135136
version = v
136137
} else {
137138
errmsg := "Invalid mysql version option:" + c.Spec.MysqlVersion
138-
log.Error(errors.New(errmsg), "currently we do not support mysql 5.6 or earlier version, default mysql version option should be 5.7 or 8.0")
139+
c.log.Error(errors.New(errmsg), "currently we do not support mysql 5.6 or earlier version, default mysql version option should be 5.7 or 8.0")
139140
return utils.InvalidMySQLVersion
140141
}
141142

mysqlcluster/mysqlcluster_test.go

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"k8s.io/apimachinery/pkg/labels"
3232
"k8s.io/apimachinery/pkg/runtime"
3333
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
34+
logf "sigs.k8s.io/controller-runtime/pkg/log"
3435

3536
mysqlv1alpha1 "github.com/radondb/radondb-mysql-kubernetes/api/v1alpha1"
3637
"github.com/radondb/radondb-mysql-kubernetes/utils"
@@ -46,13 +47,13 @@ var (
4647
},
4748
}
4849
testCluster = MysqlCluster{
49-
&mysqlCluster,
50+
&mysqlCluster, logf.Log.WithName("mysqlcluster"),
5051
}
5152
)
5253

5354
func TestNew(t *testing.T) {
5455
want := &MysqlCluster{
55-
&mysqlCluster,
56+
&mysqlCluster, logf.Log.WithName("mysqlcluster"),
5657
}
5758
assert.Equal(t, want, New(&mysqlCluster))
5859
}
@@ -69,7 +70,8 @@ func TestGetLabel(t *testing.T) {
6970
"app.kubernetes.io/instance": "instance",
7071
}
7172
testCase := MysqlCluster{
72-
&testMysqlCluster,
73+
MysqlCluster: &testMysqlCluster,
74+
log: logf.Log.WithName("mysqlcluster"),
7375
}
7476
want := labels.Set{
7577
"mysql.radondb.com/cluster": "sample",
@@ -88,7 +90,8 @@ func TestGetLabel(t *testing.T) {
8890
"app.kubernetes.io/component": "component",
8991
}
9092
testCase := MysqlCluster{
91-
&testMysqlCluster,
93+
MysqlCluster: &testMysqlCluster,
94+
log: logf.Log.WithName("mysqlcluster"),
9295
}
9396
want := labels.Set{
9497
"mysql.radondb.com/cluster": "sample",
@@ -107,7 +110,8 @@ func TestGetLabel(t *testing.T) {
107110
"app.kubernetes.io/part-of": "part-of",
108111
}
109112
testCase := MysqlCluster{
110-
&testMysqlCluster,
113+
MysqlCluster: &testMysqlCluster,
114+
log: logf.Log.WithName("mysqlcluster"),
111115
}
112116
want := labels.Set{
113117
"mysql.radondb.com/cluster": "sample",
@@ -137,7 +141,8 @@ func TestGetMySQLVersion(t *testing.T) {
137141
testMysqlCluster := mysqlCluster
138142
testMysqlCluster.Spec.MysqlVersion = "8.0"
139143
testCase := MysqlCluster{
140-
&testMysqlCluster,
144+
MysqlCluster: &testMysqlCluster,
145+
log: logf.Log.WithName("mysqlcluster"),
141146
}
142147
want := "8.0.25"
143148
assert.Equal(t, want, testCase.GetMySQLVersion())
@@ -147,7 +152,8 @@ func TestGetMySQLVersion(t *testing.T) {
147152
testMysqlCluster := mysqlCluster
148153
testMysqlCluster.Spec.MysqlVersion = "5.7"
149154
testCase := MysqlCluster{
150-
&testMysqlCluster,
155+
MysqlCluster: &testMysqlCluster,
156+
log: logf.Log.WithName("mysqlcluster"),
151157
}
152158
want := "5.7.34"
153159
assert.Equal(t, want, testCase.GetMySQLVersion())
@@ -157,7 +163,8 @@ func TestGetMySQLVersion(t *testing.T) {
157163
testMysqlCluster := mysqlCluster
158164
testMysqlCluster.Spec.MysqlVersion = "5.7.34"
159165
testCase := MysqlCluster{
160-
&testMysqlCluster,
166+
MysqlCluster: &testMysqlCluster,
167+
log: logf.Log.WithName("mysqlcluster"),
161168
}
162169
want := utils.InvalidMySQLVersion
163170
assert.Equal(t, want, testCase.GetMySQLVersion())
@@ -172,7 +179,8 @@ func TestCreatePeers(t *testing.T) {
172179
testMysqlCluster.ObjectMeta.Namespace = "default"
173180
testMysqlCluster.Spec.Replicas = &replicas
174181
testCase := MysqlCluster{
175-
&testMysqlCluster,
182+
MysqlCluster: &testMysqlCluster,
183+
log: logf.Log.WithName("mysqlcluster"),
176184
}
177185
want := "sample-mysql-0.sample-mysql.default:8801,sample-mysql-1.sample-mysql.default:8801"
178186
assert.Equal(t, want, testCase.CreatePeers())
@@ -183,7 +191,7 @@ func TestCreatePeers(t *testing.T) {
183191
testMysqlCluster.ObjectMeta.Namespace = "default"
184192
testMysqlCluster.Spec.Replicas = &replicas
185193
testCase := MysqlCluster{
186-
&testMysqlCluster,
194+
MysqlCluster: &testMysqlCluster, log: logf.Log.WithName("mysqlcluster"),
187195
}
188196
want := "sample-mysql-0.sample-mysql.default:8801,sample-mysql-1.sample-mysql.default:8801,sample-mysql-2.sample-mysql.default:8801"
189197
assert.Equal(t, want, testCase.CreatePeers())
@@ -194,7 +202,7 @@ func TestCreatePeers(t *testing.T) {
194202
testMysqlCluster.ObjectMeta.Namespace = "default"
195203
testMysqlCluster.Spec.Replicas = &replicas
196204
testCase := MysqlCluster{
197-
&testMysqlCluster,
205+
MysqlCluster: &testMysqlCluster, log: logf.Log.WithName("mysqlcluster"),
198206
}
199207
want := ""
200208
for i := 0; i < 666; i++ {
@@ -212,7 +220,7 @@ func TestCreatePeers(t *testing.T) {
212220
testMysqlCluster.ObjectMeta.Namespace = "default"
213221
testMysqlCluster.Spec.Replicas = &replicas
214222
testCase := MysqlCluster{
215-
&testMysqlCluster,
223+
MysqlCluster: &testMysqlCluster, log: logf.Log.WithName("mysqlcluster"),
216224
}
217225
want := ""
218226
assert.Equal(t, want, testCase.CreatePeers())
@@ -223,7 +231,7 @@ func TestCreatePeers(t *testing.T) {
223231
testMysqlCluster.ObjectMeta.Namespace = "default"
224232
testMysqlCluster.Spec.Replicas = &replicas
225233
testCase := MysqlCluster{
226-
&testMysqlCluster,
234+
MysqlCluster: &testMysqlCluster, log: logf.Log.WithName("mysqlcluster"),
227235
}
228236
want := ""
229237
assert.Equal(t, want, testCase.CreatePeers())
@@ -234,7 +242,7 @@ func TestGetPodHostName(t *testing.T) {
234242
testMysqlCluster := mysqlCluster
235243
testMysqlCluster.ObjectMeta.Namespace = "default"
236244
testCase := MysqlCluster{
237-
&testMysqlCluster,
245+
MysqlCluster: &testMysqlCluster, log: logf.Log.WithName("mysqlcluster"),
238246
}
239247
want0 := "sample-mysql-0.sample-mysql.default"
240248
want1 := "sample-mysql-1.sample-mysql.default"
@@ -314,7 +322,7 @@ func TestEnsureVolumes(t *testing.T) {
314322
testMysql := mysqlCluster
315323
testMysql.Spec.Persistence.Enabled = false
316324
testCase := MysqlCluster{
317-
&testMysql,
325+
MysqlCluster: &testMysql, log: logf.Log.WithName("mysqlcluster"),
318326
}
319327
want := []corev1.Volume{
320328
{
@@ -333,7 +341,7 @@ func TestEnsureVolumes(t *testing.T) {
333341
testMysql.Spec.Persistence.Enabled = true
334342
testMysql.Spec.MysqlOpts.InitTokuDB = true
335343
testCase := MysqlCluster{
336-
&testMysql,
344+
MysqlCluster: &testMysql, log: logf.Log.WithName("mysqlcluster"),
337345
}
338346
want := []corev1.Volume{
339347
{
@@ -353,7 +361,7 @@ func TestEnsureVolumes(t *testing.T) {
353361
testMysql := mysqlCluster
354362
testMysql.Spec.Persistence.Enabled = true
355363
testCase := MysqlCluster{
356-
&testMysql,
364+
MysqlCluster: &testMysql, log: logf.Log.WithName("mysqlcluster"),
357365
}
358366
assert.Equal(t, volume, testCase.EnsureVolumes())
359367
}
@@ -388,7 +396,7 @@ func TestEnsureVolumeClaimTemplates(t *testing.T) {
388396
},
389397
}
390398
testCase := MysqlCluster{
391-
&testMysql,
399+
&testMysql, logf.Log.WithName("mysqlcluster"),
392400
}
393401
want := []corev1.PersistentVolumeClaim{
394402
{
@@ -433,7 +441,7 @@ func TestEnsureVolumeClaimTemplates(t *testing.T) {
433441
testMysql.Spec.Persistence.Size = "10Gi"
434442
testMysql.Spec.Persistence.StorageClass = &storageClass
435443
testCase := MysqlCluster{
436-
&testMysql,
444+
&testMysql, logf.Log.WithName("mysqlcluster"),
437445
}
438446
guard := gomonkey.ApplyFunc(controllerutil.SetControllerReference, func(_ metav1.Object, _ metav1.Object, _ *runtime.Scheme) error {
439447
return nil
@@ -451,7 +459,7 @@ func TestEnsureVolumeClaimTemplates(t *testing.T) {
451459
testMysql.Spec.Persistence.Enabled = true
452460
testMysql.Spec.Persistence.Size = "10Gi"
453461
testCase := MysqlCluster{
454-
&testMysql,
462+
&testMysql, logf.Log.WithName("mysqlcluster"),
455463
}
456464
guard := gomonkey.ApplyFunc(controllerutil.SetControllerReference, func(_ metav1.Object, _ metav1.Object, _ *runtime.Scheme) error {
457465
return fmt.Errorf("test")
@@ -521,7 +529,7 @@ func TestEnsureMysqlConf(t *testing.T) {
521529
{
522530
testMysqlCase := testMysql
523531
testCase := MysqlCluster{
524-
&testMysqlCase,
532+
&testMysqlCase, logf.Log.WithName("mysqlcluster"),
525533
}
526534
testCase.EnsureMysqlConf()
527535
wantSize = strconv.FormatUint(uint64(0.45*float64(gb)), 10)
@@ -539,7 +547,7 @@ func TestEnsureMysqlConf(t *testing.T) {
539547
testMysqlCase := testMysql
540548
testMysqlCase.Spec.MysqlOpts.MysqlConf["innodb_buffer_pool_size"] = strconv.FormatUint(uint64(600*mb), 10)
541549
testCase := MysqlCluster{
542-
&testMysqlCase,
550+
&testMysqlCase, logf.Log.WithName("mysqlcluster"),
543551
}
544552
testCase.EnsureMysqlConf()
545553
wantSize := strconv.FormatUint(uint64(600*float64(mb)), 10)
@@ -559,7 +567,7 @@ func TestEnsureMysqlConf(t *testing.T) {
559567
testMysqlCase.Spec.MysqlOpts.Resources.Requests["memory"] = *memoryCase
560568
testMysqlCase.Spec.MysqlOpts.MysqlConf["innodb_buffer_pool_size"] = strconv.FormatUint(uint64(1.7*float64(gb)), 10)
561569
testCase := MysqlCluster{
562-
&testMysqlCase,
570+
&testMysqlCase, logf.Log.WithName("mysqlcluster"),
563571
}
564572
testCase.EnsureMysqlConf()
565573
wantSize := strconv.FormatUint(uint64(1.6*float64(gb)), 10)
@@ -578,7 +586,7 @@ func TestEnsureMysqlConf(t *testing.T) {
578586
testMysqlCase.Spec.MysqlOpts.Resources.Requests["memory"] = *memoryCase
579587
testMysqlCase.Spec.MysqlOpts.MysqlConf["innodb_buffer_pool_size"] = strconv.FormatUint(uint64(1.7*float64(gb)), 10)
580588
testCase := MysqlCluster{
581-
&testMysqlCase,
589+
&testMysqlCase, logf.Log.WithName("mysqlcluster"),
582590
}
583591
testCase.EnsureMysqlConf()
584592
wantSize := strconv.FormatUint(uint64(1.2*float64(gb)), 10)
@@ -599,7 +607,7 @@ func TestEnsureMysqlConf(t *testing.T) {
599607
testMysqlCase.Spec.MysqlOpts.Resources.Limits["cpu"] = *limitCpucorev1sCase
600608
testMysqlCase.Spec.MysqlOpts.Resources.Requests["memory"] = *memoryCase
601609
testCase := MysqlCluster{
602-
&testMysqlCase,
610+
&testMysqlCase, logf.Log.WithName("mysqlcluster"),
603611
}
604612
testCase.EnsureMysqlConf()
605613
wantSize := strconv.FormatUint(uint64(2*float64(gb)), 10)

mysqlcluster/syncer/mysql_cm.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ import (
2323

2424
"github.com/go-ini/ini"
2525
"github.com/presslabs/controller-util/syncer"
26+
2627
corev1 "k8s.io/api/core/v1"
2728
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2829
"sigs.k8s.io/controller-runtime/pkg/client"
30+
logf "sigs.k8s.io/controller-runtime/pkg/log"
2931

3032
"github.com/radondb/radondb-mysql-kubernetes/mysqlcluster"
3133
"github.com/radondb/radondb-mysql-kubernetes/utils"
@@ -66,6 +68,7 @@ func NewMysqlCMSyncer(cli client.Client, c *mysqlcluster.MysqlCluster) syncer.In
6668

6769
// buildMysqlConf build the mysql config.
6870
func buildMysqlConf(c *mysqlcluster.MysqlCluster) (string, error) {
71+
var log = logf.Log.WithName("mysqlcluster.syncer.buildMysqlConf")
6972
cfg := ini.Empty(ini.LoadOptions{IgnoreInlineComment: true})
7073
sec := cfg.Section("mysqld")
7174

@@ -114,6 +117,7 @@ func buildMysqlPluginConf(c *mysqlcluster.MysqlCluster) (string, error) {
114117

115118
// addKVConfigsToSection add a map[string]string to a ini.Section
116119
func addKVConfigsToSection(s *ini.Section, extraMysqld ...map[string]string) {
120+
var log = logf.Log.WithName("mysqlcluster.syncer.addKVConfigsToSection")
117121
for _, extra := range extraMysqld {
118122
keys := []string{}
119123
for key := range extra {

0 commit comments

Comments
 (0)