-
Notifications
You must be signed in to change notification settings - Fork 293
/
helpers.go
318 lines (289 loc) · 10.1 KB
/
helpers.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/*
Copyright 2021 Mirantis
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package libdocker
import (
"fmt"
"runtime"
"strconv"
"strings"
"time"
dockerref "github.com/docker/distribution/reference"
dockertypes "github.com/docker/docker/api/types"
dockermount "github.com/docker/docker/api/types/mount"
"github.com/docker/go-connections/nat"
godigest "github.com/opencontainers/go-digest"
"github.com/sirupsen/logrus"
v1 "k8s.io/cri-api/pkg/apis/runtime/v1"
"k8s.io/kubernetes/pkg/apis/core"
)
const windowsEtcHostsPath = "C:\\Windows\\System32\\drivers\\etc\\hosts"
// ParseDockerTimestamp parses the timestamp returned by DockerClientInterface from string to time.Time
func ParseDockerTimestamp(s string) (time.Time, error) {
// Timestamp returned by Docker is in time.RFC3339Nano format.
return time.Parse(time.RFC3339Nano, s)
}
// matchImageTagOrSHA checks if the given image specifier is a valid image ref,
// and that it matches the given image. It should fail on things like image IDs
// (config digests) and other digest-only references, but succeed on image names
// (`foo`), tag references (`foo:bar`), and manifest digest references
// (`foo@sha256:xyz`).
func matchImageTagOrSHA(inspected dockertypes.ImageInspect, image string) bool {
// The image string follows the grammar specified here
// https://github.com/docker/distribution/blob/master/reference/reference.go#L4
named, err := dockerref.ParseNormalizedNamed(image)
if err != nil {
logrus.Errorf("Couldn't parse image (%s) reference: %v", image, err)
return false
}
_, isTagged := named.(dockerref.Tagged)
digest, isDigested := named.(dockerref.Digested)
if !isTagged && !isDigested {
// No Tag or SHA specified, so just return what we have
return true
}
if isTagged {
// Check the RepoTags for a match.
for _, tag := range inspected.RepoTags {
// An image name (without the tag/digest) can be [hostname '/'] component ['/' component]*
// Because either the RepoTag or the name *may* contain the
// hostname or not, we only check for the suffix match.
if strings.HasSuffix(image, tag) || strings.HasSuffix(tag, image) {
return true
} else {
// docker distro(s) like centos/fedora/rhel image fix problems on
// their end.
// Say the tag is "docker.io/busybox:latest"
// and the image is "docker.io/library/busybox:latest"
initialName, err := dockerref.ParseNormalizedNamed(tag)
if err != nil {
continue
}
// the parsed/normalized tag will look like
// reference.taggedReference {
// namedRepository: reference.repository {
// domain: "docker.io",
// path: "library/busybox"
// },
// tag: "latest"
// }
// If it does not have tags then we bail out
maybeTag, ok := initialName.(dockerref.Tagged)
if !ok {
continue
}
normalizedTag := maybeTag.String()
if normalizedTag == "" {
continue
}
if strings.HasSuffix(image, normalizedTag) || strings.HasSuffix(normalizedTag, image) {
return true
}
}
}
}
if isDigested {
for _, repoDigest := range inspected.RepoDigests {
named, err := dockerref.ParseNormalizedNamed(repoDigest)
if err != nil {
logrus.Errorf(
"Couldn't parse image RepoDigest reference %s: %v",
repoDigest,
err,
)
continue
}
if d, isDigested := named.(dockerref.Digested); isDigested {
if digest.Digest().Algorithm().String() == d.Digest().Algorithm().String() &&
digest.Digest().Hex() == d.Digest().Hex() {
return true
}
}
}
// process the ID as a digest
id, err := godigest.Parse(inspected.ID)
if err != nil {
logrus.Errorf("Couldn't parse image ID %s reference: %v", id, err)
return false
}
if digest.Digest().Algorithm().String() == id.Algorithm().String() &&
digest.Digest().Hex() == id.Hex() {
return true
}
}
logrus.Infof(
"Inspected image ID %s does not match image %s",
inspected.ID,
image,
)
return false
}
// matchImageIDOnly checks that the given image specifier is a digest-only
// reference, and that it matches the given image.
func matchImageIDOnly(inspected dockertypes.ImageInspect, image string) bool {
// If the image ref is literally equal to the inspected image's ID,
// just return true here (this might be the case for Docker 1.9,
// where we won't have a digest for the ID)
if inspected.ID == image {
return true
}
// Otherwise, we should try actual parsing to be more correct
ref, err := dockerref.Parse(image)
if err != nil {
logrus.Infof("Couldn't parse image reference %s: %v", image, err)
return false
}
digest, isDigested := ref.(dockerref.Digested)
if !isDigested {
logrus.Infof("The image reference %s was not a digest reference", image)
return false
}
id, err := godigest.Parse(inspected.ID)
if err != nil {
logrus.Infof("Couldn't parse image ID reference %s: %v", id, err)
return false
}
if digest.Digest().Algorithm().String() == id.Algorithm().String() &&
digest.Digest().Hex() == id.Hex() {
return true
}
logrus.Infof("The image reference %s does not directly refer to the given image's ID %s",
image, inspected.ID)
return false
}
func CheckContainerStatus(
client DockerClientInterface,
containerID string,
) (*dockertypes.ContainerJSON, error) {
container, err := client.InspectContainer(containerID)
if err != nil {
return nil, err
}
if !container.State.Running {
return nil, fmt.Errorf("container not running (%s)", container.ID)
}
return container, nil
}
// generateEnvList converts KeyValue list to a list of strings, in the form of
// '<key>=<value>', which can be understood by docker.
func GenerateEnvList(envs []*v1.KeyValue) (result []string) {
for _, env := range envs {
result = append(result, fmt.Sprintf("%s=%s", env.Key, env.Value))
}
return
}
// generateMountBindings converts the mount list to a list of [dockermount.Mount] that
// can be understood by docker.
// SELinux labels are not handled here.
func GenerateMountBindings(mounts []*v1.Mount, terminationMessagePath string) []dockermount.Mount {
if terminationMessagePath == "" {
terminationMessagePath = core.TerminationMessagePathDefault
}
result := make([]dockermount.Mount, 0, len(mounts))
for _, m := range mounts {
if runtime.GOOS == "windows" && isSingleFileMount(m.HostPath, m.ContainerPath, terminationMessagePath) {
logrus.Debugf("skipping mount :%s:%s", m.HostPath, m.ContainerPath)
continue
}
bind := dockermount.Mount{
Type: dockermount.TypeBind,
Source: m.HostPath,
Target: m.ContainerPath,
BindOptions: &dockermount.BindOptions{
CreateMountpoint: true,
},
}
if m.Readonly {
bind.ReadOnly = true
// Docker v25 treats read-only mounts as recursively read-only by default,
// but this appeared to be too much breaking for Kubernetes
// https://github.com/Mirantis/cri-dockerd/issues/309
bind.BindOptions.ReadOnlyNonRecursive = true
}
switch m.Propagation {
case v1.MountPropagation_PROPAGATION_PRIVATE:
// noop, dockerd will decide the propagation.
//
// dockerd's default propagation is "rprivate": https://github.com/moby/moby/blob/v20.10.23/volume/mounts/linux_parser.go#L145
//
// However, dockerd automatically changes the default propagation to "rslave"
// when the mount source contains the daemon root (/var/lib/docker):
// - https://github.com/moby/moby/blob/v20.10.23/daemon/volumes.go#L137-L143
// - https://github.com/moby/moby/blob/v20.10.23/daemon/volumes_linux.go#L11-L36
//
// This behavior was introduced in Docker 18.03: https://github.com/moby/moby/pull/36055
case v1.MountPropagation_PROPAGATION_BIDIRECTIONAL:
bind.BindOptions.Propagation = dockermount.PropagationRShared
case v1.MountPropagation_PROPAGATION_HOST_TO_CONTAINER:
bind.BindOptions.Propagation = dockermount.PropagationRSlave
default:
logrus.Infof("Unknown propagation mode for hostPath %s", m.HostPath)
// let dockerd decide the propagation
}
result = append(result, bind)
}
return result
}
func isSingleFileMount(hostPath string, containerPath string, terminationMessagePath string) bool {
if strings.Contains(containerPath, terminationMessagePath) {
return true
}
if strings.Contains(hostPath, windowsEtcHostsPath) || strings.Contains(containerPath, windowsEtcHostsPath) {
return true
}
return false
}
func MakePortsAndBindings(
pm []*v1.PortMapping,
) (nat.PortSet, map[nat.Port][]nat.PortBinding) {
exposedPorts := nat.PortSet{}
portBindings := map[nat.Port][]nat.PortBinding{}
for _, port := range pm {
exteriorPort := port.HostPort
if exteriorPort == 0 {
// No need to do port binding when HostPort is not specified
continue
}
interiorPort := port.ContainerPort
// Some of this port stuff is under-documented voodoo.
// See http://stackoverflow.com/questions/20428302/binding-a-port-to-a-host-interface-using-the-rest-api
var protocol string
switch port.Protocol {
case v1.Protocol_UDP:
protocol = "/udp"
case v1.Protocol_TCP:
protocol = "/tcp"
case v1.Protocol_SCTP:
protocol = "/sctp"
default:
logrus.Infof("Unknown protocol %s, defaulting to TCP", port.Protocol)
protocol = "/tcp"
}
dockerPort := nat.Port(strconv.Itoa(int(interiorPort)) + protocol)
exposedPorts[dockerPort] = struct{}{}
hostBinding := nat.PortBinding{
HostPort: strconv.Itoa(int(exteriorPort)),
HostIP: port.HostIp,
}
// Allow multiple host ports bind to same docker port
if existedBindings, ok := portBindings[dockerPort]; ok {
// If a docker port already map to a host port, just append the host ports
portBindings[dockerPort] = append(existedBindings, hostBinding)
} else {
// Otherwise, it's fresh new port binding
portBindings[dockerPort] = []nat.PortBinding{
hostBinding,
}
}
}
return exposedPorts, portBindings
}