-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
kubernetes.go
168 lines (137 loc) · 5.6 KB
/
kubernetes.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
package kubenav
import (
"context"
"encoding/json"
"fmt"
"net/http"
"regexp"
"strings"
"github.com/kubenav/kubenav/cmd/kubenav/kube"
"github.com/kubenav/kubenav/cmd/kubenav/server"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)
// KubernetesRequest is used to execute a request against a Kubernetes API. The
// Kubernetes API server and it's ca are specified via the "clusterServer" and
// "clusterCertificateAuthorityData" arguments. To skip the tls verification the
// request can set the "clusterInsecureSkipTLSVerify" argument to true. To
// handle the authentication against the API server the "user*" arguments can be
// used.
// The "requestMethod", "requestURL" and "requestBody" arguments are then used
// for the actually request. E.g. to get all Pods from the Kubernetes API the
// method "GET" and the URL "/api/v1/pods" can be used.
func KubernetesRequest(clusterServer, clusterCertificateAuthorityData string, clusterInsecureSkipTLSVerify bool, userClientCertificateData, userClientKeyData, userToken, userUsername, userPassword, proxy string, timeout int64, requestMethod, requestURL, requestBody string) (string, error) {
_, clientset, err := kube.NewClient(clusterServer, clusterCertificateAuthorityData, clusterInsecureSkipTLSVerify, userClientCertificateData, userClientKeyData, userToken, userUsername, userPassword, proxy, timeout)
if err != nil {
return "", err
}
requestURL = strings.TrimRight(clusterServer, "/") + requestURL
var responseResult rest.Result
var statusCode int
ctx := context.Background()
if requestMethod == http.MethodGet {
responseResult = clientset.RESTClient().Get().RequestURI(requestURL).Do(ctx)
} else if requestMethod == http.MethodDelete {
responseResult = clientset.RESTClient().Delete().RequestURI(requestURL).Body([]byte(requestBody)).Do(ctx)
} else if requestMethod == http.MethodPatch {
responseResult = clientset.RESTClient().Patch(types.JSONPatchType).RequestURI(requestURL).Body([]byte(requestBody)).Do(ctx)
} else if requestMethod == http.MethodPost {
responseResult = clientset.RESTClient().Post().RequestURI(requestURL).Body([]byte(requestBody)).Do(ctx)
}
if responseResult.Error() != nil {
return "", responseResult.Error()
}
responseResult = responseResult.StatusCode(&statusCode)
if statusCode == http.StatusUnauthorized {
return "", fmt.Errorf(http.StatusText(http.StatusUnauthorized))
}
responseBody, err := responseResult.Raw()
if err != nil {
return "", err
}
if statusCode < 200 || statusCode >= 300 {
return "", fmt.Errorf(string(responseBody))
}
return string(responseBody), nil
}
// KubernetesGetLogs returns the logs for a list of pods. The names of the Pods
// are provided via the "names" parameter, which must be a comma separated list
// of the Pod names. To use this function a user must also provide the
// namespace, container, since and previous parameter.
func KubernetesGetLogs(clusterServer, clusterCertificateAuthorityData string, clusterInsecureSkipTLSVerify bool, userClientCertificateData, userClientKeyData, userToken, userUsername, userPassword, proxy string, timeout int64, names, namespace, container string, since int64, filter string, previous bool) (string, error) {
_, clientset, err := kube.NewClient(clusterServer, clusterCertificateAuthorityData, clusterInsecureSkipTLSVerify, userClientCertificateData, userClientKeyData, userToken, userUsername, userPassword, proxy, timeout)
if err != nil {
return "", err
}
var logs [][]string
podNames := strings.Split(names, ",")
for _, podName := range podNames {
podLogs, err := kubernetesGetLogs(clientset, clusterServer, podName, namespace, container, since, filter, previous, len(podNames) != 1)
if err != nil {
return "", err
}
logs = append(logs, podLogs)
}
data := struct {
Logs [][]string `json:"logs"`
}{
logs,
}
dataStr, err := json.Marshal(data)
if err != nil {
return "", nil
}
return string(dataStr), nil
}
func kubernetesGetLogs(clientset *kubernetes.Clientset, clusterServer, name, namespace, container string, since int64, filter string, previous, prefix bool) ([]string, error) {
var statusCode int
ctx := context.Background()
requestURL := fmt.Sprintf("%s/api/v1/namespaces/%s/pods/%s/log?container=%s&sinceSeconds=%d&previous=%t", clusterServer, namespace, name, container, since, previous)
responseResult := clientset.RESTClient().Get().RequestURI(requestURL).Do(ctx)
if responseResult.Error() != nil {
return nil, responseResult.Error()
}
responseResult = responseResult.StatusCode(&statusCode)
if statusCode == http.StatusUnauthorized {
return nil, fmt.Errorf(http.StatusText(http.StatusUnauthorized))
}
responseBody, err := responseResult.Raw()
if err != nil {
return nil, err
}
if statusCode < 200 || statusCode >= 300 {
return nil, fmt.Errorf(string(responseBody))
}
if filter == "" {
if !prefix {
return strings.Split(string(responseBody), "\n"), nil
}
var logs []string
for _, line := range strings.Split(string(responseBody), "\n") {
logs = append(logs, fmt.Sprintf("%s: %s", name, line))
}
return logs, nil
}
reg, err := regexp.Compile(filter)
if err != nil {
return nil, err
}
var logs []string
for _, line := range strings.Split(string(responseBody), "\n") {
if reg.MatchString(line) {
if prefix {
logs = append(logs, fmt.Sprintf("%s: %s", name, line))
} else {
logs = append(logs, line)
}
}
}
return logs, nil
}
// KubernetesStartServer starts an Go server which listens on "14122". The
// server is responsible for providing the port forwarding and Pod exec feature
// for kubenav.
func KubernetesStartServer() {
server.Start()
}