/
run.go
54 lines (39 loc) · 1.06 KB
/
run.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
package main
import (
"errors"
"net/http"
"github.com/rancher/wrangler/pkg/signals"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
"github.com/harvester/vm-dhcp-controller/pkg/agent"
"github.com/harvester/vm-dhcp-controller/pkg/config"
"github.com/harvester/vm-dhcp-controller/pkg/server"
)
func run(options *config.AgentOptions) error {
logrus.Infof("Starting VM DHCP Agent: %s", name)
ctx := signals.SetupSignalContext()
agent := agent.NewAgent(options)
httpServerOptions := config.HTTPServerOptions{
DebugMode: enableCacheDumpAPI,
DHCPAllocator: agent.DHCPAllocator,
}
s := server.NewHTTPServer(&httpServerOptions)
s.RegisterAgentHandlers()
eg, egctx := errgroup.WithContext(ctx)
eg.Go(func() error {
return s.Run()
})
eg.Go(func() error {
return agent.Run(egctx)
})
errCh := server.Cleanup(egctx, s)
if err := eg.Wait(); err != nil && !errors.Is(err, http.ErrServerClosed) {
return err
}
// Return cleanup error message if any
if err := <-errCh; err != nil {
return err
}
logrus.Info("finished clean")
return nil
}