-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
162 lines (131 loc) · 4.46 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"context"
"flag"
"os"
"strconv"
"github.com/logicmonitor/lm-k8s-webhook/internal/version"
lmk8swebhookconfig "github.com/logicmonitor/lm-k8s-webhook/pkg/config"
"github.com/logicmonitor/lm-k8s-webhook/pkg/handler"
"github.com/logicmonitor/lm-k8s-webhook/pkg/reloader"
"sigs.k8s.io/controller-runtime/pkg/healthz"
goruntime "runtime"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client/config"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/manager/signals"
"sigs.k8s.io/controller-runtime/pkg/webhook"
)
var (
scheme = runtime.NewScheme()
setupLog = ctrl.Log.WithName("Setup")
)
const (
webhookCertName = "tls.crt"
webhookKeyName = "tls.key"
)
func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
// +kubebuilder:scaffold:scheme
}
func main() {
var metricAddr string
var webhookPort string
var webhookCertDir string
var probeAddr string
var lmconfigFilePath string
var k8sRestConfig *rest.Config
flag.StringVar(&metricAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&webhookPort, "webhook-bind-port", "9443", "The port webhook will listen on.")
flag.StringVar(&webhookCertDir, "webhook-cert-dir", "/etc/lmk8swebhook/certs", "webhook certificate directory.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.StringVar(&lmconfigFilePath, "lmk8swebhookconfig-file-path", "/etc/lmk8swebhook/config/lmk8swebhookconfig.yaml", "File path of lmk8swebhookconfig")
var ctx context.Context
ctx = context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
opts := zap.Options{
// Development: true,
}
opts.BindFlags(flag.CommandLine)
flag.Parse()
logger := zap.New(zap.UseFlagOptions(&opts))
ctrl.SetLogger(logger)
v := version.Get()
logger.Info("Starting the LM-K8s-Webhook",
"lm-k8s-webhook-version", v.LMK8sWebhook,
"build-date", v.BuildDate,
"go-version", v.Go,
"go-arch", goruntime.GOARCH,
"go-os", goruntime.GOOS,
)
setupLog.Info("setting up manager")
port, err := strconv.Atoi(webhookPort)
if err != nil {
setupLog.Error(err, "Failed in parsing webhook port")
os.Exit(1)
}
// Load the external config
err = lmk8swebhookconfig.LoadConfig(lmconfigFilePath)
if err != nil {
// As external config is optional
if os.IsNotExist(err) {
setupLog.Info("Config file is not provided")
} else {
setupLog.Error(err, "Error in reading the config file", "lmconfigFilePath", lmconfigFilePath)
os.Exit(1)
}
}
mgrOptions := ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricAddr,
Port: port,
HealthProbeBindAddress: probeAddr,
}
k8sRestConfig = config.GetConfigOrDie()
mgr, err := manager.New(k8sRestConfig, mgrOptions)
if err != nil {
setupLog.Error(err, "unable to set up overall LM webhook")
os.Exit(1)
}
// Setup webhooks
setupLog.Info("setting up webhook server")
lmWebhookServer := mgr.GetWebhookServer()
lmWebhookServer.CertDir = webhookCertDir
lmWebhookServer.CertName = webhookCertName
lmWebhookServer.KeyName = webhookKeyName
k8sClient, err := lmk8swebhookconfig.NewK8sClient(k8sRestConfig, lmk8swebhookconfig.NewK8sClientSet)
if err != nil {
setupLog.Error(err, "error in getting k8s client")
os.Exit(1)
}
setupLog.Info("registering webhooks to the webhook server")
lmWebhookServer.Register("/mutate", &webhook.Admission{Handler: &handler.LMPodMutationHandler{Client: k8sClient, Log: ctrl.Log.WithName("lm-podmutator-webhook")}})
if err := mgr.AddHealthzCheck("health", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
os.Exit(1)
}
if err := mgr.AddReadyzCheck("check", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up ready check")
os.Exit(1)
}
if lmk8swebhookconfig.GetConfig().MutationConfigProvided {
setupLog.Info("setup config reloader")
err := reloader.SetupConfigReloader(ctx, lmconfigFilePath)
if err != nil {
setupLog.Error(err, "failed to setup config-reloader")
os.Exit(1)
}
}
setupLog.Info("starting manager")
if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
setupLog.Error(err, "unable to run manager")
cancel()
os.Exit(1)
}
}