Skip to content

Commit 5c6713f

Browse files
Md. Emruz Hossaintamalsaha
authored andcommitted
Show repository snapshot list (#417)
* Implement Get,List,Delete method for snapshots * Added test for snapshots * Added doc for snapshot * Remove unused snapshot handler.
1 parent 6d8ef78 commit 5c6713f

File tree

4 files changed

+115
-92
lines changed

4 files changed

+115
-92
lines changed

forget.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package cmds
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/appscode/kutil/meta"
7+
cs "github.com/appscode/stash/client/clientset/versioned/typed/stash/v1alpha1"
8+
"github.com/appscode/stash/pkg/registry/snapshot"
9+
"github.com/spf13/cobra"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
"k8s.io/client-go/tools/clientcmd"
12+
)
13+
14+
func NewCmdForget() *cobra.Command {
15+
var (
16+
masterURL string
17+
kubeconfigPath string
18+
repositoryName string
19+
)
20+
21+
cmd := &cobra.Command{
22+
Use: "forget [snapshotID ...]",
23+
Short: "Delete snapshots from a restic repository",
24+
DisableAutoGenTag: true,
25+
Run: func(cmd *cobra.Command, args []string) {
26+
config, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath)
27+
if err != nil {
28+
fmt.Errorf(err.Error())
29+
}
30+
31+
stashClient := cs.NewForConfigOrDie(config)
32+
33+
if repositoryName == "" {
34+
fmt.Errorf("repository name not found")
35+
return
36+
}
37+
repo, err := stashClient.Repositories(meta.Namespace()).Get(repositoryName, metav1.GetOptions{})
38+
if err != nil {
39+
fmt.Errorf(err.Error())
40+
return
41+
}
42+
43+
r := snapshot.NewREST(config)
44+
err = r.ForgetSnapshots(repo, args)
45+
if err != nil {
46+
fmt.Errorf(err.Error())
47+
}
48+
},
49+
}
50+
cmd.Flags().StringVar(&masterURL, "master", masterURL, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
51+
cmd.Flags().StringVar(&kubeconfigPath, "kubeconfig", kubeconfigPath, "Path to kubeconfig file with authorization information (the master location is set by the master flag).")
52+
cmd.Flags().StringVar(&repositoryName, "repo-name", repositoryName, "Name of the Repository CRD.")
53+
54+
return cmd
55+
}

root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ func NewRootCmd() *cobra.Command {
5858
rootCmd.AddCommand(NewCmdRecover())
5959
rootCmd.AddCommand(NewCmdCheck())
6060
rootCmd.AddCommand(NewCmdScaleDown())
61+
rootCmd.AddCommand(NewCmdSnapshots())
62+
rootCmd.AddCommand(NewCmdForget())
6163

6264
return rootCmd
6365
}

snapshot.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package cmds
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/appscode/kutil/meta"
8+
cs "github.com/appscode/stash/client/clientset/versioned/typed/stash/v1alpha1"
9+
"github.com/appscode/stash/pkg/registry/snapshot"
10+
"github.com/spf13/cobra"
11+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
"k8s.io/client-go/tools/clientcmd"
13+
)
14+
15+
func NewCmdSnapshots() *cobra.Command {
16+
var (
17+
masterURL string
18+
kubeconfigPath string
19+
repositoryName string
20+
)
21+
22+
cmd := &cobra.Command{
23+
Use: "snapshots [snapshotID ...]",
24+
Short: "Get snapshots of restic repo",
25+
DisableAutoGenTag: true,
26+
Run: func(cmd *cobra.Command, args []string) {
27+
config, err := clientcmd.BuildConfigFromFlags(masterURL, kubeconfigPath)
28+
if err != nil {
29+
fmt.Errorf(err.Error())
30+
}
31+
32+
stashClient := cs.NewForConfigOrDie(config)
33+
34+
if repositoryName == "" {
35+
fmt.Errorf("repository name not found")
36+
return
37+
}
38+
repo, err := stashClient.Repositories(meta.Namespace()).Get(repositoryName, metav1.GetOptions{})
39+
if err != nil {
40+
fmt.Errorf(err.Error())
41+
return
42+
}
43+
44+
r := snapshot.NewREST(config)
45+
snapshots, err := r.GetSnapshots(repo, args)
46+
if err != nil {
47+
fmt.Errorf(err.Error())
48+
}
49+
jsonSnaps, err := json.MarshalIndent(snapshots, "", " ")
50+
fmt.Println(string(jsonSnaps))
51+
},
52+
}
53+
cmd.Flags().StringVar(&masterURL, "master", masterURL, "The address of the Kubernetes API server (overrides any value in kubeconfig)")
54+
cmd.Flags().StringVar(&kubeconfigPath, "kubeconfig", kubeconfigPath, "Path to kubeconfig file with authorization information (the master location is set by the master flag).")
55+
cmd.Flags().StringVar(&repositoryName, "repo-name", repositoryName, "Name of the Repository CRD.")
56+
57+
return cmd
58+
}

snapshot_handler.go

Lines changed: 0 additions & 92 deletions
This file was deleted.

0 commit comments

Comments
 (0)