Skip to content

Commit 6d9e968

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]>
1 parent f9221c4 commit 6d9e968

File tree

3 files changed

+136
-205
lines changed

3 files changed

+136
-205
lines changed

pkg/controllers/gpu/controller.go

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ import (
3737
)
3838

3939
const (
40-
ownerKey = ".metadata.controller.gpu"
41-
serviceAccountPrefix = "gpu-manager-sa"
42-
roleBindingPrefix = "gpu-manager-rolebinding"
40+
ownerKey = ".metadata.controller.gpu"
41+
serviceAccountName = "gpu-manager-sa"
42+
roleBindingName = "gpu-manager-rolebinding"
4343
)
4444

4545
var defaultNodeSelector = deployments.GPUPluginDaemonSet().Spec.Template.Spec.NodeSelector
@@ -76,48 +76,54 @@ func (c *controller) Upgrade(ctx context.Context, obj client.Object) bool {
7676
return controllers.UpgradeImages(ctx, &dp.Spec.Image, &dp.Spec.InitImage)
7777
}
7878

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),
79+
func (c *controller) NewSharedServiceAccount() *v1.ServiceAccount {
80+
return &v1.ServiceAccount{
81+
ObjectMeta: metav1.ObjectMeta{
82+
Name: serviceAccountName,
83+
Namespace: c.ns,
84+
},
85+
}
86+
}
87+
88+
func (c *controller) NewSharedClusterRoleBinding() *rbacv1.ClusterRoleBinding {
89+
return &rbacv1.ClusterRoleBinding{
90+
ObjectMeta: metav1.ObjectMeta{
91+
Name: roleBindingName,
92+
Namespace: c.ns,
93+
},
94+
Subjects: []rbacv1.Subject{
95+
{
96+
Kind: "ServiceAccount",
97+
Name: serviceAccountName,
8598
Namespace: c.ns,
8699
},
87-
}
88-
89-
return &sa
100+
},
101+
RoleRef: rbacv1.RoleRef{
102+
Kind: "ClusterRole",
103+
Name: "inteldeviceplugins-gpu-manager-role",
104+
APIGroup: "rbac.authorization.k8s.io",
105+
},
90106
}
107+
}
91108

92-
return nil
109+
func (c *controller) PluginMayRequireSharedObjects() bool {
110+
return true
93111
}
94112

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-
},
115-
}
113+
func (c *controller) PluginRequiresSharedObjects(ctx context.Context, client client.Client) bool {
114+
var list devicepluginv1.GpuDevicePluginList
116115

117-
return &rb
116+
if err := client.List(ctx, &list); err != nil {
117+
return false
118118
}
119119

120-
return nil
120+
for _, cr := range list.Items {
121+
if cr.Spec.ResourceManager {
122+
return true
123+
}
124+
}
125+
126+
return false
121127
}
122128

123129
func (c *controller) NewDaemonSet(rawObj client.Object) *apps.DaemonSet {
@@ -143,7 +149,7 @@ func (c *controller) NewDaemonSet(rawObj client.Object) *apps.DaemonSet {
143149

144150
// add service account if resource manager is enabled
145151
if devicePlugin.Spec.ResourceManager {
146-
daemonSet.Spec.Template.Spec.ServiceAccountName = prefixedName(serviceAccountPrefix, devicePlugin.Name)
152+
daemonSet.Spec.Template.Spec.ServiceAccountName = serviceAccountName
147153
addVolumeIfMissing(&daemonSet.Spec.Template.Spec, "podresources", "/var/lib/kubelet/pod-resources", v1.HostPathDirectory)
148154
addVolumeMountIfMissing(&daemonSet.Spec.Template.Spec, "podresources", "/var/lib/kubelet/pod-resources", false)
149155
addVolumeIfMissing(&daemonSet.Spec.Template.Spec, "kubeletcrt", "/var/lib/kubelet/pki/kubelet.crt", v1.HostPathFileOrCreate)
@@ -324,7 +330,7 @@ func (c *controller) UpdateDaemonSet(rawObj client.Object, ds *apps.DaemonSet) (
324330

325331
newServiceAccountName := "default"
326332
if dp.Spec.ResourceManager {
327-
newServiceAccountName = prefixedName(serviceAccountPrefix, dp.Name)
333+
newServiceAccountName = serviceAccountName
328334
}
329335

330336
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)