-
Notifications
You must be signed in to change notification settings - Fork 197
/
operator.go
270 lines (243 loc) · 9.76 KB
/
operator.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
package operator
import (
"context"
"fmt"
"github.com/aquasecurity/starboard/pkg/compliance"
"github.com/aquasecurity/starboard/pkg/configauditreport"
"github.com/aquasecurity/starboard/pkg/ext"
"github.com/aquasecurity/starboard/pkg/kube"
"github.com/aquasecurity/starboard/pkg/kubebench"
"github.com/aquasecurity/starboard/pkg/operator/controller"
"github.com/aquasecurity/starboard/pkg/operator/etc"
"github.com/aquasecurity/starboard/pkg/plugin"
"github.com/aquasecurity/starboard/pkg/starboard"
"github.com/aquasecurity/starboard/pkg/vulnerabilityreport"
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
)
var (
setupLog = log.Log.WithName("operator")
)
// Start starts all registered reconcilers and blocks until the context is cancelled.
// Returns an error if there is an error starting any reconciler.
func Start(ctx context.Context, buildInfo starboard.BuildInfo, operatorConfig etc.Config) error {
installMode, operatorNamespace, targetNamespaces, err := operatorConfig.ResolveInstallMode()
if err != nil {
return fmt.Errorf("resolving install mode: %w", err)
}
setupLog.Info("Resolved install mode", "install mode", installMode,
"operator namespace", operatorNamespace,
"target namespaces", targetNamespaces,
"exclude namespaces", operatorConfig.ExcludeNamespaces)
// Set the default manager options.
options := manager.Options{
Scheme: starboard.NewScheme(),
MetricsBindAddress: operatorConfig.MetricsBindAddress,
HealthProbeBindAddress: operatorConfig.HealthProbeBindAddress,
}
if operatorConfig.LeaderElectionEnabled {
options.LeaderElection = operatorConfig.LeaderElectionEnabled
options.LeaderElectionID = operatorConfig.LeaderElectionID
options.LeaderElectionNamespace = operatorNamespace
}
switch installMode {
case etc.OwnNamespace:
// Add support for OwnNamespace set in OPERATOR_NAMESPACE (e.g. `starboard-operator`)
// and OPERATOR_TARGET_NAMESPACES (e.g. `starboard-operator`).
setupLog.Info("Constructing client cache", "namespace", targetNamespaces[0])
options.Namespace = targetNamespaces[0]
case etc.SingleNamespace:
// Add support for SingleNamespace set in OPERATOR_NAMESPACE (e.g. `starboard-operator`)
// and OPERATOR_TARGET_NAMESPACES (e.g. `default`).
cachedNamespaces := append(targetNamespaces, operatorNamespace)
if operatorConfig.CISKubernetesBenchmarkEnabled {
// Cache cluster-scoped resources such as Nodes
cachedNamespaces = append(cachedNamespaces, "")
}
setupLog.Info("Constructing client cache", "namespaces", cachedNamespaces)
options.NewCache = cache.MultiNamespacedCacheBuilder(cachedNamespaces)
case etc.MultiNamespace:
// Add support for MultiNamespace set in OPERATOR_NAMESPACE (e.g. `starboard-operator`)
// and OPERATOR_TARGET_NAMESPACES (e.g. `default,kube-system`).
// Note that you may face performance issues when using this mode with a high number of namespaces.
// More: https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/cache#MultiNamespacedCacheBuilder
cachedNamespaces := append(targetNamespaces, operatorNamespace)
if operatorConfig.CISKubernetesBenchmarkEnabled {
// Cache cluster-scoped resources such as Nodes
cachedNamespaces = append(cachedNamespaces, "")
}
setupLog.Info("Constructing client cache", "namespaces", cachedNamespaces)
options.NewCache = cache.MultiNamespacedCacheBuilder(cachedNamespaces)
case etc.AllNamespaces:
// Add support for AllNamespaces set in OPERATOR_NAMESPACE (e.g. `operators`)
// and OPERATOR_TARGET_NAMESPACES left blank.
setupLog.Info("Watching all namespaces")
default:
return fmt.Errorf("unrecognized install mode: %v", installMode)
}
kubeConfig, err := ctrl.GetConfig()
if err != nil {
return fmt.Errorf("getting kube client config: %w", err)
}
// The only reason we're using kubernetes.Clientset is that we need it to read Pod logs,
// which is not supported by the client returned by the ctrl.Manager.
kubeClientset, err := kubernetes.NewForConfig(kubeConfig)
if err != nil {
return fmt.Errorf("constructing kube client: %w", err)
}
mgr, err := ctrl.NewManager(kubeConfig, options)
if err != nil {
return fmt.Errorf("constructing controllers manager: %w", err)
}
err = mgr.AddReadyzCheck("ping", healthz.Ping)
if err != nil {
return err
}
err = mgr.AddHealthzCheck("ping", healthz.Ping)
if err != nil {
return err
}
configManager := starboard.NewConfigManager(kubeClientset, operatorNamespace)
err = configManager.EnsureDefault(context.Background())
if err != nil {
return err
}
starboardConfig, err := configManager.Read(context.Background())
if err != nil {
return err
}
objectResolver := kube.ObjectResolver{Client: mgr.GetClient()}
limitChecker := controller.NewLimitChecker(operatorConfig, mgr.GetClient(), starboardConfig)
logsReader := kube.NewLogsReader(kubeClientset)
secretsReader := kube.NewSecretsReader(mgr.GetClient())
if operatorConfig.VulnerabilityScannerEnabled {
plugin, pluginContext, err := plugin.NewResolver().
WithBuildInfo(buildInfo).
WithNamespace(operatorNamespace).
WithServiceAccountName(operatorConfig.ServiceAccount).
WithConfig(starboardConfig).
WithClient(mgr.GetClient()).
GetVulnerabilityPlugin()
if err != nil {
return err
}
err = plugin.Init(pluginContext)
if err != nil {
return fmt.Errorf("initializing %s plugin: %w", pluginContext.GetName(), err)
}
if err = (&vulnerabilityreport.WorkloadController{
Logger: ctrl.Log.WithName("reconciler").WithName("vulnerabilityreport"),
Config: operatorConfig,
ConfigData: starboardConfig,
Client: mgr.GetClient(),
ObjectResolver: objectResolver,
LimitChecker: limitChecker,
LogsReader: logsReader,
SecretsReader: secretsReader,
Plugin: plugin,
PluginContext: pluginContext,
ReadWriter: vulnerabilityreport.NewReadWriter(mgr.GetClient()),
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("unable to setup vulnerabilityreport reconciler: %w", err)
}
if operatorConfig.VulnerabilityScannerReportTTL != nil {
if err = (&controller.TTLReportReconciler{
Logger: ctrl.Log.WithName("reconciler").WithName("ttlreport"),
Config: operatorConfig,
Client: mgr.GetClient(),
Clock: ext.NewSystemClock(),
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("unable to setup TTLreport reconciler: %w", err)
}
}
}
if operatorConfig.ConfigAuditScannerEnabled {
plugin, pluginContext, err := plugin.NewResolver().
WithBuildInfo(buildInfo).
WithNamespace(operatorNamespace).
WithServiceAccountName(operatorConfig.ServiceAccount).
WithConfig(starboardConfig).
WithClient(mgr.GetClient()).
GetConfigAuditPlugin()
if err != nil {
return err
}
err = plugin.Init(pluginContext)
if err != nil {
return fmt.Errorf("initializing %s plugin: %w", pluginContext.GetName(), err)
}
if err = (&controller.ConfigAuditReportReconciler{
Logger: ctrl.Log.WithName("reconciler").WithName("configauditreport"),
Config: operatorConfig,
ConfigData: starboardConfig,
Client: mgr.GetClient(),
ObjectResolver: objectResolver,
LimitChecker: limitChecker,
LogsReader: logsReader,
Plugin: plugin,
PluginContext: pluginContext,
ReadWriter: configauditreport.NewReadWriter(mgr.GetClient()),
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("unable to setup configauditreport reconciler: %w", err)
}
if err = (&controller.PluginsConfigReconciler{
Logger: ctrl.Log.WithName("reconciler").WithName("pluginsconfig"),
Config: operatorConfig,
Client: mgr.GetClient(),
Plugin: plugin,
PluginContext: pluginContext,
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("unable to setup %T: %w", controller.PluginsConfigReconciler{}, err)
}
}
if operatorConfig.CISKubernetesBenchmarkEnabled {
if err = (&controller.CISKubeBenchReportReconciler{
Logger: ctrl.Log.WithName("reconciler").WithName("ciskubebenchreport"),
Config: operatorConfig,
ConfigData: starboardConfig,
Client: mgr.GetClient(),
LogsReader: logsReader,
LimitChecker: limitChecker,
ReadWriter: kubebench.NewReadWriter(mgr.GetClient()),
Plugin: kubebench.NewKubeBenchPlugin(ext.NewSystemClock(), starboardConfig),
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("unable to setup ciskubebenchreport reconciler: %w", err)
}
}
if operatorConfig.ConfigAuditScannerBuiltIn {
setupLog.Info("Enabling built-in configuration audit scanner")
if err = (&configauditreport.ResourceController{
Logger: ctrl.Log.WithName("resourcecontroller"),
Config: operatorConfig,
ConfigData: starboardConfig,
Client: mgr.GetClient(),
ObjectResolver: objectResolver,
ReadWriter: configauditreport.NewReadWriter(mgr.GetClient()),
BuildInfo: buildInfo,
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("unable to setup resource controller: %w", err)
}
}
if operatorConfig.ClusterComplianceEnabled {
logger := ctrl.Log.WithName("reconciler").WithName("clustercompliancereport")
cc := &compliance.ClusterComplianceReportReconciler{
Logger: logger,
Config: operatorConfig,
Client: mgr.GetClient(),
Mgr: compliance.NewMgr(mgr.GetClient(), logger),
Clock: ext.NewSystemClock(),
}
if err := cc.SetupWithManager(mgr); err != nil {
return fmt.Errorf("unable to setup clustercompliancereport reconciler: %w", err)
}
}
setupLog.Info("Starting controllers manager")
if err := mgr.Start(ctx); err != nil {
return fmt.Errorf("starting controllers manager: %w", err)
}
return nil
}