-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
305 lines (264 loc) · 9.17 KB
/
server.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
/*
Copyright @ 2021 fushaosong <fushaosong@beyondlet.com>.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package deviceplugin
import (
"github.com/bocloud/carina/pkg/devicemanager/types"
"github.com/bocloud/carina/pkg/devicemanager/volume"
"github.com/bocloud/carina/pkg/deviceplugin/v1beta1"
"github.com/bocloud/carina/utils"
"github.com/bocloud/carina/utils/log"
"fmt"
"net"
"os"
"path"
"strings"
"time"
"golang.org/x/net/context"
"google.golang.org/grpc"
//pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
)
// CarinaDevicePlugin implements the Kubernetes device plugin API
type CarinaDevicePlugin struct {
resourceName string
socket string
volumeManager volume.LocalVolume
server *grpc.Server
update chan struct{}
stop chan interface{}
}
// NewCarinaDevicePlugin returns an initialized CarinaDevicePlugin
func NewCarinaDevicePlugin(resourceName string, volumeManager volume.LocalVolume, update chan struct{}, socket string) *CarinaDevicePlugin {
return &CarinaDevicePlugin{
resourceName: resourceName,
volumeManager: volumeManager,
socket: socket,
// These will be reinitialized every
// time the plugin server is restarted.
server: nil,
update: update,
stop: nil,
}
}
func (dp *CarinaDevicePlugin) cleanup() {
close(dp.stop)
close(dp.update)
dp.server = nil
dp.update = nil
dp.stop = nil
}
// Start starts the gRPC server, registers the device plugin with the Kubelet,
// and starts the device healthchecks.
func (dp *CarinaDevicePlugin) Start() error {
dp.server = grpc.NewServer([]grpc.ServerOption{}...)
dp.stop = make(chan interface{})
err := dp.Serve()
if err != nil {
log.Errorf("Could not start device plugin for '%s': %s", dp.resourceName, err)
dp.cleanup()
return err
}
log.Infof("Starting to serve '%s' on %s", dp.resourceName, dp.socket)
err = dp.Register()
if err != nil {
log.Errorf("Could not register device plugin: %s", err)
_ = dp.Stop()
return err
}
log.Infof("Registered device plugin for '%s' with Kubelet", dp.resourceName)
return nil
}
// Stop stops the gRPC server.
func (dp *CarinaDevicePlugin) Stop() error {
if dp == nil || dp.server == nil {
return nil
}
log.Infof("Stopping to serve '%s' on %s", dp.resourceName, dp.socket)
dp.server.Stop()
if err := os.Remove(dp.socket); err != nil && !os.IsNotExist(err) {
return err
}
dp.cleanup()
return nil
}
// Serve starts the gRPC server of the device plugin.
func (dp *CarinaDevicePlugin) Serve() error {
_ = os.Remove(dp.socket)
sock, err := net.Listen("unix", dp.socket)
if err != nil {
return err
}
v1beta1.RegisterDevicePluginServer(dp.server, dp)
go func() {
lastCrashTime := time.Now()
restartCount := 0
for {
log.Infof("Starting GRPC server for '%s'", dp.resourceName)
err := dp.server.Serve(sock)
if err == nil {
break
}
log.Infof("GRPC server for '%s' crashed with error: %v", dp.resourceName, err)
// restart if it has not been too often
// i.e. if server has crashed more than 5 times and it didn't last more than one hour each time
if restartCount > 5 {
// quit
log.Fatalf("GRPC server for '%s' has repeatedly crashed recently. Quitting", dp.resourceName)
}
timeSinceLastCrash := time.Since(lastCrashTime).Seconds()
lastCrashTime = time.Now()
if timeSinceLastCrash > 3600 {
// it has been one hour since the last crash.. reset the count
// to reflect on the frequency
restartCount = 1
} else {
restartCount++
}
}
}()
// Wait for server to start by launching a blocking connexion
conn, err := dp.dial(dp.socket, 5*time.Second)
if err != nil {
return err
}
_ = conn.Close()
return nil
}
// Register registers the device plugin for the given resourceName with Kubelet.
func (dp *CarinaDevicePlugin) Register() error {
conn, err := dp.dial(v1beta1.KubeletSocket, 5*time.Second)
if err != nil {
return err
}
defer conn.Close()
client := v1beta1.NewRegistrationClient(conn)
reqt := &v1beta1.RegisterRequest{
Version: v1beta1.Version,
Endpoint: path.Base(dp.socket),
ResourceName: dp.resourceName,
Options: &v1beta1.DevicePluginOptions{
GetPreferredAllocationAvailable: true,
},
}
_, err = client.Register(context.Background(), reqt)
if err != nil {
return err
}
return nil
}
// GetDevicePluginOptions returns the values of the optional settings for this plugin
func (dp *CarinaDevicePlugin) GetDevicePluginOptions(context.Context, *v1beta1.Empty) (*v1beta1.DevicePluginOptions, error) {
options := &v1beta1.DevicePluginOptions{
GetPreferredAllocationAvailable: true,
}
return options, nil
}
// ListAndWatch lists devices and update that list according to the health status
func (dp *CarinaDevicePlugin) ListAndWatch(e *v1beta1.Empty, s v1beta1.DevicePlugin_ListAndWatchServer) error {
request, err := dp.getDeviceCapacity()
if err != nil {
log.Errorf("get device capacity error: %s", err.Error())
return err
}
_ = s.Send(&v1beta1.ListAndWatchResponse{Devices: request})
for {
select {
case <-dp.stop:
return nil
case <-dp.update:
request, err := dp.getDeviceCapacity()
if err != nil {
log.Errorf("get device capacity error: %s", err.Error())
return err
}
log.Infof("update device capacity: %s", dp.resourceName)
_ = s.Send(&v1beta1.ListAndWatchResponse{Devices: request})
}
}
}
// GetPreferredAllocation returns the preferred allocation from the set of devices specified in the request
func (m *CarinaDevicePlugin) GetPreferredAllocation(ctx context.Context, r *v1beta1.PreferredAllocationRequest) (*v1beta1.PreferredAllocationResponse, error) {
response := &v1beta1.PreferredAllocationResponse{}
for _, req := range r.ContainerRequests {
resp := &v1beta1.ContainerPreferredAllocationResponse{
DeviceIDs: req.MustIncludeDeviceIDs,
}
response.ContainerResponses = append(response.ContainerResponses, resp)
}
return response, nil
}
// Allocate which return list of devices.
func (m *CarinaDevicePlugin) Allocate(ctx context.Context, reqs *v1beta1.AllocateRequest) (*v1beta1.AllocateResponse, error) {
responses := v1beta1.AllocateResponse{}
for _, req := range reqs.ContainerRequests {
log.Infof("ignore implement %s", req.DevicesIDs)
response := v1beta1.ContainerAllocateResponse{}
responses.ContainerResponses = append(responses.ContainerResponses, &response)
}
return &responses, nil
}
// PreStartContainer is unimplemented for this plugin
func (m *CarinaDevicePlugin) PreStartContainer(context.Context, *v1beta1.PreStartContainerRequest) (*v1beta1.PreStartContainerResponse, error) {
return &v1beta1.PreStartContainerResponse{}, nil
}
// dial establishes the gRPC communication with the registered device plugin.
func (m *CarinaDevicePlugin) dial(unixSocketPath string, timeout time.Duration) (*grpc.ClientConn, error) {
ctx, _ := context.WithTimeout(context.Background(), timeout)
c, err := grpc.DialContext(ctx, unixSocketPath, grpc.WithInsecure(), grpc.WithBlock(),
grpc.WithContextDialer(func(i context.Context, addr string) (conn net.Conn, e error) {
return net.DialTimeout("unix", addr, timeout)
}))
if err != nil {
return nil, err
}
return c, nil
}
func (dp *CarinaDevicePlugin) getDeviceCapacity() ([]*v1beta1.Device, error) {
var pdevs []*v1beta1.Device
vgs, err := dp.volumeManager.GetCurrentVgStruct()
if err != nil {
return pdevs, err
}
// 对于此种存储设备实际上不适合采用device plugin模式
// device plugin 通过ListAndWatch方法上报数据只包含设备ID,而对于存储来说,只上报一个设备ID显然不满足我们需求,我们需要总容量及可用量
// 关于Kubelet如何计算设备数量参考,kubernetes/pkg/kubelet/cm/devicemanager/manager.go:GetCapacity():536
// Kubelet实质只是获取一下设备数量len(v1betal.Device),就像8个gpu
// 基于此需要展示我们的黑科技了,基于容量构建一个数组,为了避免数组太大,以G为单位
var capacity types.VgGroup
for _, v := range vgs {
if strings.HasSuffix(dp.resourceName, v.VGName) {
capacity = v
}
}
if capacity.VGName == "" {
return pdevs, nil
}
sizeGb := capacity.VGSize>>30 + 1
freeGb := uint64(0)
if capacity.VGFree > utils.DefaultReservedSpace {
freeGb = (capacity.VGFree - utils.DefaultReservedSpace) >> 30
}
// Capacity 这个是设备总资源数
// Allocatable 这个是资源可使用数,调度器使用这个指标,它的值是总量-预留-已使用
// 我们将已经使用的磁盘容量标记为unhealthy状态,如此在Node信息中看到allocatable在不断减少
for i := uint64(1); i <= sizeGb; i++ {
health := v1beta1.Healthy
if i > freeGb {
health = v1beta1.Unhealthy
}
pdevs = append(pdevs, &v1beta1.Device{
ID: fmt.Sprintf("%d", i),
Health: health,
})
}
return pdevs, nil
}