Add backup & schedule describers

Signed-off-by: Andy Goldstein <andy.goldstein@gmail.com>
This commit is contained in:
Andy Goldstein
2017-11-27 15:22:20 -05:00
parent 062a5d7557
commit c2dc41efd8
10 changed files with 524 additions and 162 deletions
+1 -3
View File
@@ -33,11 +33,9 @@ func NewCommand(f client.Factory) *cobra.Command {
NewCreateCommand(f, "create"),
NewGetCommand(f, "get"),
NewLogsCommand(f),
NewDescribeCommand(f, "describe"),
NewDownloadCommand(f),
// Will implement describe later
// NewDescribeCommand(f),
// If you delete a backup and it still exists in object storage, the backup sync controller will
// recreate it. Until we have a good UX around this, we're disabling the delete command.
// NewDeleteCommand(f),
+41 -4
View File
@@ -17,18 +17,55 @@ limitations under the License.
package backup
import (
"github.com/spf13/cobra"
"fmt"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"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 NewDescribeCommand(f client.Factory) *cobra.Command {
func NewDescribeCommand(f client.Factory, use string) *cobra.Command {
var listOptions metav1.ListOptions
c := &cobra.Command{
Use: "describe",
Short: "Describe a backup",
Use: use + " [NAME1] [NAME2] [NAME...]",
Short: "Describe backups",
Run: func(c *cobra.Command, args []string) {
arkClient, err := f.Client()
cmd.CheckError(err)
var backups *v1.BackupList
if len(args) > 0 {
backups = new(v1.BackupList)
for _, name := range args {
backup, err := arkClient.Ark().Backups(v1.DefaultNamespace).Get(name, metav1.GetOptions{})
cmd.CheckError(err)
backups.Items = append(backups.Items, *backup)
}
} else {
backups, err = arkClient.ArkV1().Backups(v1.DefaultNamespace).List(listOptions)
cmd.CheckError(err)
}
first := true
for _, backup := range backups.Items {
s := output.DescribeBackup(&backup)
if first {
first = false
fmt.Print(s)
} else {
fmt.Printf("\n\n%s", s)
}
}
cmd.CheckError(err)
},
}
c.Flags().StringVarP(&listOptions.LabelSelector, "selector", "l", listOptions.LabelSelector, "only show items matching this label selector")
return c
}
+8 -6
View File
@@ -20,7 +20,9 @@ import (
"github.com/spf13/cobra"
"github.com/heptio/ark/pkg/client"
"github.com/heptio/ark/pkg/cmd/cli/backup"
"github.com/heptio/ark/pkg/cmd/cli/restore"
"github.com/heptio/ark/pkg/cmd/cli/schedule"
)
func NewCommand(f client.Factory) *cobra.Command {
@@ -30,18 +32,18 @@ func NewCommand(f client.Factory) *cobra.Command {
Long: "Describe ark resources",
}
//backupCommand := backup.NewGetCommand(f, "backups")
//backupCommand.Aliases = []string{"backup"}
backupCommand := backup.NewDescribeCommand(f, "backups")
backupCommand.Aliases = []string{"backup"}
//scheduleCommand := schedule.NewGetCommand(f, "schedules")
//scheduleCommand.Aliases = []string{"schedule"}
scheduleCommand := schedule.NewDescribeCommand(f, "schedules")
scheduleCommand.Aliases = []string{"schedule"}
restoreCommand := restore.NewDescribeCommand(f, "restores")
restoreCommand.Aliases = []string{"restore"}
c.AddCommand(
//backupCommand,
//scheduleCommand,
backupCommand,
scheduleCommand,
restoreCommand,
)
+2 -120
View File
@@ -17,21 +17,14 @@ limitations under the License.
package restore
import (
"bytes"
"encoding/json"
"fmt"
"io"
"strings"
"time"
clientset "github.com/heptio/ark/pkg/generated/clientset/versioned"
"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/downloadrequest"
"github.com/heptio/ark/pkg/cmd/util/output"
)
@@ -39,7 +32,7 @@ func NewDescribeCommand(f client.Factory, use string) *cobra.Command {
var listOptions metav1.ListOptions
c := &cobra.Command{
Use: use,
Use: use + " [NAME1] [NAME2] [NAME...]",
Short: "Describe restores",
Run: func(c *cobra.Command, args []string) {
arkClient, err := f.Client()
@@ -60,9 +53,7 @@ func NewDescribeCommand(f client.Factory, use string) *cobra.Command {
first := true
for _, restore := range restores.Items {
s := output.Describe(func(out io.Writer) {
describeRestore(out, &restore, arkClient)
})
s := output.DescribeRestore(&restore, arkClient)
if first {
first = false
fmt.Print(s)
@@ -76,114 +67,5 @@ func NewDescribeCommand(f client.Factory, use string) *cobra.Command {
c.Flags().StringVarP(&listOptions.LabelSelector, "selector", "l", listOptions.LabelSelector, "only show items matching this label selector")
output.BindFlags(c.Flags())
return c
}
func describeRestore(out io.Writer, restore *api.Restore, arkClient clientset.Interface) {
output.DescribeMetadata(out, restore.ObjectMeta)
fmt.Fprintln(out)
fmt.Fprintf(out, "Backup:\t%s\n", restore.Spec.BackupName)
fmt.Fprintln(out)
fmt.Fprintf(out, "Namespaces:\n")
var s string
if len(restore.Spec.IncludedNamespaces) == 0 {
s = "*"
} else {
s = strings.Join(restore.Spec.IncludedNamespaces, ", ")
}
fmt.Fprintf(out, "\tIncluded:\t%s\n", s)
if len(restore.Spec.ExcludedNamespaces) == 0 {
s = "<none>"
} else {
s = strings.Join(restore.Spec.ExcludedNamespaces, ", ")
}
fmt.Fprintf(out, "\tExcluded:\t%s\n", s)
fmt.Fprintln(out)
fmt.Fprintf(out, "Resources:\n")
if len(restore.Spec.IncludedResources) == 0 {
s = "*"
} else {
s = strings.Join(restore.Spec.IncludedResources, ", ")
}
fmt.Fprintf(out, "\tIncluded:\t%s\n", s)
if len(restore.Spec.ExcludedResources) == 0 {
s = "<none>"
} else {
s = strings.Join(restore.Spec.ExcludedResources, ", ")
}
fmt.Fprintf(out, "\tExcluded:\t%s\n", s)
fmt.Fprintf(out, "\tCluster-scoped:\t%s\n", output.BoolPointerString(restore.Spec.IncludeClusterResources, "excluded", "included", "auto"))
fmt.Fprintln(out)
output.DescribeMap(out, "Namespace mappings", restore.Spec.NamespaceMapping)
fmt.Fprintln(out)
s = "<none>"
if restore.Spec.LabelSelector != nil {
s = metav1.FormatLabelSelector(restore.Spec.LabelSelector)
}
fmt.Fprintf(out, "Label selector:\t%s\n", s)
fmt.Fprintln(out)
fmt.Fprintf(out, "Restore PVs:\t%s\n", output.BoolPointerString(restore.Spec.RestorePVs, "false", "true", "auto"))
fmt.Fprintln(out)
fmt.Fprintf(out, "Phase:\t%s\n", restore.Status.Phase)
fmt.Fprintln(out)
fmt.Fprint(out, "Validation errors:")
if len(restore.Status.ValidationErrors) == 0 {
fmt.Fprintf(out, "\t<none>\n")
} else {
for _, ve := range restore.Status.ValidationErrors {
fmt.Fprintf(out, "\t%s\n", ve)
}
}
fmt.Fprintln(out)
describeRestoreResults(out, arkClient, restore)
}
func describeRestoreResults(out io.Writer, arkClient clientset.Interface, restore *api.Restore) {
if restore.Status.Warnings == 0 && restore.Status.Errors == 0 {
fmt.Fprintf(out, "Warnings:\t<none>\nErrors:\t<none>\n")
return
}
var buf bytes.Buffer
var resultMap map[string]api.RestoreResult
if err := downloadrequest.Stream(arkClient.ArkV1(), restore.Name, api.DownloadTargetKindRestoreResults, &buf, 30*time.Second); err != nil {
fmt.Fprintf(out, "Warnings:\t<error getting warnings: %v>\n\nErrors:\t<error getting errors: %v>\n", err, err)
return
}
if err := json.NewDecoder(&buf).Decode(&resultMap); err != nil {
fmt.Fprintf(out, "Warnings:\t<error decoding warnings: %v>\n\nErrors:\t<error decoding errors: %v>\n", err, err)
return
}
describeRestoreResult(out, "Warnings", resultMap["warnings"])
fmt.Fprintln(out)
describeRestoreResult(out, "Errors", resultMap["errors"])
}
func describeRestoreResult(out io.Writer, name string, result api.RestoreResult) {
fmt.Fprintf(out, "%s:\n", name)
output.DescribeSlice(out, 1, "Ark", result.Ark)
output.DescribeSlice(out, 1, "Cluster", result.Cluster)
if len(result.Namespaces) == 0 {
fmt.Fprintf(out, "\tNamespaces: <none>\n")
} else {
fmt.Fprintf(out, "\tNamespaces:\n")
for ns, warnings := range result.Namespaces {
output.DescribeSlice(out, 2, ns, warnings)
}
}
}
+41 -4
View File
@@ -17,18 +17,55 @@ limitations under the License.
package schedule
import (
"github.com/spf13/cobra"
"fmt"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"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 NewDescribeCommand(f client.Factory) *cobra.Command {
func NewDescribeCommand(f client.Factory, use string) *cobra.Command {
var listOptions metav1.ListOptions
c := &cobra.Command{
Use: "describe",
Short: "Describe a backup",
Use: use + " [NAME1] [NAME2] [NAME...]",
Short: "Describe schedules",
Run: func(c *cobra.Command, args []string) {
arkClient, err := f.Client()
cmd.CheckError(err)
var schedules *v1.ScheduleList
if len(args) > 0 {
schedules = new(v1.ScheduleList)
for _, name := range args {
schedule, err := arkClient.Ark().Schedules(v1.DefaultNamespace).Get(name, metav1.GetOptions{})
cmd.CheckError(err)
schedules.Items = append(schedules.Items, *schedule)
}
} else {
schedules, err = arkClient.ArkV1().Schedules(v1.DefaultNamespace).List(listOptions)
cmd.CheckError(err)
}
first := true
for _, schedule := range schedules.Items {
s := output.DescribeSchedule(&schedule)
if first {
first = false
fmt.Print(s)
} else {
fmt.Printf("\n\n%s", s)
}
}
cmd.CheckError(err)
},
}
c.Flags().StringVarP(&listOptions.LabelSelector, "selector", "l", listOptions.LabelSelector, "only show items matching this label selector")
return c
}
+1 -2
View File
@@ -32,8 +32,7 @@ func NewCommand(f client.Factory) *cobra.Command {
c.AddCommand(
NewCreateCommand(f, "create"),
NewGetCommand(f, "get"),
// Will implement later
// NewDescribeCommand(f),
NewDescribeCommand(f, "describe"),
NewDeleteCommand(f),
)