Skip to content

Commit a1f6979

Browse files
committed
Remove backup service account logic. Register cluster to cron.
1 parent bccf4c4 commit a1f6979

File tree

15 files changed

+39
-300
lines changed

15 files changed

+39
-300
lines changed

cmd/mysql-helper/appschedulebackup/appschedulebackup.go

Lines changed: 0 additions & 111 deletions
This file was deleted.

cmd/mysql-helper/main.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/presslabs/mysql-operator/cmd/mysql-helper/appclone"
3030
"github.com/presslabs/mysql-operator/cmd/mysql-helper/appconf"
3131
"github.com/presslabs/mysql-operator/cmd/mysql-helper/apphelper"
32-
"github.com/presslabs/mysql-operator/cmd/mysql-helper/appschedulebackup"
3332
"github.com/presslabs/mysql-operator/cmd/mysql-helper/apptakebackup"
3433
"github.com/presslabs/mysql-operator/pkg/util/logs"
3534
)
@@ -103,27 +102,6 @@ func main() {
103102
}
104103
cmd.AddCommand(takeBackupCmd)
105104

106-
var backupNamespace string
107-
scheduleBackupCmd := &cobra.Command{
108-
Use: "schedule-backup",
109-
Short: "Schedule a backup for cluster",
110-
Args: func(cmd *cobra.Command, args []string) error {
111-
if len(args) != 1 {
112-
return fmt.Errorf("require cluster name")
113-
}
114-
return nil
115-
},
116-
Run: func(cmd *cobra.Command, args []string) {
117-
err := appschedulebackup.RunCommand(stopCh, backupNamespace, args[0])
118-
if err != nil {
119-
glog.Fatalf("Schduler command failed with error: %s .", err)
120-
}
121-
},
122-
}
123-
scheduleBackupCmd.Flags().StringVar(&backupNamespace, "namespace", "default",
124-
"Specify the namespace where to create backups.")
125-
cmd.AddCommand(scheduleBackupCmd)
126-
127105
cmd.PersistentFlags().AddGoFlagSet(flag.CommandLine)
128106
flag.CommandLine.Parse([]string{})
129107
if err := cmd.Execute(); err != nil {

hack/charts/mysql-operator/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ keywords:
66
- percona
77
- orchestrator
88
- presslabs
9-
version: 0.1.9
9+
version: 0.1.10
1010
home: https://github.com/presslabs/mysql-operator
1111
sources:
1212
- https://github.com/presslabs/mysql-operator.git
13-
appVersion: 0.1.9
13+
appVersion: 0.1.10

hack/charts/mysql-operator/templates/deployment.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ spec:
4747
{{- if .Values.installCRDs }}
4848
- --install-crds
4949
{{- end }}
50-
{{- if .Values.rbac.create }}
51-
- --backup-service-account-name={{ template "mysql-operator.serviceAccountName" . }}-backups
52-
{{- end }}
5350
resources:
5451
{{ toYaml .Values.resources | nindent 12 }}
5552
livenessProbe:

hack/charts/mysql-operator/templates/rbac.yaml

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -56,38 +56,4 @@ subjects:
5656
- name: {{ template "mysql-operator.serviceAccountName" . }}
5757
namespace: {{ .Release.Namespace | quote }}
5858
kind: ServiceAccount
59-
60-
---
61-
apiVersion: rbac.authorization.k8s.io/v1beta1
62-
kind: ClusterRole
63-
metadata:
64-
name: {{ template "mysql-operator.fullname" . }}-backups
65-
labels:
66-
app: {{ template "mysql-operator.name" . }}
67-
chart: {{ template "mysql-operator.chart" . }}
68-
release: {{ .Release.Name }}
69-
heritage: {{ .Release.Service }}
70-
rules:
71-
- apiGroups: ["mysql.presslabs.org"]
72-
resources: ["mysqlbackups"]
73-
verbs: ["*"]
74-
---
75-
apiVersion: rbac.authorization.k8s.io/v1beta1
76-
kind: ClusterRoleBinding
77-
metadata:
78-
name: {{ template "mysql-operator.fullname" . }}-backups
79-
labels:
80-
app: {{ template "mysql-operator.name" . }}
81-
chart: {{ template "mysql-operator.chart" . }}
82-
release: {{ .Release.Name }}
83-
heritage: {{ .Release.Service }}
84-
roleRef:
85-
apiGroup: rbac.authorization.k8s.io
86-
kind: ClusterRole
87-
name: {{ template "mysql-operator.fullname" . }}-backups
88-
subjects:
89-
- name: {{ template "mysql-operator.serviceAccountName" . }}-backups
90-
namespace: {{ .Release.Namespace | quote }}
91-
kind: ServiceAccount
92-
9359
{{- end -}}

hack/charts/mysql-operator/templates/serviceaccount.yaml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,4 @@ metadata:
88
chart: {{ template "mysql-operator.chart" . }}
99
release: {{ .Release.Name }}
1010
heritage: {{ .Release.Service }}
11-
12-
---
13-
apiVersion: v1
14-
kind: ServiceAccount
15-
metadata:
16-
name: {{ template "mysql-operator.serviceAccountName" . }}-backups
17-
labels:
18-
app: {{ template "mysql-operator.name" . }}
19-
chart: {{ template "mysql-operator.chart" . }}
20-
release: {{ .Release.Name }}
21-
heritage: {{ .Release.Service }}
22-
2311
{{- end }}

pkg/apis/mysql/v1alpha1/status.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,6 @@ const (
151151
EventReasonServiceUpdated = "HLServiceUpdated"
152152
EventReasonSFSFailed = "StatefulSetFailed"
153153
EventReasonSFSUpdated = "StatefulSetUpdated"
154-
EventReasonCronJobFailed = "CronJobFailed"
155-
EventReasonCronJobUpdated = "CronJobUpdated"
156154
EventReasonMasterServiceFailed = "MasterServiceFailed"
157155
EventReasonMasterServiceUpdated = "MasterServiceUpdated"
158156
EventReasonHealthyNodesServiceFailed = "HealthyNodesServiceFailed"

pkg/controller/clustercontroller/backups.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ package clustercontroller
1818

1919
import (
2020
"fmt"
21+
"reflect"
2122
"sync"
2223
"time"
2324

2425
"github.com/golang/glog"
25-
//"github.com/robfig/cron"
26+
"github.com/wgliang/cron"
2627
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2728
"k8s.io/apimachinery/pkg/util/wait"
2829

@@ -57,28 +58,42 @@ func (c *Controller) registerClusterInBackupCron(cluster *api.MysqlCluster) erro
5758
return nil
5859
}
5960

61+
schedule, err := cron.Parse(cluster.Spec.BackupSchedule)
62+
if err != nil {
63+
return fmt.Errorf("fail to parse schedule: %s", err)
64+
}
65+
6066
lockJobRegister.Lock()
6167
defer lockJobRegister.Unlock()
6268

6369
for _, entry := range c.cron.Entries() {
6470
j, ok := entry.Job.(job)
6571
if ok && j.Name == cluster.Name && j.Namespace == cluster.Namespace {
6672
glog.V(3).Infof("Cluster %s already added to cron.", cluster.Name)
73+
if !reflect.DeepEqual(entry.Schedule, schedule) {
74+
glog.Infof("Update cluster '%s' scheduler to: %s",
75+
cluster.Name, cluster.Spec.BackupSchedule)
76+
c.cron.Remove(cluster.Name)
77+
break
78+
}
6779
return nil
6880
}
6981
}
7082

71-
return c.cron.AddJob(cluster.Spec.BackupSchedule, job{
83+
c.cron.Schedule(schedule, job{
7284
Name: cluster.Name,
7385
Namespace: cluster.Namespace,
7486
myClient: c.myClient,
7587
BackupRunning: new(bool),
7688
lock: new(sync.Mutex),
77-
})
89+
}, cluster.Name)
90+
91+
return nil
7892
}
7993

8094
func (j job) Run() {
8195
backupName := fmt.Sprintf("%s-auto-backup-%s", j.Name, time.Now().Format("2006-01-02t15-04-05"))
96+
glog.Infof("Schedul backup job started. Creating backup %s..", backupName)
8297

8398
// Wrap backup creation to ensure that lock is released when backup is
8499
// created

pkg/controller/clustercontroller/backups_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
. "github.com/onsi/ginkgo"
2727
. "github.com/onsi/gomega"
2828
. "github.com/onsi/gomega/gstruct"
29-
"github.com/robfig/cron"
29+
"github.com/wgliang/cron"
3030
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3131

3232
api "github.com/presslabs/mysql-operator/pkg/apis/mysql/v1alpha1"
@@ -114,12 +114,14 @@ var _ = Describe("Test cluster reconciliation queue", func() {
114114
BackupRunning: new(bool),
115115
lock: new(sync.Mutex),
116116
}
117-
go j.Run()
117+
118118
go j.Run()
119119

120-
Eventually(func() *bool {
121-
return j.BackupRunning
122-
}).Should(PointTo(Equal(true)))
120+
Eventually(func() bool {
121+
j.lock.Lock() // avoid a data race
122+
defer j.lock.Unlock()
123+
return *j.BackupRunning
124+
}).Should(Equal(true))
123125
Eventually(func() []api.MysqlBackup {
124126
bs, _ := myClient.Mysql().MysqlBackups(j.Namespace).List(metav1.ListOptions{})
125127
return bs.Items
@@ -131,9 +133,11 @@ var _ = Describe("Test cluster reconciliation queue", func() {
131133
_, err := myClient.Mysql().MysqlBackups(j.Namespace).Update(&backup)
132134
Expect(err).To(Succeed())
133135

134-
Eventually(func() *bool {
135-
return j.BackupRunning
136-
}).Should(PointTo(Equal(false)))
136+
Eventually(func() bool {
137+
j.lock.Lock() // avoid a data race
138+
defer j.lock.Unlock()
139+
return *j.BackupRunning
140+
}).Should(Equal(false))
137141
})
138142
})
139143
})

pkg/controller/clustercontroller/controller.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"time"
2424

2525
"github.com/golang/glog"
26-
"github.com/robfig/cron"
26+
"github.com/wgliang/cron"
2727
apiext_clientset "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
2828
k8errors "k8s.io/apimachinery/pkg/api/errors"
2929
"k8s.io/apimachinery/pkg/util/runtime"
@@ -151,6 +151,9 @@ func (c *Controller) Start(workers int, stopCh <-chan struct{}) error {
151151
return fmt.Errorf("error waiting for informer cache to sync")
152152
}
153153

154+
// start cron
155+
c.cron.Start()
156+
154157
for i := 0; i < workers; i++ {
155158
c.workerWg.Add(1)
156159
go wait.Until(func() { c.workerController(stopCh) }, workerPeriodTime, stopCh)
@@ -165,6 +168,7 @@ func (c *Controller) Start(workers int, stopCh <-chan struct{}) error {
165168
glog.V(2).Info("Shutting down controller.")
166169
c.queue.ShutDown()
167170
c.reconcileQueue.ShutDown()
171+
c.cron.Stop()
168172
glog.V(2).Info("Wait for workers to exit...")
169173
c.workerWg.Wait()
170174
glog.V(2).Info("Workers exited.")

0 commit comments

Comments
 (0)