mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-09-17 21:44:23 +00:00
feat: support configure BSL CR to indicate which one is the default (#3092)
* Add default field to BSL CRD Signed-off-by: JenTing Hsiao <jenting.hsiao@suse.com> * Add a new flag `--default` under `velero backup-location create` add a new flag `--default` under `velero backup-location create` to specify this new location to be the new default BSL. Signed-off-by: JenTing Hsiao <jenting.hsiao@suse.com> * Add a new default field under `velero backup-location get` add a new default field under `velero backup-location get` to indicate which BSL is the default one. Signed-off-by: JenTing Hsiao <jenting.hsiao@suse.com> * Add a new sub-command and flag under `velero backup-location` Add a new sub-command called `velero backup-location set` sub-command and a new flag `velero backup-cation set --default` to configure which BSL is the default one. Signed-off-by: JenTing Hsiao <jenting.hsiao@suse.com> * Add new flag to get the default backup-location Add a new flag `--default` under `velero backup-location get` to displays the current default BSL. Signed-off-by: JenTing Hsiao <jenting.hsiao@suse.com> * Configures default BSL in BSL controller When upgrade the BSL CRDs, none of the BSL has been labeled as default. Sets the BSL default field to true if the BSL name matches to the default BSL setting. Signed-off-by: JenTing Hsiao <jenting.hsiao@suse.com> * Configures the default BSL in BSL controller for velero upgrade When upgrade the BSL CRDs, none of the BSL be marked as the default. Sets the BSL `.spec.default: true` if the BSL name matches against the `velero server --default-backup-storage-location`. Signed-off-by: JenTing Hsiao <jenting.hsiao@suse.com> * Add unit test to test default BSL behavior Signed-off-by: JenTing Hsiao <jenting.hsiao@suse.com> * Update check which one is the default BSL in backup/backup_sync/restore controller Signed-off-by: JenTing Hsiao <jenting.hsiao@suse.com> * Add changelog Signed-off-by: JenTing Hsiao <jenting.hsiao@suse.com> * Update docs locations.md and upgrade-to-1.6.md Signed-off-by: JenTing Hsiao <jenting.hsiao@suse.com>
This commit is contained in:
@@ -33,6 +33,7 @@ func NewCommand(f client.Factory) *cobra.Command {
|
||||
NewCreateCommand(f, "create"),
|
||||
NewDeleteCommand(f, "delete"),
|
||||
NewGetCommand(f, "get"),
|
||||
NewSetCommand(f, "set"),
|
||||
)
|
||||
|
||||
return c
|
||||
|
||||
@@ -63,6 +63,7 @@ type CreateOptions struct {
|
||||
Name string
|
||||
Provider string
|
||||
Bucket string
|
||||
DefaultBackupStorageLocation bool
|
||||
Prefix string
|
||||
BackupSyncPeriod, ValidationFrequency time.Duration
|
||||
Config flag.Map
|
||||
@@ -85,6 +86,7 @@ func NewCreateOptions() *CreateOptions {
|
||||
func (o *CreateOptions) BindFlags(flags *pflag.FlagSet) {
|
||||
flags.StringVar(&o.Provider, "provider", o.Provider, "Name of the backup storage provider (e.g. aws, azure, gcp).")
|
||||
flags.StringVar(&o.Bucket, "bucket", o.Bucket, "Name of the object storage bucket where backups should be stored.")
|
||||
flags.BoolVar(&o.DefaultBackupStorageLocation, "default", o.DefaultBackupStorageLocation, "Sets this new location to be the new default backup storage location. Optional.")
|
||||
flags.StringVar(&o.Prefix, "prefix", o.Prefix, "Prefix under which all Velero data should be stored within the bucket. Optional.")
|
||||
flags.DurationVar(&o.BackupSyncPeriod, "backup-sync-period", o.BackupSyncPeriod, "How often to ensure all Velero backups in object storage exist as Backup API objects in the cluster. Optional. Set this to `0s` to disable sync. Default: 1 minute.")
|
||||
flags.DurationVar(&o.ValidationFrequency, "validation-frequency", o.ValidationFrequency, "How often to verify if the backup storage location is valid. Optional. Set this to `0s` to disable sync. Default 1 minute.")
|
||||
@@ -162,6 +164,7 @@ func (o *CreateOptions) Run(c *cobra.Command, f client.Factory) error {
|
||||
},
|
||||
},
|
||||
Config: o.Config.Data(),
|
||||
Default: o.DefaultBackupStorageLocation,
|
||||
AccessMode: velerov1api.BackupStorageLocationAccessMode(o.AccessMode.String()),
|
||||
BackupSyncPeriod: backupSyncPeriod,
|
||||
ValidationFrequency: validationFrequency,
|
||||
@@ -177,6 +180,25 @@ func (o *CreateOptions) Run(c *cobra.Command, f client.Factory) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if o.DefaultBackupStorageLocation {
|
||||
// There is one and only one default backup storage location.
|
||||
// Disable the origin default backup storage location.
|
||||
locations := new(velerov1api.BackupStorageLocationList)
|
||||
if err := kbClient.List(context.Background(), locations, &kbclient.ListOptions{Namespace: f.Namespace()}); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
for _, location := range locations.Items {
|
||||
if !location.Spec.Default {
|
||||
continue
|
||||
}
|
||||
location.Spec.Default = false
|
||||
if err := kbClient.Update(context.Background(), &location, &kbclient.UpdateOptions{}); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if err := kbClient.Create(context.Background(), backupStorageLocation, &kbclient.CreateOptions{}); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ import (
|
||||
|
||||
func NewGetCommand(f client.Factory, use string) *cobra.Command {
|
||||
var listOptions metav1.ListOptions
|
||||
var showDefaultOnly bool
|
||||
|
||||
c := &cobra.Command{
|
||||
Use: use,
|
||||
@@ -45,14 +46,21 @@ func NewGetCommand(f client.Factory, use string) *cobra.Command {
|
||||
|
||||
locations := new(velerov1api.BackupStorageLocationList)
|
||||
if len(args) > 0 {
|
||||
location := &velerov1api.BackupStorageLocation{}
|
||||
for _, name := range args {
|
||||
location := &velerov1api.BackupStorageLocation{}
|
||||
err = kbClient.Get(context.Background(), kbclient.ObjectKey{
|
||||
Namespace: f.Namespace(),
|
||||
Name: name,
|
||||
}, location)
|
||||
cmd.CheckError(err)
|
||||
locations.Items = append(locations.Items, *location)
|
||||
|
||||
if showDefaultOnly {
|
||||
if location.Spec.Default {
|
||||
locations.Items = append(locations.Items, *location)
|
||||
}
|
||||
} else {
|
||||
locations.Items = append(locations.Items, *location)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
err := kbClient.List(context.Background(), locations, &kbclient.ListOptions{
|
||||
@@ -60,6 +68,19 @@ func NewGetCommand(f client.Factory, use string) *cobra.Command {
|
||||
Raw: &listOptions,
|
||||
})
|
||||
cmd.CheckError(err)
|
||||
|
||||
if showDefaultOnly {
|
||||
for i := 0; i < len(locations.Items); i++ {
|
||||
if locations.Items[i].Spec.Default {
|
||||
continue
|
||||
}
|
||||
if i != len(locations.Items)-1 {
|
||||
copy(locations.Items[i:], locations.Items[i+1:])
|
||||
i = i - 1
|
||||
}
|
||||
locations.Items = locations.Items[:len(locations.Items)-1]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_, err = output.PrintWithFormat(c, locations)
|
||||
@@ -67,6 +88,7 @@ func NewGetCommand(f client.Factory, use string) *cobra.Command {
|
||||
},
|
||||
}
|
||||
|
||||
c.Flags().BoolVar(&showDefaultOnly, "default", false, "Displays the current default backup storage location.")
|
||||
c.Flags().StringVarP(&listOptions.LabelSelector, "selector", "l", listOptions.LabelSelector, "Only show items matching this label selector.")
|
||||
|
||||
output.BindFlags(c.Flags())
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
Copyright 2020 the Velero contributors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package backuplocation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
|
||||
kbclient "sigs.k8s.io/controller-runtime/pkg/client"
|
||||
|
||||
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
|
||||
"github.com/vmware-tanzu/velero/pkg/client"
|
||||
"github.com/vmware-tanzu/velero/pkg/cmd"
|
||||
)
|
||||
|
||||
func NewSetCommand(f client.Factory, use string) *cobra.Command {
|
||||
o := NewSetOptions()
|
||||
|
||||
c := &cobra.Command{
|
||||
Use: use + " NAME",
|
||||
Short: "Set a backup storage location",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(c *cobra.Command, args []string) {
|
||||
cmd.CheckError(o.Complete(args, f))
|
||||
cmd.CheckError(o.Run(c, f))
|
||||
},
|
||||
}
|
||||
|
||||
o.BindFlags(c.Flags())
|
||||
|
||||
return c
|
||||
}
|
||||
|
||||
type SetOptions struct {
|
||||
Name string
|
||||
DefaultBackupStorageLocation bool
|
||||
}
|
||||
|
||||
func NewSetOptions() *SetOptions {
|
||||
return &SetOptions{}
|
||||
}
|
||||
|
||||
func (o *SetOptions) BindFlags(flags *pflag.FlagSet) {
|
||||
flags.BoolVar(&o.DefaultBackupStorageLocation, "default", o.DefaultBackupStorageLocation, "Sets this new location to be the new default backup storage location. Optional.")
|
||||
}
|
||||
|
||||
func (o *SetOptions) Complete(args []string, f client.Factory) error {
|
||||
o.Name = args[0]
|
||||
return nil
|
||||
}
|
||||
|
||||
func (o *SetOptions) Run(c *cobra.Command, f client.Factory) error {
|
||||
kbClient, err := f.KubebuilderClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
location := &velerov1api.BackupStorageLocation{}
|
||||
err = kbClient.Get(context.Background(), kbclient.ObjectKey{
|
||||
Namespace: f.Namespace(),
|
||||
Name: o.Name,
|
||||
}, location)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
if o.DefaultBackupStorageLocation {
|
||||
// There is one and only one default backup storage location.
|
||||
// Disable the origin default backup storage location.
|
||||
locations := new(velerov1api.BackupStorageLocationList)
|
||||
if err := kbClient.List(context.Background(), locations, &kbclient.ListOptions{Namespace: f.Namespace()}); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
for _, location := range locations.Items {
|
||||
if !location.Spec.Default {
|
||||
continue
|
||||
}
|
||||
if location.Name == o.Name {
|
||||
// Do not update if the origin default BSL is the current one.
|
||||
break
|
||||
}
|
||||
location.Spec.Default = false
|
||||
if err := kbClient.Update(context.Background(), &location, &kbclient.UpdateOptions{}); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
location.Spec.Default = o.DefaultBackupStorageLocation
|
||||
if err := kbClient.Update(context.Background(), location, &kbclient.UpdateOptions{}); err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
fmt.Printf("Backup storage location %q configured successfully.\n", o.Name)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user