forked from kata-containers/tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
139 lines (113 loc) · 3.45 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
// Copyright (c) 2019 Intel Corporation
//
// SPDX-License-Identifier: Apache-2.0
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"strings"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
whhttp "github.com/slok/kubewebhook/pkg/http"
"github.com/slok/kubewebhook/pkg/log"
mctx "github.com/slok/kubewebhook/pkg/webhook/context"
mutatingwh "github.com/slok/kubewebhook/pkg/webhook/mutating"
)
func annotatePodMutator(ctx context.Context, obj metav1.Object) (bool, 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 anything
return false, nil
}
request := mctx.GetAdmissionRequest(ctx)
if request == nil {
return false, nil
}
// The Namespace is not always available in the pod Spec
// specially when operators create the pods. Hence access
// the Namespace in the actual request (vs the object)
// https://godoc.org/k8s.io/api/admission/v1beta1#AdmissionRequest
if whPolicy.nsBlacklist[request.Namespace] {
fmt.Println("blacklisted namespace: ", request.Namespace)
return false, 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 false, nil
}
for i := range pod.Spec.Containers {
if pod.Spec.Containers[i].SecurityContext != nil {
if *pod.Spec.Containers[i].SecurityContext.Privileged {
fmt.Println("privileged container: ", pod.GetNamespace(), pod.GetName())
return false, nil
}
}
}
if pod.Spec.RuntimeClassName != nil {
fmt.Println("explicit runtime: ", pod.GetNamespace(), pod.GetName(), pod.Spec.RuntimeClassName)
return false, nil
}
// Mutate the pod
fmt.Println("setting runtime to kata: ", pod.GetNamespace(), pod.GetName())
kataRuntimeClassName := "kata"
pod.Spec.RuntimeClassName = &kataRuntimeClassName
return false, nil
}
type config struct {
certFile string
keyFile string
nsBlacklist string
}
type policy struct {
nsBlacklist map[string]bool
}
var whPolicy *policy
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-namespaces", "", "Comma separated namespace blacklist")
fl.Parse(os.Args[1:])
return cfg
}
func main() {
logger := &log.Std{Debug: true}
cfg := initFlags()
whPolicy = &policy{}
whPolicy.nsBlacklist = make(map[string]bool)
if cfg.nsBlacklist != "" {
for _, s := range strings.Split(cfg.nsBlacklist, ",") {
whPolicy.nsBlacklist[s] = true
}
}
// Create our mutator
mt := mutatingwh.MutatorFunc(annotatePodMutator)
mcfg := mutatingwh.WebhookConfig{
Name: "podAnnotate",
Obj: &corev1.Pod{},
}
wh, err := mutatingwh.NewWebhook(mcfg, mt, nil, nil, logger)
if err != nil {
fmt.Fprintf(os.Stderr, "error creating webhook: %s", err)
os.Exit(1)
}
// Get the handler for our webhook.
whHandler, err := whhttp.HandlerFor(wh)
if err != nil {
fmt.Fprintf(os.Stderr, "error creating webhook handler: %s", err)
os.Exit(1)
}
port := ":8080"
logger.Infof("Listening on %s", port)
err = http.ListenAndServeTLS(port, cfg.certFile, cfg.keyFile, whHandler)
if err != nil {
fmt.Fprintf(os.Stderr, "error serving webhook: %s", err)
os.Exit(1)
}
}