...
1
2
3
4
5
6
7
8
9
10
11
12
13
14 package webhook
15
16 import (
17 "context"
18 "net/http"
19
20 "github.com/chaos-mesh/chaos-mesh/controllers/metrics"
21 controllerCfg "github.com/chaos-mesh/chaos-mesh/pkg/config"
22 "github.com/chaos-mesh/chaos-mesh/pkg/webhook/config"
23 "github.com/chaos-mesh/chaos-mesh/pkg/webhook/inject"
24
25 ctrl "sigs.k8s.io/controller-runtime"
26 "sigs.k8s.io/controller-runtime/pkg/client"
27 "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
28
29 v1 "k8s.io/api/core/v1"
30 )
31
32 var log = ctrl.Log.WithName("inject-webhook")
33
34
35
36
37 type PodInjector struct {
38 client client.Client
39 decoder *admission.Decoder
40 Config *config.Config
41 ControllerCfg *controllerCfg.ChaosControllerConfig
42 Metrics *metrics.ChaosCollector
43 }
44
45
46 func (v *PodInjector) Handle(ctx context.Context, req admission.Request) admission.Response {
47 pod := &v1.Pod{}
48
49 err := v.decoder.Decode(req, pod)
50 if err != nil {
51 return admission.Errored(http.StatusBadRequest, err)
52 }
53
54 log.Info("Get request from pod:", "pod", pod)
55
56 return admission.Response{
57 AdmissionResponse: *inject.Inject(&req.AdmissionRequest, v.client, v.Config, v.ControllerCfg, v.Metrics),
58 }
59 }
60
61
62 func (v *PodInjector) InjectClient(c client.Client) error {
63 v.client = c
64 return nil
65 }
66
67
68 func (v *PodInjector) InjectDecoder(d *admission.Decoder) error {
69 v.decoder = d
70 return nil
71 }
72