-
Notifications
You must be signed in to change notification settings - Fork 276
/
Copy pathcontainers.go
62 lines (57 loc) · 1.88 KB
/
containers.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
package utils
import (
"fmt"
"path/filepath"
"strconv"
"strings"
)
// GetCanonicalImageName produces a canonical image name.
// if the input name did not specify a tag, the implicit "latest" tag is returned.
func GetCanonicalImageName(imageName string) string {
// name transformation rules
// alpine == docker.io/library/alpine:latest
// foo/bar == docker.io/foo/bar:latest
// foo.bar/baz == foo.bar/bar:latest
// localhost/foo:bar == localhost/foo:bar
// docker.elastic.co/elasticsearch/elasticsearch == docker.elastic.co/elasticsearch/elasticsearch:latest
canonicalImageName := imageName
slashCount := strings.Count(imageName, "/")
switch slashCount {
case 0:
canonicalImageName = "docker.io/library/" + imageName
case 1:
// split on slash to get first element of the name
nameSplit := strings.Split(imageName, "/")
// case of foo.bar/baz
if strings.Contains(nameSplit[0], ".") {
canonicalImageName = imageName
} else if strings.Contains(nameSplit[0], "localhost") {
// case of localhost/foo:bar - podman prefixes local images with "localhost"
canonicalImageName = imageName
} else {
canonicalImageName = "docker.io/" + imageName
}
}
// append latest tag if no tag was provided
if !strings.Contains(canonicalImageName, ":") {
canonicalImageName = canonicalImageName + ":latest"
}
return canonicalImageName
}
// ContainerNSToPID resolves the name of a container via
// the "/run/netns/<CONTAINERNAME>" to its PID.
func ContainerNSToPID(cID string) (int, error) {
pnns, err := filepath.EvalSymlinks("/run/netns/" + cID)
if err != nil {
return 0, err
}
pathElem := strings.Split(pnns, "/")
if len(pathElem) != 4 {
return 0, fmt.Errorf("unexpected result looking up container PID")
}
pid, err := strconv.Atoi(pathElem[1])
if err != nil {
return 0, fmt.Errorf("error converting the string part of the namespace link to int")
}
return pid, nil
}