forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_multiclusterapp.go
317 lines (296 loc) · 10.5 KB
/
handler_multiclusterapp.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package multiclusterapp
import (
"context"
"fmt"
"strings"
"github.com/rancher/rancher/pkg/clustermanager"
"github.com/rancher/rancher/pkg/controllers/management/globalnamespacerbac"
"github.com/rancher/rancher/pkg/namespace"
"github.com/rancher/rancher/pkg/ref"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/config"
"github.com/rancher/types/user"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/cache"
)
type MCAppController struct {
multiClusterApps v3.MultiClusterAppInterface
managementContext *config.ManagementContext
prtbs v3.ProjectRoleTemplateBindingInterface
prtbLister v3.ProjectRoleTemplateBindingLister
crtbs v3.ClusterRoleTemplateBindingInterface
crtbLister v3.ClusterRoleTemplateBindingLister
rtLister v3.RoleTemplateLister
gDNSs v3.GlobalDNSInterface
users v3.UserInterface
userManager user.Manager
}
type MCAppRevisionController struct {
managementContext *config.ManagementContext
}
type ProjectController struct {
mcAppsLister v3.MultiClusterAppLister
mcApps v3.MultiClusterAppInterface
projectLister v3.ProjectLister
}
type ClusterController struct {
mcAppsLister v3.MultiClusterAppLister
mcApps v3.MultiClusterAppInterface
clusterLister v3.ClusterLister
}
func Register(ctx context.Context, management *config.ManagementContext, clusterManager *clustermanager.Manager) {
mcApps := management.Management.MultiClusterApps("")
m := MCAppController{
multiClusterApps: mcApps,
managementContext: management,
prtbs: management.Management.ProjectRoleTemplateBindings(""),
prtbLister: management.Management.ProjectRoleTemplateBindings("").Controller().Lister(),
crtbs: management.Management.ClusterRoleTemplateBindings(""),
crtbLister: management.Management.ClusterRoleTemplateBindings("").Controller().Lister(),
rtLister: management.Management.RoleTemplates("").Controller().Lister(),
gDNSs: management.Management.GlobalDNSs(""),
userManager: management.UserManager,
users: management.Management.Users(""),
}
r := MCAppRevisionController{
managementContext: management,
}
projects := management.Management.Projects("")
p := ProjectController{
mcAppsLister: mcApps.Controller().Lister(),
mcApps: mcApps,
projectLister: projects.Controller().Lister(),
}
clusters := management.Management.Clusters("")
c := ClusterController{
mcAppsLister: mcApps.Controller().Lister(),
mcApps: mcApps,
clusterLister: clusters.Controller().Lister(),
}
m.multiClusterApps.AddHandler(ctx, "management-multiclusterapp-rbac-controller", m.sync)
management.Management.MultiClusterAppRevisions("").AddHandler(ctx, "management-multiclusterapp-revisions-rbac", r.sync)
projects.AddHandler(ctx, "management-mcapp-project-controller", p.sync)
clusters.AddHandler(ctx, "management-mcapp-cluster-controller", c.sync)
StartMCAppStateController(ctx, management)
StartMCAppManagementController(ctx, management, clusterManager)
StartMCAppEnqueueController(ctx, management)
}
func (mc *MCAppController) sync(key string, mcapp *v3.MultiClusterApp) (runtime.Object, error) {
if mcapp == nil || mcapp.DeletionTimestamp != nil {
// multiclusterapp is being deleted, remove the sys acc created for it
_, mcappName, err := cache.SplitMetaNamespaceKey(key)
if err != nil {
return nil, err
}
u, err := mc.userManager.GetUserByPrincipalID(fmt.Sprintf("system://%s", mcappName))
if err != nil {
return nil, err
}
if u == nil {
// user not found, must have been removed
return nil, nil
}
if err := mc.users.Delete(u.Name, &v1.DeleteOptions{}); err != nil && !apierrors.IsNotFound(err) && !apierrors.IsGone(err) {
return nil, err
}
return nil, nil
}
metaAccessor, err := meta.Accessor(mcapp)
if err != nil {
return mcapp, err
}
creatorID, ok := metaAccessor.GetAnnotations()[globalnamespacerbac.CreatorIDAnn]
if !ok {
return mcapp, fmt.Errorf("MultiClusterApp %v has no creatorId annotation. Cannot create apps for %v", metaAccessor.GetName(), mcapp.Name)
}
systemUserPrincipalID := fmt.Sprintf("system://%s", mcapp.Name)
systemUser, err := mc.userManager.EnsureUser(systemUserPrincipalID, "System account for Multiclusterapp "+mcapp.Name)
if err != nil {
return nil, err
}
var prtbName, crtbName string
// create PRTBs with this service account for all roles of multiclusterapp
for _, r := range mcapp.Spec.Roles {
rt, err := mc.rtLister.Get("", r)
if err != nil {
return nil, err
}
for _, p := range mcapp.Spec.Targets {
if p.ProjectName == "" {
continue
}
split := strings.SplitN(p.ProjectName, ":", 2)
if len(split) != 2 {
return nil, fmt.Errorf("invalid project name")
}
clusterName := split[0]
projectName := split[1]
if rt.Context == "project" {
prtbName = mcapp.Name + "-" + r
// check if these prtbs already exist
if err = mc.createPrtb(projectName, p.ProjectName, prtbName, r, systemUser.Name, systemUserPrincipalID); err != nil {
return nil, err
}
} else if rt.Context == "cluster" {
crtbName = mcapp.Name + "-" + r
mc.createCrtb(clusterName, crtbName, r, systemUser.Name, systemUserPrincipalID)
}
}
}
if err := globalnamespacerbac.CreateRoleAndRoleBinding(globalnamespacerbac.MultiClusterAppResource, mcapp.Name, mcapp.UID,
mcapp.Spec.Members, creatorID, mc.managementContext); err != nil {
return nil, err
}
return nil, nil
}
func (r *MCAppRevisionController) sync(key string, mcappRevision *v3.MultiClusterAppRevision) (runtime.Object, error) {
if mcappRevision == nil || mcappRevision.DeletionTimestamp != nil {
return mcappRevision, nil
}
metaAccessor, err := meta.Accessor(mcappRevision)
if err != nil {
return mcappRevision, err
}
creatorID, ok := metaAccessor.GetAnnotations()[globalnamespacerbac.CreatorIDAnn]
if !ok {
return mcappRevision, fmt.Errorf("mcapp revision %v has no creatorId annotation", mcappRevision.Name)
}
if err := globalnamespacerbac.CreateRoleAndRoleBinding(
globalnamespacerbac.MultiClusterAppRevisionResource, mcappRevision.Name, mcappRevision.UID, []v3.Member{}, creatorID,
r.managementContext); err != nil {
return nil, err
}
return mcappRevision, nil
}
func (p *ProjectController) sync(key string, project *v3.Project) (runtime.Object, error) {
if project != nil && project.DeletionTimestamp == nil {
return project, nil
}
splitKey := strings.SplitN(key, "/", 2)
if len(splitKey) != 2 || splitKey[0] == "" || splitKey[1] == "" {
return project, fmt.Errorf("invalid project id %s", key)
}
clusterName, projectName := splitKey[0], splitKey[1]
key = fmt.Sprintf("%s:%s", clusterName, projectName)
mcApps, err := p.mcAppsLister.List(namespace.GlobalNamespace, labels.NewSelector())
if err != nil {
return project, err
}
for _, mcApp := range mcApps {
if mcApp.DeletionTimestamp != nil {
continue
}
var toUpdate *v3.MultiClusterApp
for i, target := range mcApp.Spec.Targets {
if target.ProjectName == key {
toUpdate = mcApp.DeepCopy()
toUpdate.Spec.Targets = append(toUpdate.Spec.Targets[:i], toUpdate.Spec.Targets[i+1:]...)
p.updateAnswersForProject(key, toUpdate)
break
}
}
if toUpdate != nil {
if _, err := p.mcApps.Update(toUpdate); err != nil {
return project, fmt.Errorf("error updating mcapp %s for project %s", mcApp.Name, key)
}
}
}
return project, nil
}
func (p *ProjectController) updateAnswersForProject(projectName string, mcapp *v3.MultiClusterApp) {
for i := len(mcapp.Spec.Answers) - 1; i >= 0; i-- {
if mcapp.Spec.Answers[i].ProjectName == projectName {
mcapp.Spec.Answers = append(mcapp.Spec.Answers[:i], mcapp.Spec.Answers[i+1:]...)
break
}
}
}
func (c *ClusterController) sync(key string, cluster *v3.Cluster) (runtime.Object, error) {
if cluster != nil && cluster.DeletionTimestamp == nil {
return cluster, nil
}
mcApps, err := c.mcAppsLister.List(namespace.GlobalNamespace, labels.NewSelector())
if err != nil {
return cluster, err
}
for _, mcApp := range mcApps {
if mcApp.DeletionTimestamp != nil {
continue
}
var toUpdate *v3.MultiClusterApp
for i := len(mcApp.Spec.Targets) - 1; i >= 0; i-- {
clusterName, _ := ref.Parse(mcApp.Spec.Targets[i].ProjectName)
if clusterName == key {
if toUpdate == nil {
toUpdate = mcApp.DeepCopy()
}
toUpdate.Spec.Targets = append(toUpdate.Spec.Targets[:i], toUpdate.Spec.Targets[i+1:]...)
}
}
if toUpdate != nil {
c.updateAnswersForCluster(key, toUpdate)
if _, err := c.mcApps.Update(toUpdate); err != nil {
return cluster, fmt.Errorf("error updating mcapp %s for cluster %s", mcApp.Name, key)
}
}
}
return cluster, nil
}
func (c *ClusterController) updateAnswersForCluster(clusterName string, mcapp *v3.MultiClusterApp) {
for i := len(mcapp.Spec.Answers) - 1; i >= 0; i-- {
projClusterName, _ := ref.Parse(mcapp.Spec.Answers[i].ProjectName)
if mcapp.Spec.Answers[i].ClusterName == clusterName || projClusterName == clusterName {
mcapp.Spec.Answers = append(mcapp.Spec.Answers[:i], mcapp.Spec.Answers[i+1:]...)
}
}
}
func (mc *MCAppController) createPrtb(projectName, projectID, prtbName, roleTemplateName, systemUserName, systemUserPrincipalID string) error {
_, err := mc.prtbLister.Get(projectName, prtbName)
if err != nil {
if apierrors.IsNotFound(err) {
_, err := mc.prtbs.Create(&v3.ProjectRoleTemplateBinding{
ObjectMeta: v1.ObjectMeta{
Name: prtbName,
Namespace: projectName,
},
UserName: systemUserName,
UserPrincipalName: systemUserPrincipalID,
RoleTemplateName: roleTemplateName,
ProjectName: projectID,
})
if err != nil && !apierrors.IsAlreadyExists(err) {
return err
}
} else {
return err
}
}
return nil
}
func (mc *MCAppController) createCrtb(clusterName, crtbName, roleTemplateName, systemUserName, systemUserPrincipalID string) error {
_, err := mc.crtbLister.Get(clusterName, crtbName)
if err != nil {
if apierrors.IsNotFound(err) {
_, err := mc.crtbs.Create(&v3.ClusterRoleTemplateBinding{
ObjectMeta: v1.ObjectMeta{
Name: crtbName,
Namespace: clusterName,
},
UserName: systemUserName,
UserPrincipalName: systemUserPrincipalID,
RoleTemplateName: roleTemplateName,
ClusterName: clusterName,
})
if err != nil && !apierrors.IsAlreadyExists(err) {
return err
}
} else {
return err
}
}
return nil
}