forked from kubermatic/kubermatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbac_controller_aggregator.go
186 lines (157 loc) · 5.3 KB
/
rbac_controller_aggregator.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
/*
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 rbac
import (
"fmt"
"strings"
"github.com/prometheus/client_golang/prometheus"
kubermaticv1 "github.com/kubermatic/kubermatic/api/pkg/crd/kubermatic/v1"
k8scorev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/klog"
)
// Metrics contains metrics that this controller will collect and expose
type Metrics struct {
Workers prometheus.Gauge
}
// NewMetrics creates RBACGeneratorControllerMetrics
// with default values initialized, so metrics always show up.
func NewMetrics() *Metrics {
subsystem := "rbac_generator_controller"
cm := &Metrics{
Workers: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: metricNamespace,
Subsystem: subsystem,
Name: "workers",
Help: "The number of running RBACGenerator controller workers",
}),
}
cm.Workers.Set(0)
return cm
}
// ControllerAggregator type holds controllers for managing RBAC for projects and theirs resources
type ControllerAggregator struct {
workerCount int
rbacProjectController *projectController
rbacResourceController *resourcesController
metrics *Metrics
allClusterProviders []*ClusterProvider
}
type projectResource struct {
gvr schema.GroupVersionResource
kind string
destination string
namespace string
// shouldEnqueue is a convenience function that is called right before
// the object is added to the queue. This is your last chance to say "no"
shouldEnqueue func(obj metav1.Object) bool
}
// New creates a new controller aggregator for managing RBAC for resources
func New(metrics *Metrics, allClusterProviders []*ClusterProvider, workerCount int) (*ControllerAggregator, error) {
projectResources := []projectResource{
{
gvr: schema.GroupVersionResource{
Group: kubermaticv1.GroupName,
Version: kubermaticv1.GroupVersion,
Resource: kubermaticv1.ClusterResourceName,
},
kind: kubermaticv1.ClusterKindName,
destination: destinationSeed,
},
{
gvr: schema.GroupVersionResource{
Group: kubermaticv1.GroupName,
Version: kubermaticv1.GroupVersion,
Resource: kubermaticv1.SSHKeyResourceName,
},
kind: kubermaticv1.SSHKeyKind,
},
{
gvr: schema.GroupVersionResource{
Group: kubermaticv1.GroupName,
Version: kubermaticv1.GroupVersion,
Resource: kubermaticv1.UserProjectBindingResourceName,
},
kind: kubermaticv1.UserProjectBindingKind,
},
{
gvr: schema.GroupVersionResource{
Group: k8scorev1.GroupName,
Version: k8scorev1.SchemeGroupVersion.Version,
Resource: "secrets",
},
kind: "Secret",
namespace: "kubermatic",
shouldEnqueue: func(obj metav1.Object) bool {
// do not reconcile secrets without "sa-token" and "credential" prefix
return shouldEnqueueSecret(obj.GetName())
},
},
{
gvr: schema.GroupVersionResource{
Group: kubermaticv1.GroupName,
Version: kubermaticv1.GroupVersion,
Resource: kubermaticv1.UserResourceName,
},
kind: kubermaticv1.UserKindName,
shouldEnqueue: func(obj metav1.Object) bool {
// do not reconcile resources without "serviceaccount" prefix
return strings.HasPrefix(obj.GetName(), "serviceaccount")
},
},
}
projectRBACCtrl, err := newProjectRBACController(metrics, allClusterProviders, projectResources)
if err != nil {
return nil, err
}
resourcesRBACCtrl, err := newResourcesController(metrics, allClusterProviders, projectResources)
if err != nil {
return nil, err
}
return &ControllerAggregator{
workerCount: workerCount,
rbacProjectController: projectRBACCtrl,
rbacResourceController: resourcesRBACCtrl,
metrics: metrics,
allClusterProviders: allClusterProviders,
}, nil
}
// Run starts the controller's worker routines. It is an implementation of
// sigs.k8s.io/controller-runtime/pkg/manager.Runnable
func (a *ControllerAggregator) Start(stopCh <-chan struct{}) error {
defer runtime.HandleCrash()
// wait for all caches in all clusters to get in-sync
for _, clusterProvider := range a.allClusterProviders {
clusterProvider.StartInformers(stopCh)
if err := clusterProvider.WaitForCachesToSync(stopCh); err != nil {
return fmt.Errorf("failed to sync cache: %v", err)
}
}
go a.rbacProjectController.run(a.workerCount, stopCh)
go a.rbacResourceController.run(a.workerCount, stopCh)
klog.Info("RBAC generator aggregator controller started")
<-stopCh
klog.Info("RBAC generator aggregator controller finished")
return nil
}
func shouldEnqueueSecret(name string) bool {
supportedPrefixes := []string{"sa-token", "credential"}
for _, prefix := range supportedPrefixes {
if strings.HasPrefix(name, prefix) {
return true
}
}
return false
}