forked from kubernetes-sigs/multi-network
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdriver.go
More file actions
176 lines (151 loc) · 5.1 KB
/
driver.go
File metadata and controls
176 lines (151 loc) · 5.1 KB
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
package dra
import (
"context"
"fmt"
"os"
"path/filepath"
"time"
resourcev1alpha3 "k8s.io/api/resource/v1alpha3"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/dynamic-resource-allocation/kubeletplugin"
"k8s.io/klog/v2"
drapb "k8s.io/kubelet/pkg/apis/dra/v1alpha4"
)
type PodResourceStore interface {
Add(podUID types.UID, allocation *resourcev1alpha3.ResourceClaim)
}
type Driver struct {
driverName string
kubeClient kubernetes.Interface
draPlugin kubeletplugin.DRAPlugin
podResourceStore PodResourceStore
}
func Start(
ctx context.Context,
driverName string,
nodeName string,
kubeClient kubernetes.Interface,
podResourceStore PodResourceStore,
) (*Driver, error) {
d := &Driver{
driverName: driverName,
kubeClient: kubeClient,
podResourceStore: podResourceStore,
}
pluginRegistrationPath := filepath.Join("/var/lib/kubelet/plugins_registry/", fmt.Sprintf("%s.sock", driverName))
driverPluginPath := filepath.Join("/var/lib/kubelet/plugins/", driverName)
err := os.MkdirAll(driverPluginPath, 0750)
if err != nil {
return nil, fmt.Errorf("failed to create plugin path %s: %v", driverPluginPath, err)
}
driverPluginSocketPath := filepath.Join(driverPluginPath, "plugin.sock")
opts := []kubeletplugin.Option{
kubeletplugin.DriverName(driverName),
kubeletplugin.NodeName(nodeName),
kubeletplugin.KubeClient(kubeClient),
kubeletplugin.RegistrarSocketPath(pluginRegistrationPath),
kubeletplugin.PluginSocketPath(driverPluginSocketPath),
kubeletplugin.KubeletPluginSocketPath(driverPluginSocketPath),
}
driver, err := kubeletplugin.Start(ctx, d, opts...)
if err != nil {
return nil, fmt.Errorf("start kubelet plugin: %w", err)
}
d.draPlugin = driver
err = wait.PollUntilContextTimeout(ctx, 1*time.Second, 30*time.Second, true, func(context.Context) (bool, error) {
status := d.draPlugin.RegistrationStatus()
if status == nil {
return false, nil
}
return status.PluginRegistered, nil
})
if err != nil {
return nil, err
}
return d, nil
}
func (d *Driver) Stop() {
if d.draPlugin != nil {
d.draPlugin.Stop()
}
}
func (d *Driver) NodePrepareResources(ctx context.Context, request *drapb.NodePrepareResourcesRequest) (*drapb.NodePrepareResourcesResponse, error) {
if request == nil {
return nil, nil
}
resp := &drapb.NodePrepareResourcesResponse{
Claims: make(map[string]*drapb.NodePrepareResourceResponse),
}
for uid, claimReq := range request.GetClaims() {
klog.Infof("NodePrepareResources: Claim Request (%d) %#v", uid, claimReq)
devices, err := d.nodePrepareResource(ctx, claimReq)
if err != nil {
resp.Claims[claimReq.UID] = &drapb.NodePrepareResourceResponse{
Error: err.Error(),
}
} else {
resp.Claims[claimReq.UID] = &drapb.NodePrepareResourceResponse{
Devices: devices,
}
}
}
return resp, nil
}
func (d *Driver) nodePrepareResource(ctx context.Context, claimReq *drapb.Claim) ([]*drapb.Device, error) {
// The plugin must retrieve the claim itself to get it in the version that it understands.
claim, err := d.kubeClient.ResourceV1alpha3().ResourceClaims(claimReq.Namespace).Get(ctx, claimReq.Name, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("retrieve claim %s/%s: %w", claimReq.Namespace, claimReq.Name, err)
}
if claim.Status.Allocation == nil {
return nil, fmt.Errorf("claim %s/%s not allocated", claimReq.Namespace, claimReq.Name)
}
if claim.UID != types.UID(claim.UID) {
return nil, fmt.Errorf("claim %s/%s got replaced", claimReq.Namespace, claimReq.Name)
}
for _, reserved := range claim.Status.ReservedFor {
if reserved.Resource != "pods" || reserved.APIGroup != "" {
klog.Infof("claim reference unsupported for %#v", reserved)
continue
}
klog.Infof("nodePrepareResource: Claim Request (%s) reserved for pod %s (%s)", claimReq.UID, reserved.Name, reserved.UID)
d.podResourceStore.Add(reserved.UID, claim)
}
var devices []*drapb.Device
for _, result := range claim.Status.Allocation.Devices.Results {
device := &drapb.Device{
PoolName: result.Pool,
DeviceName: result.Device,
}
devices = append(devices, device)
}
klog.Infof("nodePrepareResource: Devices for Claim Request (%s) %#v", claimReq.UID, devices)
return devices, nil
}
func (d *Driver) NodeUnprepareResources(ctx context.Context, request *drapb.NodeUnprepareResourcesRequest) (*drapb.NodeUnprepareResourcesResponse, error) {
if request == nil {
return nil, nil
}
resp := &drapb.NodeUnprepareResourcesResponse{
Claims: make(map[string]*drapb.NodeUnprepareResourceResponse),
}
for _, claimReq := range request.Claims {
err := d.nodeUnprepareResource(ctx, claimReq)
if err != nil {
klog.Infof("error unpreparing ressources for claim %s/%s : %v", claimReq.Namespace, claimReq.Name, err)
resp.Claims[claimReq.UID] = &drapb.NodeUnprepareResourceResponse{
Error: err.Error(),
}
} else {
resp.Claims[claimReq.UID] = &drapb.NodeUnprepareResourceResponse{}
}
}
return resp, nil
}
func (d *Driver) nodeUnprepareResource(_ context.Context, _ *drapb.Claim) error {
// TODO
return nil
}