-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmutating_webhook.go
104 lines (85 loc) · 2.94 KB
/
mutating_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
package main
import (
"context"
"fmt"
"net/http"
"os"
appsv1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
whhttp "github.com/slok/kubewebhook/pkg/http"
whlog "github.com/slok/kubewebhook/pkg/log"
"github.com/slok/kubewebhook/pkg/observability/metrics"
mutatingwh "github.com/slok/kubewebhook/pkg/webhook/mutating"
)
// MutatingWebhook is the mutating webhook
type MutatingWebhook struct {
callback ResourceHandler
metricsRecorder *metrics.Prometheus
}
// NewMutatingWebhook creates a new MutatingWebhook instance
func NewMutatingWebhook(
callback ResourceHandler,
rec *metrics.Prometheus,
) *MutatingWebhook {
return &MutatingWebhook{callback, rec}
}
// ListenAndServeTLS creates the HTTPS server and start listening connections
func (m *MutatingWebhook) ListenAndServeTLS(port string, tlsCertFile string, tlsPrivateKeyFile string) error {
// Get the daemonsets handler for our webhook.
ds, err := createWebhookHandler("daemonsets", &appsv1.DaemonSet{}, m.callback, m.metricsRecorder)
if err != nil {
fmt.Fprintf(os.Stderr, "error creating webhook handler for daemonsets: %s", err)
return err
}
// Get the deployments handler for our webhook.
deploy, err := createWebhookHandler("deployments", &appsv1.Deployment{}, m.callback, m.metricsRecorder)
if err != nil {
fmt.Fprintf(os.Stderr, "error creating webhook handler for deployments: %s", err)
return err
}
// Get the statefulsets handler for our webhook.
sts, err := createWebhookHandler("statefulsets", &appsv1.StatefulSet{}, m.callback, m.metricsRecorder)
if err != nil {
fmt.Fprintf(os.Stderr, "error creating webhook handler for statefulsets: %s", err)
return err
}
mux := http.NewServeMux()
mux.Handle("/daemonsets", ds)
mux.Handle("/deployments", deploy)
mux.Handle("/statefulsets", sts)
// Create HTTPS server
fmt.Println("Webhook listening on port " + port)
err = http.ListenAndServeTLS(port, tlsCertFile, tlsPrivateKeyFile, mux)
if err != nil {
fmt.Fprintf(os.Stderr, "error creating webhook handler: %s", err)
return err
}
return nil
}
func createWebhookHandler(name string, obj metav1.Object, callback ResourceHandler, m *metrics.Prometheus) (http.Handler, error) {
config := mutatingwh.WebhookConfig{
Name: name,
Obj: obj,
}
fn := handlerFunc(callback)
// Create our mutator
mutatorFunc := mutatingwh.MutatorFunc(fn)
webhook, err := mutatingwh.NewWebhook(config, mutatorFunc, nil, m, &whlog.Std{Debug: true})
if err != nil {
fmt.Fprintf(os.Stderr, "error creating webhook: %s", err)
return nil, err
}
// Get the handler for our webhook.
handler, err := whhttp.HandlerFor(webhook)
if err != nil {
fmt.Fprintf(os.Stderr, "error creating webhook handler: %s", err)
return nil, err
}
return handler, nil
}
func handlerFunc(callback ResourceHandler) func(ctx context.Context, obj metav1.Object) (bool, error) {
return func(ctx context.Context, obj metav1.Object) (bool, error) {
callback(ctx, &obj)
return false, nil
}
}