1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package main
17
18 import (
19 "flag"
20 stdlog "log"
21 "os"
22 "os/signal"
23 "syscall"
24
25 "github.com/prometheus/client_golang/prometheus"
26 "github.com/prometheus/client_golang/prometheus/collectors"
27 ctrl "sigs.k8s.io/controller-runtime"
28
29 "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon"
30 "github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/crclients"
31 "github.com/chaos-mesh/chaos-mesh/pkg/fusedev"
32 "github.com/chaos-mesh/chaos-mesh/pkg/log"
33 "github.com/chaos-mesh/chaos-mesh/pkg/sysfs"
34 "github.com/chaos-mesh/chaos-mesh/pkg/version"
35 )
36
37 var (
38 conf = &chaosdaemon.Config{Host: "0.0.0.0", CrClientConfig: &crclients.CrClientConfig{}}
39
40 printVersion bool
41 )
42
43 func init() {
44 flag.BoolVar(&printVersion, "version", false, "print version information and exit")
45 flag.IntVar(&conf.GRPCPort, "grpc-port", 31767, "the port which grpc server listens on")
46 flag.IntVar(&conf.HTTPPort, "http-port", 31766, "the port which http server listens on")
47 flag.StringVar(&conf.CrClientConfig.Runtime, "runtime", "docker", "current container runtime")
48 flag.StringVar(&conf.CrClientConfig.SocketPath, "runtime-socket-path", "", "current container runtime socket path")
49 flag.StringVar(&conf.CrClientConfig.ContainerdNS, "containerd-ns", "k8s.io", "namespace used for containerd")
50 flag.StringVar(&conf.CaCert, "ca", "", "ca certificate of grpc server")
51 flag.StringVar(&conf.Cert, "cert", "", "certificate of grpc server")
52 flag.StringVar(&conf.Key, "key", "", "key of grpc server")
53 flag.BoolVar(&conf.Profiling, "pprof", false, "enable pprof")
54
55 flag.Parse()
56 }
57
58 func main() {
59 version.PrintVersionInfo("Chaos-daemon")
60
61 if printVersion {
62 os.Exit(0)
63 }
64
65 rootLogger, err := log.NewDefaultZapLogger()
66 if err != nil {
67 stdlog.Fatal("failed to create root logger", err)
68 }
69 rootLogger = rootLogger.WithName("chaos-daemon.daemon-server")
70 log.ReplaceGlobals(rootLogger)
71 ctrl.SetLogger(rootLogger)
72
73 reg := prometheus.NewRegistry()
74 reg.MustRegister(
75
76 collectors.NewGoCollector(),
77 collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
78 )
79
80 rootLogger.Info("grant access to /dev/fuse")
81 err = fusedev.GrantAccess()
82 if err != nil {
83 rootLogger.Error(err, "grant access to /dev/fuse")
84 }
85
86 rootLogger.Info("remount /sys with read-write permission")
87 err = sysfs.RemountWithOption()
88 if err != nil {
89 rootLogger.Error(err, "remount /sys with read-write permission")
90 }
91
92 server, err := chaosdaemon.BuildServer(conf, reg, rootLogger)
93 if err != nil {
94 rootLogger.Error(err, "build chaos-daemon server")
95 os.Exit(1)
96 }
97
98 errs := make(chan error)
99 go func() {
100 errs <- server.Start()
101 }()
102
103 sigc := make(chan os.Signal, 1)
104 signal.Notify(sigc,
105 syscall.SIGINT,
106 syscall.SIGTERM)
107
108 select {
109 case sig := <-sigc:
110 rootLogger.Info("received signal", "signal", sig)
111 case err = <-errs:
112 if err != nil {
113 rootLogger.Error(err, "chaos-daemon runtime")
114 }
115 }
116 if err = server.Shutdown(); err != nil {
117 rootLogger.Error(err, "chaos-daemon shutdown")
118 }
119 }
120