Skip to content

Commit 62e836f

Browse files
liyinancndoit18
authored andcommitted
feat(cron): upgrade cron
Signed-off-by: liyinan <[email protected]>
1 parent 7aa3ea1 commit 62e836f

File tree

5 files changed

+37
-23
lines changed

5 files changed

+37
-23
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
2424
different compressors/decompressors when backing up or restoring.
2525
* Add support for MySQL version 8.0
2626
* Add `go modules` cache
27+
* Support cron timezone
2728
### Changed
2829
* Only add `binlog-space-limit` for `percona` image
2930
* Make user-defined InitContainer take the precedence

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ require (
1313
github.com/onsi/ginkgo v1.16.4
1414
github.com/onsi/gomega v1.13.0
1515
github.com/presslabs/controller-util v0.3.0
16+
github.com/robfig/cron/v3 v3.0.1
1617
github.com/spf13/cobra v1.1.1
1718
github.com/spf13/pflag v1.0.5
18-
github.com/wgliang/cron v0.0.0-20180129105837-79834306f643
1919
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781
2020
gopkg.in/ini.v1 v1.57.0 // indirect
2121

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ github.com/prometheus/procfs v0.2.0/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O
363363
github.com/prometheus/procfs v0.6.0 h1:mxy4L2jP6qMonqmq+aTtOx1ifVWUgG/TAmntgbh3xv4=
364364
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
365365
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
366+
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
367+
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
366368
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
367369
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
368370
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
@@ -407,8 +409,6 @@ github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69
407409
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
408410
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
409411
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
410-
github.com/wgliang/cron v0.0.0-20180129105837-79834306f643 h1:1fftKBnwgZ+quDW84aOBX0WY1/AivOUlimBSt5XKtKY=
411-
github.com/wgliang/cron v0.0.0-20180129105837-79834306f643/go.mod h1:8vrxYe6J+ZIHJViXE2UhdSbbu3VWHGxLo+QzdqeGDvM=
412412
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
413413
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
414414
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

pkg/controller/mysqlbackupcron/mysqlbackupcron_controller.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
"reflect"
2323
"sync"
2424

25-
"github.com/wgliang/cron"
25+
cron "github.com/robfig/cron/v3"
2626
"k8s.io/apimachinery/pkg/api/errors"
2727
"k8s.io/apimachinery/pkg/runtime"
2828
"k8s.io/apimachinery/pkg/types"
@@ -43,18 +43,25 @@ const (
4343
controllerName = "mysqlbackupcron-controller"
4444
)
4545

46-
var log = logf.Log.WithName(controllerName)
46+
var (
47+
log = logf.Log.WithName(controllerName)
48+
defaultParser = cron.NewParser(
49+
cron.Second | cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.DowOptional | cron.Descriptor,
50+
)
51+
)
4752

4853
// Add creates a new MysqlBackup Controller and adds it to the Manager with default RBAC. The Manager will set fields on the Controller
4954
// and Start it when the Manager is Started.
5055
func Add(mgr manager.Manager) error {
5156
sscron := startStopCron{
5257
Cron: cron.New(),
5358
}
59+
5460
err := mgr.Add(sscron)
5561
if err != nil {
5662
return err
5763
}
64+
5865
return add(mgr, newReconciler(mgr, sscron.Cron))
5966
}
6067

@@ -131,7 +138,7 @@ func (r *ReconcileMysqlBackup) Reconcile(ctx context.Context, request reconcile.
131138
return reconcile.Result{}, nil
132139
}
133140

134-
schedule, err := cron.Parse(cluster.Spec.BackupSchedule)
141+
schedule, err := defaultParser.Parse(cluster.Spec.BackupSchedule)
135142
if err != nil {
136143
return reconcile.Result{}, fmt.Errorf("failed to parse schedule: %s", err)
137144
}
@@ -149,16 +156,16 @@ func (r *ReconcileMysqlBackup) updateClusterSchedule(cluster *mysqlv1alpha1.Mysq
149156
for _, entry := range r.cron.Entries() {
150157
j, ok := entry.Job.(*job)
151158
if ok && j.ClusterName == cluster.Name && j.Namespace == cluster.Namespace {
159+
152160
log.V(1).Info("cluster already added to cron.", "key", cluster)
153161

154162
// change scheduler for already added crons
155163
if !reflect.DeepEqual(entry.Schedule, schedule) {
156164
log.Info("update cluster scheduler", "key", cluster,
157165
"scheduler", cluster.Spec.BackupSchedule)
158166

159-
if err := r.cron.Remove(cluster.Name); err != nil {
160-
return err
161-
}
167+
r.cron.Remove(entry.ID)
168+
162169
break
163170
}
164171

@@ -169,9 +176,8 @@ func (r *ReconcileMysqlBackup) updateClusterSchedule(cluster *mysqlv1alpha1.Mysq
169176
newValFmt = fmt.Sprintf("%d", cluster.Spec.BackupScheduleJobsHistoryLimit)
170177
}
171178
log.Info("update cluster backup limit", "key", cluster, "limit_val", newValFmt)
172-
if err := r.cron.Remove(cluster.Name); err != nil {
173-
return err
174-
}
179+
r.cron.Remove(entry.ID)
180+
175181
break
176182

177183
}
@@ -187,7 +193,7 @@ func (r *ReconcileMysqlBackup) updateClusterSchedule(cluster *mysqlv1alpha1.Mysq
187193
c: r.Client,
188194
BackupScheduleJobsHistoryLimit: cluster.Spec.BackupScheduleJobsHistoryLimit,
189195
BackupRemoteDeletePolicy: cluster.Spec.BackupRemoteDeletePolicy,
190-
}, cluster.Name)
196+
})
191197

192198
return nil
193199
}
@@ -196,8 +202,11 @@ func (r *ReconcileMysqlBackup) unregisterCluster(clusterKey types.NamespacedName
196202
r.lockJobRegister.Lock()
197203
defer r.lockJobRegister.Unlock()
198204

199-
if err := r.cron.Remove(clusterKey.Name); err != nil {
200-
return err
205+
for _, entry := range r.cron.Entries() {
206+
j, ok := entry.Job.(*job)
207+
if ok && j.ClusterName == clusterKey.Name && j.Namespace == clusterKey.Namespace {
208+
r.cron.Remove(entry.ID)
209+
}
201210
}
202211

203212
return nil
@@ -209,6 +218,7 @@ func addBackupFieldIndexers(mgr manager.Manager) error {
209218
if b.(*mysqlv1alpha1.MysqlBackup).Status.Completed {
210219
completed = "true"
211220
}
221+
212222
return []string{completed}
213223
})
214224
}

pkg/controller/mysqlbackupcron/mysqlbackupcron_controller_test.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
. "github.com/onsi/gomega/gstruct"
2828
gomegatypes "github.com/onsi/gomega/types"
2929

30-
cronpkg "github.com/wgliang/cron"
30+
cronpkg "github.com/robfig/cron/v3"
3131
"golang.org/x/net/context"
3232
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3333
"k8s.io/apimachinery/pkg/labels"
@@ -50,7 +50,10 @@ var _ = Describe("MysqlBackupCron controller", func() {
5050
// cron job
5151
cron *cronpkg.Cron
5252

53-
ctxCancel func()
53+
ctxCancel func()
54+
defaultParser = cronpkg.NewParser(
55+
cronpkg.Second | cronpkg.Minute | cronpkg.Hour | cronpkg.Dom | cronpkg.Month | cronpkg.DowOptional | cronpkg.Descriptor,
56+
)
5457
)
5558

5659
BeforeEach(func() {
@@ -107,7 +110,7 @@ var _ = Describe("MysqlBackupCron controller", func() {
107110
},
108111
}
109112

110-
schedule, err = cronpkg.Parse(cluster.Spec.BackupSchedule)
113+
schedule, err = defaultParser.Parse(cluster.Spec.BackupSchedule)
111114
Expect(err).To(Succeed())
112115
})
113116

@@ -142,7 +145,7 @@ var _ = Describe("MysqlBackupCron controller", func() {
142145
It("should update cluster backup schedule", func() {
143146
// update cluster scheduler
144147
cluster.Spec.BackupSchedule = "0 0 0 * * *"
145-
newSchedule, _ := cronpkg.Parse(cluster.Spec.BackupSchedule)
148+
newSchedule, _ := defaultParser.Parse(cluster.Spec.BackupSchedule)
146149
Expect(c.Update(context.TODO(), cluster)).To(Succeed())
147150

148151
// expect an reconcile event
@@ -176,12 +179,12 @@ var _ = Describe("MysqlBackupCron controller", func() {
176179
Eventually(requests, timeout).Should(Receive(Equal(expectedRequest)))
177180

178181
// check cron entry to have a single entry
179-
Expect(cron.Entries()).To(ContainElement(PointTo(MatchFields(IgnoreExtras, Fields{
182+
Expect(cron.Entries()).To(ContainElement(MatchFields(IgnoreExtras, Fields{
180183
"Job": PointTo(MatchFields(IgnoreExtras, Fields{
181184
"ClusterName": Equal(cluster.Name),
182185
"BackupScheduleJobsHistoryLimit": PointTo(Equal(limit)),
183186
})),
184-
}))))
187+
})))
185188
})
186189

187190
When("backup is executed once per second", func() {
@@ -221,10 +224,10 @@ var _ = Describe("MysqlBackupCron controller", func() {
221224
})
222225

223226
func haveCronJob(name string, sched cronpkg.Schedule) gomegatypes.GomegaMatcher {
224-
return ContainElement(PointTo(MatchFields(IgnoreExtras, Fields{
227+
return ContainElement(MatchFields(IgnoreExtras, Fields{
225228
"Job": PointTo(MatchFields(IgnoreExtras, Fields{
226229
"ClusterName": Equal(name),
227230
})),
228231
"Schedule": Equal(sched),
229-
})))
232+
}))
230233
}

0 commit comments

Comments
 (0)