forked from OlfillasOdikno/generic-cdi-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
210 lines (181 loc) · 5.21 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"log"
"sync"
"time"
"github.com/google/uuid"
"github.com/kubevirt/device-plugin-manager/pkg/dpm"
"google.golang.org/grpc"
pluginapi "k8s.io/kubelet/pkg/apis/deviceplugin/v1beta1"
podresourcesv1 "k8s.io/kubelet/pkg/apis/podresources/v1"
"k8s.io/kubernetes/pkg/kubelet/apis/podresources"
"tags.cncf.io/container-device-interface/pkg/cdi"
)
type GenericCDIPlugin struct {
resource string
kind string
update chan interface{}
stop chan interface{}
client podresourcesv1.PodResourcesListerClient
conn *grpc.ClientConn
mu sync.Mutex
devices []*pluginapi.Device
}
func (dp *GenericCDIPlugin) printf(format string, v ...any) {
s := fmt.Sprintf(format, v...)
log.Printf("%s=%s: %s", dp.kind, dp.resource, s)
}
func (dp *GenericCDIPlugin) createDevice() {
id := uuid.New()
dp.devices = append(dp.devices, &pluginapi.Device{
ID: id.String(),
Health: pluginapi.Healthy,
})
dp.printf("created new device: %s", id.String())
}
func (dp *GenericCDIPlugin) collectGarbage() {
resource := fmt.Sprintf("%s-%s", dp.kind, dp.resource)
dp.mu.Lock()
dp.printf("collecting garbage...")
start := time.Now()
resp, err := dp.client.List(context.Background(), &podresourcesv1.ListPodResourcesRequest{})
if err != nil {
log.Fatalf("Failed to list pod resources: %v", err)
}
newDevices := []*pluginapi.Device{}
for _, res := range resp.PodResources {
for _, cont := range res.Containers {
for _, dev := range cont.Devices {
if dev.ResourceName != resource {
continue
}
for _, deviceID := range dev.DeviceIds {
newDevices = append(newDevices, &pluginapi.Device{
ID: deviceID,
Health: pluginapi.Healthy,
})
}
}
}
}
dp.devices = newDevices
dp.createDevice()
duration := time.Since(start)
dp.printf("garbage collection took %v seconds", duration.Seconds())
dp.mu.Unlock()
dp.update <- true
}
func (dp *GenericCDIPlugin) Allocate(ctx context.Context, r *pluginapi.AllocateRequest) (*pluginapi.AllocateResponse, error) {
responses := &pluginapi.AllocateResponse{}
for _, req := range r.ContainerRequests {
devices := []*pluginapi.CDIDevice{}
for _, id := range req.DevicesIDs {
dp.printf("got Allocate request: %s", id)
devices = append(devices, &pluginapi.CDIDevice{
Name: fmt.Sprintf("%s=%s", dp.kind, dp.resource),
})
}
responses.ContainerResponses = append(responses.ContainerResponses, &pluginapi.ContainerAllocateResponse{
CDIDevices: devices,
})
}
dp.mu.Lock()
dp.createDevice()
dp.mu.Unlock()
dp.update <- true
return responses, nil
}
func (*GenericCDIPlugin) GetDevicePluginOptions(context.Context, *pluginapi.Empty) (*pluginapi.DevicePluginOptions, error) {
return &pluginapi.DevicePluginOptions{
PreStartRequired: false,
GetPreferredAllocationAvailable: false,
}, nil
}
func (*GenericCDIPlugin) GetPreferredAllocation(context.Context, *pluginapi.PreferredAllocationRequest) (*pluginapi.PreferredAllocationResponse, error) {
return &pluginapi.PreferredAllocationResponse{}, nil
}
func (dp *GenericCDIPlugin) ListAndWatch(e *pluginapi.Empty, s pluginapi.DevicePlugin_ListAndWatchServer) error {
dp.printf("listening...")
for {
s.Send(&pluginapi.ListAndWatchResponse{
Devices: dp.devices,
})
select {
case <-dp.stop:
dp.printf("stopping.")
return nil
case <-dp.update:
continue
}
}
}
func (*GenericCDIPlugin) PreStartContainer(context.Context, *pluginapi.PreStartContainerRequest) (*pluginapi.PreStartContainerResponse, error) {
return &pluginapi.PreStartContainerResponse{}, nil
}
func (dp *GenericCDIPlugin) Start() error {
dp.createDevice()
go func(dp *GenericCDIPlugin) {
for {
select {
case <-dp.stop:
dp.printf("stopping garbage collector.")
return
case <-time.After(30 * time.Second):
dp.collectGarbage()
}
}
}(dp)
return nil
}
func (dp *GenericCDIPlugin) Stop() error {
dp.stop <- true
dp.conn.Close()
return nil
}
type GenericCDIPluginLister struct {
spec *cdi.Spec
}
func (l *GenericCDIPluginLister) Discover(pluginListCh chan dpm.PluginNameList) {
plugins := dpm.PluginNameList{}
for _, device := range l.spec.Devices {
plugins = append(plugins, fmt.Sprintf("%s-%s", l.spec.GetClass(), device.Name))
}
pluginListCh <- plugins
}
func (l *GenericCDIPluginLister) GetResourceNamespace() string {
return l.spec.GetVendor()
}
func (l *GenericCDIPluginLister) NewPlugin(name string) dpm.PluginInterface {
resource := name[len(l.spec.GetClass())+1:]
log.Printf("Registering plugin for %s=%s", l.spec.Kind, resource)
client, conn, err := podresources.GetV1Client("unix:///var/lib/kubelet/pod-resources/kubelet.sock", 60*time.Second, 1024*1024)
if err != nil {
log.Fatalf("Failed to connect to pod-resources kubelet: %v", err)
}
return &GenericCDIPlugin{
resource: resource,
kind: l.spec.Kind,
update: make(chan interface{}),
stop: make(chan interface{}),
client: client,
conn: conn,
devices: []*pluginapi.Device{},
}
}
func main() {
flag.Parse()
if flag.NArg() != 1 {
log.Fatal("No path to CDI JSON provided. Exiting.")
}
cdiJSON := flag.Arg(0)
spec, err := cdi.ReadSpec(cdiJSON, 0)
if err != nil {
panic(err)
}
dpm.NewManager(&GenericCDIPluginLister{
spec: spec,
}).Run()
}