Skip to content

Commit 3e364c3

Browse files
authored
opt(operator-v2): Resolve BR todos (#6086)
1 parent 24fc7b3 commit 3e364c3

37 files changed

+850
-453
lines changed

.golangci.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,6 @@ issues:
126126

127127
run:
128128
timeout: 15m
129+
skip-files:
130+
- ".*/br/.*/_test.go"
131+
- ".*/br/.*/testutils/.*"

api/br/v1alpha1/backup.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,6 @@ func (bk *Backup) GetAllLogBackupJobName() []string {
6868
}
6969
}
7070

71-
// TODO(ideascf): do we need these functions?
72-
// // GetTidbEndpointHash return the hash string base on tidb cluster's host and port
73-
//
74-
// func (bk *Backup) GetTidbEndpointHash() string {
75-
// return HashContents([]byte(bk.Spec.From.GetTidbEndpoint()))
76-
// }
77-
//
78-
// // GetBackupPVCName return the backup pvc name
79-
//
80-
// func (bk *Backup) GetBackupPVCName() string {
81-
// return fmt.Sprintf("backup-pvc-%s", bk.GetTidbEndpointHash())
82-
// }
83-
//
8471
// GetInstanceName return the backup instance name
8572
func (bk *Backup) GetInstanceName() string {
8673
if bk.Labels != nil {
@@ -121,7 +108,6 @@ var (
121108
RestoreControllerKind = SchemeGroupVersion.WithKind("Restore")
122109
)
123110

124-
// TODO(ideascf): copy UT
125111
// GetBackupOwnerRef returns Backup's OwnerReference
126112
func GetBackupOwnerRef(backup *Backup) metav1.OwnerReference {
127113
controller := true

api/br/v1alpha1/backup_test.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// Copyright 2024 PingCAP, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package v1alpha1
16+
17+
import (
18+
"testing"
19+
20+
. "github.com/onsi/gomega"
21+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
"k8s.io/apimachinery/pkg/types"
23+
24+
metav1alpha1 "github.com/pingcap/tidb-operator/api/v2/meta/v1alpha1"
25+
)
26+
27+
func TestGetBackupOwnerRef(t *testing.T) {
28+
g := NewGomegaWithT(t)
29+
30+
b := newBackup()
31+
b.UID = types.UID("demo-uid")
32+
ref := GetBackupOwnerRef(b)
33+
g.Expect(ref.APIVersion).To(Equal(BackupControllerKind.GroupVersion().String()))
34+
g.Expect(ref.Kind).To(Equal(BackupControllerKind.Kind))
35+
g.Expect(ref.Name).To(Equal(b.GetName()))
36+
g.Expect(ref.UID).To(Equal(types.UID("demo-uid")))
37+
g.Expect(*ref.Controller).To(BeTrue())
38+
g.Expect(*ref.BlockOwnerDeletion).To(BeTrue())
39+
}
40+
41+
func TestGetRestoreOwnerRef(t *testing.T) {
42+
g := NewGomegaWithT(t)
43+
44+
r := newRestore()
45+
r.UID = types.UID("demo-uid")
46+
ref := GetRestoreOwnerRef(r)
47+
g.Expect(ref.APIVersion).To(Equal(RestoreControllerKind.GroupVersion().String()))
48+
g.Expect(ref.Kind).To(Equal(RestoreControllerKind.Kind))
49+
g.Expect(ref.Name).To(Equal(r.GetName()))
50+
g.Expect(ref.UID).To(Equal(types.UID("demo-uid")))
51+
g.Expect(*ref.Controller).To(BeTrue())
52+
g.Expect(*ref.BlockOwnerDeletion).To(BeTrue())
53+
}
54+
55+
func TestGetBackupScheduleOwnerRef(t *testing.T) {
56+
g := NewGomegaWithT(t)
57+
58+
b := newBackupSchedule()
59+
b.UID = types.UID("demo-uid")
60+
ref := GetBackupScheduleOwnerRef(b)
61+
g.Expect(ref.APIVersion).To(Equal(backupScheduleControllerKind.GroupVersion().String()))
62+
g.Expect(ref.Kind).To(Equal(backupScheduleControllerKind.Kind))
63+
g.Expect(ref.Name).To(Equal(b.GetName()))
64+
g.Expect(ref.UID).To(Equal(types.UID("demo-uid")))
65+
g.Expect(*ref.Controller).To(BeTrue())
66+
g.Expect(*ref.BlockOwnerDeletion).To(BeTrue())
67+
}
68+
69+
func newBackup() *Backup {
70+
backup := &Backup{
71+
ObjectMeta: metav1.ObjectMeta{
72+
Name: "demo-backup",
73+
Namespace: metav1.NamespaceDefault,
74+
Labels: map[string]string{
75+
metav1alpha1.NameLabelKey: metav1alpha1.BackupJobLabelVal,
76+
},
77+
},
78+
Spec: BackupSpec{},
79+
}
80+
return backup
81+
}
82+
83+
func newRestore() *Restore {
84+
restore := &Restore{
85+
ObjectMeta: metav1.ObjectMeta{
86+
Name: "demo-backup",
87+
Namespace: metav1.NamespaceDefault,
88+
Labels: map[string]string{
89+
metav1alpha1.NameLabelKey: metav1alpha1.RestoreJobLabelVal,
90+
},
91+
},
92+
Spec: RestoreSpec{},
93+
}
94+
return restore
95+
}
96+
97+
func newBackupSchedule() *BackupSchedule {
98+
backup := &BackupSchedule{
99+
ObjectMeta: metav1.ObjectMeta{
100+
Name: "demo-backup",
101+
Namespace: metav1.NamespaceDefault,
102+
Labels: map[string]string{
103+
metav1alpha1.NameLabelKey: metav1alpha1.BackupScheduleJobLabelVal,
104+
},
105+
},
106+
Spec: BackupScheduleSpec{
107+
BackupTemplate: BackupSpec{},
108+
},
109+
}
110+
return backup
111+
}

api/br/v1alpha1/backup_types.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ type Progress struct {
259259
// Step is the step name of progress
260260
Step string `json:"step,omitempty"`
261261
// Progress is the backup progress value
262-
Progress int `json:"progress,omitempty"` // TODO(ideascf): type changed from float64 to int
262+
Progress int `json:"progress,omitempty"`
263263
// LastTransitionTime is the update time
264264
// +nullable
265265
LastTransitionTime metav1.Time `json:"lastTransitionTime,omitempty"`
@@ -295,40 +295,41 @@ type BackupSpec struct {
295295
// Mode is the backup mode, such as snapshot backup or log backup.
296296
// +kubebuilder:default=snapshot
297297
Mode BackupMode `json:"backupMode,omitempty"`
298-
// TikvGCLifeTime is to specify the safe gc life time for backup.
299-
// The time limit during which data is retained for each GC, in the format of Go Duration.
300-
// When a GC happens, the current time minus this value is the safe point.
301-
TikvGCLifeTime *string `json:"tikvGCLifeTime,omitempty"`
302298
// StorageProvider configures where and how backups should be stored.
303299
// *** Note: This field should generally not be left empty, unless you are certain the storage provider
304300
// *** can be obtained from another source, such as a schedule CR.
305301
StorageProvider `json:",inline"`
302+
// BRConfig is the configs for BR
303+
// *** Note: This field should generally not be left empty, unless you are certain the BR config
304+
// *** can be obtained from another source, such as a schedule CR.
305+
BR *BRConfig `json:"br,omitempty"`
306+
306307
// The storageClassName of the persistent volume for Backup data storage.
307308
// Defaults to Kubernetes default storage class.
308309
// +optional
309310
StorageClassName *string `json:"storageClassName,omitempty"`
310311
// StorageSize is the request storage size for backup job
311312
StorageSize string `json:"storageSize,omitempty"`
312-
// BRConfig is the configs for BR
313-
// *** Note: This field should generally not be left empty, unless you are certain the BR config
314-
// *** can be obtained from another source, such as a schedule CR.
315-
BR *BRConfig `json:"br,omitempty"`
313+
316314
// CommitTs is the commit ts of the backup, snapshot ts for full backup or start ts for log backup.
317315
// Format supports TSO or datetime, e.g. '400036290571534337', '2018-05-11 01:42:23'.
318316
// Default is current timestamp.
319317
// +optional
320318
CommitTs string `json:"commitTs,omitempty"`
321319
// Subcommand is the subcommand for BR, such as start, stop, pause etc.
322320
// +optional
323-
// +kubebuilder:validation:Enum:="log-start";"log-stop";"log-pause"
321+
// +kubebuilder:validation:Enum:="log-start";"log-stop";"log-pause";"log-resume";"log-truncate"
324322
LogSubcommand LogSubCommandType `json:"logSubcommand,omitempty"`
325323
// LogTruncateUntil is log backup truncate until timestamp.
326324
// Format supports TSO or datetime, e.g. '400036290571534337', '2018-05-11 01:42:23'.
325+
// It's required when LogSubcommand is "log-truncate".
327326
// +optional
328327
LogTruncateUntil string `json:"logTruncateUntil,omitempty"`
328+
// Deprecated: use LogSubcommand instead. it will be removed later.
329329
// LogStop indicates that will stop the log backup.
330330
// +optional
331331
LogStop bool `json:"logStop,omitempty"`
332+
332333
// Base tolerations of backup Pods, components may add more tolerations upon this respectively
333334
// +optional
334335
Tolerations []corev1.Toleration `json:"tolerations,omitempty"`
@@ -349,6 +350,7 @@ type BackupSpec struct {
349350
UseKMS bool `json:"useKMS,omitempty"`
350351
// Specify service account of backup
351352
ServiceAccount string `json:"serviceAccount,omitempty"`
353+
352354
// CleanPolicy denotes whether to clean backup data when the object is deleted from the cluster, if not set, the backup data will be retained
353355
// +kubebuilder:validation:Enum:=Retain;OnFailure;Delete
354356
// +kubebuilder:default=Retain

api/br/v1alpha1/restore.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,3 @@ func IsRestoreFailed(restore *Restore) bool {
121121
_, condition := GetRestoreCondition(&restore.Status, RestoreFailed)
122122
return condition != nil && condition.Status == metav1.ConditionTrue
123123
}
124-
125-
// IsRestoreTiKVComplete returns true if all TiKVs run successfully during volume restore
126-
func IsRestoreTiKVComplete(restore *Restore) bool {
127-
_, condition := GetRestoreCondition(&restore.Status, RestoreTiKVComplete)
128-
return condition != nil && condition.Status == metav1.ConditionTrue
129-
}
130-
131-
// IsRestoreDataComplete returns true if a Restore for data consistency has successfully completed
132-
func IsRestoreDataComplete(restore *Restore) bool {
133-
_, condition := GetRestoreCondition(&restore.Status, RestoreDataComplete)
134-
return condition != nil && condition.Status == metav1.ConditionTrue
135-
}

api/br/v1alpha1/restore_types.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,6 @@ const (
7676
RestoreScheduled RestoreConditionType = "Scheduled"
7777
// RestoreRunning means the Restore is currently being executed.
7878
RestoreRunning RestoreConditionType = "Running"
79-
// RestoreDataComplete means the Restore has successfully executed part-2 and the
80-
// data in restore volumes has been deal with consistency based on min_resolved_ts
81-
RestoreDataComplete RestoreConditionType = "DataComplete"
82-
// RestoreTiKVComplete means in volume restore, all TiKV instances are started and up
83-
RestoreTiKVComplete RestoreConditionType = "TikvComplete"
8479
// RestoreComplete means the Restore has successfully executed and the
8580
// backup data has been loaded into tidb cluster.
8681
RestoreComplete RestoreConditionType = "Complete"
@@ -131,28 +126,27 @@ type RestoreSpec struct {
131126
// Mode is the restore mode. such as snapshot or pitr.
132127
// +kubebuilder:default=snapshot
133128
Mode RestoreMode `json:"restoreMode,omitempty"`
129+
// StorageProvider configures where and how backups should be stored.
130+
StorageProvider `json:",inline"`
131+
// BR is the configs for BR.
132+
BR *BRConfig `json:"br,omitempty"`
133+
134134
// PitrRestoredTs is the pitr restored ts.
135135
PitrRestoredTs string `json:"pitrRestoredTs,omitempty"`
136136
// LogRestoreStartTs is the start timestamp which log restore from.
137137
// +optional
138138
LogRestoreStartTs string `json:"logRestoreStartTs,omitempty"`
139-
// TikvGCLifeTime is to specify the safe gc life time for restore.
140-
// The time limit during which data is retained for each GC, in the format of Go Duration.
141-
// When a GC happens, the current time minus this value is the safe point.
142-
TikvGCLifeTime *string `json:"tikvGCLifeTime,omitempty"`
143-
// StorageProvider configures where and how backups should be stored.
144-
StorageProvider `json:",inline"`
145139
// PitrFullBackupStorageProvider configures where and how pitr dependent full backup should be stored.
146140
// +optional
147141
PitrFullBackupStorageProvider StorageProvider `json:"pitrFullBackupStorageProvider,omitempty"`
142+
148143
// The storageClassName of the persistent volume for Restore data storage.
149144
// Defaults to Kubernetes default storage class.
150145
// +optional
151146
StorageClassName *string `json:"storageClassName,omitempty"`
152147
// StorageSize is the request storage size for backup job
153148
StorageSize string `json:"storageSize,omitempty"`
154-
// BR is the configs for BR.
155-
BR *BRConfig `json:"br,omitempty"`
149+
156150
// Base tolerations of restore Pods, components may add more tolerations upon this respectively
157151
// +optional
158152
Tolerations []corev1.Toleration `json:"tolerations,omitempty"`

api/br/v1alpha1/zz_generated.deepcopy.go

Lines changed: 10 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/core/v1alpha1/names.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ const (
9090
DirPathClusterTLSPD = "/var/lib/pd-tls"
9191
DirPathClusterTLSTiKV = "/var/lib/tikv-tls"
9292
DirPathClusterTLSTiDB = "/var/lib/tidb-tls"
93-
DirPathTiDBClientTLS = "/var/lib/tidb-client-tls" // FIXME(ideascf): do we need this?
9493
DirPathClusterTLSTiFlash = "/var/lib/tiflash-tls"
9594
DirPathClusterClientTLS = "/var/lib/cluster-client-tls"
9695
DirPathClusterTLSTiCDC = "/var/lib/ticdc-tls"

api/go.mod

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.22.0
55
toolchain go1.22.3
66

77
require (
8+
github.com/onsi/gomega v1.36.2
89
k8s.io/api v0.31.0
910
k8s.io/apimachinery v0.31.0
1011
)
@@ -13,15 +14,17 @@ require (
1314
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
1415
github.com/go-logr/logr v1.4.2 // indirect
1516
github.com/gogo/protobuf v1.3.2 // indirect
17+
github.com/google/go-cmp v0.6.0 // indirect
1618
github.com/google/gofuzz v1.2.0 // indirect
1719
github.com/json-iterator/go v1.1.12 // indirect
1820
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
1921
github.com/modern-go/reflect2 v1.0.2 // indirect
2022
github.com/x448/float16 v0.8.4 // indirect
21-
golang.org/x/net v0.28.0 // indirect
22-
golang.org/x/text v0.17.0 // indirect
23+
golang.org/x/net v0.33.0 // indirect
24+
golang.org/x/text v0.21.0 // indirect
2325
gopkg.in/inf.v0 v0.9.1 // indirect
2426
gopkg.in/yaml.v2 v2.4.0 // indirect
27+
gopkg.in/yaml.v3 v3.0.1 // indirect
2528
k8s.io/klog/v2 v2.130.1 // indirect
2629
k8s.io/utils v0.0.0-20240711033017-18e509b52bc8 // indirect
2730
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect

0 commit comments

Comments
 (0)