/
client.go
155 lines (133 loc) · 3.47 KB
/
client.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package client
import (
"context"
"encoding/json"
"fmt"
"os"
"time"
"github.com/bk201/support-bundle-utils/pkg/client/utils"
wait "k8s.io/apimachinery/pkg/util/wait"
)
type SupportBundleClient struct {
url string
User string
Password string
NoAuth bool
OutputFile string
Insecure bool
IssueURL string
IssueDescription string
r *RESTClient
}
type SupportBundleInitateInput struct {
IssueURL string `json:"issueURL"`
Description string `json:"description"`
}
type BundleState string
const (
BundleStateInProgress = BundleState("InProgress")
BundleStateReadyForDownload = BundleState("ReadyForDownload")
BundleStateError = BundleState("Error")
)
type BundleError string
type SupportBundleResource struct {
PodID string `json:"podID"`
NodeID string `json:"nodeID"`
State BundleState `json:"state"`
Name string `json:"name"`
ErrorMessage BundleError `json:"errorMessage"`
ProgressPercentage int `json:"progressPercentage"`
}
func (sbr *SupportBundleResource) BackendID() string {
// This is just to be compatible with Longhorn support bundle
if sbr.PodID != "" {
return sbr.PodID
}
return sbr.NodeID
}
func (sbr *SupportBundleResource) readyCondition(r *RESTClient) wait.ConditionFunc {
return func() (done bool, err error) {
url := fmt.Sprintf("%s/v1/supportbundles/%s/%s", r.apiURL, sbr.BackendID(), sbr.Name)
resp, err := r.Get(url)
if err != nil {
return false, err
}
var newSbr SupportBundleResource
err = json.Unmarshal(resp, &newSbr)
if err != nil {
return false, err
}
if newSbr.State == BundleStateReadyForDownload {
utils.WriteStdout(".100\n")
return true, nil
}
utils.WriteStdout(".")
return false, nil
}
}
func (c *SupportBundleClient) Run(url string) error {
c.url = url
c.r = NewRESTClient(context.TODO(), c.url, c.User, c.Password, c.Insecure)
if !c.NoAuth {
err := c.r.Login()
if err != nil {
return fmt.Errorf("fail to login: %s", err)
}
defer func() {
err = c.r.Logout()
if err != nil {
fmt.Fprintf(os.Stderr, "fail to logout: %s\n", err)
}
}()
}
sbr, err := c.create()
if err != nil {
return err
}
fmt.Printf("bundle %s is being generated...", sbr.Name)
err = c.wait(sbr)
if err != nil {
return err
}
saved, err := c.download(sbr, c.OutputFile)
if err != nil {
return err
}
fmt.Printf("bundle is saved to %s\n", saved)
return nil
}
func (c *SupportBundleClient) create() (*SupportBundleResource, error) {
sbi := SupportBundleInitateInput{
IssueURL: c.IssueURL,
Description: c.IssueDescription,
}
url := c.url + "/v1/supportbundles"
data, err := json.Marshal(sbi)
if err != nil {
return nil, err
}
resp, err := c.r.Post(url, data)
if err != nil {
return nil, err
}
var sbr SupportBundleResource
err = json.Unmarshal(resp, &sbr)
if err != nil {
return nil, err
}
return &sbr, nil
}
func (c *SupportBundleClient) wait(sbr *SupportBundleResource) error {
interval := 5 * time.Second
timeout := 2 * time.Minute
utils.WriteStdout("\n")
err := wait.PollImmediate(interval, timeout, sbr.readyCondition(c.r))
if err != nil {
return fmt.Errorf("timeout for waiting a bundle: %s", err)
}
return nil
}
func (c *SupportBundleClient) download(sbr *SupportBundleResource, path string) (string, error) {
url := fmt.Sprintf("%s/v1/supportbundles/%s/%s/download", c.url, sbr.BackendID(), sbr.Name)
return c.r.Download(url, path)
}