-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
95 lines (81 loc) · 2.2 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
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"github.com/devopstoday11/sigrun/pkg/controller"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/api/admission/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const PORT = "8080"
func main() {
var tlscert, tlskey string
flag.StringVar(&tlscert, "tlsCertFile", "/etc/certs/tls.crt", "File containing the x509 Certificate for HTTPS.")
flag.StringVar(&tlskey, "tlsKeyFile", "/etc/certs/tls.key", "File containing the x509 private key to --tlsCertFile.")
flag.Parse()
kRestConf, err := rest.InClusterConfig()
if err != nil {
log.Fatal(err)
}
kclient, err := kubernetes.NewForConfig(kRestConf)
if err != nil {
log.Fatal(err)
}
cache := controller.NewConfigMapCache(kclient)
mux := http.NewServeMux()
mux.HandleFunc("/validate", func(w http.ResponseWriter, r *http.Request) {
arResponse, err := validateHandler(cache, r)
if err != nil {
switch err.(type) {
case controller.ContainerValidationError:
json.NewEncoder(w).Encode(v1beta1.AdmissionReview{
Response: &v1beta1.AdmissionResponse{
Allowed: false,
Result: &metav1.Status{
Message: err.Error(),
},
},
})
return
default:
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
}
json.NewEncoder(w).Encode(arResponse)
})
log.Printf("Server running listening in port: %s\n", PORT)
if err := http.ListenAndServeTLS(fmt.Sprintf(":%v", PORT), tlscert, tlskey, mux); err != nil {
log.Printf("Failed to listen and serve webhook server: %v", err)
}
}
func validateHandler(cache *controller.ConfigMapCache, r *http.Request) (*v1beta1.AdmissionReview, error) {
var req v1beta1.AdmissionReview
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
return nil, err
}
configMap, err := cache.Get()
if err != nil {
return nil, err
}
containers, err := controller.GetContainersFromResource(&req)
if err != nil {
return nil, err
}
err = controller.ValidateContainers(configMap, containers)
if err != nil {
return nil, err
}
return &v1beta1.AdmissionReview{
Response: &v1beta1.AdmissionResponse{
Allowed: true,
},
}, nil
}