/
wireupwebhook.go
485 lines (381 loc) · 14.7 KB
/
wireupwebhook.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
// Copyright 2021 The Kubernetes Authors.
//
// 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 webhook
import (
"context"
"errors"
"fmt"
"os"
"strings"
gerr "github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
apixv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/intstr"
admissionv1 "k8s.io/api/admissionregistration/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"github.com/go-logr/logr"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)
type WireUp struct {
mgr manager.Manager
ctx context.Context
Server *webhook.Server
Handler webhook.AdmissionHandler
CertDir string
logr.Logger
WebhookName string
WebHookPort int
WebHookeSvcKey types.NamespacedName
WebHookServicePort int
ValidtorPath string
DeployLabel string
DeploymentSelector map[string]string
}
func NewWireUp(ctx context.Context, mgr manager.Manager,
opts ...func(*WireUp)) (*WireUp, error) {
WebhookName := "channels-apps-open-cluster-management-webhook"
deployLabelEnvVar := "DEPLOYMENT_LABEL"
deploymentLabel, err := findEnvVariable(deployLabelEnvVar)
if err != nil {
return nil, gerr.Wrap(err, "failed to create a new webhook wireup")
}
deploymentSelectKey := "app"
deploymentSelector := map[string]string{deploymentSelectKey: deploymentLabel}
podNamespaceEnvVar := "POD_NAMESPACE"
podNamespace, err := findEnvVariable(podNamespaceEnvVar)
if err != nil {
return nil, gerr.Wrap(err, "failed to create a new webhook wireup")
}
wireUp := &WireUp{
mgr: mgr,
ctx: ctx,
Server: mgr.GetWebhookServer(),
Logger: logf.Log.WithName("webhook"),
WebhookName: WebhookName,
WebHookPort: 9443,
WebHookServicePort: 443,
ValidtorPath: "/duplicate-validation",
WebHookeSvcKey: types.NamespacedName{Name: GetWebHookServiceName(WebhookName), Namespace: podNamespace},
DeployLabel: deploymentLabel,
DeploymentSelector: deploymentSelector,
}
for _, op := range opts {
op(wireUp)
}
return wireUp, nil
}
func GetValidatorName(wbhName string) string {
//domain style, separate by dots
return strings.ReplaceAll(wbhName, "-", ".") + ".validator"
}
func GetWebHookServiceName(wbhName string) string {
//k8s resrouce nameing, separate by '-'
return wbhName + "-svc"
}
func (w *WireUp) Attach(clt client.Client) ([]byte, error) {
w.Logger.Info("entry Attach webhook")
defer w.Logger.Info("exit Attach webhook")
w.Server.Port = w.WebHookPort
w.Logger.Info("registering webhooks to the webhook server")
w.Server.Register(w.ValidtorPath, &webhook.Admission{Handler: w.Handler})
return GenerateWebhookCerts(clt, w.CertDir, w.WebHookeSvcKey.Namespace, w.WebHookeSvcKey.Name)
}
type CleanUpFunc func(client.Client) error
func DelPreValiationCfg20(clt client.Client) error {
pCfg := &admissionv1.ValidatingWebhookConfiguration{}
pCfgKey := types.NamespacedName{Name: "channel-webhook-validator"}
ctx := context.TODO()
if err := clt.Get(ctx, pCfgKey, pCfg); err != nil {
if apierrors.IsNotFound(err) {
return nil
}
return err
}
return clt.Delete(ctx, pCfg)
}
// assuming we have a service set up for the webhook, and the service is linking
// to a secret which has the CA
func (w *WireUp) WireUpWebhookSupplymentryResource(isExternalAPIServer bool, inClient client.Client,
caCert []byte, gvk schema.GroupVersionKind, ops []admissionv1.OperationType, cFuncs ...CleanUpFunc) error {
w.Logger.Info("entry wire up webhook resources")
defer w.Logger.Info("exit wire up webhook resources")
if !w.mgr.GetCache().WaitForCacheSync(w.ctx) {
w.Logger.Error(gerr.New("cache not started"), "failed to start up cache")
}
w.Logger.Info("cache is ready to consume")
for _, cf := range cFuncs {
if err := cf(w.mgr.GetClient()); err != nil {
return gerr.Wrap(err, "failed to clean up")
}
}
return gerr.Wrap(w.createOrUpdateValiationWebhook(isExternalAPIServer, inClient, caCert, gvk, ops), "failed to set up the validation webhook config")
}
func findEnvVariable(envName string) (string, error) {
val, found := os.LookupEnv(envName)
if !found {
return "", fmt.Errorf("%s env var is not set", envName)
}
return val, nil
}
func (w *WireUp) getOrCreateWebhookService(isExternalAPIServer bool, inClusterClient client.Client) error {
service := &corev1.Service{}
outCLusterClient := w.mgr.GetClient()
// 1. On the management cluster, create the service for exposing the channel webhook server (channel pod)
err := inClusterClient.Get(context.TODO(), w.WebHookeSvcKey, service)
if err == nil {
// This channel container could be running in a different pod. Delete and re-create the service
// to ensure that the service always points to the correct target pod.
deleteErr := inClusterClient.Delete(context.TODO(), service)
if deleteErr != nil {
w.Logger.Error(gerr.New(fmt.Sprintf("failed to delete existing service %s", w.WebHookeSvcKey.String())),
fmt.Sprintf("failed to delete existing service %s", w.WebHookeSvcKey.String()))
return deleteErr
}
w.Logger.Info(fmt.Sprintf("deleted existing service %s", w.WebHookeSvcKey.String()))
}
w.Logger.Info(fmt.Sprintf("creating in Cluster service %s ", w.WebHookeSvcKey.String()))
newService := newWebhookServiceTemplate(false, w.WebHookeSvcKey, w.WebHookPort, w.WebHookServicePort, w.DeploymentSelector)
setOwnerReferences(inClusterClient, w.Logger, w.WebHookeSvcKey.Namespace, w.DeployLabel, newService)
if err := inClusterClient.Create(context.TODO(), newService); err != nil {
return err
}
w.Logger.Info(fmt.Sprintf("created in Cluster service %s ", w.WebHookeSvcKey.String()))
// 2. If isExternalAPIServer = true, create the additional service in the hosted cluster
if !isExternalAPIServer {
return nil
}
service = &corev1.Service{}
err = outCLusterClient.Get(context.TODO(), w.WebHookeSvcKey, service)
if err == nil {
// Delete and re-create the service to ensure that the service always points to the correct target pod.
deleteErr := outCLusterClient.Delete(context.TODO(), service)
if deleteErr != nil {
w.Logger.Error(gerr.New(fmt.Sprintf("failed to delete existing service %s on hosted cluster", w.WebHookeSvcKey.String())),
fmt.Sprintf("failed to delete existing service %s on hosted cluster", w.WebHookeSvcKey.String()))
return deleteErr
}
w.Logger.Info(fmt.Sprintf("deleted existing service %s on hosted cluster", w.WebHookeSvcKey.String()))
}
newService = newWebhookServiceTemplate(true, w.WebHookeSvcKey, w.WebHookPort, w.WebHookServicePort, w.DeploymentSelector)
if err := outCLusterClient.Create(context.TODO(), newService); err != nil {
return err
}
w.Logger.Info(fmt.Sprintf("created hosted cluster service %s ", w.WebHookeSvcKey.String()))
// 3. get the service Cluster IP of the webhook server running on the management cluster. The service is created in step 1
service = &corev1.Service{}
if err = inClusterClient.Get(context.TODO(), w.WebHookeSvcKey, service); err != nil {
return err
}
serviceClusterIP := service.Spec.ClusterIP
if serviceClusterIP == "" {
return errors.New("no service Cluster IP found: " + w.WebHookeSvcKey.String())
}
// 4. If isExternalAPIServer = true, create the additional endpoint in the hosted cluster
endpoint := &corev1.Endpoints{}
err = outCLusterClient.Get(context.TODO(), w.WebHookeSvcKey, endpoint)
if err == nil {
// Delete and re-create the endpoint to ensure that the service always points to the correct endpoint
deleteErr := outCLusterClient.Delete(context.TODO(), endpoint)
if deleteErr != nil {
w.Logger.Error(gerr.New(fmt.Sprintf("failed to delete existing endpoint %s on hosted cluster", w.WebHookeSvcKey.String())),
fmt.Sprintf("failed to delete existing endpoint %s on hosted cluster", w.WebHookeSvcKey.String()))
return deleteErr
}
w.Logger.Info(fmt.Sprintf("deleted existing endpoint %s on hosted cluster", w.WebHookeSvcKey.String()))
}
newEndpoint := newWebhookEndpointTemplate(w.WebHookeSvcKey, w.WebHookServicePort, serviceClusterIP)
if err := outCLusterClient.Create(context.TODO(), newEndpoint); err != nil {
return err
}
w.Logger.Info(fmt.Sprintf("created hosted cluster endpoint %s ", w.WebHookeSvcKey.String()))
return nil
}
func (w *WireUp) createOrUpdateValiationWebhook(isExternalAPIServer bool, inClient client.Client,
ca []byte, gvk schema.GroupVersionKind,
ops []admissionv1.OperationType) error {
validator := &admissionv1.ValidatingWebhookConfiguration{}
key := types.NamespacedName{Name: GetValidatorName(w.WebhookName)}
validatorName := GetValidatorName(w.WebhookName)
if err := w.mgr.GetClient().Get(context.TODO(), key, validator); err != nil {
if apierrors.IsNotFound(err) { // create a new validator
cfg := newValidatingWebhookCfg(w.WebHookeSvcKey, validatorName, w.ValidtorPath, ca, gvk, ops)
setWebhookOwnerReferences(w.mgr.GetClient(), w.Logger, cfg)
if err := w.mgr.GetClient().Create(context.TODO(), cfg); err != nil {
return gerr.Wrap(err, fmt.Sprintf("Failed to create validating webhook %s", validatorName))
}
w.Logger.Info(fmt.Sprintf("Create validating webhook %s", validatorName))
}
} else { // update the existing validator
validator.Webhooks[0].ClientConfig.Service.Namespace = w.WebHookeSvcKey.Namespace
validator.Webhooks[0].ClientConfig.Service.Name = w.WebHookeSvcKey.Name
validator.Webhooks[0].ClientConfig.CABundle = ca
ignore := admissionv1.Ignore
timeoutSeconds := int32(30)
validator.Webhooks[0].FailurePolicy = &ignore
validator.Webhooks[0].TimeoutSeconds = &timeoutSeconds
setWebhookOwnerReferences(w.mgr.GetClient(), w.Logger, validator)
if err := w.mgr.GetClient().Update(context.TODO(), validator); err != nil {
return gerr.Wrap(err, fmt.Sprintf("Failed to update validating webhook %s", validatorName))
}
w.Logger.Info(fmt.Sprintf("Update validating webhook %s", validatorName))
}
// make sure the service of the validator exists
return gerr.Wrap(w.getOrCreateWebhookService(isExternalAPIServer, inClient), "failed to set up service for webhook")
}
func setOwnerReferences(c client.Client, logger logr.Logger, deployNs string, deployLabel string, obj metav1.Object) {
key := types.NamespacedName{Name: deployLabel, Namespace: deployNs}
owner := &appsv1.Deployment{}
if err := c.Get(context.TODO(), key, owner); err != nil {
logger.Error(err, fmt.Sprintf("Failed to set owner references for %s", obj.GetName()))
return
}
logger.Info(fmt.Sprintf("apiversion: %v, kind: %v, name: %v, uid: %v", owner.APIVersion, owner.Kind, owner.Name, owner.UID))
obj.SetOwnerReferences([]metav1.OwnerReference{
{
APIVersion: "apps/v1",
Kind: "Deployment",
Name: owner.Name,
UID: owner.UID,
},
})
}
func setWebhookOwnerReferences(c client.Client, logger logr.Logger, obj metav1.Object) {
channelCrdName := "channels.apps.open-cluster-management.io"
key := types.NamespacedName{Name: channelCrdName}
owner := &apixv1.CustomResourceDefinition{}
if err := c.Get(context.TODO(), key, owner); err != nil {
logger.Error(err, fmt.Sprintf("Failed to set webhook owner references for %s", obj.GetName()))
return
}
obj.SetOwnerReferences([]metav1.OwnerReference{
{
APIVersion: owner.APIVersion,
Kind: owner.Kind,
Name: owner.Name,
UID: owner.UID,
},
})
}
func newWebhookServiceTemplate(isExternalAPIServer bool, svcKey types.NamespacedName, webHookPort,
webHookServicePort int, deploymentSelector map[string]string) *corev1.Service {
if isExternalAPIServer {
// if the service is created on hosted cluster, no selector and target port should be specicified as the webhook server pod is not running there
return &corev1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: svcKey.Name,
Namespace: svcKey.Namespace,
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Port: int32(webHookServicePort),
},
},
},
}
}
return &corev1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: svcKey.Name,
Namespace: svcKey.Namespace,
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Port: int32(webHookServicePort),
TargetPort: intstr.FromInt(webHookPort),
},
},
Selector: deploymentSelector,
},
}
}
func newWebhookEndpointTemplate(svcKey types.NamespacedName, webHookServicePort int, serviceClusterIP string) *corev1.Endpoints {
return &corev1.Endpoints{
TypeMeta: metav1.TypeMeta{
Kind: "Endpoints",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: svcKey.Name,
Namespace: svcKey.Namespace,
},
Subsets: []corev1.EndpointSubset{
{
Addresses: []corev1.EndpointAddress{
{
IP: serviceClusterIP,
},
},
Ports: []corev1.EndpointPort{
{
Port: int32(webHookServicePort),
},
},
},
},
}
}
func newValidatingWebhookCfg(svcKey types.NamespacedName, validatorName, path string, ca []byte,
gvk schema.GroupVersionKind, ops []admissionv1.OperationType) *admissionv1.ValidatingWebhookConfiguration {
ignore := admissionv1.Ignore
side := admissionv1.SideEffectClassNone
timeoutSeconds := int32(30)
return &admissionv1.ValidatingWebhookConfiguration{
ObjectMeta: metav1.ObjectMeta{
Name: validatorName,
},
Webhooks: []admissionv1.ValidatingWebhook{{
Name: validatorName,
AdmissionReviewVersions: []string{"v1beta1"},
SideEffects: &side,
FailurePolicy: &ignore,
ClientConfig: admissionv1.WebhookClientConfig{
Service: &admissionv1.ServiceReference{
Name: svcKey.Name,
Namespace: svcKey.Namespace,
Path: &path,
},
CABundle: ca,
},
Rules: []admissionv1.RuleWithOperations{
{
Rule: admissionv1.Rule{
APIGroups: []string{gvk.Group},
APIVersions: []string{gvk.Version},
Resources: []string{gvk.Kind},
},
Operations: ops,
},
},
TimeoutSeconds: &timeoutSeconds,
},
},
}
}