forked from goodrain/rainbond
-
Notifications
You must be signed in to change notification settings - Fork 0
/
host_node.go
358 lines (331 loc) · 11.1 KB
/
host_node.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package controller
import (
"net/http"
"github.com/goodrain/rainbond/entrance/core"
"github.com/goodrain/rainbond/entrance/store"
"k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"github.com/Sirupsen/logrus"
"github.com/coreos/etcd/client"
restful "github.com/emicklei/go-restful"
"github.com/twinj/uuid"
"github.com/goodrain/rainbond/entrance/api/model"
apistore "github.com/goodrain/rainbond/entrance/api/store"
"fmt"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
//NodeSource 集群节点管理接口
type NodeSource struct {
coreManager core.Manager
readStore store.ReadStore
apiStoreManager *apistore.Manager
clientSet *kubernetes.Clientset
}
func returns201(b *restful.RouteBuilder) {
b.Returns(http.StatusCreated, "", model.HostNode{})
}
//Register 注册
func (u NodeSource) Register(container *restful.Container) {
ws := new(restful.WebService)
ws.Path("/nodes").
Doc("Manage cluster computer nodes").
Consumes(restful.MIME_XML, restful.MIME_JSON).
Produces(restful.MIME_JSON, restful.MIME_XML) // you can specify this per route as well
ws.Route(ws.POST("").To(u.addNode).
// docs
Doc("create a host node").
Operation("createNode").
Reads(model.HostNode{}).Do(returns201)) // from the request
ws.Route(ws.GET("").To(u.getNodes).
// docs
Doc("get all host node").
Operation("getNodes").
Writes(ResponseType{})) // from the request
ws.Route(ws.GET("{host_name}").To(u.getNodeInfo).
// docs
Doc("get all host node").
Operation("getNodes").
Param(ws.PathParameter("host_name", "the name of the host").DataType("string")).
Writes(ResponseType{})) // from the request
ws.Route(ws.DELETE("/{host_name}").To(u.removeNode).
// docs
Doc("delete a host node").
Operation("removeDomain").
Param(ws.PathParameter("host_name", "the name of the host").DataType("string")))
ws.Route(ws.PUT("/{host_name}").To(u.updateNode).
// docs
Doc("update a host node").
Operation("updateNode").
Param(ws.PathParameter("host_name", "the name of the host").DataType("string")).
Reads(model.HostNode{})) // from the request
container.Add(ws)
}
//addNode 添加节点
func (u *NodeSource) addNode(request *restful.Request, response *restful.Response) {
node := new(model.HostNode)
err := request.ReadEntity(node)
if err != nil {
NewFaliResponse(http.StatusBadRequest, "request body error."+err.Error(), "读取请求数据错误,数据不合法", response)
return
}
if node.UUID == "" {
node.UUID = uuid.NewV4().String()
}
if node.HostName == "" {
NewFaliResponse(http.StatusBadRequest, "node host name cannot be empty", "节点主机名不能为空", response)
return
}
if node.InternalIP == "" {
NewFaliResponse(http.StatusBadRequest, "node host ip cannot be empty", "节点内网ip不能为空", response)
return
}
if node.ExternalIP == "" {
NewFaliResponse(http.StatusBadRequest, "node host ip cannot be empty", "节点外网ip不能为空", response)
return
}
if node.Role == "" {
node.Role = "tree"
}
if node.Status == "" {
node.Status = "create"
}
var create bool
var status v1.NodeStatus
// if node already init
if node.Status == "init" && node.Role == "tree" {
logrus.Info("create node to kubernetes")
k8sNode, err := u.clientSet.Core().Nodes().Get(node.HostName, metav1.GetOptions{})
if err != nil {
if apierrors.IsNotFound(err) {
newk8sNode, err := createK8sNode(node)
if err != nil {
NewFaliResponse(400, err.Error(), "创建集群节点失败", response)
return
}
v1Node, err := u.clientSet.Core().Nodes().Create(newk8sNode)
if err != nil {
if !apierrors.IsAlreadyExists(err) {
node.Status = "running"
}
NewFaliResponse(400, err.Error(), "创建集群节点失败", response)
return
}
status = v1Node.Status
create = true
}
} else {
status = k8sNode.Status
}
node.Status = "running"
}
err = u.apiStoreManager.AddSource("/store/nodes/"+node.HostName, node)
if err != nil {
if create {
if err := u.clientSet.Core().Nodes().Delete(node.HostName, nil); err != nil {
logrus.Errorf("Unable to register node %q to etcd: error deleting old node: %v", node.HostName, err)
} else {
logrus.Errorf("Deleted old node object %q", node.HostName)
}
}
if cerr, ok := err.(client.Error); ok {
if cerr.Code == client.ErrorCodeNodeExist {
NewFaliResponse(400, "node is exist.", "节点已存在", response)
return
}
}
NewFaliResponse(500, err.Error(), "存储节点信息失败", response)
return
}
node.NodeStatus = &status
NewPostSuccessResponse(node, nil, response)
}
//removeNode
func (u *NodeSource) removeNode(request *restful.Request, response *restful.Response) {
hostName := request.PathParameter("host_name")
if hostName == "" {
NewFaliResponse(400, "host name can not be empty", "节点的主机名不能为空", response)
return
}
node := model.HostNode{}
err := u.apiStoreManager.GetSource("/store/nodes/"+hostName, &node)
if err != nil {
if client.IsKeyNotFound(err) {
NewFaliResponse(404, "host name not found", "节点的主机名不存在", response)
return
}
NewFaliResponse(500, err.Error(), "获取节点数据错误", response)
return
}
if err := u.clientSet.Core().Nodes().Delete(hostName, nil); err != nil {
if !apierrors.IsNotFound(err) {
logrus.Errorf("Unable to register node %q to etcd: error deleting old node: %v", hostName, err)
NewFaliResponse(500, err.Error(), "删除集群节点错误", response)
return
}
}
err = u.apiStoreManager.DeleteSource("/store/nodes/"+hostName, false)
if err != nil {
if client.IsKeyNotFound(err) {
NewFaliResponse(404, "host name not found", "节点的主机名不存在", response)
return
}
NewFaliResponse(500, err.Error(), "删除节点数据错误", response)
return
}
NewSuccessResponse(node, nil, response)
}
//updateNode
func (u *NodeSource) updateNode(request *restful.Request, response *restful.Response) {
node := new(model.HostNode)
err := request.ReadEntity(node)
if err != nil {
NewFaliResponse(http.StatusBadRequest, "request body error."+err.Error(), "读取请求数据错误,数据不合法", response)
return
}
if node.UUID == "" {
NewFaliResponse(http.StatusBadRequest, "node uuid cannot be empty", "节点UUID不能为空", response)
return
}
if node.HostName == "" {
NewFaliResponse(http.StatusBadRequest, "node host name cannot be empty", "节点主机名不能为空", response)
return
}
if node.InternalIP == "" {
NewFaliResponse(http.StatusBadRequest, "node host ip cannot be empty", "节点内网ip不能为空", response)
return
}
if node.ExternalIP == "" {
NewFaliResponse(http.StatusBadRequest, "node host ip cannot be empty", "节点外网ip不能为空", response)
return
}
var create bool
var status v1.NodeStatus
// if node already init
if node.Status == "init" && node.Role == "tree" {
k8sNode, err := createK8sNode(node)
if err != nil {
NewFaliResponse(400, err.Error(), "创建集群节点失败", response)
return
}
v1Node, err := u.clientSet.Core().Nodes().Create(k8sNode)
if err != nil {
if !apierrors.IsAlreadyExists(err) {
NewFaliResponse(400, err.Error(), "集群节点已存在", response)
return
}
NewFaliResponse(400, err.Error(), "创建集群节点失败", response)
return
}
status = v1Node.Status
node.Status = "running"
create = true
}
err = u.apiStoreManager.UpdateSource("/store/nodes/"+node.HostName, node)
if err != nil {
if create {
if err := u.clientSet.Core().Nodes().Delete(node.HostName, nil); err != nil {
logrus.Errorf("Unable to register node %q to etcd: error deleting old node: %v", node.HostName, err)
} else {
logrus.Errorf("Deleted old node object %q", node.HostName)
}
}
if cerr, ok := err.(client.Error); ok {
if cerr.Code == client.ErrorCodeNodeExist {
NewFaliResponse(400, "node is exist.", "节点已存在", response)
return
}
}
NewFaliResponse(500, err.Error(), "存储节点信息失败", response)
return
}
node.NodeStatus = &status
NewSuccessResponse(node, nil, response)
}
func (u *NodeSource) getNodes(request *restful.Request, response *restful.Response) {
list, err := u.apiStoreManager.GetSourceList("/store/nodes", "host_node")
if err != nil {
if client.IsKeyNotFound(err) {
NewFaliResponse(404, "not found nodes", "节点不存在", response)
return
}
NewFaliResponse(500, err.Error(), "查询节点错误", response)
return
}
NewSuccessResponse(nil, list, response)
}
func (u *NodeSource) getNodeInfo(request *restful.Request, response *restful.Response) {
hostName := request.PathParameter("host_name")
if hostName == "" {
NewFaliResponse(400, "host name can not be empty", "节点的主机名不能为空", response)
return
}
node := model.HostNode{}
err := u.apiStoreManager.GetSource("/store/nodes/"+hostName, &node)
if err != nil {
if client.IsKeyNotFound(err) {
NewFaliResponse(404, "host name not found", "节点的主机名不存在", response)
return
}
NewFaliResponse(500, err.Error(), "获取节点数据错误", response)
return
}
k8sNode, err := u.clientSet.Core().Nodes().Get(hostName, metav1.GetOptions{})
if err == nil {
node.NodeStatus = &k8sNode.Status
NewSuccessResponse(node, nil, response)
} else {
NewSuccessMessageResponse(node, nil, err.Error(), "获取节点运行状态错误", response)
}
}
func createK8sNode(node *model.HostNode) (*v1.Node, error) {
cpu, err := resource.ParseQuantity(fmt.Sprintf("%dm", node.AvailableCPU*1000))
if err != nil {
return nil, err
}
mem, err := resource.ParseQuantity(fmt.Sprintf("%dKi", node.AvailableMemory*1024*1024))
if err != nil {
return nil, err
}
nameAddress := v1.NodeAddress{
Type: v1.NodeHostName,
Address: node.HostName,
}
internalIP := v1.NodeAddress{
Type: v1.NodeInternalIP,
Address: node.InternalIP,
}
externalIP := v1.NodeAddress{
Type: v1.NodeExternalIP,
Address: node.ExternalIP,
}
k8sNode := &v1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: node.HostName,
Labels: node.Labels,
},
Spec: v1.NodeSpec{
Unschedulable: node.Unschedulable,
},
Status: v1.NodeStatus{
Allocatable: v1.ResourceList{v1.ResourceCPU: cpu, v1.ResourceMemory: mem},
Addresses: []v1.NodeAddress{nameAddress, internalIP, externalIP},
},
}
return k8sNode, nil
}