forked from fidencio/kata-webhook
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
157 lines (130 loc) · 4.31 KB
/
main.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
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"regexp"
"strings"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/sirupsen/logrus"
kwhhttp "github.com/slok/kubewebhook/v2/pkg/http"
kwhlogrus "github.com/slok/kubewebhook/v2/pkg/log/logrus"
kwhmodel "github.com/slok/kubewebhook/v2/pkg/model"
kwhmutating "github.com/slok/kubewebhook/v2/pkg/webhook/mutating"
)
func annotatePodMutator(_ context.Context, ar *kwhmodel.AdmissionReview, obj metav1.Object) (*kwhmutating.MutatorResult, error) {
pod, ok := obj.(*corev1.Pod)
if !ok {
// If not a pod just continue the mutation chain(if there is one) and don't do nothing.
return &kwhmutating.MutatorResult{}, nil
}
for k, v := range whPolicy.nsBlacklist {
fmt.Printf("key[%s] value[%s]\n", k, v)
match, _ := regexp.MatchString(k, ar.Namespace)
if match {
fmt.Println("blacklisted namespace: ", ar.Namespace)
return &kwhmutating.MutatorResult{}, nil
}
}
if whPolicy.nsBlacklist[ar.Namespace] {
fmt.Println("blacklisted namespace: ", ar.Namespace)
return &kwhmutating.MutatorResult{}, nil
}
if validRegex != nil {
if !validRegex.MatchString(ar.Namespace) {
fmt.Println("namespace doesn't match regex expression", ar.Namespace)
return &kwhmutating.MutatorResult{}, nil
}
}
// We cannot support --net=host in Kata
// https://github.com/kata-containers/documentation/blob/master/Limitations.md#docker---nethost
if pod.Spec.HostNetwork {
fmt.Println("host network: ", pod.GetNamespace(), pod.GetName())
return &kwhmutating.MutatorResult{}, nil
}
for i := range pod.Spec.Containers {
if pod.Spec.Containers[i].SecurityContext != nil && pod.Spec.Containers[i].SecurityContext.Privileged != nil {
if *pod.Spec.Containers[i].SecurityContext.Privileged {
fmt.Println("privileged container: ", pod.GetNamespace(), pod.GetName())
return &kwhmutating.MutatorResult{}, nil
}
}
}
if pod.Spec.RuntimeClassName != nil {
fmt.Println("explicit runtime: ", pod.GetNamespace(), pod.GetName(), pod.Spec.RuntimeClassName)
return &kwhmutating.MutatorResult{}, nil
}
// Mutate the pod
fmt.Println("setting runtime to kata: ", pod.GetNamespace(), pod.GetName())
kataRuntimeClassName := "kata"
pod.Spec.RuntimeClassName = &kataRuntimeClassName
return &kwhmutating.MutatorResult{
MutatedObject: pod,
}, nil
}
type config struct {
certFile string
keyFile string
nsBlacklist string
regexStr string
}
type policy struct {
nsBlacklist map[string]bool
}
var whPolicy *policy
var validRegex *regexp.Regexp
func initFlags() *config {
cfg := &config{}
fl := flag.NewFlagSet(os.Args[0], flag.ExitOnError)
fl.StringVar(&cfg.certFile, "tls-cert-file", "", "TLS certificate file")
fl.StringVar(&cfg.keyFile, "tls-key-file", "", "TLS key file")
fl.StringVar(&cfg.nsBlacklist, "exclude-regex-namespaces", "", "Comma separated namespace blacklist")
fl.StringVar(&cfg.regexStr, "regex-matching-namespaces", "", "regex string, will mutate only on matching namespaces, blacklisted namespaces will be ingnored")
fl.Parse(os.Args[1:])
return cfg
}
func main() {
logrusLogEntry := logrus.NewEntry(logrus.New())
logrusLogEntry.Logger.SetLevel(logrus.DebugLevel)
logger := kwhlogrus.NewLogrus(logrusLogEntry)
cfg := initFlags()
whPolicy = &policy{}
whPolicy.nsBlacklist = make(map[string]bool)
if cfg.nsBlacklist != "" {
for _, s := range strings.Split(cfg.nsBlacklist, ",") {
whPolicy.nsBlacklist[s] = true
}
}
if cfg.regexStr != "" {
// panic on invalid expression
validRegex = regexp.MustCompile(cfg.regexStr)
}
// Create our mutator
mt := kwhmutating.MutatorFunc(annotatePodMutator)
mcfg := kwhmutating.WebhookConfig{
ID: "podAnnotate",
Obj: &corev1.Pod{},
Mutator: mt,
Logger: logger,
}
wh, err := kwhmutating.NewWebhook(mcfg)
if err != nil {
fmt.Fprintf(os.Stderr, "error creating webhook: %s", err)
os.Exit(1)
}
// Get the handler for our webhook.
whHandler, err := kwhhttp.HandlerFor(kwhhttp.HandlerConfig{Webhook: wh, Logger: logger})
if err != nil {
fmt.Fprintf(os.Stderr, "error creating webhook handler: %s", err)
os.Exit(1)
}
logger.Infof("Listening on :8080")
err = http.ListenAndServeTLS(":8080", cfg.certFile, cfg.keyFile, whHandler)
if err != nil {
fmt.Fprintf(os.Stderr, "error serving webhook: %s", err)
os.Exit(1)
}
}