This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
forked from istio/istio
/
webhook.go
575 lines (511 loc) · 17.1 KB
/
webhook.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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
// Copyright 2018 Istio 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 inject
import (
"crypto/sha256"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
"strings"
"sync"
"time"
"github.com/ghodss/yaml"
"github.com/howeyc/fsnotify"
"k8s.io/api/admission/v1beta1"
admissionregistrationv1beta1 "k8s.io/api/admissionregistration/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/kubernetes/pkg/apis/core/v1"
meshconfig "istio.io/api/mesh/v1alpha1"
"istio.io/istio/pilot/cmd"
"istio.io/istio/pkg/log"
)
var (
runtimeScheme = runtime.NewScheme()
codecs = serializer.NewCodecFactory(runtimeScheme)
deserializer = codecs.UniversalDeserializer()
// TODO(https://github.com/kubernetes/kubernetes/issues/57982)
defaulter = runtime.ObjectDefaulter(runtimeScheme)
)
const (
watchDebounceDelay = 100 * time.Millisecond
)
func init() {
_ = corev1.AddToScheme(runtimeScheme)
_ = admissionregistrationv1beta1.AddToScheme(runtimeScheme)
// The `v1` package from k8s.io/kubernetes/pkgp/apis/core/v1 has
// the object defaulting functions which are not included in
// k8s.io/api/corev1. The default functions are required by
// runtime.ObjectDefaulter to workaround lack of server-side
// defaulting with webhooks (see
// https://github.com/kubernetes/kubernetes/issues/57982).
_ = v1.AddToScheme(runtimeScheme)
}
// Webhook implements a mutating webhook for automatic proxy injection.
type Webhook struct {
mu sync.RWMutex
sidecarConfig *Config
sidecarTemplateVersion string
meshConfig *meshconfig.MeshConfig
healthCheckInterval time.Duration
healthCheckFile string
server *http.Server
meshFile string
configFile string
watcher *fsnotify.Watcher
certFile string
keyFile string
cert *tls.Certificate
}
func loadConfig(injectFile, meshFile string) (*Config, *meshconfig.MeshConfig, error) {
data, err := ioutil.ReadFile(injectFile)
if err != nil {
return nil, nil, err
}
var c Config
if err := yaml.Unmarshal(data, &c); err != nil { // nolint: vetshadow
return nil, nil, err
}
meshConfig, err := cmd.ReadMeshConfig(meshFile)
if err != nil {
return nil, nil, err
}
log.Infof("New configuration: sha256sum %x", sha256.Sum256(data))
log.Infof("Policy: %v", c.Policy)
log.Infof("Template: |\n %v", strings.Replace(c.Template, "\n", "\n ", -1))
return &c, meshConfig, nil
}
// WebhookParameters configures parameters for the sidecar injection
// webhook.
type WebhookParameters struct {
// ConfigFile is the path to the sidecar injection configuration file.
ConfigFile string
// MeshFile is the path to the mesh configuration file.
MeshFile string
// CertFile is the path to the x509 certificate for https.
CertFile string
// KeyFile is the path to the x509 private key matching `CertFile`.
KeyFile string
// Port is the webhook port, e.g. typically 443 for https.
Port int
// HealthCheckInterval configures how frequently the health check
// file is updated. Value of zero disables the health check
// update.
HealthCheckInterval time.Duration
// HealthCheckFile specifies the path to the health check file
// that is periodically updated.
HealthCheckFile string
}
// NewWebhook creates a new instance of a mutating webhook for automatic sidecar injection.
func NewWebhook(p WebhookParameters) (*Webhook, error) {
sidecarConfig, meshConfig, err := loadConfig(p.ConfigFile, p.MeshFile)
if err != nil {
return nil, err
}
pair, err := tls.LoadX509KeyPair(p.CertFile, p.KeyFile)
if err != nil {
return nil, err
}
watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
}
// watch the parent directory of the target files so we can catch
// symlink updates of k8s ConfigMaps volumes.
for _, file := range []string{p.ConfigFile, p.MeshFile, p.CertFile, p.KeyFile} {
watchDir, _ := filepath.Split(file)
if err := watcher.Watch(watchDir); err != nil {
return nil, fmt.Errorf("could not watch %v: %v", file, err)
}
}
wh := &Webhook{
server: &http.Server{
Addr: fmt.Sprintf(":%v", p.Port),
},
sidecarConfig: sidecarConfig,
sidecarTemplateVersion: sidecarTemplateVersionHash(sidecarConfig.Template),
meshConfig: meshConfig,
configFile: p.ConfigFile,
meshFile: p.MeshFile,
watcher: watcher,
healthCheckInterval: p.HealthCheckInterval,
healthCheckFile: p.HealthCheckFile,
certFile: p.CertFile,
keyFile: p.KeyFile,
cert: &pair,
}
// mtls disabled because apiserver webhook cert usage is still TBD.
wh.server.TLSConfig = &tls.Config{GetCertificate: wh.getCert}
h := http.NewServeMux()
h.HandleFunc("/inject", wh.serveInject)
wh.server.Handler = h
return wh, nil
}
// Run implements the webhook server
func (wh *Webhook) Run(stop <-chan struct{}) {
go func() {
if err := wh.server.ListenAndServeTLS("", ""); err != nil {
log.Errorf("ListenAndServeTLS for admission webhook returned error: %v", err)
}
}()
defer wh.watcher.Close() // nolint: errcheck
defer wh.server.Close() // nolint: errcheck
var healthC <-chan time.Time
if wh.healthCheckInterval != 0 && wh.healthCheckFile != "" {
t := time.NewTicker(wh.healthCheckInterval)
healthC = t.C
defer t.Stop()
}
var timerC <-chan time.Time
for {
select {
case <-timerC:
sidecarConfig, meshConfig, err := loadConfig(wh.configFile, wh.meshFile)
if err != nil {
log.Errorf("update error: %v", err)
break
}
version := sidecarTemplateVersionHash(sidecarConfig.Template)
pair, err := tls.LoadX509KeyPair(wh.certFile, wh.keyFile)
if err != nil {
log.Errorf("reload cert error: %v", err)
break
}
wh.mu.Lock()
wh.sidecarConfig = sidecarConfig
wh.sidecarTemplateVersion = version
wh.meshConfig = meshConfig
wh.cert = &pair
wh.mu.Unlock()
case event := <-wh.watcher.Event:
// use a timer to debounce configuration updates
if event.IsModify() || event.IsCreate() {
timerC = time.After(watchDebounceDelay)
}
case err := <-wh.watcher.Error:
log.Errorf("Watcher error: %v", err)
case <-healthC:
content := []byte(`ok`)
if err := ioutil.WriteFile(wh.healthCheckFile, content, 0644); err != nil {
log.Errorf("Health check update of %q failed: %v", wh.healthCheckFile, err)
}
case <-stop:
return
}
}
}
func (wh *Webhook) getCert(*tls.ClientHelloInfo) (*tls.Certificate, error) {
wh.mu.Lock()
defer wh.mu.Unlock()
return wh.cert, nil
}
// TODO(https://github.com/kubernetes/kubernetes/issues/57982)
// remove this workaround once server-side defaulting is fixed.
func applyDefaultsWorkaround(initContainers, containers []corev1.Container, volumes []corev1.Volume) {
// runtime.ObjectDefaulter only accepts top-level resources. Construct
// a dummy pod with fields we needed defaulted.
defaulter.Default(&corev1.Pod{
Spec: corev1.PodSpec{
InitContainers: initContainers,
Containers: containers,
Volumes: volumes,
},
})
}
// It would be great to use https://github.com/mattbaird/jsonpatch to
// generate RFC6902 JSON patches. Unfortunately, it doesn't produce
// correct patches for object removal. Fortunately, our patching needs
// are fairly simple so generating them manually isn't horrible (yet).
type rfc6902PatchOperation struct {
Op string `json:"op"`
Path string `json:"path"`
Value interface{} `json:"value,omitempty"`
}
// JSONPatch `remove` is applied sequentially. Remove items in reverse
// order to avoid renumbering indices.
func removeContainers(containers []corev1.Container, removed []string, path string) (patch []rfc6902PatchOperation) {
names := map[string]bool{}
for _, name := range removed {
names[name] = true
}
for i := len(containers) - 1; i >= 0; i-- {
if _, ok := names[containers[i].Name]; ok {
patch = append(patch, rfc6902PatchOperation{
Op: "remove",
Path: fmt.Sprintf("%v/%v", path, i),
})
}
}
return patch
}
func removeVolumes(volumes []corev1.Volume, removed []string, path string) (patch []rfc6902PatchOperation) {
names := map[string]bool{}
for _, name := range removed {
names[name] = true
}
for i := len(volumes) - 1; i >= 0; i-- {
if _, ok := names[volumes[i].Name]; ok {
patch = append(patch, rfc6902PatchOperation{
Op: "remove",
Path: fmt.Sprintf("%v/%v", path, i),
})
}
}
return patch
}
func removeImagePullSecrets(imagePullSecrets []corev1.LocalObjectReference, removed []string, path string) (patch []rfc6902PatchOperation) {
names := map[string]bool{}
for _, name := range removed {
names[name] = true
}
for i := len(imagePullSecrets) - 1; i >= 0; i-- {
if _, ok := names[imagePullSecrets[i].Name]; ok {
patch = append(patch, rfc6902PatchOperation{
Op: "remove",
Path: fmt.Sprintf("%v/%v", path, i),
})
}
}
return patch
}
func addContainer(target, added []corev1.Container, basePath string) (patch []rfc6902PatchOperation) {
first := len(target) == 0
var value interface{}
for _, add := range added {
value = add
path := basePath
if first {
first = false
value = []corev1.Container{add}
} else {
path = path + "/-"
}
patch = append(patch, rfc6902PatchOperation{
Op: "add",
Path: path,
Value: value,
})
}
return patch
}
func addVolume(target, added []corev1.Volume, basePath string) (patch []rfc6902PatchOperation) {
first := len(target) == 0
var value interface{}
for _, add := range added {
value = add
path := basePath
if first {
first = false
value = []corev1.Volume{add}
} else {
path = path + "/-"
}
patch = append(patch, rfc6902PatchOperation{
Op: "add",
Path: path,
Value: value,
})
}
return patch
}
func addImagePullSecrets(target, added []corev1.LocalObjectReference, basePath string) (patch []rfc6902PatchOperation) {
first := len(target) == 0
var value interface{}
for _, add := range added {
value = add
path := basePath
if first {
first = false
value = []corev1.LocalObjectReference{add}
} else {
path = path + "/-"
}
patch = append(patch, rfc6902PatchOperation{
Op: "add",
Path: path,
Value: value,
})
}
return patch
}
// escape JSON Pointer value per https://tools.ietf.org/html/rfc6901
func escapeJSONPointerValue(in string) string {
step := strings.Replace(in, "~", "~0", -1)
return strings.Replace(step, "/", "~1", -1)
}
func updateAnnotation(target map[string]string, added map[string]string) (patch []rfc6902PatchOperation) {
for key, value := range added {
if target == nil {
target = map[string]string{}
patch = append(patch, rfc6902PatchOperation{
Op: "add",
Path: "/metadata/annotations",
Value: map[string]string{
key: value,
},
})
} else {
op := "add"
if target[key] != "" {
op = "replace"
}
patch = append(patch, rfc6902PatchOperation{
Op: op,
Path: "/metadata/annotations/" + escapeJSONPointerValue(key),
Value: value,
})
}
}
return patch
}
func createPatch(pod *corev1.Pod, prevStatus *SidecarInjectionStatus, annotations map[string]string, sic *SidecarInjectionSpec) ([]byte, error) {
var patch []rfc6902PatchOperation
// Remove any containers previously injected by kube-inject using
// container and volume name as unique key for removal.
patch = append(patch, removeContainers(pod.Spec.InitContainers, prevStatus.InitContainers, "/spec/initContainers")...)
patch = append(patch, removeContainers(pod.Spec.Containers, prevStatus.Containers, "/spec/containers")...)
patch = append(patch, removeVolumes(pod.Spec.Volumes, prevStatus.Volumes, "/spec/volumes")...)
patch = append(patch, removeImagePullSecrets(pod.Spec.ImagePullSecrets, prevStatus.ImagePullSecrets, "/spec/imagePullSecrets")...)
patch = append(patch, addContainer(pod.Spec.InitContainers, sic.InitContainers, "/spec/initContainers")...)
patch = append(patch, addContainer(pod.Spec.Containers, sic.Containers, "/spec/containers")...)
patch = append(patch, addVolume(pod.Spec.Volumes, sic.Volumes, "/spec/volumes")...)
patch = append(patch, addImagePullSecrets(pod.Spec.ImagePullSecrets, sic.ImagePullSecrets, "/spec/imagePullSecrets")...)
patch = append(patch, updateAnnotation(pod.Annotations, annotations)...)
return json.Marshal(patch)
}
// Retain deprecated hardcoded container and volumes names to aid in
// backwards compatible migration to the new SidecarInjectionStatus.
var (
legacyInitContainerNames = []string{"istio-init", "enable-core-dump"}
legacyContainerNames = []string{"istio-proxy"}
legacyVolumeNames = []string{"istio-certs", "istio-envoy"}
)
func injectionStatus(pod *corev1.Pod) *SidecarInjectionStatus {
var statusBytes []byte
if pod.ObjectMeta.Annotations != nil {
if value, ok := pod.ObjectMeta.Annotations[sidecarAnnotationStatusKey]; ok {
statusBytes = []byte(value)
}
}
// default case when injected pod has explicit status
var status SidecarInjectionStatus
if err := json.Unmarshal(statusBytes, &status); err == nil {
// heuristic assumes status is valid if any of the resource
// lists is non-empty.
if len(status.InitContainers) != 0 ||
len(status.Containers) != 0 ||
len(status.Volumes) != 0 ||
len(status.ImagePullSecrets) != 0 {
return &status
}
}
// backwards compatibility case when injected pod has legacy
// status. Infer status from the list of legacy hardcoded
// container and volume names.
return &SidecarInjectionStatus{
InitContainers: legacyInitContainerNames,
Containers: legacyContainerNames,
Volumes: legacyVolumeNames,
}
}
func toAdmissionResponse(err error) *v1beta1.AdmissionResponse {
return &v1beta1.AdmissionResponse{Result: &metav1.Status{Message: err.Error()}}
}
func (wh *Webhook) inject(ar *v1beta1.AdmissionReview) *v1beta1.AdmissionResponse {
req := ar.Request
var pod corev1.Pod
if err := json.Unmarshal(req.Object.Raw, &pod); err != nil {
log.Errorf("Could not unmarshal raw object: %v", err)
return toAdmissionResponse(err)
}
log.Infof("AdmissionReview for Kind=%v Namespace=%v Name=%v (%v) UID=%v Rfc6902PatchOperation=%v UserInfo=%v",
req.Kind, req.Namespace, req.Name, pod.Name, req.UID, req.Operation, req.UserInfo)
log.Debugf("Object: %v", string(req.Object.Raw))
log.Debugf("OldObject: %v", string(req.OldObject.Raw))
if !injectRequired(ignoredNamespaces, wh.sidecarConfig.Policy, &pod.Spec, &pod.ObjectMeta) {
log.Infof("Skipping %s/%s due to policy check", pod.Namespace, pod.Name)
return &v1beta1.AdmissionResponse{
Allowed: true,
}
}
spec, status, err := injectionData(wh.sidecarConfig.Template, wh.sidecarTemplateVersion, &pod.Spec, &pod.ObjectMeta, wh.meshConfig.DefaultConfig, wh.meshConfig) // nolint: lll
if err != nil {
return toAdmissionResponse(err)
}
applyDefaultsWorkaround(spec.InitContainers, spec.Containers, spec.Volumes)
annotations := map[string]string{sidecarAnnotationStatusKey: status}
patchBytes, err := createPatch(&pod, injectionStatus(&pod), annotations, spec)
if err != nil {
return toAdmissionResponse(err)
}
log.Infof("AdmissionResponse: patch=%v\n", string(patchBytes))
reviewResponse := v1beta1.AdmissionResponse{
Allowed: true,
Patch: patchBytes,
PatchType: func() *v1beta1.PatchType {
pt := v1beta1.PatchTypeJSONPatch
return &pt
}(),
}
return &reviewResponse
}
func (wh *Webhook) serveInject(w http.ResponseWriter, r *http.Request) {
var body []byte
if r.Body != nil {
if data, err := ioutil.ReadAll(r.Body); err == nil {
body = data
}
}
if len(body) == 0 {
log.Errorf("no body found")
http.Error(w, "no body found", http.StatusBadRequest)
return
}
// verify the content type is accurate
contentType := r.Header.Get("Content-Type")
if contentType != "application/json" {
log.Errorf("contentType=%s, expect application/json", contentType)
http.Error(w, "invalid Content-Type, want `application/json`", http.StatusUnsupportedMediaType)
return
}
var reviewResponse *v1beta1.AdmissionResponse
ar := v1beta1.AdmissionReview{}
if _, _, err := deserializer.Decode(body, nil, &ar); err != nil {
log.Errorf("Could not decode body: %v", err)
reviewResponse = toAdmissionResponse(err)
} else {
reviewResponse = wh.inject(&ar)
}
response := v1beta1.AdmissionReview{}
if reviewResponse != nil {
response.Response = reviewResponse
if ar.Request != nil {
response.Response.UID = ar.Request.UID
}
}
resp, err := json.Marshal(response)
if err != nil {
log.Errorf("Could not encode response: %v", err)
http.Error(w, fmt.Sprintf("could encode response: %v", err), http.StatusInternalServerError)
}
if _, err := w.Write(resp); err != nil {
log.Errorf("Could not write response: %v", err)
http.Error(w, fmt.Sprintf("could write response: %v", err), http.StatusInternalServerError)
}
}