-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
146 lines (123 loc) · 3.25 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
package main
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"time"
vault "github.com/hashicorp/vault/api"
"k8s.io/api/admission/v1beta1"
"k8s.io/klog"
)
type Instance struct {
Name string
Classification string
ExternalUrl string
Short string
}
var instances []Instance
var defaultInstances = `
{"name": "minio_standard", "short": "standard", "classification": "unclassified", "externalUrl": "https://minio-standard.aaw-dev.cloud.statcan.ca:443"}
{"name": "minio_premium", "short": "standard", "classification": "unclassified", "externalUrl": "https://minio-premium.aaw-dev.cloud.statcan.ca:443"}
`
// Sets the global instances variable
func configInstances() {
var config string
if _, err := os.Stat("instances.json"); os.IsNotExist(err) {
config = defaultInstances
} else {
config_bytes, err := ioutil.ReadFile("instances.json") // just pass the file name
if err != nil {
log.Fatal(err)
}
config = string(config_bytes)
}
dec := json.NewDecoder(strings.NewReader(config))
for {
var instance Instance
err := dec.Decode(&instance)
if err != nil {
if err == io.EOF {
break
}
log.Fatal(err)
}
fmt.Println(instance)
instances = append(instances, instance)
}
}
type server struct {
vault *vault.Client
}
// 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() {
var err error
s := server{}
// Set the global instances list
configInstances()
s.vault, err = vault.NewClient(&vault.Config{
AgentAddress: os.Getenv("VAULT_AGENT_ADDR"),
})
if err != nil {
klog.Fatalf("Error initializing Vault client: %s", err)
}
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"))
}