/
main.go
75 lines (63 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
75
package main
import (
goflag "flag"
"fmt"
"math/rand"
"net/http"
"os"
"time"
"github.com/baidubce/baiducloud-cce-cni-driver/cce-network-v2/pkg/k8s"
"github.com/prometheus/client_golang/prometheus/promhttp"
"k8s.io/klog/v2"
controllerruntime "sigs.k8s.io/controller-runtime"
"github.com/baidubce/baiducloud-cce-cni-driver/cce-network-v2/cmd/webhook/webhook"
"github.com/baidubce/baiducloud-cce-cni-driver/cce-network-v2/cmd/webhook/webhook/podmutating"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
var metricsPort int = 18902
func NewRootCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "webhook",
Run: func(cmd *cobra.Command, args []string) {
klog.Info("cce-ipam-webhook starts...")
cmd.Flags().VisitAll(func(flag *pflag.Flag) {
klog.Infof("FLAG: --%s=%q", flag.Name, flag.Value)
})
_, _, err := k8s.InitNewK8sClient()
if err != nil {
klog.Fatalf("create k8s client failed: %v", err)
}
go runMetricServer()
controllerruntime.SetLogger(klog.NewKlogr())
if err := webhook.RunWebhookServer(); err != nil {
klog.Fatalf("webhook to run: %v", err)
}
},
}
webhook.RegisterWebhookFlags(cmd.Flags())
podmutating.RegisterCustomerFlags(cmd.Flags())
cmd.Flags().IntVar(&metricsPort, "metrics-port", metricsPort, "port for webhook server")
return cmd
}
func runMetricServer() {
address := fmt.Sprintf(":%d", metricsPort)
klog.Infof("webhook metrics serving handler on %v", address)
http.Handle("/metrics", promhttp.Handler())
err := http.ListenAndServe(address, nil)
if err != nil {
klog.Fatalf("failed to run webhook metrics server: %v", err)
}
}
func main() {
rand.Seed(time.Now().UnixNano())
pflag.CommandLine.AddGoFlagSet(goflag.CommandLine)
command := NewRootCommand()
if err := command.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func init() {
klog.InitFlags(nil)
}