forked from moby/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer_create.go
More file actions
125 lines (106 loc) · 3.54 KB
/
Copy pathcontainer_create.go
File metadata and controls
125 lines (106 loc) · 3.54 KB
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
package client
import (
"context"
"encoding/json"
"net/url"
"path"
"sort"
"strings"
cerrdefs "github.com/containerd/errdefs"
"github.com/moby/moby/api/types/container"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)
// ContainerCreate creates a new container based on the given configuration.
// It can be associated with a name, but it's not mandatory.
func (cli *Client) ContainerCreate(ctx context.Context, options ContainerCreateOptions) (ContainerCreateResult, error) {
cfg := options.Config
if cfg == nil {
cfg = &container.Config{}
}
if options.Image != "" {
if cfg.Image != "" {
return ContainerCreateResult{}, cerrdefs.ErrInvalidArgument.WithMessage("either Image or config.Image should be set")
}
newCfg := *cfg
newCfg.Image = options.Image
cfg = &newCfg
}
if cfg.Image == "" {
return ContainerCreateResult{}, cerrdefs.ErrInvalidArgument.WithMessage("config.Image or Image is required")
}
var response container.CreateResponse
if options.HostConfig != nil {
options.HostConfig.CapAdd = normalizeCapabilities(options.HostConfig.CapAdd)
options.HostConfig.CapDrop = normalizeCapabilities(options.HostConfig.CapDrop)
}
query := url.Values{}
if options.Platform != nil {
if p := formatPlatform(*options.Platform); p != "unknown" {
query.Set("platform", p)
}
}
if options.Name != "" {
query.Set("name", options.Name)
}
body := container.CreateRequest{
Config: cfg,
HostConfig: options.HostConfig,
NetworkingConfig: options.NetworkingConfig,
}
resp, err := cli.post(ctx, "/containers/create", query, body, nil)
defer ensureReaderClosed(resp)
if err != nil {
return ContainerCreateResult{}, err
}
err = json.NewDecoder(resp.Body).Decode(&response)
return ContainerCreateResult{ID: response.ID, Warnings: response.Warnings}, err
}
// formatPlatform returns a formatted string representing platform (e.g., "linux/arm/v7").
//
// It is a fork of [platforms.Format], and does not yet support "os.version",
// as [platforms.FormatAll] does.
//
// [platforms.Format]: https://github.com/containerd/platforms/blob/v1.0.0-rc.1/platforms.go#L309-L316
// [platforms.FormatAll]: https://github.com/containerd/platforms/blob/v1.0.0-rc.1/platforms.go#L318-L330
func formatPlatform(platform ocispec.Platform) string {
if platform.OS == "" {
return "unknown"
}
return path.Join(platform.OS, platform.Architecture, platform.Variant)
}
// allCapabilities is a magic value for "all capabilities"
const allCapabilities = "ALL"
// normalizeCapabilities normalizes capabilities to their canonical form,
// removes duplicates, and sorts the results.
//
// It is similar to [caps.NormalizeLegacyCapabilities],
// but performs no validation based on supported capabilities.
//
// [caps.NormalizeLegacyCapabilities]: https://github.com/moby/moby/blob/v28.3.2/oci/caps/utils.go#L56
func normalizeCapabilities(caps []string) []string {
var normalized []string
unique := make(map[string]struct{})
for _, c := range caps {
c = normalizeCap(c)
if _, ok := unique[c]; ok {
continue
}
unique[c] = struct{}{}
normalized = append(normalized, c)
}
sort.Strings(normalized)
return normalized
}
// normalizeCap normalizes a capability to its canonical format by upper-casing
// and adding a "CAP_" prefix (if not yet present). It also accepts the "ALL"
// magic-value.
func normalizeCap(capability string) string {
capability = strings.ToUpper(capability)
if capability == allCapabilities {
return capability
}
if !strings.HasPrefix(capability, "CAP_") {
capability = "CAP_" + capability
}
return capability
}