Skip to content

Commit 5a164ad

Browse files
committed
operator: modify service accounts and role bindings to be shared
Additional objects are shared between device plugin CRs. Once the last CR is removed, the additional objects are also removed. Signed-off-by: Tuomas Katila <[email protected]> asdf Signed-off-by: Tuomas Katila <[email protected]>
1 parent 162ce7d commit 5a164ad

File tree

3 files changed

+192
-204
lines changed

3 files changed

+192
-204
lines changed

pkg/controllers/gpu/controller.go

Lines changed: 51 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package gpu
1717

1818
import (
1919
"context"
20+
"encoding/json"
2021
"reflect"
2122
"strconv"
2223
"strings"
@@ -37,9 +38,9 @@ import (
3738
)
3839

3940
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"
4344
)
4445

4546
var defaultNodeSelector = deployments.GPUPluginDaemonSet().Spec.Template.Spec.NodeSelector
@@ -76,48 +77,61 @@ func (c *controller) Upgrade(ctx context.Context, obj client.Object) bool {
7677
return controllers.UpgradeImages(ctx, &dp.Spec.Image, &dp.Spec.InitImage)
7778
}
7879

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,
8599
Namespace: c.ns,
86100
},
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+
},
90107
}
108+
}
91109

92-
return nil
110+
func (c *controller) PluginMayRequireSharedObjects() bool {
111+
return true
93112
}
94113

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
115127
}
116128

117-
return &rb
129+
if cr.Spec.ResourceManager {
130+
return true
131+
}
118132
}
119133

120-
return nil
134+
return false
121135
}
122136

123137
func (c *controller) NewDaemonSet(rawObj client.Object) *apps.DaemonSet {
@@ -143,7 +157,7 @@ func (c *controller) NewDaemonSet(rawObj client.Object) *apps.DaemonSet {
143157

144158
// add service account if resource manager is enabled
145159
if devicePlugin.Spec.ResourceManager {
146-
daemonSet.Spec.Template.Spec.ServiceAccountName = prefixedName(serviceAccountPrefix, devicePlugin.Name)
160+
daemonSet.Spec.Template.Spec.ServiceAccountName = serviceAccountName
147161
addVolumeIfMissing(&daemonSet.Spec.Template.Spec, "podresources", "/var/lib/kubelet/pod-resources", v1.HostPathDirectory)
148162
addVolumeMountIfMissing(&daemonSet.Spec.Template.Spec, "podresources", "/var/lib/kubelet/pod-resources", false)
149163
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) (
324338

325339
newServiceAccountName := "default"
326340
if dp.Spec.ResourceManager {
327-
newServiceAccountName = prefixedName(serviceAccountPrefix, dp.Name)
341+
newServiceAccountName = serviceAccountName
328342
}
329343

330344
if ds.Spec.Template.Spec.ServiceAccountName != newServiceAccountName {

pkg/controllers/gpu/controller_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func (c *controller) newDaemonSetExpected(rawObj client.Object) *apps.DaemonSet
146146

147147
// add service account if resource manager is enabled
148148
if devicePlugin.Spec.ResourceManager {
149-
daemonSet.Spec.Template.Spec.ServiceAccountName = serviceAccountPrefix + "-" + devicePlugin.Name
149+
daemonSet.Spec.Template.Spec.ServiceAccountName = serviceAccountName
150150

151151
addVolumeIfMissing(&daemonSet.Spec.Template.Spec, "podresources", "/var/lib/kubelet/pod-resources", v1.HostPathDirectory)
152152
addVolumeMountIfMissing(&daemonSet.Spec.Template.Spec, "podresources", "/var/lib/kubelet/pod-resources", false)
@@ -169,7 +169,7 @@ func (c *controller) updateDaemonSetExpected(rawObj client.Object, ds *apps.Daem
169169
hadRM := strings.Contains(argString, "-resource-manager")
170170

171171
if !hadRM && dp.Spec.ResourceManager {
172-
ds.Spec.Template.Spec.ServiceAccountName = serviceAccountPrefix + "-" + dp.Name
172+
ds.Spec.Template.Spec.ServiceAccountName = serviceAccountName
173173

174174
addVolumeIfMissing(&ds.Spec.Template.Spec, "podresources", "/var/lib/kubelet/pod-resources", v1.HostPathDirectory)
175175
addVolumeMountIfMissing(&ds.Spec.Template.Spec, "podresources", "/var/lib/kubelet/pod-resources", false)

0 commit comments

Comments
 (0)