From 2a34772ed5ebf3592be89c900c20290a0f76c6ac Mon Sep 17 00:00:00 2001 From: Nolan Brubaker Date: Thu, 9 Aug 2018 12:41:31 -0400 Subject: [PATCH] Add --storage-location argument to create commands Closes #738 Signed-off-by: Nolan Brubaker --- docs/cli-reference/ark_backup_create.md | 1 + docs/cli-reference/ark_create_backup.md | 1 + docs/cli-reference/ark_create_schedule.md | 1 + docs/cli-reference/ark_schedule_create.md | 1 + pkg/cmd/cli/backup/create.go | 33 ++++++++++++++++------- pkg/cmd/cli/schedule/create.go | 13 ++++----- 6 files changed, 34 insertions(+), 16 deletions(-) diff --git a/docs/cli-reference/ark_backup_create.md b/docs/cli-reference/ark_backup_create.md index 2e5467683..f6a8569ef 100644 --- a/docs/cli-reference/ark_backup_create.md +++ b/docs/cli-reference/ark_backup_create.md @@ -26,6 +26,7 @@ ark backup create NAME [flags] -l, --selector labelSelector only back up resources matching this label selector (default ) --show-labels show labels in the last column --snapshot-volumes optionalBool[=true] take snapshots of PersistentVolumes as part of the backup + --storage-location string location in which to store the backup --ttl duration how long before the backup can be garbage collected (default 720h0m0s) -w, --wait wait for the operation to complete ``` diff --git a/docs/cli-reference/ark_create_backup.md b/docs/cli-reference/ark_create_backup.md index f3fd16f2c..8d45c9692 100644 --- a/docs/cli-reference/ark_create_backup.md +++ b/docs/cli-reference/ark_create_backup.md @@ -26,6 +26,7 @@ ark create backup NAME [flags] -l, --selector labelSelector only back up resources matching this label selector (default ) --show-labels show labels in the last column --snapshot-volumes optionalBool[=true] take snapshots of PersistentVolumes as part of the backup + --storage-location string location in which to store the backup --ttl duration how long before the backup can be garbage collected (default 720h0m0s) -w, --wait wait for the operation to complete ``` diff --git a/docs/cli-reference/ark_create_schedule.md b/docs/cli-reference/ark_create_schedule.md index 4ddb7b575..809d67870 100644 --- a/docs/cli-reference/ark_create_schedule.md +++ b/docs/cli-reference/ark_create_schedule.md @@ -41,6 +41,7 @@ ark create schedule NAME --schedule="0 */6 * * *" -l, --selector labelSelector only back up resources matching this label selector (default ) --show-labels show labels in the last column --snapshot-volumes optionalBool[=true] take snapshots of PersistentVolumes as part of the backup + --storage-location string location in which to store the backup --ttl duration how long before the backup can be garbage collected (default 720h0m0s) ``` diff --git a/docs/cli-reference/ark_schedule_create.md b/docs/cli-reference/ark_schedule_create.md index c60c91a0d..c445f080e 100644 --- a/docs/cli-reference/ark_schedule_create.md +++ b/docs/cli-reference/ark_schedule_create.md @@ -41,6 +41,7 @@ ark create schedule NAME --schedule="0 */6 * * *" -l, --selector labelSelector only back up resources matching this label selector (default ) --show-labels show labels in the last column --snapshot-volumes optionalBool[=true] take snapshots of PersistentVolumes as part of the backup + --storage-location string location in which to store the backup --ttl duration how long before the backup can be garbage collected (default 720h0m0s) ``` diff --git a/pkg/cmd/cli/backup/create.go b/pkg/cmd/cli/backup/create.go index d899ffacd..837a87db7 100644 --- a/pkg/cmd/cli/backup/create.go +++ b/pkg/cmd/cli/backup/create.go @@ -32,6 +32,7 @@ import ( "github.com/heptio/ark/pkg/cmd" "github.com/heptio/ark/pkg/cmd/util/flag" "github.com/heptio/ark/pkg/cmd/util/output" + arkclient "github.com/heptio/ark/pkg/generated/clientset/versioned" ) func NewCreateCommand(f client.Factory, use string) *cobra.Command { @@ -42,8 +43,8 @@ func NewCreateCommand(f client.Factory, use string) *cobra.Command { Short: "Create a backup", Args: cobra.ExactArgs(1), Run: func(c *cobra.Command, args []string) { - cmd.CheckError(o.Complete(args)) - cmd.CheckError(o.Validate(c, args)) + cmd.CheckError(o.Complete(args, f)) + cmd.CheckError(o.Validate(c, args, f)) cmd.CheckError(o.Run(c, f)) }, } @@ -68,6 +69,9 @@ type CreateOptions struct { Selector flag.LabelSelector IncludeClusterResources flag.OptionalBool Wait bool + StorageLocation string + + client arkclient.Interface } func NewCreateOptions() *CreateOptions { @@ -87,6 +91,7 @@ func (o *CreateOptions) BindFlags(flags *pflag.FlagSet) { flags.Var(&o.IncludeResources, "include-resources", "resources to include in the backup, 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 backup, formatted as resource.group, such as storageclasses.storage.k8s.io") flags.Var(&o.Labels, "labels", "labels to apply to the backup") + flags.StringVar(&o.StorageLocation, "storage-location", "", "location in which to store the backup") flags.VarP(&o.Selector, "selector", "l", "only back up resources matching this label selector") f := flags.VarPF(&o.SnapshotVolumes, "snapshot-volumes", "", "take snapshots of PersistentVolumes as part of the backup") // this allows the user to just specify "--snapshot-volumes" as shorthand for "--snapshot-volumes=true" @@ -103,24 +108,31 @@ func (o *CreateOptions) BindWait(flags *pflag.FlagSet) { flags.BoolVarP(&o.Wait, "wait", "w", o.Wait, "wait for the operation to complete") } -func (o *CreateOptions) Validate(c *cobra.Command, args []string) error { +func (o *CreateOptions) Validate(c *cobra.Command, args []string, f client.Factory) error { if err := output.ValidateFlags(c); err != nil { return err } + if o.StorageLocation != "" { + if _, err := o.client.ArkV1().BackupStorageLocations(f.Namespace()).Get(o.StorageLocation, metav1.GetOptions{}); err != nil { + return err + } + } + return nil } -func (o *CreateOptions) Complete(args []string) error { +func (o *CreateOptions) Complete(args []string, f client.Factory) error { o.Name = args[0] + client, err := f.Client() + if err != nil { + return err + } + o.client = client return nil } func (o *CreateOptions) Run(c *cobra.Command, f client.Factory) error { - arkClient, err := f.Client() - if err != nil { - return err - } backup := &api.Backup{ ObjectMeta: metav1.ObjectMeta{ @@ -137,6 +149,7 @@ func (o *CreateOptions) Run(c *cobra.Command, f client.Factory) error { SnapshotVolumes: o.SnapshotVolumes.Value, TTL: metav1.Duration{Duration: o.TTL}, IncludeClusterResources: o.IncludeClusterResources.Value, + StorageLocation: o.StorageLocation, }, } @@ -152,7 +165,7 @@ func (o *CreateOptions) Run(c *cobra.Command, f client.Factory) error { updates = make(chan *api.Backup) - backupInformer = v1.NewBackupInformer(arkClient, f.Namespace(), 0, nil) + backupInformer = v1.NewBackupInformer(o.client, f.Namespace(), 0, nil) backupInformer.AddEventHandler( cache.FilteringResourceEventHandler{ @@ -184,7 +197,7 @@ func (o *CreateOptions) Run(c *cobra.Command, f client.Factory) error { go backupInformer.Run(stop) } - _, err = arkClient.ArkV1().Backups(backup.Namespace).Create(backup) + _, err := o.client.ArkV1().Backups(backup.Namespace).Create(backup) if err != nil { return err } diff --git a/pkg/cmd/cli/schedule/create.go b/pkg/cmd/cli/schedule/create.go index a49c86be9..079414615 100644 --- a/pkg/cmd/cli/schedule/create.go +++ b/pkg/cmd/cli/schedule/create.go @@ -51,8 +51,8 @@ func NewCreateCommand(f client.Factory, use string) *cobra.Command { Example: `ark create schedule NAME --schedule="0 */6 * * *"`, Args: cobra.ExactArgs(1), Run: func(c *cobra.Command, args []string) { - cmd.CheckError(o.Complete(args)) - cmd.CheckError(o.Validate(c, args)) + cmd.CheckError(o.Complete(args, f)) + cmd.CheckError(o.Validate(c, args, f)) cmd.CheckError(o.Run(c, f)) }, } @@ -82,16 +82,16 @@ func (o *CreateOptions) BindFlags(flags *pflag.FlagSet) { flags.StringVar(&o.Schedule, "schedule", o.Schedule, "a cron expression specifying a recurring schedule for this backup to run") } -func (o *CreateOptions) Validate(c *cobra.Command, args []string) error { +func (o *CreateOptions) Validate(c *cobra.Command, args []string, f client.Factory) error { if len(o.Schedule) == 0 { return errors.New("--schedule is required") } - return o.BackupOptions.Validate(c, args) + return o.BackupOptions.Validate(c, args, f) } -func (o *CreateOptions) Complete(args []string) error { - return o.BackupOptions.Complete(args) +func (o *CreateOptions) Complete(args []string, f client.Factory) error { + return o.BackupOptions.Complete(args, f) } func (o *CreateOptions) Run(c *cobra.Command, f client.Factory) error { @@ -114,6 +114,7 @@ func (o *CreateOptions) Run(c *cobra.Command, f client.Factory) error { LabelSelector: o.BackupOptions.Selector.LabelSelector, SnapshotVolumes: o.BackupOptions.SnapshotVolumes.Value, TTL: metav1.Duration{Duration: o.BackupOptions.TTL}, + StorageLocation: o.BackupOptions.StorageLocation, }, Schedule: o.Schedule, },