-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
117 lines (95 loc) · 2.69 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"time"
"github.com/spf13/cobra"
"k8s.io/api/admission/v1beta1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
var apiserver string
var kubeconfig string
var rootCmd = &cobra.Command{
Use: "blob-csi-webhook",
Short: "A Mutating webhook for automounting labeled PVCs",
Long: `A Mutating webhook for automounting labeled PVCs`,
}
type server struct {
client *kubernetes.Clientset
}
// Based on https://medium.com/ovni/writing-a-very-basic-kubernetes-mutating-admission-webhook-398dbbcb63ec
func (s *server) handleRoot(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, world!")
}
func (s *server) handleHealthz(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "ok")
}
func (s *server) handleMutate(w http.ResponseWriter, r *http.Request) {
// Decode the request
body, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%s", err)
return
}
admissionReview := v1beta1.AdmissionReview{}
if err := json.Unmarshal(body, &admissionReview); err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%s", err)
return
}
response, err := s.mutate(*admissionReview.Request)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%s", err)
return
}
reviewResponse := v1beta1.AdmissionReview{
Response: &response,
}
if body, err = json.Marshal(reviewResponse); err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%s", err)
return
}
w.WriteHeader(http.StatusOK)
w.Write(body)
}
func main() {
rootCmd.PersistentFlags().StringVar(&apiserver, "apiserver", "", "URL to the Kubernetes API server")
rootCmd.PersistentFlags().StringVar(&kubeconfig, "kubeconfig", "", "Path to the Kubeconfig file")
rootCmd.Execute()
// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags(apiserver, kubeconfig)
if err != nil {
panic(err.Error())
}
// create the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
s := server{clientset}
mux := http.NewServeMux()
mux.HandleFunc("/", s.handleRoot)
mux.HandleFunc("/_healthz", s.handleHealthz)
mux.HandleFunc("/mutate", s.handleMutate)
httpServer := &http.Server{
Addr: ":8443",
Handler: mux,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
log.Println("Listening on :8443")
log.Fatal(httpServer.ListenAndServeTLS("./certs/tls.crt", "./certs/tls.key"))
}