forked from gardener/kupid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
333 lines (290 loc) · 11 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file.
//
// 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 (
"context"
"flag"
"os"
"time"
uberzap "go.uber.org/zap"
"go.uber.org/zap/zapcore"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
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"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/manager"
appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
extensionswebhook "github.com/gardener/gardener/extensions/pkg/webhook"
kupidv1alpha1 "github.com/gardener/kupid/api/v1alpha1"
"github.com/gardener/kupid/pkg/webhook"
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("setup")
)
const (
webhookName = "kupid"
webhookFullName = "gardener-extension-" + webhookName
flagWebhookPort = "webhook-port"
flagMetricsAddr = "metrics-addr"
flagHealthzAddr = "healthz-addr"
flagCertDir = "cert-dir"
flagRegisterWebhooks = "register-webhooks"
flagWebhookTimeoutSeconds = "webhook-timeout-seconds"
flagSyncPeriod = "sync-period"
flagQPS = "qps"
flagBurst = "burst"
envNamespace = "WEBHOOK_CONFIG_NAMESPACE"
defaultWebhookPort = 9443
defaultWebhookTimeoutSeconds = 30
defaultMetricsAddr = ":8081"
defaultHealthzAddr = ":8080"
defaultSyncPeriod = 1 * time.Hour
)
func init() {
_ = clientgoscheme.AddToScheme(scheme)
_ = kupidv1alpha1.AddToScheme(scheme)
_ = corev1.AddToScheme(scheme)
_ = appsv1.AddToScheme(scheme)
_ = batchv1.AddToScheme(scheme)
_ = batchv1beta1.AddToScheme(scheme)
}
func main() {
var (
webhookPort int
metricsAddr string
healthzAddr string
certDir string
registerWebhooks bool
webhookTimeoutSeconds int
syncPeriod time.Duration
qps float64
burst int
namespace string
logLevel = uberzap.LevelFlag("v", zapcore.InfoLevel, "Logging level")
)
flag.IntVar(&webhookPort, flagWebhookPort, defaultWebhookPort, "The port for the webhook server to listen on.")
flag.StringVar(&metricsAddr, flagMetricsAddr, defaultMetricsAddr, "The address the metric endpoint binds to.")
flag.StringVar(&healthzAddr, flagHealthzAddr, defaultHealthzAddr, "The address the healthz endpoint binds to.")
flag.StringVar(&certDir, flagCertDir, "./certs", "The directory where the serving certs are kept.")
flag.BoolVar(®isterWebhooks, flagRegisterWebhooks, false, "If enabled registers the webhook configurations automatically. The webhook is assumed to be reachable by a service with the name 'gardener-extension-kupid' within the same namespace. If necessary this will also generate TLS certificates for the webhook as well as the webhook configurations. The generated certificates will be published by creating a secret with the name 'gardener-extension-webhook-cert'. If the secret already exists then it is reused and no new certificates are generated.")
flag.IntVar(&webhookTimeoutSeconds, flagWebhookTimeoutSeconds, defaultWebhookTimeoutSeconds, "If webhooks are registered automatically then they are configured with this timeout.")
flag.DurationVar(&syncPeriod, flagSyncPeriod, defaultSyncPeriod, "SyncPeriod determines the minimum frequency at which watched resources are reconciled. A lower period will correct entropy more quickly, but reduce responsiveness to change if there are many watched resources. Change this value only if you know what you are doing.")
flag.Float64Var(&qps, flagQPS, float64(rest.DefaultQPS), "Throttling QPS configuration for the client to host apiserver.")
flag.IntVar(&burst, flagBurst, rest.DefaultBurst, "Throttling burst configuration for the client to host apiserver.")
flag.Parse()
level := uberzap.NewAtomicLevelAt(*logLevel)
ctrl.SetLogger(zap.New(zap.Level(&level)))
namespace = os.Getenv(envNamespace)
setupLog.Info("Running with",
flagWebhookPort, webhookPort,
flagMetricsAddr, metricsAddr,
flagCertDir, certDir,
flagRegisterWebhooks, registerWebhooks,
flagWebhookTimeoutSeconds, webhookTimeoutSeconds,
flagSyncPeriod, syncPeriod,
flagQPS, qps,
flagBurst, burst,
envNamespace, namespace)
config := ctrl.GetConfigOrDie()
config.QPS = float32(qps)
config.Burst = burst
mgr, err := ctrl.NewManager(config, ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: webhookPort,
HealthProbeBindAddress: healthzAddr,
SyncPeriod: &syncPeriod,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
os.Exit(1)
}
// Initialize webhook certificates
func() {
ws := mgr.GetWebhookServer()
ws.CertDir = certDir
}()
if registerWebhooks {
if err := doRegisterWebhooks(mgr, certDir, namespace, int32(webhookTimeoutSeconds)); err != nil {
setupLog.Error(err, "Error registering webhooks. Aborting startup...")
os.Exit(1)
}
}
if w, err := webhook.NewDefaultWebhook(); err != nil {
setupLog.Error(err, "unable to create default webhook", "webhook", webhookName)
} else if err := w.SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to setup", "webhook", webhookName)
os.Exit(1)
}
setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
os.Exit(1)
}
}
func doRegisterWebhooks(mgr manager.Manager, certDir, namespace string, timeoutSeconds int32) error {
client, err := getClient(mgr)
if err != nil {
return err
}
ctx := context.TODO()
setupLog.Info("Registering TLS certificates if necessary.")
caBundle, err := extensionswebhook.GenerateCertificates(
ctx,
mgr,
certDir,
namespace,
webhookName,
extensionswebhook.ModeService,
"",
)
if err != nil {
return err
}
clientConfig := buildWebhookClientConfig(namespace, caBundle)
setupLog.Info("Registering webhooks if necessary.")
for _, f := range []webhookConfigGeneratorFn{
newValidatingWebhookConfig,
newMutatingWebhookConfig,
} {
obj, mutateFn := f(clientConfig, timeoutSeconds)
if _, err := controllerutil.CreateOrUpdate(ctx, client, obj, mutateFn); err != nil {
return err
}
}
return nil
}
func buildWebhookClientConfig(namespace string, caBundle []byte) admissionregistrationv1beta1.WebhookClientConfig {
var path = webhook.WebhookPath
return admissionregistrationv1beta1.WebhookClientConfig{
CABundle: caBundle,
Service: &admissionregistrationv1beta1.ServiceReference{
Namespace: namespace,
Name: webhookFullName,
Path: &path,
},
}
}
func buildRuleWithOperations(gv schema.GroupVersion, resources []string) admissionregistrationv1beta1.RuleWithOperations {
return admissionregistrationv1beta1.RuleWithOperations{
Operations: []admissionregistrationv1beta1.OperationType{
admissionregistrationv1beta1.Create,
admissionregistrationv1beta1.Update,
},
Rule: admissionregistrationv1beta1.Rule{
APIGroups: []string{gv.Group},
APIVersions: []string{gv.Version},
Resources: resources,
},
}
}
type webhookConfigGeneratorFn func(clientConfig admissionregistrationv1beta1.WebhookClientConfig, timeoutSeconds int32) (runtime.Object, controllerutil.MutateFn)
func newValidatingWebhookConfig(clientConfig admissionregistrationv1beta1.WebhookClientConfig, timeoutSeconds int32) (runtime.Object, controllerutil.MutateFn) {
var (
ignore = admissionregistrationv1beta1.Ignore
exact = admissionregistrationv1beta1.Exact
none = admissionregistrationv1beta1.SideEffectClassNone
)
obj := &admissionregistrationv1beta1.ValidatingWebhookConfiguration{
ObjectMeta: metav1.ObjectMeta{
Name: webhookFullName,
},
}
return obj, func() error {
obj.Webhooks = []admissionregistrationv1beta1.ValidatingWebhook{
{
Name: "validate." + kupidv1alpha1.GroupVersion.Group,
ClientConfig: clientConfig,
Rules: []admissionregistrationv1beta1.RuleWithOperations{
buildRuleWithOperations(kupidv1alpha1.GroupVersion, []string{
"clusterpodschedulingpolicies",
"podschedulingpolicies",
}),
},
FailurePolicy: &ignore,
MatchPolicy: &exact,
SideEffects: &none,
TimeoutSeconds: &timeoutSeconds,
},
}
return nil
}
}
func newMutatingWebhookConfig(clientConfig admissionregistrationv1beta1.WebhookClientConfig, timeoutSeconds int32) (runtime.Object, controllerutil.MutateFn) {
var (
ignore = admissionregistrationv1beta1.Ignore
equivalent = admissionregistrationv1beta1.Equivalent
none = admissionregistrationv1beta1.SideEffectClassNone
ifNeeded = admissionregistrationv1beta1.IfNeededReinvocationPolicy
)
obj := &admissionregistrationv1beta1.MutatingWebhookConfiguration{
ObjectMeta: metav1.ObjectMeta{
Name: webhookFullName,
},
}
return obj, func() error {
obj.Webhooks = []admissionregistrationv1beta1.MutatingWebhook{
{
Name: "mutate." + kupidv1alpha1.GroupVersion.Group,
NamespaceSelector: &metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
metav1.LabelSelectorRequirement{
Key: "role",
Operator: metav1.LabelSelectorOpNotIn,
Values: []string{"kube-system"},
},
},
},
ClientConfig: clientConfig,
Rules: []admissionregistrationv1beta1.RuleWithOperations{
buildRuleWithOperations(appsv1.SchemeGroupVersion, []string{
"daemonsets",
"deployments",
"statefulsets",
}),
buildRuleWithOperations(batchv1.SchemeGroupVersion, []string{
"jobs",
}),
buildRuleWithOperations(batchv1beta1.SchemeGroupVersion, []string{
"cronjobs",
}),
},
FailurePolicy: &ignore,
MatchPolicy: &equivalent,
SideEffects: &none,
TimeoutSeconds: &timeoutSeconds,
ReinvocationPolicy: &ifNeeded,
},
}
return nil
}
}
func getClient(mgr manager.Manager) (client.Client, error) {
s := runtime.NewScheme()
if err := clientgoscheme.AddToScheme(s); err != nil {
return nil, err
}
return client.New(mgr.GetConfig(), client.Options{Scheme: s})
}