-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
74 lines (60 loc) · 1.84 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
package main
import (
"fmt"
"net/http"
"path/filepath"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"github.com/asankov-cb/kubernetes-extensibility/admission/handlers"
"github.com/asankov-cb/kubernetes-extensibility/admission/validators/conferencetalks"
istaconv1 "github.com/asankov-cb/kubernetes-extensibility/api/v1"
"github.com/sirupsen/logrus"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
)
const (
tlsDir = `/run/secrets/tls`
tlsCertFile = `tls.crt`
tlsKeyFile = `tls.key`
port = 8080
)
// paths to the cert and key files
var (
certPath = filepath.Join(tlsDir, tlsCertFile)
keyPath = filepath.Join(tlsDir, tlsKeyFile)
)
var (
scheme = runtime.NewScheme()
)
// init registers the API resources to the scheme
// used to initialize the Kubernetes client.
//
// This is needed in order for Kubernetes to be able to map
// API responses to their Go types.
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
utilruntime.Must(istaconv1.AddToScheme(scheme))
}
func main() {
config, err := rest.InClusterConfig()
if err != nil {
logrus.Fatalf("Error while initializing in-cluster Kubernetes config - service probably not running in Kubernetes: %v", err)
}
k8sClient, err := client.New(config, client.Options{
Scheme: scheme,
})
if err != nil {
logrus.Fatalf("Error while initializing Kubernetes client - service probably not running in Kubernetes: %v", err)
}
validator := conferencetalks.NewValidator(k8sClient)
handler := handlers.NewHandler(validator)
server := &http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: handler,
}
logrus.Infoln("Admission controller listening on port [%d]", port)
if err := server.ListenAndServeTLS(certPath, keyPath); err != nil {
logrus.Fatal(err)
}
}