From 6e9e653f76c2a8c7f2a4efbf062c4be435e211b0 Mon Sep 17 00:00:00 2001 From: Steve Kriss Date: Tue, 12 Jun 2018 16:53:57 -0700 Subject: [PATCH] add restic repo getter and reorg restic cmds Signed-off-by: Steve Kriss --- docs/cli-reference/ark.md | 2 +- docs/cli-reference/ark_restic.md | 6 +- docs/cli-reference/ark_restic_repo.md | 35 +++++++++ docs/cli-reference/ark_restic_repo_get.md | 41 ++++++++++ ...-repository.md => ark_restic_repo_init.md} | 8 +- docs/cli-reference/ark_restic_server.md | 2 +- pkg/cmd/cli/restic/repo/get.go | 66 ++++++++++++++++ .../{init_repository.go => repo/init.go} | 6 +- .../init_test.go} | 18 ++++- pkg/cmd/cli/restic/repo/repo.go | 38 ++++++++++ pkg/cmd/cli/restic/restic.go | 7 +- pkg/cmd/util/output/output.go | 2 + pkg/cmd/util/output/restic_repo_printer.go | 76 +++++++++++++++++++ 13 files changed, 291 insertions(+), 16 deletions(-) create mode 100644 docs/cli-reference/ark_restic_repo.md create mode 100644 docs/cli-reference/ark_restic_repo_get.md rename docs/cli-reference/{ark_restic_init-repository.md => ark_restic_repo_init.md} (90%) create mode 100644 pkg/cmd/cli/restic/repo/get.go rename pkg/cmd/cli/restic/{init_repository.go => repo/init.go} (97%) rename pkg/cmd/cli/restic/{init_repository_test.go => repo/init_test.go} (75%) create mode 100644 pkg/cmd/cli/restic/repo/repo.go create mode 100644 pkg/cmd/util/output/restic_repo_printer.go diff --git a/docs/cli-reference/ark.md b/docs/cli-reference/ark.md index e09a97148..b66760527 100644 --- a/docs/cli-reference/ark.md +++ b/docs/cli-reference/ark.md @@ -38,7 +38,7 @@ operations can also be performed as 'ark backup get' and 'ark schedule create'. * [ark describe](ark_describe.md) - Describe ark resources * [ark get](ark_get.md) - Get ark resources * [ark plugin](ark_plugin.md) - Work with plugins -* [ark restic](ark_restic.md) - Work with restic repositories +* [ark restic](ark_restic.md) - Work with restic * [ark restore](ark_restore.md) - Work with restores * [ark schedule](ark_schedule.md) - Work with schedules * [ark server](ark_server.md) - Run the ark server diff --git a/docs/cli-reference/ark_restic.md b/docs/cli-reference/ark_restic.md index b4ee4dd71..27ba211f1 100644 --- a/docs/cli-reference/ark_restic.md +++ b/docs/cli-reference/ark_restic.md @@ -1,11 +1,11 @@ ## ark restic -Work with restic repositories +Work with restic ### Synopsis -Work with restic repositories +Work with restic ### Options @@ -30,6 +30,6 @@ Work with restic repositories ### SEE ALSO * [ark](ark.md) - Back up and restore Kubernetes cluster resources. -* [ark restic init-repository](ark_restic_init-repository.md) - initialize a restic repository for a specified namespace +* [ark restic repo](ark_restic_repo.md) - Work with restic repositories * [ark restic server](ark_restic_server.md) - Run the ark restic server diff --git a/docs/cli-reference/ark_restic_repo.md b/docs/cli-reference/ark_restic_repo.md new file mode 100644 index 000000000..43285031b --- /dev/null +++ b/docs/cli-reference/ark_restic_repo.md @@ -0,0 +1,35 @@ +## ark restic repo + +Work with restic repositories + +### Synopsis + + +Work with restic repositories + +### Options + +``` + -h, --help help for repo +``` + +### Options inherited from parent commands + +``` + --alsologtostderr log to standard error as well as files + --kubeconfig string Path to the kubeconfig file to use to talk to the Kubernetes apiserver. If unset, try the environment variable KUBECONFIG, as well as in-cluster configuration + --kubecontext string The context to use to talk to the Kubernetes apiserver. If unset defaults to whatever your current-context is (kubectl config current-context) + --log_backtrace_at traceLocation when logging hits line file:N, emit a stack trace (default :0) + --log_dir string If non-empty, write log files in this directory + --logtostderr log to standard error instead of files + -n, --namespace string The namespace in which Ark should operate (default "heptio-ark") + --stderrthreshold severity logs at or above this threshold go to stderr (default 2) + -v, --v Level log level for V logs + --vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging +``` + +### SEE ALSO +* [ark restic](ark_restic.md) - Work with restic +* [ark restic repo get](ark_restic_repo_get.md) - Get restic repositories +* [ark restic repo init](ark_restic_repo_init.md) - initialize a restic repository for a specified namespace + diff --git a/docs/cli-reference/ark_restic_repo_get.md b/docs/cli-reference/ark_restic_repo_get.md new file mode 100644 index 000000000..de9fa38ea --- /dev/null +++ b/docs/cli-reference/ark_restic_repo_get.md @@ -0,0 +1,41 @@ +## ark restic repo get + +Get restic repositories + +### Synopsis + + +Get restic repositories + +``` +ark restic repo get [flags] +``` + +### Options + +``` + -h, --help help for get + --label-columns stringArray a comma-separated list of labels to be displayed as columns + -o, --output string Output display format. For create commands, display the object but do not send it to the server. Valid formats are 'table', 'json', and 'yaml'. (default "table") + -l, --selector string only show items matching this label selector + --show-labels show labels in the last column +``` + +### Options inherited from parent commands + +``` + --alsologtostderr log to standard error as well as files + --kubeconfig string Path to the kubeconfig file to use to talk to the Kubernetes apiserver. If unset, try the environment variable KUBECONFIG, as well as in-cluster configuration + --kubecontext string The context to use to talk to the Kubernetes apiserver. If unset defaults to whatever your current-context is (kubectl config current-context) + --log_backtrace_at traceLocation when logging hits line file:N, emit a stack trace (default :0) + --log_dir string If non-empty, write log files in this directory + --logtostderr log to standard error instead of files + -n, --namespace string The namespace in which Ark should operate (default "heptio-ark") + --stderrthreshold severity logs at or above this threshold go to stderr (default 2) + -v, --v Level log level for V logs + --vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging +``` + +### SEE ALSO +* [ark restic repo](ark_restic_repo.md) - Work with restic repositories + diff --git a/docs/cli-reference/ark_restic_init-repository.md b/docs/cli-reference/ark_restic_repo_init.md similarity index 90% rename from docs/cli-reference/ark_restic_init-repository.md rename to docs/cli-reference/ark_restic_repo_init.md index bab4ebe47..16d59e39c 100644 --- a/docs/cli-reference/ark_restic_init-repository.md +++ b/docs/cli-reference/ark_restic_repo_init.md @@ -1,4 +1,4 @@ -## ark restic init-repository +## ark restic repo init initialize a restic repository for a specified namespace @@ -8,13 +8,13 @@ initialize a restic repository for a specified namespace initialize a restic repository for a specified namespace ``` -ark restic init-repository NAMESPACE [flags] +ark restic repo init NAMESPACE [flags] ``` ### Options ``` - -h, --help help for init-repository + -h, --help help for init --key-data string Encryption key for the restic repository. Optional; if unset, Ark will generate a random key for you. --key-file string Path to file containing the encryption key for the restic repository. Optional; if unset, Ark will generate a random key for you. --key-size int Size of the generated key for the restic repository (default 1024) @@ -36,5 +36,5 @@ ark restic init-repository NAMESPACE [flags] ``` ### SEE ALSO -* [ark restic](ark_restic.md) - Work with restic repositories +* [ark restic repo](ark_restic_repo.md) - Work with restic repositories diff --git a/docs/cli-reference/ark_restic_server.md b/docs/cli-reference/ark_restic_server.md index eaca3b98d..02e9df772 100644 --- a/docs/cli-reference/ark_restic_server.md +++ b/docs/cli-reference/ark_restic_server.md @@ -34,5 +34,5 @@ ark restic server [flags] ``` ### SEE ALSO -* [ark restic](ark_restic.md) - Work with restic repositories +* [ark restic](ark_restic.md) - Work with restic diff --git a/pkg/cmd/cli/restic/repo/get.go b/pkg/cmd/cli/restic/repo/get.go new file mode 100644 index 000000000..844c0ace0 --- /dev/null +++ b/pkg/cmd/cli/restic/repo/get.go @@ -0,0 +1,66 @@ +/* +Copyright 2018 the Heptio Ark 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 repo + +import ( + "github.com/spf13/cobra" + + 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/output" +) + +func NewGetCommand(f client.Factory, use string) *cobra.Command { + var listOptions metav1.ListOptions + + c := &cobra.Command{ + Use: use, + Short: "Get restic repositories", + Run: func(c *cobra.Command, args []string) { + err := output.ValidateFlags(c) + cmd.CheckError(err) + + arkClient, err := f.Client() + cmd.CheckError(err) + + var repos *api.ResticRepositoryList + if len(args) > 0 { + repos = new(api.ResticRepositoryList) + for _, name := range args { + repo, err := arkClient.Ark().ResticRepositories(f.Namespace()).Get(name, metav1.GetOptions{}) + cmd.CheckError(err) + repos.Items = append(repos.Items, *repo) + } + } else { + repos, err = arkClient.ArkV1().ResticRepositories(f.Namespace()).List(listOptions) + cmd.CheckError(err) + } + + _, err = output.PrintWithFormat(c, repos) + cmd.CheckError(err) + }, + } + + c.Flags().StringVarP(&listOptions.LabelSelector, "selector", "l", listOptions.LabelSelector, "only show items matching this label selector") + + output.BindFlags(c.Flags()) + + return c +} diff --git a/pkg/cmd/cli/restic/init_repository.go b/pkg/cmd/cli/restic/repo/init.go similarity index 97% rename from pkg/cmd/cli/restic/init_repository.go rename to pkg/cmd/cli/restic/repo/init.go index 4f8dd3c87..a49e00b7d 100644 --- a/pkg/cmd/cli/restic/init_repository.go +++ b/pkg/cmd/cli/restic/repo/init.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package restic +package repo import ( "crypto/rand" @@ -34,11 +34,11 @@ import ( "github.com/heptio/ark/pkg/util/filesystem" ) -func NewInitRepositoryCommand(f client.Factory) *cobra.Command { +func NewInitCommand(f client.Factory) *cobra.Command { o := NewInitRepositoryOptions() c := &cobra.Command{ - Use: "init-repository NAMESPACE", + Use: "init NAMESPACE", Short: "initialize a restic repository for a specified namespace", Long: "initialize a restic repository for a specified namespace", Args: cobra.ExactArgs(1), diff --git a/pkg/cmd/cli/restic/init_repository_test.go b/pkg/cmd/cli/restic/repo/init_test.go similarity index 75% rename from pkg/cmd/cli/restic/init_repository_test.go rename to pkg/cmd/cli/restic/repo/init_test.go index 6a7b4584e..eea504964 100644 --- a/pkg/cmd/cli/restic/init_repository_test.go +++ b/pkg/cmd/cli/restic/repo/init_test.go @@ -1,4 +1,20 @@ -package restic +/* +Copyright 2018 the Heptio Ark 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 repo import ( "testing" diff --git a/pkg/cmd/cli/restic/repo/repo.go b/pkg/cmd/cli/restic/repo/repo.go new file mode 100644 index 000000000..ff7bb3b37 --- /dev/null +++ b/pkg/cmd/cli/restic/repo/repo.go @@ -0,0 +1,38 @@ +/* +Copyright 2018 the Heptio Ark 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 repo + +import ( + "github.com/spf13/cobra" + + "github.com/heptio/ark/pkg/client" +) + +func NewRepositoryCommand(f client.Factory) *cobra.Command { + c := &cobra.Command{ + Use: "repo", + Short: "Work with restic repositories", + Long: "Work with restic repositories", + } + + c.AddCommand( + NewInitCommand(f), + NewGetCommand(f, "get"), + ) + + return c +} diff --git a/pkg/cmd/cli/restic/restic.go b/pkg/cmd/cli/restic/restic.go index 3ab5f7e07..171f6e81e 100644 --- a/pkg/cmd/cli/restic/restic.go +++ b/pkg/cmd/cli/restic/restic.go @@ -20,17 +20,18 @@ import ( "github.com/spf13/cobra" "github.com/heptio/ark/pkg/client" + "github.com/heptio/ark/pkg/cmd/cli/restic/repo" ) func NewCommand(f client.Factory) *cobra.Command { c := &cobra.Command{ Use: "restic", - Short: "Work with restic repositories", - Long: "Work with restic repositories", + Short: "Work with restic", + Long: "Work with restic", } c.AddCommand( - NewInitRepositoryCommand(f), + repo.NewRepositoryCommand(f), NewServerCommand(f), ) diff --git a/pkg/cmd/util/output/output.go b/pkg/cmd/util/output/output.go index 260bc8098..a621f5af3 100644 --- a/pkg/cmd/util/output/output.go +++ b/pkg/cmd/util/output/output.go @@ -141,6 +141,8 @@ func printTable(cmd *cobra.Command, obj runtime.Object) (bool, error) { printer.Handler(restoreColumns, nil, printRestoreList) printer.Handler(scheduleColumns, nil, printSchedule) printer.Handler(scheduleColumns, nil, printScheduleList) + printer.Handler(resticRepoColumns, nil, printResticRepo) + printer.Handler(resticRepoColumns, nil, printResticRepoList) err = printer.PrintObj(obj, os.Stdout) if err != nil { diff --git a/pkg/cmd/util/output/restic_repo_printer.go b/pkg/cmd/util/output/restic_repo_printer.go new file mode 100644 index 000000000..a2e374232 --- /dev/null +++ b/pkg/cmd/util/output/restic_repo_printer.go @@ -0,0 +1,76 @@ +/* +Copyright 2018 the Heptio Ark 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 output + +import ( + "fmt" + "io" + + "k8s.io/kubernetes/pkg/printers" + + "github.com/heptio/ark/pkg/apis/ark/v1" +) + +var ( + resticRepoColumns = []string{"NAME", "STATUS", "LAST MAINTENANCE"} +) + +func printResticRepoList(list *v1.ResticRepositoryList, w io.Writer, options printers.PrintOptions) error { + for i := range list.Items { + if err := printResticRepo(&list.Items[i], w, options); err != nil { + return err + } + } + return nil +} + +func printResticRepo(repo *v1.ResticRepository, w io.Writer, options printers.PrintOptions) error { + name := printers.FormatResourceName(options.Kind, repo.Name, options.WithKind) + + if options.WithNamespace { + if _, err := fmt.Fprintf(w, "%s\t", repo.Namespace); err != nil { + return err + } + } + + status := repo.Status.Phase + if status == "" { + status = v1.ResticRepositoryPhaseNew + } + + lastMaintenance := repo.Status.LastMaintenanceTime.String() + if repo.Status.LastMaintenanceTime.IsZero() { + lastMaintenance = "" + } + + if _, err := fmt.Fprintf( + w, + "%s\t%s\t%s", + name, + status, + lastMaintenance, + ); err != nil { + return err + } + + if _, err := fmt.Fprint(w, printers.AppendLabels(repo.Labels, options.ColumnLabels)); err != nil { + return err + } + + _, err := fmt.Fprint(w, printers.AppendAllLabels(options.ShowLabels, repo.Labels)) + return err +}