forked from kubermatic/kubermatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
role_cloner.go
220 lines (190 loc) · 6.95 KB
/
role_cloner.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
/*
Copyright 2020 The Kubermatic Kubernetes Platform contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package rolecloner
import (
"context"
"fmt"
"reflect"
"go.uber.org/zap"
kubermaticapiv1 "github.com/kubermatic/kubermatic/api/pkg/api/v1"
"github.com/kubermatic/kubermatic/api/pkg/handler/v1/cluster"
kuberneteshelper "github.com/kubermatic/kubermatic/api/pkg/kubernetes"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/record"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
)
const (
// This controller duplicate roles with label component=userClusterRole for all namespaces
controllerName = "clone_role_controller"
)
type reconciler struct {
ctx context.Context
log *zap.SugaredLogger
client ctrlruntimeclient.Client
recorder record.EventRecorder
}
func Add(ctx context.Context, log *zap.SugaredLogger, mgr manager.Manager) error {
log = log.Named(controllerName)
r := &reconciler{
ctx: ctx,
log: log,
client: mgr.GetClient(),
recorder: mgr.GetEventRecorderFor(controllerName),
}
c, err := controller.New(controllerName, mgr, controller.Options{
Reconciler: r,
})
if err != nil {
return fmt.Errorf("failed to create controller: %v", err)
}
// Watch for changes to Roles and Namespaces
if err = c.Watch(&source.Kind{Type: &rbacv1.Role{}}, enqueueTemplateRoles(mgr.GetClient())); err != nil {
return fmt.Errorf("failed to establish watch for the Roles %v", err)
}
if err = c.Watch(&source.Kind{Type: &corev1.Namespace{}}, enqueueTemplateRoles(mgr.GetClient())); err != nil {
return fmt.Errorf("failed to establish watch for the Namespace %v", err)
}
return nil
}
func (r *reconciler) Reconcile(request reconcile.Request) (reconcile.Result, error) {
log := r.log.With("Role", request.Name)
log.Debug("Reconciling")
role := &rbacv1.Role{}
if err := r.client.Get(r.ctx, request.NamespacedName, role); err != nil {
if kerrors.IsNotFound(err) {
log.Debug("role not found, returning")
return reconcile.Result{}, nil
}
return reconcile.Result{}, fmt.Errorf("failed to get role: %v", err)
}
err := r.reconcile(log, role)
if err != nil {
log.Errorw("Reconciling failed", zap.Error(err))
r.recorder.Event(role, corev1.EventTypeWarning, "CloningRoleFailed", err.Error())
}
return reconcile.Result{}, err
}
func (r *reconciler) reconcile(log *zap.SugaredLogger, role *rbacv1.Role) error {
namespaces := []string{}
namespaceList := &corev1.NamespaceList{}
if err := r.client.List(r.ctx, namespaceList); err != nil {
return fmt.Errorf("failed to get namespaces: %v", err)
}
for _, n := range namespaceList.Items {
// This NS is the authoritative source of roles we configure
if n.Name == v1.NamespaceSystem {
continue
}
// No point in trying to create something in a deleted namespace
if n.DeletionTimestamp != nil {
log.Debugf("Skipping namespace %s", n.Name)
continue
}
namespaces = append(namespaces, n.Name)
}
return r.reconcileRoles(log, role, namespaces)
}
func (r *reconciler) reconcileRoles(log *zap.SugaredLogger, oldRole *rbacv1.Role, namespaces []string) error {
if oldRole.DeletionTimestamp != nil {
if !kuberneteshelper.HasFinalizer(oldRole, kubermaticapiv1.UserClusterRoleCleanupFinalizer) {
return nil
}
for _, namespace := range namespaces {
if err := r.client.Delete(r.ctx, &rbacv1.Role{
ObjectMeta: v1.ObjectMeta{
Name: oldRole.Name,
Namespace: namespace,
},
}); err != nil {
if kerrors.IsNotFound(err) {
continue
}
return fmt.Errorf("failed to delete role: %v", err)
}
}
kuberneteshelper.RemoveFinalizer(oldRole, kubermaticapiv1.UserClusterRoleCleanupFinalizer)
if err := r.client.Update(r.ctx, oldRole); err != nil {
return fmt.Errorf("failed to update role: %v", err)
}
return nil
}
if !kuberneteshelper.HasFinalizer(oldRole, kubermaticapiv1.UserClusterRoleCleanupFinalizer) {
kuberneteshelper.AddFinalizer(oldRole, kubermaticapiv1.UserClusterRoleCleanupFinalizer)
if err := r.client.Update(r.ctx, oldRole); err != nil {
return fmt.Errorf("failed to update role: %v", err)
}
}
for _, namespace := range namespaces {
log := log.With("namespace", namespace)
wasCreated := false
role := &rbacv1.Role{}
if err := r.client.Get(r.ctx, ctrlruntimeclient.ObjectKey{
Namespace: namespace,
Name: oldRole.Name,
}, role); err != nil {
if kerrors.IsNotFound(err) {
log.Debug("role not found, creating")
newRole := &rbacv1.Role{
ObjectMeta: v1.ObjectMeta{
Name: oldRole.Name,
Namespace: namespace,
Labels: oldRole.Labels,
},
Rules: oldRole.Rules,
}
if err := r.client.Create(r.ctx, newRole); err != nil {
return fmt.Errorf("failed to create role: %v", err)
}
wasCreated = true
} else {
return fmt.Errorf("failed to get role: %v", err)
}
}
// update only existing roles, not already created
if !wasCreated {
if !reflect.DeepEqual(role.Rules, oldRole.Rules) {
log.Debug("Role was changed, updating")
role.Rules = oldRole.Rules
if err := r.client.Update(r.ctx, role); err != nil {
return fmt.Errorf("failed to update role: %v", err)
}
}
}
}
return nil
}
// enqueueTemplateRoles enqueues the roles from kube-system namespace and special label component=userClusterRole
func enqueueTemplateRoles(client ctrlruntimeclient.Client) *handler.EnqueueRequestsFromMapFunc {
return &handler.EnqueueRequestsFromMapFunc{ToRequests: handler.ToRequestsFunc(func(a handler.MapObject) []reconcile.Request {
roleList := &rbacv1.RoleList{}
if err := client.List(context.Background(), roleList, ctrlruntimeclient.MatchingLabels{cluster.UserClusterComponentKey: cluster.UserClusterRoleComponentValue}, ctrlruntimeclient.InNamespace(v1.NamespaceSystem)); err != nil {
utilruntime.HandleError(fmt.Errorf("failed to list Roles: %v", err))
return []reconcile.Request{}
}
request := []reconcile.Request{}
for _, role := range roleList.Items {
request = append(request, reconcile.Request{NamespacedName: types.NamespacedName{Name: role.Name, Namespace: role.Namespace}})
}
return request
})}
}