/
rbac.go
174 lines (166 loc) · 5.38 KB
/
rbac.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
package controllers
import (
"context"
"fmt"
v1 "github.com/FFFFFaraway/MPI-Operator/api/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
)
func getObjectMeta(mpiJob *v1.MPIJob, suffix string) metav1.ObjectMeta {
return metav1.ObjectMeta{
Name: mpiJob.Name + suffix,
Namespace: mpiJob.Namespace,
Labels: map[string]string{
"app": mpiJob.Name,
},
}
}
// newLauncherServiceAccount creates a new launcher ServiceAccount for an MPIJob
// resource. It also sets the appropriate OwnerReferences on the resource so
// handleObject can discover the MPIJob resource that 'owns' it.
func newLauncherServiceAccount(mpiJob *v1.MPIJob) *corev1.ServiceAccount {
return &corev1.ServiceAccount{
ObjectMeta: getObjectMeta(mpiJob, launcherSuffix),
}
}
// newLauncherRole creates a new launcher Role for an MPIJob resource. It also
// sets the appropriate OwnerReferences on the resource so handleObject can
// discover the MPIJob resource that 'owns' it.
func newLauncherRole(mpiJob *v1.MPIJob) *rbacv1.Role {
var podNames []string
for i := 0; i < int(*mpiJob.Spec.NumWorkers); i++ {
podNames = append(podNames, fmt.Sprintf("%s%s-%d", mpiJob.Name, workerSuffix, i))
}
return &rbacv1.Role{
ObjectMeta: getObjectMeta(mpiJob, launcherSuffix),
Rules: []rbacv1.PolicyRule{
{
Verbs: []string{"get", "list", "watch"},
APIGroups: []string{""},
Resources: []string{"pods"},
},
{
Verbs: []string{"create"},
APIGroups: []string{""},
Resources: []string{"pods/exec"},
ResourceNames: podNames,
},
},
}
}
// newLauncherRoleBinding creates a new launcher RoleBinding for an MPIJob
// resource. It also sets the appropriate OwnerReferences on the resource so
// handleObject can discover the MPIJob resource that 'owns' it.
func newLauncherRoleBinding(mpiJob *v1.MPIJob) *rbacv1.RoleBinding {
launcherName := mpiJob.Name + launcherSuffix
return &rbacv1.RoleBinding{
ObjectMeta: getObjectMeta(mpiJob, launcherSuffix),
Subjects: []rbacv1.Subject{
{
Kind: rbacv1.ServiceAccountKind,
Name: launcherName,
Namespace: mpiJob.Namespace,
},
},
RoleRef: rbacv1.RoleRef{
APIGroup: rbacv1.GroupName,
Kind: "Role",
Name: launcherName,
},
}
}
func (r *MPIJobReconciler) getOrCreateLauncherServiceAccount(ctx context.Context, mpiJob *v1.MPIJob) error {
logger := log.FromContext(ctx)
newSA := newLauncherServiceAccount(mpiJob)
if err := ctrl.SetControllerReference(mpiJob, newSA, r.Scheme); err != nil {
return err
}
var sa corev1.ServiceAccount
err := r.Get(ctx, client.ObjectKey{Namespace: mpiJob.Namespace, Name: mpiJob.Name + launcherSuffix}, &sa)
if errors.IsNotFound(err) {
logger.V(1).Info("ServiceAccount doesn't exist, creating...")
// If the ConfigMap doesn't exist, we'll create it.
if err := r.Create(ctx, newSA); err != nil {
return err
}
return nil
}
if err != nil {
return err
}
// If the ConfigMap is not controlled by this MPIJob resource, we
// should log a warning to the event recorder and return.
if !metav1.IsControlledBy(&sa, mpiJob) {
logger.Info("WARN:ServiceAccount is not controlled by this MPIJob resource. Skipping")
return nil
}
if err := r.Update(ctx, newSA); err != nil {
return err
}
return nil
}
func (r *MPIJobReconciler) getOrCreateLauncherRole(ctx context.Context, mpiJob *v1.MPIJob) error {
logger := log.FromContext(ctx)
newRole := newLauncherRole(mpiJob)
if err := ctrl.SetControllerReference(mpiJob, newRole, r.Scheme); err != nil {
return err
}
var role rbacv1.Role
err := r.Get(ctx, client.ObjectKey{Namespace: mpiJob.Namespace, Name: mpiJob.Name + launcherSuffix}, &role)
if errors.IsNotFound(err) {
logger.V(1).Info("Role doesn't exist, creating...")
// If the ConfigMap doesn't exist, we'll create it.
if err := r.Create(ctx, newRole); err != nil {
return err
}
return nil
}
if err != nil {
return err
}
// If the ConfigMap is not controlled by this MPIJob resource, we
// should log a warning to the event recorder and return.
if !metav1.IsControlledBy(&role, mpiJob) {
logger.Info("WARN:Role is not controlled by this MPIJob resource. Skipping")
return nil
}
if err := r.Update(ctx, newRole); err != nil {
return err
}
return nil
}
func (r *MPIJobReconciler) getOrCreateLauncherRoleBinding(ctx context.Context, mpiJob *v1.MPIJob) error {
logger := log.FromContext(ctx)
newRb := newLauncherRoleBinding(mpiJob)
if err := ctrl.SetControllerReference(mpiJob, newRb, r.Scheme); err != nil {
return err
}
var rb rbacv1.RoleBinding
err := r.Get(ctx, client.ObjectKey{Namespace: mpiJob.Namespace, Name: mpiJob.Name + launcherSuffix}, &rb)
if errors.IsNotFound(err) {
logger.V(1).Info("RoleBinding doesn't exist, creating...")
// If the ConfigMap doesn't exist, we'll create it.
if err := r.Create(ctx, newRb); err != nil {
return err
}
return nil
}
if err != nil {
return err
}
// If the ConfigMap is not controlled by this MPIJob resource, we
// should log a warning to the event recorder and return.
if !metav1.IsControlledBy(&rb, mpiJob) {
logger.Info("WARN:RoleBinding is not controlled by this MPIJob resource. Skipping")
return nil
}
if err := r.Update(ctx, newRb); err != nil {
return err
}
return nil
}