Skip to content

Commit 1413f93

Browse files
committed
fix: lint and some syntax errors
1 parent 67fd416 commit 1413f93

File tree

15 files changed

+136
-48
lines changed

15 files changed

+136
-48
lines changed

internal/cli/backup/create.go

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,32 @@ import (
1818
"errors"
1919
"fmt"
2020

21-
apitypes "github.com/oceanbase/ob-operator/api/types"
22-
"github.com/oceanbase/ob-operator/api/v1alpha1"
23-
"github.com/oceanbase/ob-operator/internal/cli/cmd/util"
24-
"github.com/oceanbase/ob-operator/internal/cli/generic"
25-
"github.com/oceanbase/ob-operator/internal/clients"
2621
"github.com/robfig/cron/v3"
2722
"github.com/spf13/cobra"
2823
"github.com/spf13/pflag"
24+
corev1 "k8s.io/api/core/v1"
2925
kubeerrors "k8s.io/apimachinery/pkg/api/errors"
26+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3027
"k8s.io/apimachinery/pkg/types"
28+
"k8s.io/apimachinery/pkg/util/rand"
29+
30+
apitypes "github.com/oceanbase/ob-operator/api/types"
31+
"github.com/oceanbase/ob-operator/api/v1alpha1"
32+
"github.com/oceanbase/ob-operator/internal/cli/cmd/util"
33+
"github.com/oceanbase/ob-operator/internal/cli/generic"
34+
"github.com/oceanbase/ob-operator/internal/clients"
35+
oceanbaseconst "github.com/oceanbase/ob-operator/internal/const/oceanbase"
36+
"github.com/oceanbase/ob-operator/pkg/k8s/client"
3137
)
3238

3339
type CreateOptions struct {
3440
generic.ResourceOption
41+
BackupPolicyBase
42+
OSSAccessID string `json:"ossAccessId,omitempty" example:"encryptedPassword"`
43+
OSSAccessKey string `json:"ossAccessKey,omitempty" example:"encryptedPassword"`
44+
BakEncryptionPassword string `json:"bakEncryptionPassword,omitempty" example:"encryptedPassword"`
45+
}
46+
type BackupPolicyBase struct {
3547
DestType string `json:"destType" binding:"required"`
3648
ArchivePath string `json:"archivePath" binding:"required"`
3749
BakDataPath string `json:"bakDataPath" binding:"required"`
@@ -41,6 +53,7 @@ type CreateOptions struct {
4153
RecoveryDays int `json:"recoveryDays,omitempty" example:"3"`
4254
}
4355

56+
// checkCrontabSyntax checks the syntax of the crontab
4457
func checkCrontabSyntax(crontab string) bool {
4558
if _, err := cron.ParseStandard(crontab); err != nil {
4659
return false
@@ -105,6 +118,48 @@ func CreateTenantBackupPolicy(ctx context.Context, o *CreateOptions) (*v1alpha1.
105118
if err != nil {
106119
return nil, err
107120
}
121+
if o.DestType == "OSS" && o.OSSAccessID != "" && o.OSSAccessKey != "" {
122+
ossSecretName := nn.Name + "-backup-oss-secret-" + rand.String(6)
123+
backupPolicy.Spec.LogArchive.Destination.OSSAccessSecret = ossSecretName
124+
backupPolicy.Spec.DataBackup.Destination.OSSAccessSecret = ossSecretName
125+
secret := &corev1.Secret{
126+
ObjectMeta: metav1.ObjectMeta{
127+
Name: ossSecretName,
128+
Namespace: nn.Namespace,
129+
},
130+
StringData: map[string]string{
131+
"accessId": o.OSSAccessID,
132+
"accessKey": o.OSSAccessKey,
133+
},
134+
}
135+
_, err := client.GetClient().ClientSet.CoreV1().Secrets(nn.Namespace).Create(ctx, secret, metav1.CreateOptions{})
136+
if err != nil {
137+
return nil, err
138+
}
139+
}
140+
if o.BakEncryptionPassword != "" {
141+
encryptionSecretName := nn.Name + "-backup-encryption-secret-" + rand.String(6)
142+
backupPolicy.Spec.DataBackup.EncryptionSecret = encryptionSecretName
143+
secret := &corev1.Secret{
144+
ObjectMeta: metav1.ObjectMeta{
145+
Name: encryptionSecretName,
146+
Namespace: nn.Namespace,
147+
},
148+
StringData: map[string]string{
149+
"password": o.BakEncryptionPassword,
150+
},
151+
}
152+
_, err := client.GetClient().ClientSet.CoreV1().Secrets(nn.Namespace).Create(ctx, secret, metav1.CreateOptions{})
153+
if err != nil {
154+
return nil, err
155+
}
156+
}
157+
// set labels for backup policy
158+
backupPolicy.Labels = map[string]string{
159+
oceanbaseconst.LabelTenantName: o.Name,
160+
oceanbaseconst.LabelRefUID: string(tenant.GetObjectMeta().GetUID()),
161+
oceanbaseconst.LabelRefBackupPolicy: o.Name + "-backup-policy",
162+
}
108163
policy, err := clients.CreateTenantBackupPolicy(ctx, backupPolicy)
109164
if err != nil {
110165
return nil, err
@@ -152,6 +207,7 @@ func (o *CreateOptions) AddFlags(cmd *cobra.Command) {
152207
o.AddBaseFlags(cmd)
153208
o.AddDaysFieldFlags(cmd)
154209
o.AddScheduleFlags(cmd)
210+
o.AddAccessFlags(cmd)
155211
}
156212

157213
// AddBaseFlags adds the base flags for the create command
@@ -179,3 +235,12 @@ func (o *CreateOptions) AddScheduleFlags(cmd *cobra.Command) {
179235
scheduleFlags.StringVar(&o.FullCrontab, FLAG_FULL, "", "The full backup schedule, crontab format, e.g. 0 0 * * 4,5")
180236
cmd.Flags().AddFlagSet(scheduleFlags)
181237
}
238+
239+
// AddAccessFlags adds the access-related flags for the create command
240+
func (o *CreateOptions) AddAccessFlags(cmd *cobra.Command) {
241+
accessFlags := pflag.NewFlagSet(FLAGSET_ACCESS, pflag.ContinueOnError)
242+
accessFlags.StringVar(&o.OSSAccessID, FLAG_OSS_ACCESS_ID, "", "The OSS access id for OSS destination")
243+
accessFlags.StringVar(&o.OSSAccessKey, FLAG_OSS_ACCESS_KEY, "", "The OSS access key for OSS destination")
244+
accessFlags.StringVar(&o.BakEncryptionPassword, FLAG_BAK_ENCRYPTION_PASSWORD, "", "The backup encryption password")
245+
cmd.Flags().AddFlagSet(accessFlags)
246+
}

internal/cli/backup/delete.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ See the Mulan PSL v2 for more details.
1414
package backup
1515

1616
import (
17-
"github.com/oceanbase/ob-operator/internal/cli/generic"
1817
"github.com/spf13/cobra"
18+
19+
"github.com/oceanbase/ob-operator/internal/cli/generic"
1920
)
2021

2122
type DeleteOptions struct {

internal/cli/backup/enter.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,30 @@ const (
1818
// Flagsets
1919
FLAGSET_DAYS_FIELD = "days-field-flags"
2020
FLAGSET_SCHEDULE = "schedule-flags"
21+
FLAGSET_ACCESS = "access-flags"
2122

22-
// Flags
23-
FLAG_NAMESPACE = "namespace"
24-
FLAG_NAME = "name"
25-
FLAG_SCHEDULE_TYPE = "schedule-type"
26-
FLAG_DEST_TYPE = "dest-type"
27-
FLAG_ARCHIVE_PATH = "archive-path"
28-
FLAG_BAK_DATA_PATH = "bak-data-path"
23+
// Base flags
24+
FLAG_NAMESPACE = "namespace"
25+
FLAG_NAME = "name"
26+
FLAG_SCHEDULE_TYPE = "schedule-type"
27+
FLAG_DEST_TYPE = "dest-type"
28+
FLAG_ARCHIVE_PATH = "archive-path"
29+
FLAG_BAK_DATA_PATH = "bak-data-path"
30+
FLAG_STATUS = "status"
31+
32+
// Day field flags
2933
FLAG_JOB_KEEP_DAYS = "job-keep-days"
3034
FLAG_RECOVERY_DAYS = "recovery-days"
3135
FLAG_PIECE_INTERVAL_DAYS = "piece-interval-days"
3236
FLAG_SCHEDULE_TIME = "schedule-time"
33-
FLAG_INCREMENTAL = "inc"
34-
FLAG_FULL = "full"
35-
FLAG_STATUS = "status"
37+
38+
// Schedule flags
39+
FLAG_INCREMENTAL = "inc"
40+
FLAG_FULL = "full"
41+
// Access flags
42+
FLAG_OSS_ACCESS_ID = "oss-access-id"
43+
FLAG_OSS_ACCESS_KEY = "oss-access-key"
44+
FLAG_BAK_ENCRYPTION_PASSWORD = "bak-encryption-password"
3645
)
3746

3847
// Default values for backup policy management

internal/cli/backup/list.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ See the Mulan PSL v2 for more details.
1414
package backup
1515

1616
import (
17-
"github.com/oceanbase/ob-operator/internal/cli/generic"
1817
"github.com/spf13/cobra"
18+
19+
"github.com/oceanbase/ob-operator/internal/cli/generic"
1920
)
2021

2122
type ListOptions struct {

internal/cli/backup/pause.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type PauseOptions struct {
2222
func NewPauseOptions() *PauseOptions {
2323
return &PauseOptions{
2424
UpdateOptions: UpdateOptions{
25-
Status: "PAUSE",
25+
Status: "PAUSED",
2626
},
2727
}
2828
}

internal/cli/backup/update.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ import (
1818
"errors"
1919
"strings"
2020

21-
"github.com/oceanbase/ob-operator/internal/cli/cmd/util"
22-
"github.com/oceanbase/ob-operator/internal/cli/generic"
23-
"github.com/oceanbase/ob-operator/internal/clients"
2421
"github.com/spf13/cobra"
2522
"github.com/spf13/pflag"
2623
"k8s.io/apimachinery/pkg/types"
24+
25+
"github.com/oceanbase/ob-operator/internal/cli/cmd/util"
26+
"github.com/oceanbase/ob-operator/internal/cli/generic"
27+
"github.com/oceanbase/ob-operator/internal/clients"
2728
)
2829

2930
type UpdateOptions struct {
@@ -56,6 +57,9 @@ func UpdateTenantBackupPolicy(ctx context.Context, o *UpdateOptions) error {
5657
if err != nil {
5758
return err
5859
}
60+
if policy == nil {
61+
return errors.New("tenant backup policy not found")
62+
}
5963
if o.JobKeepDays != 0 {
6064
policy.Spec.JobKeepWindow = numberToDay(o.JobKeepDays)
6165
}

internal/cli/cmd/backup/backup.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ import "github.com/spf13/cobra"
1818
// NewCmd is command for backup policy management
1919
func NewCmd() *cobra.Command {
2020
cmd := &cobra.Command{
21-
Use: "backup <subcommand>",
21+
Use: "backup-policy <subcommand>",
2222
Short: "Command for backup policy management",
23-
Long: `Command for backup policy management, such as create, list, delete`,
23+
Long: `Command for backup policy management, such as create, list, delete, pause, resume, update`,
2424
}
2525
cmd.AddCommand(NewCreateCmd())
2626
cmd.AddCommand(NewListCmd())

internal/cli/cmd/backup/create.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ See the Mulan PSL v2 for more details.
1414
package backup
1515

1616
import (
17+
"github.com/spf13/cobra"
18+
1719
"github.com/oceanbase/ob-operator/internal/cli/backup"
1820
cmdUtil "github.com/oceanbase/ob-operator/internal/cli/cmd/util"
19-
"github.com/spf13/cobra"
2021
)
2122

2223
// NewCreateCmd create an new backup policy
@@ -40,7 +41,7 @@ func NewCreateCmd() *cobra.Command {
4041
if err != nil {
4142
logger.Fatalln(err)
4243
}
43-
logger.Printf("Create Backup policy for OBTenant %s created successfully\n", obBackupPolicy.Name)
44+
logger.Printf("Create Backup policy %s for OBTenant %s successfully\n", obBackupPolicy.Name, o.Name)
4445
},
4546
}
4647
o.AddFlags(cmd)

internal/cli/cmd/backup/delete.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ See the Mulan PSL v2 for more details.
1414
package backup
1515

1616
import (
17+
"github.com/spf13/cobra"
18+
"k8s.io/apimachinery/pkg/types"
19+
1720
"github.com/oceanbase/ob-operator/internal/cli/backup"
1821
cmdUtil "github.com/oceanbase/ob-operator/internal/cli/cmd/util"
1922
"github.com/oceanbase/ob-operator/internal/clients"
20-
"github.com/spf13/cobra"
21-
"k8s.io/apimachinery/pkg/types"
2223
)
2324

2425
// NewDeleteCmd delete backup policy
@@ -34,12 +35,12 @@ func NewDeleteCmd() *cobra.Command {
3435
Run: func(cmd *cobra.Command, args []string) {
3536
err := clients.DeleteTenantBackupPolicy(cmd.Context(), types.NamespacedName{
3637
Namespace: o.Namespace,
37-
Name: o.Name,
38+
Name: o.Name + "-backup-policy",
3839
})
3940
if err != nil {
4041
logger.Fatalln(err)
4142
}
42-
logger.Printf("Delete backup policy for ob tenant %s successfully", o.Name)
43+
logger.Printf("Delete backup policy for OBTenant %s successfully", o.Name)
4344
},
4445
}
4546
o.AddFlags(cmd)

internal/cli/cmd/backup/list.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ package backup
1616
import (
1717
"sort"
1818

19+
"github.com/spf13/cobra"
20+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
1922
"github.com/oceanbase/ob-operator/internal/cli/backup"
2023
cmdUtil "github.com/oceanbase/ob-operator/internal/cli/cmd/util"
2124
"github.com/oceanbase/ob-operator/internal/clients"
22-
"github.com/spf13/cobra"
23-
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2425
)
2526

2627
// NewListCmd list all backup policies
@@ -44,9 +45,9 @@ func NewListCmd() *cobra.Command {
4445
logger.Println("No backup policies found")
4546
return
4647
}
47-
tbLog.Println("NAMESPACE \t POLICYNAME \t TENANTNAME \t JOBKEEP \t SUSPEND \t CREATETIME \t NEXTFULL \t NEXTINCREMENTAL \t STATUS")
48-
for _, policy := range obBackupPolicyList.Items {
49-
tbLog.Printf("%s \t %s \t %s \t %s \t %t \t %s \t %s \t %s \t %s\n", policy.Namespace, policy.Name, policy.Spec.TenantName, policy.Spec.JobKeepWindow, policy.Spec.Suspend, policy.CreationTimestamp, policy.Status.NextFull, policy.Status.NextIncremental, policy.Status.Status)
48+
tbLog.Println("NAMESPACE \t POLICYNAME \t STATUS \t TENANTNAME \t NEXTFULL \t NEXTINCREMENTAL \t FULLCRONTAB \t INCREMENTALCRONTAB \t")
49+
for _, obBackupPolicy := range obBackupPolicyList.Items {
50+
tbLog.Printf("%s \t %s \t %s \t %s \t %s \t %s \t %s \t %s \n", obBackupPolicy.Namespace, obBackupPolicy.Name, obBackupPolicy.Status.Status, obBackupPolicy.Spec.TenantName, obBackupPolicy.Status.NextFull, obBackupPolicy.Status.NextIncremental, obBackupPolicy.Spec.DataBackup.FullCrontab, obBackupPolicy.Spec.DataBackup.IncrementalCrontab)
5051
}
5152
if err := tbw.Flush(); err != nil {
5253
logger.Fatalln(err)

0 commit comments

Comments
 (0)