-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
150 lines (127 loc) · 3.3 KB
/
config.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
package runconfig
import (
"encoding/json"
"io"
"github.com/docker/docker/nat"
)
// Entrypoint encapsulates the container entrypoint.
// It might be represented as a string or an array of strings.
// We need to override the json decoder to accept both options.
// The JSON decoder will fail if the api sends an string and
// we try to decode it into an array of string.
type Entrypoint struct {
parts []string
}
func (e *Entrypoint) MarshalJSON() ([]byte, error) {
if e == nil {
return []byte{}, nil
}
return json.Marshal(e.Slice())
}
// UnmarshalJSON decoded the entrypoint whether it's a string or an array of strings.
func (e *Entrypoint) UnmarshalJSON(b []byte) error {
if len(b) == 0 {
return nil
}
p := make([]string, 0, 1)
if err := json.Unmarshal(b, &p); err != nil {
p = append(p, string(b))
}
e.parts = p
return nil
}
func (e *Entrypoint) Len() int {
if e == nil {
return 0
}
return len(e.parts)
}
func (e *Entrypoint) Slice() []string {
if e == nil {
return nil
}
return e.parts
}
func NewEntrypoint(parts ...string) *Entrypoint {
return &Entrypoint{parts}
}
type Command struct {
parts []string
}
func (e *Command) MarshalJSON() ([]byte, error) {
if e == nil {
return []byte{}, nil
}
return json.Marshal(e.Slice())
}
// UnmarshalJSON decoded the entrypoint whether it's a string or an array of strings.
func (e *Command) UnmarshalJSON(b []byte) error {
if len(b) == 0 {
return nil
}
p := make([]string, 0, 1)
if err := json.Unmarshal(b, &p); err != nil {
p = append(p, string(b))
}
e.parts = p
return nil
}
func (e *Command) Len() int {
if e == nil {
return 0
}
return len(e.parts)
}
func (e *Command) Slice() []string {
if e == nil {
return nil
}
return e.parts
}
func NewCommand(parts ...string) *Command {
return &Command{parts}
}
// Note: the Config structure should hold only portable information about the container.
// Here, "portable" means "independent from the host we are running on".
// Non-portable information *should* appear in HostConfig.
type Config struct {
Hostname string
Domainname string
User string
AttachStdin bool
AttachStdout bool
AttachStderr bool
PortSpecs []string // Deprecated - Can be in the format of 8080/tcp
ExposedPorts map[nat.Port]struct{}
Tty bool // Attach standard streams to a tty, including stdin if it is not closed.
OpenStdin bool // Open stdin
StdinOnce bool // If true, close stdin after the 1 attached client disconnects.
Env []string
Cmd *Command
Image string // Name of the image as it was passed by the operator (eg. could be symbolic)
Volumes map[string]struct{}
WorkingDir string
Entrypoint *Entrypoint
NetworkDisabled bool
MacAddress string
OnBuild []string
Labels map[string]string
}
type ContainerConfigWrapper struct {
*Config
*hostConfigWrapper
}
func (c ContainerConfigWrapper) HostConfig() *HostConfig {
if c.hostConfigWrapper == nil {
return new(HostConfig)
}
return c.hostConfigWrapper.GetHostConfig()
}
func DecodeContainerConfig(src io.Reader) (*Config, *HostConfig, error) {
decoder := json.NewDecoder(src)
var w ContainerConfigWrapper
if err := decoder.Decode(&w); err != nil {
return nil, nil, err
}
return w.Config, w.HostConfig(), nil
}