-
Notifications
You must be signed in to change notification settings - Fork 452
/
containerd.go
143 lines (117 loc) · 3.76 KB
/
containerd.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
// 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"
"fmt"
"path/filepath"
"github.com/containerd/containerd"
"google.golang.org/grpc/health/grpc_health_v1"
"github.com/talos-systems/talos/internal/app/machined/pkg/runtime"
"github.com/talos-systems/talos/internal/app/machined/pkg/system/events"
"github.com/talos-systems/talos/internal/app/machined/pkg/system/health"
"github.com/talos-systems/talos/internal/app/machined/pkg/system/runner"
"github.com/talos-systems/talos/internal/app/machined/pkg/system/runner/process"
"github.com/talos-systems/talos/internal/app/machined/pkg/system/runner/restart"
"github.com/talos-systems/talos/pkg/conditions"
"github.com/talos-systems/talos/pkg/machinery/constants"
)
// Containerd implements the Service interface. It serves as the concrete type with
// the required methods.
type Containerd struct {
// client is a lazy-initialized containerd client. It should be accessed using the Client() method.
client *containerd.Client
}
// Client lazy-initializes the containerd client if needed and returns it.
func (c *Containerd) Client() (*containerd.Client, error) {
if c.client != nil {
return c.client, nil
}
client, err := containerd.New(constants.SystemContainerdAddress)
if err != nil {
return nil, err
}
c.client = client
return c.client, err
}
// ID implements the Service interface.
func (c *Containerd) ID(r runtime.Runtime) string {
return "containerd"
}
// PreFunc implements the Service interface.
func (c *Containerd) PreFunc(ctx context.Context, r runtime.Runtime) error {
return nil
}
// PostFunc implements the Service interface.
func (c *Containerd) PostFunc(r runtime.Runtime, state events.ServiceState) (err error) {
if c.client != nil {
return c.client.Close()
}
return nil
}
// Condition implements the Service interface.
func (c *Containerd) Condition(r runtime.Runtime) conditions.Condition {
return nil
}
// DependsOn implements the Service interface.
func (c *Containerd) DependsOn(r runtime.Runtime) []string {
return nil
}
// Runner implements the Service interface.
func (c *Containerd) Runner(r runtime.Runtime) (runner.Runner, error) {
// Set the process arguments.
args := &runner.Args{
ID: c.ID(r),
ProcessArgs: []string{
"/bin/containerd",
"--address",
constants.SystemContainerdAddress,
"--state",
filepath.Join(constants.SystemRunPath, "containerd"),
"--root",
filepath.Join(constants.SystemVarPath, "lib", "containerd"),
},
}
env := []string{}
if r.Config() != nil {
for key, val := range r.Config().Machine().Env() {
env = append(env, fmt.Sprintf("%s=%s", key, val))
}
}
debug := false
if r.Config() != nil {
debug = r.Config().Debug()
}
return restart.New(process.NewRunner(
debug,
args,
runner.WithLoggingManager(r.Logging()),
runner.WithEnv(env),
runner.WithOOMScoreAdj(-999),
runner.WithCgroupPath(constants.CgroupSystemRuntime),
),
restart.WithType(restart.Forever),
), nil
}
// HealthFunc implements the HealthcheckedService interface.
func (c *Containerd) HealthFunc(runtime.Runtime) health.Check {
return func(ctx context.Context) error {
client, err := c.Client()
if err != nil {
return err
}
resp, err := client.HealthService().Check(ctx, &grpc_health_v1.HealthCheckRequest{})
if err != nil {
return err
}
if resp.Status != grpc_health_v1.HealthCheckResponse_SERVING {
return fmt.Errorf("unexpected serving status: %d", resp.Status)
}
return nil
}
}
// HealthSettings implements the HealthcheckedService interface.
func (c *Containerd) HealthSettings(runtime.Runtime) *health.Settings {
return &health.DefaultSettings
}