-
Notifications
You must be signed in to change notification settings - Fork 497
/
common.go
184 lines (147 loc) · 4.46 KB
/
common.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package command
import (
"bytes"
"fmt"
"io/ioutil"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"net/http"
"os"
yamljsontool "github.com/ghodss/yaml"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2"
)
type (
// GlobalFlags is the global flags for the whole client.
GlobalFlags struct {
Server string
OutputFormat string
}
// APIErr is the standard return of error.
APIErr struct {
Code int `yaml:"code"`
Message string `yaml:"message"`
}
)
var (
// CommandlineGlobalFlags is the singleton of GlobalFlags.
CommandlineGlobalFlags GlobalFlags
)
const (
apiURL = "/apis/v3"
healthURL = apiURL + "/healthz"
membersURL = apiURL + "/status/members"
memberURL = apiURL + "/status/members/%s"
objectKindsURL = apiURL + "/object-kinds"
objectsURL = apiURL + "/objects"
objectURL = apiURL + "/objects/%s"
statusObjectURL = apiURL + "/status/objects/%s"
statusObjectsURL = apiURL + "/status/objects"
// MeshTenantsURL is the mesh tenant prefix.
MeshTenantsURL = apiURL + "/mesh/tenants"
// MeshTenantURL is the mesh tenant path.
MeshTenantURL = apiURL + "/mesh/tenants/%s"
// MeshServicesURL is mesh service prefix.
MeshServicesURL = apiURL + "/mesh/services"
// MeshServiceURL is the mesh service path.
MeshServiceURL = apiURL + "/mesh/services/%s"
// MeshServiceCanaryURL is the mesh service canary path.
MeshServiceCanaryURL = apiURL + "/mesh/services/%s/canary"
// MeshServiceResilienceURL is the mesh service resilience path.
MeshServiceResilienceURL = apiURL + "/mesh/services/%s/resilience"
// MeshServiceLoadBalanceURL is the mesh service load balance path.
MeshServiceLoadBalanceURL = apiURL + "/mesh/services/%s/loadbalance"
// MeshServiceOutputServerURL is the mesh service output server path.
MeshServiceOutputServerURL = apiURL + "/mesh/services/%s/outputserver"
// MeshServiceTracingsURL is the mesh service tracings path.
MeshServiceTracingsURL = apiURL + "/mesh/services/%s/tracings"
// MeshServiceMetricsURL is the mesh service metrics path.
MeshServiceMetricsURL = apiURL + "/mesh/services/%s/metrics"
// MeshServiceInstancesURL is the mesh service prefix.
MeshServiceInstancesURL = apiURL + "/mesh/serviceinstances"
// MeshServiceInstanceURL is the mesh service path.
MeshServiceInstanceURL = apiURL + "/mesh/serviceinstances/%s/%s"
)
func makeURL(urlTemplate string, a ...interface{}) string {
return "https://" + CommandlineGlobalFlags.Server + fmt.Sprintf(urlTemplate, a...)
}
func successfulStatusCode(code int) bool {
return code >= 200 && code < 300
}
func handleRequest(httpMethod string, url string, reqBody []byte, cmd *cobra.Command) {
req, err := http.NewRequest(httpMethod, url, bytes.NewReader(reqBody))
if err != nil {
ExitWithError(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
ExitWithErrorf("%s failed: %v", cmd.Short, err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
ExitWithErrorf("%s failed: %v", cmd.Short, err)
}
if !successfulStatusCode(resp.StatusCode) {
msg := string(body)
apiErr := &APIErr{}
err = yaml.Unmarshal(body, apiErr)
if err == nil {
msg = apiErr.Message
}
ExitWithErrorf("%d: %s", apiErr.Code, msg)
}
if len(body) != 0 {
printBody(body)
}
}
func printBody(body []byte) {
var output []byte
switch CommandlineGlobalFlags.OutputFormat {
case "yaml":
output = body
case "json":
var err error
output, err = yamljsontool.YAMLToJSON(body)
if err != nil {
ExitWithErrorf("yaml %s to json failed: %v", body, err)
}
}
fmt.Printf("%s", output)
}
func readFromFileOrStdin(specFile string, cmd *cobra.Command) ([]byte, string) {
var buff []byte
var err error
if specFile != "" {
buff, err = ioutil.ReadFile(specFile)
if err != nil {
ExitWithErrorf("%s failed: %v", cmd.Short, err)
}
} else {
buff, err = ioutil.ReadAll(os.Stdin)
if err != nil {
ExitWithErrorf("%s failed: %v", cmd.Short, err)
}
}
var spec struct {
Kind string `yaml:"kind"`
Name string `yaml:"name"`
}
err = yaml.Unmarshal(buff, &spec)
if err != nil {
ExitWithErrorf("%s failed, invalid spec: %v", cmd.Short, err)
}
return buff, spec.Name
}
func newKubernetesClient() (*kubernetes.Clientset, error) {
config, err := clientcmd.BuildConfigFromFlags("", defaultKubernetesConfigPath)
if err != nil {
return nil, err
}
// create the clientset
kubeClient, err := kubernetes.NewForConfig(config)
if err != nil {
ExitWithErrorf("Can't connect to K8S.")
}
return kubeClient, nil
}