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