-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (58 loc) · 1.56 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
package main
import (
"errors"
"fmt"
"github.com/pete911/template-wh/internal/k8s"
"github.com/pete911/template-wh/internal/server"
"log/slog"
"net/http"
"os"
"path/filepath"
)
var (
tlsCertFile string
tlsKeyFile string
)
func init() {
homeDir, err := os.UserHomeDir()
if err != nil {
slog.Error(fmt.Sprintf("get user home dir: %v", err))
os.Exit(1)
}
tlsCertFile = filepath.Join(homeDir, "tls.crt")
tlsKeyFile = filepath.Join(homeDir, "tls.key")
}
func main() {
flags, err := ParseFlags()
if err != nil {
slog.Error(fmt.Sprintf("parse flags: %v", err))
os.Exit(1)
}
slog.Info(fmt.Sprintf("starting template admission webhook with flags: %s", flags))
if err := os.WriteFile(tlsCertFile, []byte(flags.TLSCrt), 0640); err != nil {
slog.Error(fmt.Sprintf("write tls.crt: %v", err))
os.Exit(1)
}
if err := os.WriteFile(tlsKeyFile, []byte(flags.TLSKey), 0600); err != nil {
slog.Error(fmt.Sprintf("write tls.key: %v", err))
os.Exit(1)
}
slog.Info("loading kubeconfig")
kubeconfig, err := k8s.LoadKubeconfig(flags.Kubeconfig)
if err != nil {
slog.Error(fmt.Sprintf("load kubeconfig: %v", err))
os.Exit(1)
}
var mutateFn = func(body []byte) ([]byte, error) {
values, err := k8s.GetConfigMapData(kubeconfig.Clientset, flags.ConfigmapNamespace, flags.ConfigmapName)
if err != nil {
return nil, err
}
return k8s.Mutate(body, values)
}
if err := server.ListenAndServeTLS(mutateFn, tlsCertFile, tlsKeyFile); err != nil && !errors.Is(err, http.ErrServerClosed) {
slog.Error(err.Error())
os.Exit(1)
}
slog.Info("server closed")
}