-
Notifications
You must be signed in to change notification settings - Fork 842
/
worker_linux.go
241 lines (195 loc) · 7.29 KB
/
worker_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
package workercmd
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strings"
"time"
"code.cloudfoundry.org/lager"
"github.com/concourse/concourse/atc"
concourseCmd "github.com/concourse/concourse/cmd"
"github.com/concourse/flag"
"github.com/jessevdk/go-flags"
"github.com/tedsuo/ifrit"
)
type Certs struct {
Dir string `long:"certs-dir" description:"Directory to use when creating the resource certificates volume."`
}
type RuntimeConfiguration struct {
Runtime string `long:"runtime" default:"guardian" choice:"guardian" choice:"containerd" choice:"houdini" description:"Runtime to use with the worker. Please note that Houdini is insecure and doesn't run 'tasks' in containers."`
}
type GuardianRuntime struct {
Bin string `long:"bin" description:"Path to a garden server executable (non-absolute names get resolved from $PATH)."`
Config flag.File `long:"config" description:"Path to a config file to use for the Garden backend. Guardian flags as env vars, e.g. 'CONCOURSE_GARDEN_FOO_BAR=a,b' for '--foo-bar a --foo-bar b'."`
DNS DNSConfig `group:"DNS Proxy Configuration" namespace:"dns-proxy"`
RequestTimeout time.Duration `long:"request-timeout" default:"5m" description:"How long to wait for requests to the Garden server to complete. 0 means no timeout."`
}
type ContainerdRuntime struct {
Config flag.File `long:"config" description:"Path to a config file to use for the Containerd daemon."`
Bin string `long:"bin" description:"Path to a containerd executable (non-absolute names get resolved from $PATH)."`
RequestTimeout time.Duration `long:"request-timeout" default:"5m" description:"How long to wait for requests to Containerd to complete. 0 means no timeout."`
//TODO can DNSConfig be simplifed to just a bool rather than struct with a bool?
DNS DNSConfig `group:"DNS Proxy Configuration" namespace:"dns-proxy"`
DNSServers []string `long:"dns-server" description:"DNS server IP address to use instead of automatically determined servers. Can be specified multiple times."`
RestrictedNetworks []string `long:"restricted-network" description:"Network ranges to which traffic from containers will be restricted. Can be specified multiple times."`
MaxContainers int `long:"max-containers" default:"0" description:"Max container capacity. 0 means no limit."`
NetworkPool string `long:"network-pool" default:"10.80.0.0/16" description:"Network range to use for dynamically allocated container subnets."`
}
const containerdRuntime = "containerd"
const guardianRuntime = "guardian"
const houdiniRuntime = "houdini"
func (cmd WorkerCommand) LessenRequirements(prefix string, command *flags.Command) {
// configured as work-dir/volumes
command.FindOptionByLongName(prefix + "baggageclaim-volumes").Required = false
}
// Chooses the appropriate runtime based on CONCOURSE_RUNTIME_TYPE.
// The runtime is represented as a Ifrit runner that must include a Garden Server process. The Garden server exposes API
// endpoints that allow the ATC to make container related requests to the worker.
// The runner may also include additional processes such as the runtime's daemon or a DNS proxy server.
func (cmd *WorkerCommand) gardenServerRunner(logger lager.Logger) (atc.Worker, ifrit.Runner, error) {
err := cmd.checkRoot()
if err != nil {
return atc.Worker{}, nil, err
}
err = cmd.verifyRuntimeFlags()
if err != nil {
return atc.Worker{}, nil, err
}
worker := cmd.Worker.Worker()
worker.Platform = "linux"
if cmd.Certs.Dir != "" {
worker.CertsPath = &cmd.Certs.Dir
}
worker.ResourceTypes, err = cmd.loadResources(logger.Session("load-resources"))
if err != nil {
return atc.Worker{}, nil, err
}
worker.Name, err = cmd.workerName()
if err != nil {
return atc.Worker{}, nil, err
}
trySetConcourseDirInPATH()
var runner ifrit.Runner
switch {
case cmd.Runtime == houdiniRuntime:
runner, err = cmd.houdiniRunner(logger)
case cmd.Runtime == containerdRuntime:
runner, err = cmd.containerdRunner(logger)
case cmd.Runtime == guardianRuntime:
runner, err = cmd.guardianRunner(logger)
default:
err = fmt.Errorf("unsupported Runtime :%s", cmd.Runtime)
}
if err != nil {
return atc.Worker{}, nil, err
}
return worker, runner, nil
}
func trySetConcourseDirInPATH() {
binDir := concourseCmd.DiscoverAsset("bin")
if binDir == "" {
return
}
err := os.Setenv("PATH", binDir+":"+os.Getenv("PATH"))
if err != nil {
// programming mistake
panic(fmt.Errorf("failed to set PATH environment variable: %w", err))
}
}
var ErrNotRoot = errors.New("worker must be run as root")
func (cmd *WorkerCommand) checkRoot() error {
currentUser, err := user.Current()
if err != nil {
return err
}
if currentUser.Uid != "0" {
return ErrNotRoot
}
return nil
}
func (cmd *WorkerCommand) dnsProxyRunner(logger lager.Logger) (ifrit.Runner, error) {
server, err := cmd.Guardian.DNS.Server()
if err != nil {
return nil, err
}
return ifrit.RunFunc(func(signals <-chan os.Signal, ready chan<- struct{}) error {
server.NotifyStartedFunc = func() {
close(ready)
logger.Info("started")
}
serveErr := make(chan error, 1)
go func() {
serveErr <- server.ListenAndServe()
}()
for {
select {
case err := <-serveErr:
return err
case <-signals:
server.Shutdown()
}
}
}), nil
}
func (cmd *WorkerCommand) loadResources(logger lager.Logger) ([]atc.WorkerResourceType, error) {
var types []atc.WorkerResourceType
if cmd.ResourceTypes != "" {
basePath := cmd.ResourceTypes.Path()
entries, err := ioutil.ReadDir(basePath)
if err != nil {
logger.Error("failed-to-read-resources-dir", err)
return nil, err
}
for _, e := range entries {
meta, err := ioutil.ReadFile(filepath.Join(basePath, e.Name(), "resource_metadata.json"))
if err != nil {
logger.Error("failed-to-read-resource-type-metadata", err)
return nil, err
}
var t atc.WorkerResourceType
err = json.Unmarshal(meta, &t)
if err != nil {
logger.Error("failed-to-unmarshal-resource-type-metadata", err)
return nil, err
}
t.Image = filepath.Join(basePath, e.Name(), "rootfs.tgz")
types = append(types, t)
}
}
return types, nil
}
func (cmd *WorkerCommand) hasFlags(prefix string) bool {
env := os.Environ()
for _, envVar := range env {
if strings.HasPrefix(envVar, prefix) {
return true
}
}
return false
}
const guardianEnvPrefix = "CONCOURSE_GARDEN_"
const containerdEnvPrefix = "CONCOURSE_CONTAINERD_"
// Checks if runtime specific flags provided match the selected runtime type
func (cmd *WorkerCommand) verifyRuntimeFlags() error {
switch {
case cmd.Runtime == houdiniRuntime:
if cmd.hasFlags(guardianEnvPrefix) || cmd.hasFlags(containerdEnvPrefix) {
return fmt.Errorf("cannot use %s or %s environment variables with Houdini", guardianEnvPrefix, containerdEnvPrefix)
}
case cmd.Runtime == containerdRuntime:
if cmd.hasFlags(guardianEnvPrefix) {
return fmt.Errorf("cannot use %s environment variables with Containerd", guardianEnvPrefix)
}
case cmd.Runtime == guardianRuntime:
if cmd.hasFlags(containerdEnvPrefix) {
return fmt.Errorf("cannot use %s environment variables with Guardian", containerdEnvPrefix)
}
default:
return fmt.Errorf("unsupported Runtime :%s", cmd.Runtime)
}
return nil
}