-
Notifications
You must be signed in to change notification settings - Fork 456
/
extension.go
244 lines (199 loc) · 6.91 KB
/
extension.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package services
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"github.com/containerd/containerd/oci"
"github.com/hashicorp/go-envparse"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/siderolabs/gen/maps"
"github.com/siderolabs/talos/internal/app/machined/pkg/runtime"
"github.com/siderolabs/talos/internal/app/machined/pkg/system/events"
"github.com/siderolabs/talos/internal/app/machined/pkg/system/runner"
"github.com/siderolabs/talos/internal/app/machined/pkg/system/runner/containerd"
"github.com/siderolabs/talos/internal/app/machined/pkg/system/runner/restart"
"github.com/siderolabs/talos/internal/pkg/capability"
"github.com/siderolabs/talos/internal/pkg/environment"
"github.com/siderolabs/talos/internal/pkg/mount"
"github.com/siderolabs/talos/pkg/conditions"
"github.com/siderolabs/talos/pkg/machinery/constants"
extservices "github.com/siderolabs/talos/pkg/machinery/extensions/services"
"github.com/siderolabs/talos/pkg/machinery/resources/network"
"github.com/siderolabs/talos/pkg/machinery/resources/time"
)
// Extension service is a generic wrapper around extension services spec.
type Extension struct {
Spec extservices.Spec
overlay *mount.Point
}
// ID implements the Service interface.
func (svc *Extension) ID(r runtime.Runtime) string {
return "ext-" + svc.Spec.Name
}
// PreFunc implements the Service interface.
func (svc *Extension) PreFunc(ctx context.Context, r runtime.Runtime) error {
// re-mount service rootfs as overlay rw mount to allow containerd to mount there /dev, /proc, etc.
svc.overlay = mount.NewMountPoint(
"",
filepath.Join(constants.ExtensionServicesRootfsPath, svc.Spec.Name),
"",
0,
"",
mount.WithFlags(mount.Overlay|mount.SystemOverlay),
)
return svc.overlay.Mount()
}
// PostFunc implements the Service interface.
func (svc *Extension) PostFunc(r runtime.Runtime, state events.ServiceState) (err error) {
return svc.overlay.Unmount()
}
// Condition implements the Service interface.
func (svc *Extension) Condition(r runtime.Runtime) conditions.Condition {
conds := []conditions.Condition{}
if svc.Spec.Container.EnvironmentFile != "" {
// add a dependency on the environment file
conds = append(conds, conditions.WaitForFileToExist(svc.Spec.Container.EnvironmentFile))
}
for _, dep := range svc.Spec.Depends {
switch {
case dep.Path != "":
conds = append(conds, conditions.WaitForFileToExist(dep.Path))
case len(dep.Network) > 0:
conds = append(conds, network.NewReadyCondition(r.State().V1Alpha2().Resources(), network.StatusChecksFromStatuses(dep.Network...)...))
case dep.Time:
conds = append(conds, time.NewSyncCondition(r.State().V1Alpha2().Resources()))
}
}
if len(conds) == 0 {
return nil
}
return conditions.WaitForAll(conds...)
}
// DependsOn implements the Service interface.
func (svc *Extension) DependsOn(r runtime.Runtime) []string {
deps := []string{"containerd"}
for _, dep := range svc.Spec.Depends {
if dep.Service != "" {
deps = append(deps, dep.Service)
}
}
return deps
}
func (svc *Extension) getOCIOptions(envVars []string) []oci.SpecOpts {
ociOpts := []oci.SpecOpts{
oci.WithRootFSPath(filepath.Join(constants.ExtensionServicesRootfsPath, svc.Spec.Name)),
containerd.WithRootfsPropagation(svc.Spec.Container.Security.RootfsPropagation),
oci.WithCgroup(filepath.Join(constants.CgroupExtensions, svc.Spec.Name)),
oci.WithMounts(svc.Spec.Container.Mounts),
oci.WithHostNamespace(specs.NetworkNamespace),
oci.WithSelinuxLabel(""),
oci.WithApparmorProfile(""),
oci.WithCapabilities(capability.AllGrantableCapabilities()),
oci.WithAllDevicesAllowed,
oci.WithEnv(envVars),
}
if !svc.Spec.Container.Security.WriteableRootfs {
ociOpts = append(ociOpts, oci.WithRootFSReadonly())
}
if svc.Spec.Container.Security.WriteableSysfs {
ociOpts = append(ociOpts, oci.WithWriteableSysfs)
}
if svc.Spec.Container.Security.MaskedPaths != nil {
ociOpts = append(ociOpts, oci.WithMaskedPaths(svc.Spec.Container.Security.MaskedPaths))
}
if svc.Spec.Container.Security.ReadonlyPaths != nil {
ociOpts = append(ociOpts, oci.WithReadonlyPaths(svc.Spec.Container.Security.ReadonlyPaths))
}
return ociOpts
}
// Runner implements the Service interface.
func (svc *Extension) Runner(r runtime.Runtime) (runner.Runner, error) {
args := runner.Args{
ID: svc.ID(r),
ProcessArgs: append([]string{svc.Spec.Container.Entrypoint}, svc.Spec.Container.Args...),
}
for _, mount := range svc.Spec.Container.Mounts {
if _, err := os.Stat(mount.Source); err == nil {
// already exists, skip
continue
} else if !errors.Is(err, os.ErrNotExist) {
return nil, err
}
if err := os.MkdirAll(mount.Source, 0o700); err != nil {
return nil, err
}
}
var restartType restart.Type
switch svc.Spec.Restart {
case extservices.RestartAlways:
restartType = restart.Forever
case extservices.RestartNever:
restartType = restart.Once
case extservices.RestartUntilSuccess:
restartType = restart.UntilSuccess
}
envVars, err := svc.parseEnvironment()
if err != nil {
return nil, err
}
ociSpecOpts := svc.getOCIOptions(envVars)
debug := false
if r.Config() != nil {
debug = r.Config().Debug()
}
return restart.New(containerd.NewRunner(
debug,
&args,
runner.WithLoggingManager(r.Logging()),
runner.WithNamespace(constants.SystemContainerdNamespace),
runner.WithContainerdAddress(constants.SystemContainerdAddress),
runner.WithEnv(environment.Get(r.Config())),
runner.WithOCISpecOpts(ociSpecOpts...),
runner.WithOOMScoreAdj(-600),
),
restart.WithType(restartType),
), nil
}
// APIRestartAllowed implements APIRestartableService.
func (svc *Extension) APIRestartAllowed(runtime.Runtime) bool {
return true
}
// APIStartAllowed implements APIStartableService.
func (svc *Extension) APIStartAllowed(runtime.Runtime) bool {
return true
}
// APIStopAllowed implements APIStoppableService.
func (svc *Extension) APIStopAllowed(runtime.Runtime) bool {
return true
}
func (svc *Extension) parseEnvironment() ([]string, error) {
var envVars []string
if svc.Spec.Container.EnvironmentFile != "" {
envFile, err := os.OpenFile(svc.Spec.Container.EnvironmentFile, os.O_RDONLY, 0)
if err != nil {
return nil, err
}
defer func() {
if closeErr := envFile.Close(); err != nil {
err = closeErr
}
}()
parsedEnvVars, err := envparse.Parse(envFile)
if err != nil {
return nil, fmt.Errorf("failed to parse environment file %q: %w", svc.Spec.Container.EnvironmentFile, err)
}
envVarsSlice := maps.ToSlice(parsedEnvVars, func(k, v string) string {
return fmt.Sprintf("%s=%s", k, v)
})
envVars = append(envVars, envVarsSlice...)
}
if svc.Spec.Container.Environment != nil {
envVars = append(envVars, svc.Spec.Container.Environment...)
}
return envVars, nil
}