-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunc.go
186 lines (156 loc) · 4.15 KB
/
func.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 main
import (
"slices"
"strconv"
"strings"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/oci/caps"
)
type twoOf[T any] struct {
first T
second T
name string
}
func handleCapabilities(cap twoOf[[]string]) (ret []string) {
defaults := caps.DefaultCapabilities()
for _, c := range cap.first {
if !slices.Contains(defaults, "CAP_"+c) {
ret = append(ret, "--cap-add=CAP_"+c)
}
}
for _, c := range cap.second {
if slices.Contains(defaults, "CAP_"+c) {
ret = append(ret, "--cap-drop=CAP_"+c)
}
}
return
}
func handleRestart(r container.RestartPolicy) []string {
if r.IsNone() {
return nil
}
restartStr := string(r.Name)
if r.IsOnFailure() && r.MaximumRetryCount > 0 {
restartStr += ":" + strconv.Itoa(r.MaximumRetryCount)
}
return []string{"--restart=" + restartStr}
}
func handleIsolation(iso container.Isolation) []string {
if !iso.IsDefault() {
return []string{"--isolation=" + string(iso)}
}
return nil
}
// TODO: May need more work: https://docs.docker.com/reference/cli/docker/container/run/#network
func handleNetworkMode(n container.NetworkMode) []string {
if !(n.IsDefault() || n.IsBridge()) {
return []string{"--network=" + string(n)}
}
return nil
}
// NOTE: Links are deprecated, per https://docs.docker.com/engine/network/links/
func handleLinks(links []string) (ret []string) {
for _, l := range links {
src, dst, _ := strings.Cut(l, ":")
src = strings.TrimPrefix(src, "/")
dst = strings.TrimPrefix(dst, "/")
if src != dst {
src += ":" + dst
}
ret = append(ret, "--link "+src)
}
return
}
func handleDevices(devices []container.DeviceMapping) (ret []string) {
for _, device := range devices {
deviceStr := device.PathOnHost + ":" + device.PathInContainer
if device.CgroupPermissions != "rwm" {
deviceStr += ":" + device.CgroupPermissions
}
ret = append(ret, "--device "+deviceStr)
}
return
}
func handleLabels(l twoOf[map[string]string]) (ret []string) {
for k, v := range l.first {
if iv, ok := l.second[k]; !ok || v != iv {
ret = append(ret, l.name+"'"+k+"="+v+"'")
}
}
return
}
func handleHealthcheck(health *container.HealthConfig) (ret []string) {
if health == nil {
return
}
if len(health.Test) > 0 {
if health.Test[0] == "NONE" {
ret = append(ret, "--no-healthcheck")
return
}
ret = append(ret, "--health-cmd="+strconv.Quote(strings.Join(health.Test[1:], " "))) // TODO: This is probably not right
}
var opts = map[string]time.Duration{
"--health-interval=": health.Interval,
"--health-retries=": time.Duration(health.Retries),
"--health-timeout=": health.Timeout,
"--health-start-interval=": health.StartInterval,
"--health-start-period=": health.StartPeriod,
}
for name, val := range opts {
if val != 0 {
ret = append(ret, name+val.String())
}
}
return
}
func handlePorts(ctdata *types.ContainerJSON) (ret []string) {
if ctdata.HostConfig.PublishAllPorts {
ret = append(ret, "-P")
return
}
ports := ctdata.HostConfig.PortBindings
if ports == nil {
return
}
for ctport, bindings := range ports {
protocol := ""
if ctport.Proto() == "udp" { // TODO: "sctp" is listed as an option, should that be included?
protocol = "/udp"
}
for _, b := range bindings {
host := ""
if !(b.HostIP == "0.0.0.0" || b.HostIP == "" || b.HostIP == "::") {
host = b.HostIP + ":"
}
// If no host port mapping is defined then we know to expose it
if b.HostPort == "" {
ret = append(ret, "--expose "+ctport.Port())
break
} else if b.HostPort != "0" {
host += b.HostPort + ":"
}
ret = append(ret, "-p "+host+ctport.Port()+protocol)
}
}
return
}
// https://github.com/moby/moby/blob/27.x/integration/internal/container/ops.go#L138
func handleTmpFS(tmpfs map[string]string) (ret []string) {
for k, v := range tmpfs {
ret = append(ret, "--tmpfs="+k+":"+v)
}
return
}
func handleCommand(cmds twoOf[[]string]) []string {
if len(cmds.first) == 0 {
return nil
}
if slices.Compare(cmds.first, cmds.second) == 0 {
return nil
}
// TODO: Escape quotes
return []string{cmds.name + strconv.Quote(strings.Join(cmds.first, " "))}
}