@@ -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
3339type 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
4457func 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+ }
0 commit comments