-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
138 lines (118 loc) · 4.34 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
package main
import (
"context"
"fmt"
"os"
ctlcni "github.com/harvester/harvester/pkg/generated/controllers/k8s.cni.cncf.io"
ctlkubevirt "github.com/harvester/harvester/pkg/generated/controllers/kubevirt.io"
"github.com/harvester/webhook/pkg/config"
"github.com/harvester/webhook/pkg/server"
ctlcore "github.com/rancher/wrangler/pkg/generated/controllers/core"
"github.com/rancher/wrangler/pkg/kubeconfig"
"github.com/rancher/wrangler/pkg/signals"
"github.com/rancher/wrangler/pkg/start"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"k8s.io/client-go/rest"
ctllb "github.com/harvester/harvester-load-balancer/pkg/generated/controllers/loadbalancer.harvesterhci.io"
"github.com/harvester/harvester-load-balancer/pkg/utils"
"github.com/harvester/harvester-load-balancer/pkg/webhook/ippool"
"github.com/harvester/harvester-load-balancer/pkg/webhook/loadbalancer"
)
const name = "harvester-load-balancer-webhook"
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: 2,
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",
},
cli.StringFlag{
Name: "controller-user",
EnvVar: "CONTROLLER_USER_NAME",
Destination: &options.ControllerUsername,
Value: "harvester-load-balancer-webhook",
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) {
utils.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 {
cniFactory := ctlcni.NewFactoryFromConfigOrDie(cfg)
lbFactory := ctllb.NewFactoryFromConfigOrDie(cfg)
kubevirtFactory := ctlkubevirt.NewFactoryFromConfigOrDie(cfg)
coreFactory := ctlcore.NewFactoryFromConfigOrDie(cfg)
// must declare before start the nad factory
poolCache := lbFactory.Loadbalancer().V1beta1().IPPool().Cache()
vmiCache := kubevirtFactory.Kubevirt().V1().VirtualMachineInstance().Cache()
nadCache := cniFactory.K8s().V1().NetworkAttachmentDefinition().Cache()
namespaceCache := coreFactory.Core().V1().Namespace().Cache()
if err := start.All(ctx, options.Threadiness,
cniFactory, lbFactory, kubevirtFactory, coreFactory); err != nil {
return fmt.Errorf("failed to start controller: %w", err)
}
webhookServer := server.NewWebhookServer(ctx, cfg, name, options)
if err := webhookServer.RegisterValidators(ippool.NewIPPoolValidator(poolCache),
loadbalancer.NewValidator(vmiCache)); err != nil {
return fmt.Errorf("failed to register ip pool validator: %w", err)
}
if err := webhookServer.RegisterMutators(ippool.NewIPPoolMutator(nadCache),
loadbalancer.NewMutator(namespaceCache, vmiCache)); err != nil {
return fmt.Errorf("failed to register ip pool mutator: %w", err)
}
if err := webhookServer.RegisterConverters(loadbalancer.NewConverter(vmiCache, poolCache)); err != nil {
return fmt.Errorf("failed to register load balancer converter: %w", err)
}
if err := webhookServer.Start(); err != nil {
return fmt.Errorf("failed to start webhook server: %w", err)
}
<-ctx.Done()
return nil
}