2017-08-02 17:27:17 +00:00
/ *
2019-03-20 19:32:48 +00:00
Copyright 2017 the Velero contributors .
2017-08-02 17:27:17 +00:00
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 backup
import (
"fmt"
2018-08-07 23:07:01 +00:00
"github.com/pkg/errors"
2017-08-02 17:27:17 +00:00
"github.com/spf13/cobra"
2018-03-14 18:17:27 +00:00
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2018-08-07 23:07:01 +00:00
"k8s.io/apimachinery/pkg/labels"
kubeerrs "k8s.io/apimachinery/pkg/util/errors"
2017-08-02 17:27:17 +00:00
2019-01-25 03:33:07 +00:00
velerov1api "github.com/heptio/velero/pkg/apis/velero/v1"
"github.com/heptio/velero/pkg/backup"
"github.com/heptio/velero/pkg/client"
"github.com/heptio/velero/pkg/cmd"
"github.com/heptio/velero/pkg/cmd/cli"
2017-08-02 17:27:17 +00:00
)
2018-04-05 18:21:45 +00:00
// NewDeleteCommand creates a new command that deletes a backup.
2017-12-15 00:40:02 +00:00
func NewDeleteCommand ( f client . Factory , use string ) * cobra . Command {
2018-09-25 15:47:42 +00:00
o := cli . NewDeleteOptions ( "backup" )
2018-02-12 16:32:49 +00:00
2017-08-02 17:27:17 +00:00
c := & cobra . Command {
2018-08-07 23:07:01 +00:00
Use : fmt . Sprintf ( "%s [NAMES]" , use ) ,
Short : "Delete backups" ,
Example : ` # delete a backup named "backup-1"
2019-01-25 03:33:07 +00:00
velero backup delete backup - 1
2018-08-07 23:07:01 +00:00
# delete a backup named "backup-1" without prompting for confirmation
2019-01-25 03:33:07 +00:00
velero backup delete backup - 1 -- confirm
2018-08-07 23:07:01 +00:00
# delete backups named "backup-1" and "backup-2"
2019-01-25 03:33:07 +00:00
velero backup delete backup - 1 backup - 2
2018-08-07 23:07:01 +00:00
# delete all backups triggered by schedule "schedule-1"
2019-01-25 03:33:07 +00:00
velero backup delete -- selector velero . io / schedule - name = schedule - 1
2018-08-07 23:07:01 +00:00
# delete all backups
2019-01-25 03:33:07 +00:00
velero backup delete -- all
2018-08-07 23:07:01 +00:00
` ,
2017-08-02 17:27:17 +00:00
Run : func ( c * cobra . Command , args [ ] string ) {
2018-02-12 16:32:49 +00:00
cmd . CheckError ( o . Complete ( f , args ) )
2018-09-25 15:47:42 +00:00
cmd . CheckError ( o . Validate ( c , f , args ) )
cmd . CheckError ( Run ( o ) )
2018-02-12 16:32:49 +00:00
} ,
}
o . BindFlags ( c . Flags ( ) )
return c
}
2017-08-02 17:27:17 +00:00
2018-04-05 18:21:45 +00:00
// Run performs the delete backup operation.
2018-09-25 15:47:42 +00:00
func Run ( o * cli . DeleteOptions ) error {
2018-09-24 06:47:54 +00:00
if ! o . Confirm && ! cli . GetConfirmation ( ) {
2018-03-14 18:17:27 +00:00
// Don't do anything unless we get confirmation
return nil
2018-02-12 16:32:49 +00:00
}
2018-08-07 23:07:01 +00:00
var (
2019-01-25 03:33:07 +00:00
backups [ ] * velerov1api . Backup
2018-08-07 23:07:01 +00:00
errs [ ] error
)
// get the list of backups to delete
switch {
case len ( o . Names ) > 0 :
for _ , name := range o . Names {
2019-01-25 03:33:07 +00:00
backup , err := o . Client . VeleroV1 ( ) . Backups ( o . Namespace ) . Get ( name , metav1 . GetOptions { } )
2018-08-07 23:07:01 +00:00
if err != nil {
errs = append ( errs , errors . WithStack ( err ) )
continue
}
backups = append ( backups , backup )
}
default :
selector := labels . Everything ( ) . String ( )
if o . Selector . LabelSelector != nil {
selector = o . Selector . String ( )
}
2018-02-12 16:32:49 +00:00
2019-01-25 03:33:07 +00:00
res , err := o . Client . VeleroV1 ( ) . Backups ( o . Namespace ) . List ( metav1 . ListOptions { LabelSelector : selector } )
2018-08-07 23:07:01 +00:00
if err != nil {
return errors . WithStack ( err )
}
for i := range res . Items {
backups = append ( backups , & res . Items [ i ] )
}
2018-02-12 16:32:49 +00:00
}
2018-08-07 23:07:01 +00:00
if len ( backups ) == 0 {
fmt . Println ( "No backups found" )
return nil
}
// create a backup deletion request for each
for _ , b := range backups {
deleteRequest := backup . NewDeleteBackupRequest ( b . Name , string ( b . UID ) )
2019-01-25 03:33:07 +00:00
if _ , err := o . Client . VeleroV1 ( ) . DeleteBackupRequests ( o . Namespace ) . Create ( deleteRequest ) ; err != nil {
2018-08-07 23:07:01 +00:00
errs = append ( errs , err )
continue
}
fmt . Printf ( "Request to delete backup %q submitted successfully.\nThe backup will be fully deleted after all associated data (disk snapshots, backup files, restores) are removed.\n" , b . Name )
}
return kubeerrs . NewAggregate ( errs )
2018-02-12 16:32:49 +00:00
}