This repository has been archived by the owner on Aug 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
daemon_linux.go
257 lines (248 loc) · 7.59 KB
/
daemon_linux.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
package daemon
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/containerd/containerd/cmd/containerd/command"
"github.com/containerd/containerd/plugin"
"github.com/containerd/containerd/services/opt"
"github.com/containerd/cri"
criconfig "github.com/containerd/cri/pkg/config"
"github.com/rancher/k3c/pkg/daemon"
"github.com/rancher/k3c/pkg/daemon/config"
"github.com/sirupsen/logrus"
cliv1 "github.com/urfave/cli"
cliv2 "github.com/urfave/cli/v2"
"k8s.io/klog"
)
func newApp() *cliv1.App {
app := command.App()
app.Name = "k3c daemon"
app.Usage = "containerd++ (cri, buildkit and k3c)"
app.Description = `
k3c daemon is containerd work-alike presenting the CRI, BuildKit, K3C and
containerd APIs all on a single gRPC socket. It is meant to be a drop-in
replacement for a CRI-enabled containerd with additional functionality on
the backend to support the Docker work-alike frontend of k3c.`
app.HelpName = app.Name
for i := range app.Flags {
flag := app.Flags[i]
logrus.Debugf("%+v", flag)
switch n := flag.GetName(); n {
case "address,a":
if sf, ok := flag.(cliv1.StringFlag); ok {
sf.Value = filepath.Join(config.DefaultDaemonStateDir, "k3c.sock")
sf.EnvVar = "K3C_ADDRESS"
sf.Destination = &config.Socket.Address
app.Flags[i] = sf
} else {
logrus.Warnf("unexpected type for flag %q = %#v", flag.GetName(), flag)
}
case "config,c":
if sf, ok := flag.(cliv1.StringFlag); ok {
sf.Value = config.DefaultDaemonConfigFile
sf.EnvVar = "K3C_CONFIG"
app.Flags[i] = sf
} else {
logrus.Warnf("unexpected type for flag %q = %#v", flag.GetName(), flag)
}
case "root":
if sf, ok := flag.(cliv1.StringFlag); ok {
sf.Value = config.DefaultDaemonRootDir
sf.EnvVar = "K3C_ROOT"
sf.Destination = &opt.PluginConfig.Path
app.Flags[i] = sf
} else {
logrus.Warnf("unexpected type for flag %q = %#v", flag.GetName(), flag)
}
case "state":
if sf, ok := flag.(cliv1.StringFlag); ok {
sf.Value = config.DefaultDaemonStateDir
sf.EnvVar = "K3C_STATE"
app.Flags[i] = sf
} else {
logrus.Warnf("unexpected type for flag %q = %#v", flag.GetName(), flag)
}
}
}
app.Flags = append(app.Flags, []cliv1.Flag{
cliv1.StringFlag{
Name: "bridge-name",
Value: config.DefaultBridgeName,
EnvVar: "K3C_BRIDGE_NAME",
Destination: &daemon.Config.BridgeName,
},
cliv1.StringFlag{
Name: "bridge-cidr",
Value: config.DefaultBridgeCIDR,
EnvVar: "K3C_BRIDGE_CIDR",
Destination: &daemon.Config.BridgeCIDR,
},
cliv1.StringFlag{
Name: "bootstrap-image",
Value: config.DefaultBootstrapImage,
EnvVar: "K3C_BOOTSTRAP_IMAGE",
Usage: "containerd-style image ref to install",
Destination: &daemon.Config.BootstrapImage,
},
}...)
defaultBootstrapSkip := true
requiredExecutables := map[string]string{
"bridge": "", // cni
"containerd-shim": "", // containerd
"containerd-shim-runc-v1": "", // buildkit
"containerd-shim-runc-v2": "", // cri
"host-local": "", // cni
"iptables": "", // cni, buildkit
"loopback": "", // cni
"portmap": "", // cni
"runc": "", // cri, buildkit
"socat": "", // cri
}
for bin := range requiredExecutables {
if path, err := exec.LookPath(bin); err != nil {
defaultBootstrapSkip = false
} else {
requiredExecutables[bin] = path
}
}
if defaultBootstrapSkip {
app.Flags = append(app.Flags, cliv1.BoolTFlag{
Name: "bootstrap-skip",
EnvVar: "K3C_BOOTSTRAP_SKIP",
Usage: "skip bootstrap install (default: true)",
Destination: &daemon.Config.BootstrapSkip,
})
} else {
app.Flags = append(app.Flags, cliv1.BoolFlag{
Name: "bootstrap-skip",
EnvVar: "K3C_BOOTSTRAP_SKIP",
Usage: "skip bootstrap install (default: false)",
Destination: &daemon.Config.BootstrapSkip,
})
}
app.Flags = append(app.Flags, []cliv1.Flag{
cliv1.StringFlag{
Name: "cni-bin",
EnvVar: "K3C_CNI_BIN",
Destination: &cri.Config.NetworkPluginBinDir,
},
cliv1.StringFlag{
Name: "cni-netconf",
EnvVar: "K3C_CNI_NETCONF",
Destination: &cri.Config.NetworkPluginConfDir,
},
cliv1.StringFlag{
Name: "sandbox-image",
Value: config.DefaultSandboxImage,
EnvVar: "K3C_SANDBOX_IMAGE",
Usage: "containerd-style image ref for sandboxes",
Destination: &cri.Config.SandboxImage,
},
cliv1.IntFlag{
Name: "socket-gid,group",
EnvVar: "K3C_SOCKET_GID",
Usage: "gRPC socket gid",
Destination: &config.Socket.GID,
},
cliv1.IntFlag{
Name: "socket-uid",
EnvVar: "K3C_SOCKET_UID",
Usage: "gRPC socket uid",
Destination: &config.Socket.UID,
},
}...)
app.Action = func(action interface{}) cliv1.ActionFunc {
return func(clx *cliv1.Context) error {
// setup env
for i := range clx.App.Flags {
var (
f = clx.App.Flags[i]
n = strings.SplitN(f.GetName(), `,`, 2)[0]
e string
)
switch t := f.(type) {
case cliv1.BoolFlag:
e = t.EnvVar
case cliv1.BoolTFlag:
e = t.EnvVar
case cliv1.StringFlag:
e = t.EnvVar
case cliv1.IntFlag:
e = t.EnvVar
}
if e != "" {
if err := os.Setenv(e, clx.GlobalString(n)); err != nil {
return err
}
}
}
// setup cfg
var (
root = clx.GlobalString("root")
)
if cniBin := clx.GlobalString("cni-bin"); cniBin == "" {
cri.Config.NetworkPluginBinDir = filepath.Join(root, "bin")
} else {
cri.Config.NetworkPluginBinDir = cniBin
}
if cniNetConf := clx.GlobalString("cni-netconf"); cniNetConf == "" {
cri.Config.NetworkPluginConfDir = filepath.Join(root, "etc", "cni", "net.d")
} else {
cri.Config.NetworkPluginConfDir = cniNetConf
}
cri.Config.DefaultRuntimeName = "runc"
cri.Config.Runtimes = map[string]criconfig.Runtime{
cri.Config.DefaultRuntimeName: {
Type: plugin.RuntimeRuncV2,
},
}
if err := os.MkdirAll(cri.Config.NetworkPluginBinDir, 0700); err != nil {
return err
}
for exe, loc := range requiredExecutables {
if defaultBootstrapSkip {
// the symlinking is to make buildkit happy
if err := os.Symlink(loc, filepath.Join(cri.Config.NetworkPluginBinDir, exe)); err != nil {
logrus.WithError(err).Warn("k3s bootstrap skip")
}
} else if loc == "" {
logrus.WithField("executable", exe).Warn("k3c bootstrap check: missing")
} else {
logrus.WithField("executable", exe).WithField("location", loc).Debug("k3c bootstrap check: found")
}
}
if err := os.MkdirAll(cri.Config.NetworkPluginConfDir, 0700); err != nil {
return err
}
if err := config.WriteFileJSON(filepath.Join(cri.Config.NetworkPluginConfDir, "90-k3c.conflist"), config.DefaultCniConflist(daemon.Config.BridgeName, daemon.Config.BridgeCIDR), 0600); err != nil {
return err
}
if err := os.Setenv("PATH", fmt.Sprintf("%s%c%s", os.Getenv("PATH"), os.PathListSeparator, filepath.Join(root, "bin", "aux"))); err != nil {
return err
}
return cliv1.HandleAction(action, clx)
}
}(app.Action)
return app
}
func Command() *cliv2.Command {
app := newApp()
return &cliv2.Command{
Name: "daemon",
Usage: "Run the container daemon",
Description: app.Description,
SkipFlagParsing: true,
Before: func(clx *cliv2.Context) error {
klog.InitFlags(nil)
return nil
},
Action: func(clx *cliv2.Context) error {
args := []string{app.Name}
args = append(args, clx.Args().Slice()...)
return app.Run(args)
},
}
}