@@ -17,6 +17,7 @@ package gpu
17
17
18
18
import (
19
19
"context"
20
+ "encoding/json"
20
21
"reflect"
21
22
"strconv"
22
23
"strings"
@@ -37,9 +38,9 @@ import (
37
38
)
38
39
39
40
const (
40
- ownerKey = ".metadata.controller.gpu"
41
- serviceAccountPrefix = "gpu-manager-sa"
42
- roleBindingPrefix = "gpu-manager-rolebinding"
41
+ ownerKey = ".metadata.controller.gpu"
42
+ serviceAccountName = "gpu-manager-sa"
43
+ roleBindingName = "gpu-manager-rolebinding"
43
44
)
44
45
45
46
var defaultNodeSelector = deployments .GPUPluginDaemonSet ().Spec .Template .Spec .NodeSelector
@@ -76,48 +77,61 @@ func (c *controller) Upgrade(ctx context.Context, obj client.Object) bool {
76
77
return controllers .UpgradeImages (ctx , & dp .Spec .Image , & dp .Spec .InitImage )
77
78
}
78
79
79
- func (c * controller ) NewServiceAccount (rawObj client.Object ) * v1.ServiceAccount {
80
- devicePlugin := rawObj .(* devicepluginv1.GpuDevicePlugin )
81
- if devicePlugin .Spec .ResourceManager {
82
- sa := v1.ServiceAccount {
83
- ObjectMeta : metav1.ObjectMeta {
84
- Name : prefixedName (serviceAccountPrefix , devicePlugin .Name ),
80
+ func (c * controller ) NewSharedServiceAccount () * v1.ServiceAccount {
81
+ return & v1.ServiceAccount {
82
+ ObjectMeta : metav1.ObjectMeta {
83
+ Name : serviceAccountName ,
84
+ Namespace : c .ns ,
85
+ },
86
+ }
87
+ }
88
+
89
+ func (c * controller ) NewSharedClusterRoleBinding () * rbacv1.ClusterRoleBinding {
90
+ return & rbacv1.ClusterRoleBinding {
91
+ ObjectMeta : metav1.ObjectMeta {
92
+ Name : roleBindingName ,
93
+ Namespace : c .ns ,
94
+ },
95
+ Subjects : []rbacv1.Subject {
96
+ {
97
+ Kind : "ServiceAccount" ,
98
+ Name : serviceAccountName ,
85
99
Namespace : c .ns ,
86
100
},
87
- }
88
-
89
- return & sa
101
+ },
102
+ RoleRef : rbacv1.RoleRef {
103
+ Kind : "ClusterRole" ,
104
+ Name : "inteldeviceplugins-gpu-manager-role" ,
105
+ APIGroup : "rbac.authorization.k8s.io" ,
106
+ },
90
107
}
108
+ }
91
109
92
- return nil
110
+ func (c * controller ) PluginMayRequireSharedObjects () bool {
111
+ return true
93
112
}
94
113
95
- func (c * controller ) NewClusterRoleBinding (rawObj client.Object ) * rbacv1.ClusterRoleBinding {
96
- devicePlugin := rawObj .(* devicepluginv1.GpuDevicePlugin )
97
- if devicePlugin .Spec .ResourceManager {
98
- rb := rbacv1.ClusterRoleBinding {
99
- ObjectMeta : metav1.ObjectMeta {
100
- Name : prefixedName (roleBindingPrefix , devicePlugin .Name ),
101
- Namespace : c .ns ,
102
- },
103
- Subjects : []rbacv1.Subject {
104
- {
105
- Kind : "ServiceAccount" ,
106
- Name : prefixedName (serviceAccountPrefix , devicePlugin .Name ),
107
- Namespace : c .ns ,
108
- },
109
- },
110
- RoleRef : rbacv1.RoleRef {
111
- Kind : "ClusterRole" ,
112
- Name : "inteldeviceplugins-gpu-manager-role" ,
113
- APIGroup : "rbac.authorization.k8s.io" ,
114
- },
114
+ func (c * controller ) PluginRequiresSharedObjects (rawObjects []map [string ]interface {}) bool {
115
+ for _ , obj := range rawObjects {
116
+ cr := devicepluginv1.GpuDevicePlugin {}
117
+
118
+ // Convert map of interfaces..
119
+ jsonObj , err := json .Marshal (obj )
120
+ if err != nil {
121
+ return false
122
+ }
123
+
124
+ // to the device plugin struct.
125
+ if err := json .Unmarshal (jsonObj , & cr ); err != nil {
126
+ return false
115
127
}
116
128
117
- return & rb
129
+ if cr .Spec .ResourceManager {
130
+ return true
131
+ }
118
132
}
119
133
120
- return nil
134
+ return false
121
135
}
122
136
123
137
func (c * controller ) NewDaemonSet (rawObj client.Object ) * apps.DaemonSet {
@@ -143,7 +157,7 @@ func (c *controller) NewDaemonSet(rawObj client.Object) *apps.DaemonSet {
143
157
144
158
// add service account if resource manager is enabled
145
159
if devicePlugin .Spec .ResourceManager {
146
- daemonSet .Spec .Template .Spec .ServiceAccountName = prefixedName ( serviceAccountPrefix , devicePlugin . Name )
160
+ daemonSet .Spec .Template .Spec .ServiceAccountName = serviceAccountName
147
161
addVolumeIfMissing (& daemonSet .Spec .Template .Spec , "podresources" , "/var/lib/kubelet/pod-resources" , v1 .HostPathDirectory )
148
162
addVolumeMountIfMissing (& daemonSet .Spec .Template .Spec , "podresources" , "/var/lib/kubelet/pod-resources" , false )
149
163
addVolumeIfMissing (& daemonSet .Spec .Template .Spec , "kubeletcrt" , "/var/lib/kubelet/pki/kubelet.crt" , v1 .HostPathFileOrCreate )
@@ -324,7 +338,7 @@ func (c *controller) UpdateDaemonSet(rawObj client.Object, ds *apps.DaemonSet) (
324
338
325
339
newServiceAccountName := "default"
326
340
if dp .Spec .ResourceManager {
327
- newServiceAccountName = prefixedName ( serviceAccountPrefix , dp . Name )
341
+ newServiceAccountName = serviceAccountName
328
342
}
329
343
330
344
if ds .Spec .Template .Spec .ServiceAccountName != newServiceAccountName {
0 commit comments