-
Notifications
You must be signed in to change notification settings - Fork 15
/
etcd_backup_list.go
89 lines (78 loc) · 1.77 KB
/
etcd_backup_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package cmd
import (
"context"
"errors"
"fmt"
"io/ioutil"
"net/http"
"github.com/cybozu-go/cke/op"
"github.com/cybozu-go/cke"
"github.com/cybozu-go/well"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func backupList(ctx context.Context, cmd *cobra.Command) error {
cfg, err := storage.GetCluster(ctx)
if err != nil {
return err
}
if !cfg.EtcdBackup.Enabled {
return errors.New("etcd backup is disabled")
}
var controlPlane *cke.Node
for _, node := range cfg.Nodes {
if node.ControlPlane {
controlPlane = node
break
}
}
if controlPlane == nil {
return errors.New("control plane not found")
}
cs, err := inf.K8sClient(ctx, controlPlane)
if err != nil {
return err
}
svc, err := cs.CoreV1().Services("kube-system").Get(op.EtcdBackupAppName, metav1.GetOptions{})
if err != nil {
return err
}
if len(svc.Spec.Ports) == 0 {
return errors.New("service.ports is empty")
}
port := svc.Spec.Ports[0].NodePort
nodeIP := cfg.Nodes[0].Address
url := fmt.Sprintf("http://%s:%d/api/v1/backup", nodeIP, port)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
req = req.WithContext(ctx)
resp, err := inf.HTTPClient().Do(req)
if err != nil {
return err
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return err
}
fmt.Println(string(body))
return nil
}
var etcdBackupListCmd = &cobra.Command{
Use: "list",
Short: "List etcd backups",
Long: `List etcd backup file names.
The filenames are usually contained created date string.`,
RunE: func(cmd *cobra.Command, args []string) error {
well.Go(func(ctx context.Context) error {
return backupList(ctx, cmd)
})
well.Stop()
return well.Wait()
},
}
func init() {
etcdBackupCmd.AddCommand(etcdBackupListCmd)
}