-
Notifications
You must be signed in to change notification settings - Fork 1k
/
infra.go
186 lines (160 loc) · 4.89 KB
/
infra.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
package sandbox
import (
"errors"
"fmt"
"io"
"os"
"strings"
"github.com/containers/storage/pkg/idtools"
"github.com/cri-o/cri-o/internal/factory/container"
"github.com/cri-o/cri-o/internal/storage"
libconfig "github.com/cri-o/cri-o/pkg/config"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
spec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/opencontainers/runtime-tools/generate"
"github.com/opencontainers/selinux/go-selinux/label"
"golang.org/x/sys/unix"
types "k8s.io/cri-api/pkg/apis/runtime/v1"
)
func (s *sandbox) InitInfraContainer(serverConfig *libconfig.Config, podContainer *storage.ContainerInfo, sandboxIDMappings *idtools.IDMappings) error {
var err error
s.infra, err = container.New()
if err != nil {
return err
}
// determine pause command before initializing generator
// so a failure will not result in a half configured generator
pauseCommand, err := PauseCommand(serverConfig, podContainer.Config)
if err != nil {
return err
}
g := s.infra.Spec()
g.HostSpecific = true
g.ClearProcessRlimits()
// setup defaults for the pod sandbox
g.SetRootReadonly(true)
// configure default ulimits
for _, u := range serverConfig.Ulimits() {
g.AddProcessRlimits(u.Name, u.Hard, u.Soft)
}
g.SetProcessArgs(pauseCommand)
if err := s.createResolvConf(podContainer, sandboxIDMappings); err != nil {
return fmt.Errorf("create resolv conf: %w", err)
}
// Add capabilities from crio.conf if default_capabilities is defined
if err := s.infra.SpecSetupCapabilities(&types.Capability{}, serverConfig.DefaultCapabilities, serverConfig.AddInheritableCapabilities); err != nil {
return err
}
return nil
}
// Spec can only be called after a successful call to InitInfraContainer
func (s *sandbox) Spec() *generate.Generator {
return s.infra.Spec()
}
// PauseCommand returns the pause command for the provided image configuration.
func PauseCommand(cfg *libconfig.Config, image *v1.Image) ([]string, error) {
if cfg == nil {
return nil, errors.New("provided configuration is nil")
}
// This has been explicitly set by the user, since the configuration
// default is `/pause`
if cfg.PauseCommand != "" {
return []string{cfg.PauseCommand}, nil
}
if image == nil || (len(image.Config.Entrypoint) == 0 && len(image.Config.Cmd) == 0) {
return nil, fmt.Errorf(
"unable to run pause image %q: %s",
cfg.PauseImage,
"neither Cmd nor Entrypoint specified",
)
}
cmd := []string{}
cmd = append(cmd, image.Config.Entrypoint...)
cmd = append(cmd, image.Config.Cmd...)
return cmd, nil
}
func (s *sandbox) createResolvConf(podContainer *storage.ContainerInfo, sandboxIDMappings *idtools.IDMappings) (retErr error) {
// set DNS options
s.resolvPath = podContainer.RunDir + "/resolv.conf"
if s.config.DnsConfig == nil {
// Ref https://github.com/kubernetes/kubernetes/issues/120748#issuecomment-1922220911
s.config.DnsConfig = &types.DNSConfig{}
}
dnsServers := s.config.DnsConfig.Servers
dnsSearches := s.config.DnsConfig.Searches
dnsOptions := s.config.DnsConfig.Options
err := ParseDNSOptions(dnsServers, dnsSearches, dnsOptions, s.resolvPath)
defer func() {
if retErr != nil {
if err := os.Remove(s.resolvPath); err != nil {
retErr = fmt.Errorf("failed to remove resolvPath after failing to create it: %w", retErr)
}
}
}()
if err != nil {
return err
}
if err := label.Relabel(s.resolvPath, podContainer.MountLabel, false); err != nil && !errors.Is(err, unix.ENOTSUP) {
return err
}
if sandboxIDMappings != nil {
rootPair := sandboxIDMappings.RootPair()
if err := os.Chown(s.resolvPath, rootPair.UID, rootPair.GID); err != nil {
return fmt.Errorf("cannot chown %s to %d:%d: %w", s.resolvPath, rootPair.UID, rootPair.GID, err)
}
}
mnt := spec.Mount{
Type: "bind",
Source: s.resolvPath,
Destination: "/etc/resolv.conf",
Options: []string{"ro", "bind", "nodev", "nosuid", "noexec"},
}
s.infra.Spec().AddMount(mnt)
return nil
}
func ParseDNSOptions(servers, searches, options []string, path string) (retErr error) {
nServers := len(servers)
nSearches := len(searches)
nOptions := len(options)
if nServers == 0 && nSearches == 0 && nOptions == 0 {
return copyFile("/etc/resolv.conf", path)
}
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
if nSearches > 0 {
_, err = f.WriteString("search " + strings.Join(searches, " ") + "\n")
if err != nil {
return err
}
}
if nServers > 0 {
_, err = f.WriteString("nameserver " + strings.Join(servers, "\nnameserver ") + "\n")
if err != nil {
return err
}
}
if nOptions > 0 {
_, err = f.WriteString("options " + strings.Join(options, " ") + "\n")
if err != nil {
return err
}
}
return nil
}
func copyFile(src, dest string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dest)
if err != nil {
return err
}
defer out.Close()
_, err = io.Copy(out, in)
return err
}