forked from AliyunContainerService/pouch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
279 lines (234 loc) · 9.63 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package main
import (
"fmt"
"os"
"os/signal"
"path"
"strings"
"syscall"
"time"
"github.com/alibaba/pouch/config/opt"
"github.com/alibaba/pouch/daemon"
"github.com/alibaba/pouch/daemon/config"
"github.com/alibaba/pouch/lxcfs"
"github.com/alibaba/pouch/pkg/debug"
"github.com/alibaba/pouch/pkg/exec"
"github.com/alibaba/pouch/pkg/utils"
"github.com/alibaba/pouch/storage/quota"
"github.com/alibaba/pouch/version"
"github.com/docker/docker/pkg/reexec"
"github.com/google/gops/agent"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
var (
sigHandles []func() error
printVersion bool
)
var cfg = &config.Config{}
func main() {
if reexec.Init() {
return
}
var cmdServe = &cobra.Command{
Use: "pouchd",
Args: cobra.NoArgs,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
return runDaemon(cmd)
},
}
setupFlags(cmdServe)
if err := cmdServe.Execute(); err != nil {
logrus.Error(err)
os.Exit(1)
}
}
// setupFlags setups flags for command line.
func setupFlags(cmd *cobra.Command) {
// Cobra supports Persistent Flags, which, if defined here,
// will be global for your application.
// flagSet := cmd.PersistentFlags()
// Cobra also supports local flags, which will only run
// when this action is called directly.
flagSet := cmd.Flags()
flagSet.StringVar(&cfg.HomeDir, "home-dir", "/var/lib/pouch", "Specify root dir of pouchd")
flagSet.StringArrayVarP(&cfg.Listen, "listen", "l", []string{"unix:///var/run/pouchd.sock"}, "Specify listening addresses of Pouchd")
flagSet.BoolVar(&cfg.IsCriEnabled, "enable-cri", false, "Specify whether enable the cri part of pouchd which is used to support Kubernetes")
flagSet.StringVar(&cfg.CriConfig.CriVersion, "cri-version", "v1alpha2", "Specify the version of cri which is used to support Kubernetes")
flagSet.StringVar(&cfg.CriConfig.Listen, "listen-cri", "/var/run/pouchcri.sock", "Specify listening address of CRI")
flagSet.StringVar(&cfg.CriConfig.NetworkPluginBinDir, "cni-bin-dir", "/opt/cni/bin", "The directory for putting cni plugin binaries.")
flagSet.StringVar(&cfg.CriConfig.NetworkPluginConfDir, "cni-conf-dir", "/etc/cni/net.d", "The directory for putting cni plugin configuration files.")
flagSet.StringVar(&cfg.CriConfig.SandboxImage, "sandbox-image", "registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0", "The image used by sandbox container.")
flagSet.BoolVarP(&cfg.Debug, "debug", "D", false, "Switch daemon log level to DEBUG mode")
flagSet.StringVarP(&cfg.ContainerdAddr, "containerd", "c", "/var/run/containerd.sock", "Specify listening address of containerd")
flagSet.StringVar(&cfg.ContainerdPath, "containerd-path", "", "Specify the path of containerd binary")
flagSet.StringVar(&cfg.TLS.Key, "tlskey", "", "Specify key file of TLS")
flagSet.StringVar(&cfg.TLS.Cert, "tlscert", "", "Specify cert file of TLS")
flagSet.StringVar(&cfg.TLS.CA, "tlscacert", "", "Specify CA file of TLS")
flagSet.BoolVar(&cfg.TLS.VerifyRemote, "tlsverify", false, "Use TLS and verify remote")
flagSet.StringVar(&cfg.TLS.ManagerWhiteList, "manager-whitelist", "", "Set tls name whitelist, multiple values are separated by commas")
flagSet.BoolVarP(&printVersion, "version", "v", false, "Print daemon version")
flagSet.StringVar(&cfg.DefaultRuntime, "default-runtime", "runc", "Default OCI Runtime")
flagSet.BoolVar(&cfg.IsLxcfsEnabled, "enable-lxcfs", false, "Enable Lxcfs to make container to isolate /proc")
flagSet.StringVar(&cfg.LxcfsBinPath, "lxcfs", "/usr/local/bin/lxcfs", "Specify the path of lxcfs binary")
flagSet.StringVar(&cfg.LxcfsHome, "lxcfs-home", "/var/lib/lxcfs", "Specify the mount dir of lxcfs")
flagSet.StringVar(&cfg.DefaultRegistry, "default-registry", "registry.hub.docker.com", "Default Image Registry")
flagSet.StringVar(&cfg.DefaultRegistryNS, "default-registry-namespace", "library", "Default Image Registry namespace")
flagSet.StringVar(&cfg.ImageProxy, "image-proxy", "", "Http proxy to pull image")
flagSet.StringVar(&cfg.QuotaDriver, "quota-driver", "", "Set quota driver(grpquota/prjquota), if not set, it will set by kernel version")
flagSet.StringVar(&cfg.ConfigFile, "config-file", "/etc/pouch/config.json", "Configuration file of pouchd")
// volume config
flagSet.StringVar(&cfg.VolumeConfig.DriverAlias, "volume-driver-alias", "", "Set volume driver alias, <name=alias>[;name1=alias1]")
// network config
flagSet.StringVar(&cfg.NetworkConfig.BridgeConfig.Name, "bridge-name", "", "Set default bridge name")
flagSet.StringVar(&cfg.NetworkConfig.BridgeConfig.IP, "bip", "", "Set bridge IP")
flagSet.StringVar(&cfg.NetworkConfig.BridgeConfig.GatewayIPv4, "default-gateway", "", "Set default bridge gateway")
flagSet.StringVar(&cfg.NetworkConfig.BridgeConfig.FixedCIDR, "fixed-cidr", "", "Set bridge fixed CIDR")
flagSet.IntVar(&cfg.NetworkConfig.BridgeConfig.Mtu, "mtu", 1500, "Set bridge MTU")
flagSet.BoolVar(&cfg.NetworkConfig.BridgeConfig.IPTables, "iptables", true, "Enable iptables")
flagSet.BoolVar(&cfg.NetworkConfig.BridgeConfig.IPForward, "ipforward", true, "Enable ipforward")
flagSet.BoolVar(&cfg.NetworkConfig.BridgeConfig.UserlandProxy, "userland-proxy", false, "Enable userland proxy")
// cgroup-path flag is to set parent cgroup for all containers, default is "default" staying with containerd's configuration.
flagSet.StringVar(&cfg.CgroupParent, "cgroup-parent", "default", "Set parent cgroup for all containers")
flagSet.StringVar(&cfg.PluginPath, "plugin", "", "Set the path where plugin shared library file put")
flagSet.StringSliceVar(&cfg.Labels, "label", []string{}, "Set metadata for Pouch daemon")
flagSet.BoolVar(&cfg.EnableProfiler, "enable-profiler", false, "Set if pouchd setup profiler")
flagSet.StringVar(&cfg.Pidfile, "pidfile", "/var/run/pouch.pid", "Save daemon pid")
flagSet.IntVar(&cfg.OOMScoreAdjust, "oom-score-adj", -500, "Set the oom_score_adj for the daemon")
flagSet.Var(opt.NewRuntime(&cfg.Runtimes), "add-runtime", "register a OCI runtime to daemon")
}
// runDaemon prepares configs, setups essential details and runs pouchd daemon.
func runDaemon(cmd *cobra.Command) error {
if err := loadDaemonFile(cfg, cmd.Flags()); err != nil {
return fmt.Errorf("failed to load daemon file: %s", err)
}
//user specifies --version or -v, print version and return.
if printVersion {
fmt.Printf("pouchd version: %s, build: %s, build at: %s\n", version.Version, version.GitCommit, version.BuildTime)
return nil
}
// initialize log.
initLog()
if err := cfg.Validate(); err != nil {
logrus.Fatal(err)
}
// import debugger tools for pouch when in debug mode.
if cfg.Debug || cfg.EnableProfiler {
if err := agent.Listen(agent.Options{}); err != nil {
logrus.Fatal(err)
}
debug.SetupDumpStackTrap()
}
// initialize home dir.
dir := cfg.HomeDir
if dir == "" || !path.IsAbs(dir) {
return fmt.Errorf("invalid pouchd's home dir: %s", dir)
}
if _, err := os.Stat(dir); err != nil && os.IsNotExist(err) {
if err := os.MkdirAll(dir, 0666); err != nil {
return fmt.Errorf("failed to mkdir: %v", err)
}
}
// saves daemon pid to pidfile.
if cfg.Pidfile != "" {
if err := utils.NewPidfile(cfg.Pidfile); err != nil {
logrus.Errorf("failed to create pidfile: %s", err)
return err
}
defer func() {
if err := os.Remove(cfg.Pidfile); err != nil {
logrus.Errorf("failed to delete pidfile: %s", err)
}
}()
}
// set pouchd oom-score
if err := utils.SetOOMScore(os.Getpid(), cfg.OOMScoreAdjust); err != nil {
logrus.Errorf("failed to set oom-score for pouchd: %v", err)
}
// define and start all required processes.
if cfg.QuotaDriver != "" {
quota.SetQuotaDriver(cfg.QuotaDriver)
}
if err := checkLxcfsCfg(); err != nil {
return err
}
// initialize signal and handle method.
var (
errCh = make(chan error, 1)
signalCh = make(chan os.Signal, 1)
)
// new daemon instance, this is core.
d := daemon.NewDaemon(cfg)
if d == nil {
return fmt.Errorf("failed to new daemon")
}
signal.Notify(signalCh, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGHUP)
sigHandles = append(sigHandles, d.ShutdownPlugin, d.Shutdown)
go func() {
// FIXME: I think the Run() should always return error.
errCh <- d.Run()
}()
select {
case sig := <-signalCh:
logrus.Warnf("received signal: %s", sig)
for _, handle := range sigHandles {
if err := handle(); err != nil {
logrus.Errorf("failed to handle signal: %v", err)
}
}
os.Exit(1)
case err := <-errCh:
// FIXME: should we do the cleanup like signal handle?
return err
}
return nil
}
// initLog initializes log Level and log format of daemon.
func initLog() {
if cfg.Debug {
logrus.SetLevel(logrus.DebugLevel)
logrus.Infof("start daemon at debug level")
}
formatter := &logrus.TextFormatter{
FullTimestamp: true,
TimestampFormat: time.RFC3339Nano,
}
logrus.SetFormatter(formatter)
}
// define lxcfs processe.
func setLxcfsProcess(processes exec.Processes) exec.Processes {
if !cfg.IsLxcfsEnabled {
return processes
}
p := &exec.Process{
Path: cfg.LxcfsBinPath,
Args: []string{
cfg.LxcfsHome,
},
}
processes = append(processes, p)
return processes
}
// check lxcfs config
func checkLxcfsCfg() error {
if !cfg.IsLxcfsEnabled {
return nil
}
if !path.IsAbs(cfg.LxcfsHome) {
return fmt.Errorf("invalid lxcfs home dir: %s", cfg.LxcfsHome)
}
cfg.LxcfsHome = strings.TrimSuffix(cfg.LxcfsHome, "/")
lxcfs.IsLxcfsEnabled = cfg.IsLxcfsEnabled
lxcfs.LxcfsHomeDir = cfg.LxcfsHome
lxcfs.LxcfsParentDir = path.Dir(cfg.LxcfsHome)
return lxcfs.CheckLxcfsMount()
}
// load daemon config file
func loadDaemonFile(cfg *config.Config, flagSet *pflag.FlagSet) error {
if cfg.ConfigFile == "" {
return nil
}
return cfg.MergeConfigurations(flagSet)
}