2017-08-02 17:27:17 +00:00
/ *
Copyright 2017 Heptio Inc .
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 restore
import (
"fmt"
"time"
2017-09-14 21:27:31 +00:00
"github.com/pkg/errors"
2017-08-02 17:27:17 +00:00
"github.com/spf13/cobra"
"github.com/spf13/pflag"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
api "github.com/heptio/ark/pkg/apis/ark/v1"
"github.com/heptio/ark/pkg/client"
"github.com/heptio/ark/pkg/cmd"
"github.com/heptio/ark/pkg/cmd/util/flag"
"github.com/heptio/ark/pkg/cmd/util/output"
)
2017-09-12 21:36:57 +00:00
func NewCreateCommand ( f client . Factory , use string ) * cobra . Command {
2017-08-02 17:27:17 +00:00
o := NewCreateOptions ( )
c := & cobra . Command {
2017-09-12 21:36:57 +00:00
Use : use + " BACKUP" ,
2017-08-02 17:27:17 +00:00
Short : "Create a restore" ,
Run : func ( c * cobra . Command , args [ ] string ) {
cmd . CheckError ( o . Validate ( c , args ) )
cmd . CheckError ( o . Complete ( args ) )
cmd . CheckError ( o . Run ( c , f ) )
} ,
}
o . BindFlags ( c . Flags ( ) )
output . BindFlags ( c . Flags ( ) )
output . ClearOutputFlagDefault ( c )
return c
}
type CreateOptions struct {
BackupName string
2017-08-18 22:11:42 +00:00
RestoreVolumes flag . OptionalBool
2017-08-02 17:27:17 +00:00
Labels flag . Map
2017-08-27 16:42:10 +00:00
IncludeNamespaces flag . StringArray
ExcludeNamespaces flag . StringArray
2017-09-01 21:39:30 +00:00
IncludeResources flag . StringArray
ExcludeResources flag . StringArray
2017-08-02 17:27:17 +00:00
NamespaceMappings flag . Map
Selector flag . LabelSelector
}
func NewCreateOptions ( ) * CreateOptions {
return & CreateOptions {
Labels : flag . NewMap ( ) ,
2017-08-27 16:42:10 +00:00
IncludeNamespaces : flag . NewStringArray ( "*" ) ,
2017-08-02 17:27:17 +00:00
NamespaceMappings : flag . NewMap ( ) . WithEntryDelimiter ( "," ) . WithKeyValueDelimiter ( ":" ) ,
2017-08-18 22:11:42 +00:00
RestoreVolumes : flag . NewOptionalBool ( nil ) ,
2017-08-02 17:27:17 +00:00
}
}
func ( o * CreateOptions ) BindFlags ( flags * pflag . FlagSet ) {
2017-08-30 02:14:21 +00:00
flags . Var ( & o . IncludeNamespaces , "include-namespaces" , "namespaces to include in the restore (use '*' for all namespaces)" )
flags . Var ( & o . ExcludeNamespaces , "exclude-namespaces" , "namespaces to exclude from the restore" )
2017-08-02 17:27:17 +00:00
flags . Var ( & o . NamespaceMappings , "namespace-mappings" , "namespace mappings from name in the backup to desired restored name in the form src1:dst1,src2:dst2,..." )
2017-08-27 16:42:10 +00:00
flags . Var ( & o . Labels , "labels" , "labels to apply to the restore" )
2017-09-01 21:39:30 +00:00
flags . Var ( & o . IncludeResources , "include-resources" , "resources to include in the restore, formatted as resource.group, such as storageclasses.storage.k8s.io (use '*' for all resources)" )
flags . Var ( & o . ExcludeResources , "exclude-resources" , "resources to exclude from the restore, formatted as resource.group, such as storageclasses.storage.k8s.io" )
2017-08-02 17:27:17 +00:00
flags . VarP ( & o . Selector , "selector" , "l" , "only restore resources matching this label selector" )
2017-08-18 22:11:42 +00:00
f := flags . VarPF ( & o . RestoreVolumes , "restore-volumes" , "" , "whether to restore volumes from snapshots" )
// this allows the user to just specify "--restore-volumes" as shorthand for "--restore-volumes=true"
// like a normal bool flag
f . NoOptDefVal = "true"
2017-08-02 17:27:17 +00:00
}
func ( o * CreateOptions ) Validate ( c * cobra . Command , args [ ] string ) error {
if len ( args ) != 1 {
return errors . New ( "you must specify only one argument, the backup's name" )
}
if err := output . ValidateFlags ( c ) ; err != nil {
return err
}
return nil
}
func ( o * CreateOptions ) Complete ( args [ ] string ) error {
o . BackupName = args [ 0 ]
return nil
}
func ( o * CreateOptions ) Run ( c * cobra . Command , f client . Factory ) error {
arkClient , err := f . Client ( )
if err != nil {
return err
}
restore := & api . Restore {
ObjectMeta : metav1 . ObjectMeta {
Namespace : api . DefaultNamespace ,
Name : fmt . Sprintf ( "%s-%s" , o . BackupName , time . Now ( ) . Format ( "20060102150405" ) ) ,
Labels : o . Labels . Data ( ) ,
} ,
Spec : api . RestoreSpec {
2017-08-27 16:42:10 +00:00
BackupName : o . BackupName ,
IncludedNamespaces : o . IncludeNamespaces ,
ExcludedNamespaces : o . ExcludeNamespaces ,
2017-09-01 21:39:30 +00:00
IncludedResources : o . IncludeResources ,
ExcludedResources : o . ExcludeResources ,
2017-08-27 16:42:10 +00:00
NamespaceMapping : o . NamespaceMappings . Data ( ) ,
LabelSelector : o . Selector . LabelSelector ,
RestorePVs : o . RestoreVolumes . Value ,
2017-08-02 17:27:17 +00:00
} ,
}
if printed , err := output . PrintWithFormat ( c , restore ) ; printed || err != nil {
return err
}
restore , err = arkClient . ArkV1 ( ) . Restores ( restore . Namespace ) . Create ( restore )
if err != nil {
return err
}
fmt . Printf ( "Restore %q created successfully.\n" , restore . Name )
return nil
}