Skip to content

Commit 343f881

Browse files
committed
fix: bugs in backup policy creation
1 parent 1413f93 commit 343f881

File tree

6 files changed

+47
-25
lines changed

6 files changed

+47
-25
lines changed

internal/cli/backup/create.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,18 @@ func buildBackupPolicyApiType(nn types.NamespacedName, obcluster string, p *Crea
7575
JobKeepWindow: numberToDay(p.JobKeepDays),
7676
LogArchive: v1alpha1.LogArchiveConfig{
7777
Destination: apitypes.BackupDestination{
78-
Path: p.ArchivePath,
79-
Type: apitypes.BackupDestType(p.DestType),
80-
OSSAccessSecret: "",
78+
Path: p.ArchivePath,
79+
Type: apitypes.BackupDestType(p.DestType),
8180
},
8281
SwitchPieceInterval: "1d",
8382
},
8483
DataBackup: v1alpha1.DataBackupConfig{
8584
Destination: apitypes.BackupDestination{
86-
Path: p.BakDataPath,
87-
Type: apitypes.BackupDestType(p.DestType),
88-
OSSAccessSecret: "",
85+
Path: p.BakDataPath,
86+
Type: apitypes.BackupDestType(p.DestType),
8987
},
9088
FullCrontab: p.FullCrontab,
9189
IncrementalCrontab: p.IncrementalCrontab,
92-
EncryptionSecret: "",
9390
},
9491
DataClean: v1alpha1.CleanPolicy{
9592
RecoveryWindow: numberToDay(p.RecoveryDays),
@@ -114,6 +111,10 @@ func CreateTenantBackupPolicy(ctx context.Context, o *CreateOptions) (*v1alpha1.
114111
if err := util.CheckTenantStatus(tenant); err != nil {
115112
return nil, err
116113
}
114+
// Check if backup policy already exists
115+
if backupPolicy, _ := clients.GetTenantBackupPolicy(ctx, nn); backupPolicy != nil {
116+
return nil, errors.New("Backup policy already exists")
117+
}
117118
backupPolicy, err := buildBackupPolicyApiType(nn, tenant.Spec.ClusterName, o)
118119
if err != nil {
119120
return nil, err
@@ -154,11 +155,17 @@ func CreateTenantBackupPolicy(ctx context.Context, o *CreateOptions) (*v1alpha1.
154155
return nil, err
155156
}
156157
}
158+
blockOwnerDeletion := true
159+
backupPolicy.SetOwnerReferences([]metav1.OwnerReference{{
160+
APIVersion: tenant.APIVersion,
161+
Kind: tenant.Kind,
162+
Name: tenant.GetObjectMeta().GetName(),
163+
UID: tenant.GetObjectMeta().GetUID(),
164+
BlockOwnerDeletion: &blockOwnerDeletion,
165+
}})
157166
// set labels for backup policy
158167
backupPolicy.Labels = map[string]string{
159-
oceanbaseconst.LabelTenantName: o.Name,
160-
oceanbaseconst.LabelRefUID: string(tenant.GetObjectMeta().GetUID()),
161-
oceanbaseconst.LabelRefBackupPolicy: o.Name + "-backup-policy",
168+
oceanbaseconst.LabelRefOBServer: string(tenant.Spec.ClusterName),
162169
}
163170
policy, err := clients.CreateTenantBackupPolicy(ctx, backupPolicy)
164171
if err != nil {

internal/cli/backup/delete.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,39 @@ See the Mulan PSL v2 for more details.
1414
package backup
1515

1616
import (
17+
"context"
18+
1719
"github.com/spf13/cobra"
20+
"k8s.io/apimachinery/pkg/types"
1821

1922
"github.com/oceanbase/ob-operator/internal/cli/generic"
23+
"github.com/oceanbase/ob-operator/internal/clients"
2024
)
2125

2226
type DeleteOptions struct {
2327
generic.ResourceOption
28+
force bool
2429
}
2530

2631
func NewDeleteOptions() *DeleteOptions {
2732
return &DeleteOptions{}
2833
}
2934

35+
func DeleteTenantBackupPolicy(ctx context.Context, o *DeleteOptions) error {
36+
nn := types.NamespacedName{Name: o.Name, Namespace: o.Namespace}
37+
policy, err := clients.GetTenantBackupPolicy(ctx, nn)
38+
if err != nil {
39+
return err
40+
}
41+
if o.force {
42+
return clients.ForceDeleteTenantBackupPolicy(ctx, types.NamespacedName{Name: policy.Name, Namespace: policy.Namespace})
43+
}
44+
return clients.DeleteTenantBackupPolicy(ctx, types.NamespacedName{Name: policy.Name, Namespace: policy.Namespace})
45+
}
46+
3047
// AddFlags add basic flags for tenant management
3148
func (o *DeleteOptions) AddFlags(cmd *cobra.Command) {
3249
cmd.Flags().StringVar(&o.Name, FLAG_NAME, "", "The name of the ob tenant")
3350
cmd.Flags().StringVar(&o.Namespace, FLAG_NAMESPACE, DEFAULT_NAMESPACE, "The namespace of the ob tenant")
51+
cmd.Flags().BoolVarP(&o.force, FLAG_FORCE, "f", DEFAULT_FORCE, "Force delete the ob tenant backup policy")
3452
}

internal/cli/backup/enter.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,14 @@ const (
3838
// Schedule flags
3939
FLAG_INCREMENTAL = "inc"
4040
FLAG_FULL = "full"
41+
4142
// Access flags
4243
FLAG_OSS_ACCESS_ID = "oss-access-id"
4344
FLAG_OSS_ACCESS_KEY = "oss-access-key"
4445
FLAG_BAK_ENCRYPTION_PASSWORD = "bak-encryption-password"
46+
47+
// Force flag
48+
FLAG_FORCE = "force"
4549
)
4650

4751
// Default values for backup policy management
@@ -55,4 +59,5 @@ const (
5559
DEFAULT_SCHEDULE_TYPE = "weekly"
5660
DEFAULT_BACKUP_TYPE = "full"
5761
DEFAULT_STATUS = "RUNNING"
62+
DEFAULT_FORCE = false
5863
)

internal/cli/cmd/backup/delete.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ package backup
1515

1616
import (
1717
"github.com/spf13/cobra"
18-
"k8s.io/apimachinery/pkg/types"
1918

2019
"github.com/oceanbase/ob-operator/internal/cli/backup"
2120
cmdUtil "github.com/oceanbase/ob-operator/internal/cli/cmd/util"
22-
"github.com/oceanbase/ob-operator/internal/clients"
2321
)
2422

2523
// NewDeleteCmd delete backup policy
@@ -33,11 +31,7 @@ func NewDeleteCmd() *cobra.Command {
3331
Args: cobra.ExactArgs(1),
3432
PreRunE: o.Parse,
3533
Run: func(cmd *cobra.Command, args []string) {
36-
err := clients.DeleteTenantBackupPolicy(cmd.Context(), types.NamespacedName{
37-
Namespace: o.Namespace,
38-
Name: o.Name + "-backup-policy",
39-
})
40-
if err != nil {
34+
if err := backup.DeleteTenantBackupPolicy(cmd.Context(), o); err != nil {
4135
logger.Fatalln(err)
4236
}
4337
logger.Printf("Delete backup policy for OBTenant %s successfully", o.Name)

internal/cli/generated/bindata/bindata.go

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

internal/cli/tenant/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ func NewListOptions() *ListOptions {
2929
}
3030

3131
func (o *ListOptions) AddFlags(cmd *cobra.Command) {
32-
cmd.Flags().StringVar(&o.Namespace, FLAG_NAMESPACE, DEFAULT_NAMESPACE, "The cluster name tenant belonged to in k8s, if not set, use the default namespace")
32+
cmd.Flags().StringVar(&o.Namespace, FLAG_NAMESPACE, DEFAULT_NAMESPACE, "The namespace tenant belonged to in k8s, if not set, use the default namespace")
3333
}

0 commit comments

Comments
 (0)