forked from harvester/webhook-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
124 lines (108 loc) · 3.1 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
//go:generate go run pkg/codegen/cleanup/main.go
//go:generate go run pkg/codegen/main.go
//go:generate /bin/bash scripts/generate-manifest
package main
import (
"context"
"os"
"github.com/rancher/wrangler/pkg/kubeconfig"
"github.com/rancher/wrangler/pkg/signals"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"k8s.io/client-go/rest"
"github.com/harvester/webhook-sample/pkg/admitter"
"github.com/harvester/webhook-sample/pkg/converter"
"github.com/harvester/webhook/pkg/config"
"github.com/harvester/webhook/pkg/server"
)
const webhookName = "webhook-sample"
func main() {
var options config.Options
var logLevel string
flags := []cli.Flag{
cli.StringFlag{
Name: "loglevel",
Usage: "Specify log level",
EnvVar: "LOGLEVEL",
Value: "info",
Destination: &logLevel,
},
cli.IntFlag{
Name: "threadiness",
EnvVar: "THREADINESS",
Usage: "Specify controller threads",
Value: 5,
Destination: &options.Threadiness,
},
cli.IntFlag{
Name: "https-port",
EnvVar: "WEBHOOK_SERVER_HTTPS_PORT",
Usage: "HTTPS listen port",
Value: 8443,
Destination: &options.HTTPSListenPort,
},
cli.StringFlag{
Name: "namespace",
EnvVar: "NAMESPACE",
Destination: &options.Namespace,
Usage: "The harvester namespace",
Value: "harvester-system",
Required: true,
},
cli.StringFlag{
Name: "controller-user",
EnvVar: "CONTROLLER_USER_NAME",
Destination: &options.ControllerUsername,
Value: "harvester-load-balancer",
Usage: "The harvester controller username",
},
cli.StringFlag{
Name: "gc-user",
EnvVar: "GARBAGE_COLLECTION_USER_NAME",
Destination: &options.GarbageCollectionUsername,
Usage: "The system username that performs garbage collection",
Value: "system:serviceaccount:kube-system:generic-garbage-collector",
},
}
cfg, err := kubeconfig.GetNonInteractiveClientConfig(os.Getenv("KUBECONFIG")).ClientConfig()
if err != nil {
logrus.Fatal(err)
}
ctx := signals.SetupSignalContext()
app := cli.NewApp()
app.Flags = flags
app.Action = func(c *cli.Context) {
setLogLevel(logLevel)
if err := run(ctx, cfg, &options); err != nil {
logrus.Fatalf("run webhook server failed: %v", err)
}
}
if err := app.Run(os.Args); err != nil {
logrus.Fatalf("run webhook server failed: %v", err)
}
}
func run(ctx context.Context, cfg *rest.Config, options *config.Options) error {
webhookServer := server.NewWebhookServer(ctx, cfg, webhookName, options)
if err := webhookServer.RegisterValidators(admitter.NewValidator()); err != nil {
return err
}
if err := webhookServer.RegisterMutators(admitter.NewMutator()); err != nil {
return err
}
if err := webhookServer.RegisterConverters(converter.NewFooConverter()); err != nil {
return err
}
if err := webhookServer.Start(); err != nil {
return err
}
<-ctx.Done()
return nil
}
func setLogLevel(level string) {
ll, err := logrus.ParseLevel(level)
if err != nil {
ll = logrus.DebugLevel
}
// set global log level
logrus.SetLevel(ll)
}