forked from open-policy-agent/gatekeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
308 lines (270 loc) · 9.67 KB
/
main.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
/*
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 main
import (
"flag"
"fmt"
"os"
"time"
"github.com/go-logr/zapr"
opa "github.com/open-policy-agent/frameworks/constraint/pkg/client"
"github.com/open-policy-agent/frameworks/constraint/pkg/client/drivers/local"
"github.com/open-policy-agent/gatekeeper/api"
configv1alpha1 "github.com/open-policy-agent/gatekeeper/api/v1alpha1"
"github.com/open-policy-agent/gatekeeper/pkg/audit"
"github.com/open-policy-agent/gatekeeper/pkg/controller"
configController "github.com/open-policy-agent/gatekeeper/pkg/controller/config"
"github.com/open-policy-agent/gatekeeper/pkg/controller/constrainttemplate"
"github.com/open-policy-agent/gatekeeper/pkg/metrics"
"github.com/open-policy-agent/gatekeeper/pkg/target"
"github.com/open-policy-agent/gatekeeper/pkg/upgrade"
"github.com/open-policy-agent/gatekeeper/pkg/util"
"github.com/open-policy-agent/gatekeeper/pkg/watch"
"github.com/open-policy-agent/gatekeeper/pkg/webhook"
"github.com/open-policy-agent/gatekeeper/third_party/sigs.k8s.io/controller-runtime/pkg/dynamiccache"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
k8sCli "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
"sigs.k8s.io/controller-runtime/pkg/healthz"
crzap "sigs.k8s.io/controller-runtime/pkg/log/zap"
// +kubebuilder:scaffold:imports
)
const (
secretName = "gatekeeper-webhook-server-cert"
vwhName = "gatekeeper-validating-webhook-configuration"
serviceName = "gatekeeper-webhook-service"
caName = "gatekeeper-ca"
caOrganization = "gatekeeper"
)
var (
// DNSName is <service name>.<namespace>.svc
dnsName = fmt.Sprintf("%s.%s.svc", serviceName, util.GetNamespace())
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
operations = newOperationSet()
)
var (
logLevel = flag.String("log-level", "INFO", "Minimum log level. For example, DEBUG, INFO, WARNING, ERROR. Defaulted to INFO if unspecified.")
healthAddr = flag.String("health-addr", ":9090", "The address to which the health endpoint binds.")
metricsAddr = flag.String("metrics-addr", "0", "The address the metric endpoint binds to.")
port = flag.Int("port", 443, "port for the server. defaulted to 443 if unspecified ")
certDir = flag.String("cert-dir", "/certs", "The directory where certs are stored, defaults to /certs")
disableCertRotation = flag.Bool("disable-cert-rotation", false, "disable automatic generation and rotation of webhook TLS certificates/keys")
)
func init() {
_ = clientgoscheme.AddToScheme(scheme)
_ = api.AddToScheme(scheme)
_ = configv1alpha1.AddToScheme(scheme)
// +kubebuilder:scaffold:scheme
flag.Var(operations, "operation", "The operation to be performed by this instance. e.g. audit, webhook. This flag can be declared more than once. Omitting will default to supporting all operations.")
}
type opSet map[string]bool
var _ flag.Value = opSet{}
func newOperationSet() opSet {
return make(map[string]bool)
}
func (l opSet) String() string {
contents := make([]string, 0)
for k := range l {
contents = append(contents, k)
}
return fmt.Sprintf("%s", contents)
}
func (l opSet) Set(s string) error {
l[s] = true
return nil
}
func main() {
flag.Parse()
switch *logLevel {
case "DEBUG":
ctrl.SetLogger(crzap.Logger(true))
case "WARNING", "ERROR":
setLoggerForProduction()
case "INFO":
fallthrough
default:
ctrl.SetLogger(crzap.Logger(false))
}
ctrl.SetLogger(crzap.Logger(true))
// set default if --operation is not provided
if len(operations) == 0 {
operations["audit"] = true
operations["webhook"] = true
}
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
NewCache: dynamiccache.New,
Scheme: scheme,
MetricsBindAddress: *metricsAddr,
LeaderElection: false,
Port: *port,
CertDir: *certDir,
HealthProbeBindAddress: *healthAddr,
MapperProvider: func(c *rest.Config) (meta.RESTMapper, error) {
return apiutil.NewDynamicRESTMapper(c)
},
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
// Make sure certs are generated and valid if cert rotation is enabled.
setupFinished := make(chan struct{})
if !*disableCertRotation && operations["webhook"] {
setupLog.Info("setting up cert rotation")
if err := webhook.AddRotator(mgr, &webhook.CertRotator{
SecretKey: types.NamespacedName{
Namespace: util.GetNamespace(),
Name: secretName,
},
CertDir: *certDir,
CAName: caName,
CAOrganization: caOrganization,
DNSName: dnsName,
CertsMounted: setupFinished,
}, vwhName); err != nil {
setupLog.Error(err, "unable to set up cert rotation")
os.Exit(1)
}
} else {
close(setupFinished)
}
// ControllerSwitch will be used to disable controllers during our teardown process,
// avoiding conflicts in finalizer cleanup.
sw := watch.NewSwitch()
go startControllers(mgr, sw, setupFinished)
// +kubebuilder:scaffold:builder
if err := mgr.AddReadyzCheck("default", healthz.Ping); err != nil {
setupLog.Error(err, "unable to create ready check")
os.Exit(1)
}
if err := mgr.AddHealthzCheck("default", healthz.Ping); err != nil {
setupLog.Error(err, "unable to create health check")
os.Exit(1)
}
setupLog.Info("starting manager")
hadError := false
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
hadError = true
}
// Manager stops controllers asynchronously.
// Instead, we use ControllerSwitch to synchronously prevent them from doing more work.
// This can be removed when finalizer and status teardown is removed.
setupLog.Info("disabling controllers...")
sw.Stop()
// Create a fresh client to be sure RESTmapper is up-to-date
setupLog.Info("cleaning state...")
cli, err := k8sCli.New(mgr.GetConfig(), k8sCli.Options{Scheme: mgr.GetScheme(), Mapper: nil})
if err != nil {
setupLog.Error(err, "unable to create cleanup client")
os.Exit(1)
}
// Clean up sync finalizers
// This logic should be disabled if OPA is run as a sidecar
syncCleaned := make(chan struct{})
go configController.TearDownState(cli, syncCleaned)
// Clean up constraint finalizers
templatesCleaned := make(chan struct{})
go constrainttemplate.TearDownState(cli, templatesCleaned)
<-syncCleaned
<-templatesCleaned
setupLog.Info("state cleaned")
if hadError {
os.Exit(1)
}
}
func startControllers(mgr ctrl.Manager, sw *watch.ControllerSwitch, setupFinished chan struct{}) {
// Block until the setup finishes.
<-setupFinished
// initialize OPA
driver := local.New(local.Tracing(false))
backend, err := opa.NewBackend(opa.Driver(driver))
if err != nil {
setupLog.Error(err, "unable to set up OPA backend")
os.Exit(1)
}
client, err := backend.NewClient(opa.Targets(&target.K8sValidationTarget{}))
if err != nil {
setupLog.Error(err, "unable to set up OPA client")
}
c := mgr.GetCache()
dc, ok := c.(watch.RemovableCache)
if !ok {
err := fmt.Errorf("expected dynamic cache, got: %T", c)
setupLog.Error(err, "fetching dynamic cache")
os.Exit(1)
}
wm, err := watch.New(dc)
if err != nil {
setupLog.Error(err, "unable to create watch manager")
os.Exit(1)
}
if err := mgr.Add(wm); err != nil {
setupLog.Error(err, "unable to register watch manager to the manager")
os.Exit(1)
}
// Setup all Controllers
setupLog.Info("setting up controller")
if err := controller.AddToManager(mgr, client, wm, sw); err != nil {
setupLog.Error(err, "unable to register controllers to the manager")
os.Exit(1)
}
if operations["webhook"] {
setupLog.Info("setting up webhooks")
if err := webhook.AddToManager(mgr, client); err != nil {
setupLog.Error(err, "unable to register webhooks to the manager")
os.Exit(1)
}
}
if operations["audit"] {
setupLog.Info("setting up audit")
if err := audit.AddToManager(mgr, client); err != nil {
setupLog.Error(err, "unable to register audit to the manager")
os.Exit(1)
}
}
setupLog.Info("setting up upgrade")
if err := upgrade.AddToManager(mgr); err != nil {
setupLog.Error(err, "unable to register upgrade to the manager")
os.Exit(1)
}
setupLog.Info("setting up metrics")
if err := metrics.AddToManager(mgr); err != nil {
setupLog.Error(err, "unable to register metrics to the manager")
os.Exit(1)
}
}
func setLoggerForProduction() {
sink := zapcore.AddSync(os.Stderr)
var opts []zap.Option
encCfg := zap.NewProductionEncoderConfig()
enc := zapcore.NewJSONEncoder(encCfg)
lvl := zap.NewAtomicLevelAt(zap.WarnLevel)
opts = append(opts, zap.AddStacktrace(zap.ErrorLevel),
zap.WrapCore(func(core zapcore.Core) zapcore.Core {
return zapcore.NewSampler(core, time.Second, 100, 100)
}))
opts = append(opts, zap.AddCallerSkip(1), zap.ErrorOutput(sink))
zlog := zap.New(zapcore.NewCore(&crzap.KubeAwareEncoder{Encoder: enc, Verbose: false}, sink, lvl))
zlog = zlog.WithOptions(opts...)
newlogger := zapr.NewLogger(zlog)
ctrl.SetLogger(newlogger)
}