forked from docker/app
/
stack.go
277 lines (238 loc) · 10.8 KB
/
stack.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
package templatev1beta2
import (
"github.com/docker/app/internal/helm/templatetypes"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)
// StackList is a list of stacks
type StackList struct {
metav1.TypeMeta `yaml:",inline"`
metav1.ListMeta `yaml:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`
Items []Stack `yaml:"items" protobuf:"bytes,2,rep,name=items"`
}
// TypeMeta is a rewrite of metav1.TypeMeta which doesn't have yaml annotations
type TypeMeta struct {
Kind string `json:"kind,omitempty" yaml:"kind,omitempty"`
APIVersion string `json:"apiVersion,omitempty" yaml:"apiVersion,omitempty"`
}
// GetObjectKind implements the ObjectKind interface
func (obj *TypeMeta) GetObjectKind() schema.ObjectKind {
return obj
}
// GroupVersionKind implements the ObjectKind interface
func (obj *TypeMeta) GroupVersionKind() schema.GroupVersionKind {
return schema.FromAPIVersionAndKind(obj.APIVersion, obj.Kind)
}
// SetGroupVersionKind implements the ObjectKind interface
func (obj *TypeMeta) SetGroupVersionKind(gvk schema.GroupVersionKind) {
obj.APIVersion, obj.Kind = gvk.ToAPIVersionAndKind()
}
// Stack is v1beta2's representation of a Stack
type Stack struct {
TypeMeta `yaml:",inline" json:",inline"`
metav1.ObjectMeta `yaml:"metadata,omitempty" json:"metadata,omitempty"`
Spec *StackSpec `yaml:"spec,omitempty"`
Status *StackStatus `yaml:"status,omitempty"`
}
// DeepCopyObject clones the stack
func (s *Stack) DeepCopyObject() runtime.Object {
return s.clone()
}
// DeepCopyObject clones the stack list
func (s *StackList) DeepCopyObject() runtime.Object {
if s == nil {
return nil
}
result := new(StackList)
result.TypeMeta = s.TypeMeta
result.ListMeta = s.ListMeta
if s.Items == nil {
return result
}
result.Items = make([]Stack, len(s.Items))
for ix, s := range s.Items {
result.Items[ix] = *s.clone()
}
return result
}
func (s *Stack) clone() *Stack {
if s == nil {
return nil
}
result := new(Stack)
result.TypeMeta = s.TypeMeta
result.ObjectMeta = s.ObjectMeta
result.Spec = s.Spec.clone()
result.Status = s.Status.clone()
return result
}
// StackSpec defines the desired state of Stack
type StackSpec struct {
Services []ServiceConfig `yaml:"services,omitempty"`
Secrets map[string]SecretConfig `yaml:"secrets,omitempty"`
Configs map[string]ConfigObjConfig `yaml:"configs,omitempty"`
}
// ServiceConfig is the configuration of one service
type ServiceConfig struct {
Name string `yaml:"name,omitempty"`
CapAdd []templatetypes.StringTemplate `yaml:"cap_add,omitempty"`
CapDrop []templatetypes.StringTemplate `yaml:"cap_drop,omitempty"`
Command []templatetypes.StringTemplate `yaml:"command,omitempty"`
Configs []ServiceConfigObjConfig `yaml:"configs,omitempty"`
Deploy DeployConfig `yaml:"deploy,omitempty"`
Entrypoint []templatetypes.StringTemplate `yaml:"entrypoint,omitempty"`
Environment map[templatetypes.StringTemplate]*templatetypes.StringTemplate `yaml:"environment,omitempty"`
ExtraHosts []templatetypes.StringTemplate `yaml:"extra_hosts,omitempty"`
Hostname templatetypes.StringTemplate `yaml:"hostname,omitempty"`
HealthCheck *HealthCheckConfig `yaml:"health_check,omitempty"`
Image templatetypes.StringTemplate `yaml:"image,omitempty"`
Ipc templatetypes.StringTemplate `yaml:"ipc,omitempty"`
Labels map[templatetypes.StringTemplate]templatetypes.StringTemplate `yaml:"labels,omitempty"`
Pid templatetypes.StringTemplate `yaml:"pid,omitempty"`
Ports []ServicePortConfig `yaml:"ports,omitempty"`
Privileged templatetypes.BoolOrTemplate `yaml:"privileged,omitempty" yaml:"privileged,omitempty"`
ReadOnly templatetypes.BoolOrTemplate `yaml:"read_only,omitempty"`
Secrets []ServiceSecretConfig `yaml:"secrets,omitempty"`
StdinOpen templatetypes.BoolOrTemplate `yaml:"stdin_open,omitempty"`
StopGracePeriod templatetypes.DurationOrTemplate `yaml:"stop_grace_period,omitempty"`
Tmpfs templatetypes.StringTemplateList `yaml:"tmpfs,omitempty"`
Tty templatetypes.BoolOrTemplate `yaml:"tty,omitempty"`
User *int64 `yaml:"user,omitempty"`
Volumes []ServiceVolumeConfig `yaml:"volumes,omitempty"`
WorkingDir templatetypes.StringTemplate `yaml:"working_dir,omitempty"`
}
// ServicePortConfig is the port configuration for a service
type ServicePortConfig struct {
Mode templatetypes.StringTemplate `yaml:"mode,omitempty"`
Target templatetypes.UInt64OrTemplate `yaml:"target,omitempty"`
Published templatetypes.UInt64OrTemplate `yaml:"published,omitempty"`
Protocol templatetypes.StringTemplate `yaml:"protocol,omitempty"`
}
// FileObjectConfig is a config type for a file used by a service
type FileObjectConfig struct {
Name templatetypes.StringTemplate `yaml:"name,omitempty"`
File templatetypes.StringTemplate `yaml:"file,omitempty"`
External External `yaml:"external,omitempty"`
Labels map[string]string `yaml:"labels,omitempty"`
}
// SecretConfig for a secret
type SecretConfig FileObjectConfig
// ConfigObjConfig is the config for the swarm "Config" object
type ConfigObjConfig FileObjectConfig
// External identifies a Volume or Network as a reference to a resource that is
// not managed, and should already exist.
// External.name is deprecated and replaced by Volume.name
type External struct {
Name string `yaml:"name,omitempty"`
External bool `yaml:"external,omitempty"`
}
// FileReferenceConfig for a reference to a swarm file object
type FileReferenceConfig struct {
Source templatetypes.StringTemplate `yaml:"source,omitempty"`
Target templatetypes.StringTemplate `yaml:"target,omitempty"`
UID templatetypes.StringTemplate `yaml:"uid,omitempty"`
GID templatetypes.StringTemplate `yaml:"gid,omitempty"`
Mode templatetypes.UInt64OrTemplate `yaml:"mode,omitempty"`
}
// ServiceConfigObjConfig is the config obj configuration for a service
type ServiceConfigObjConfig FileReferenceConfig
// ServiceSecretConfig is the secret configuration for a service
type ServiceSecretConfig FileReferenceConfig
// DeployConfig is the deployment configuration for a service
type DeployConfig struct {
Mode templatetypes.StringTemplate `yaml:"mode,omitempty"`
Replicas templatetypes.UInt64OrTemplate `yaml:"replicas,omitempty"`
Labels map[templatetypes.StringTemplate]templatetypes.StringTemplate `yaml:"labels,omitempty"`
UpdateConfig *UpdateConfig `yaml:"update_config,omitempty"`
Resources Resources `yaml:"resources,omitempty"`
RestartPolicy *RestartPolicy `yaml:"restart_policy,omitempty"`
Placement Placement `yaml:"placement,omitempty"`
}
// UpdateConfig is the service update configuration
type UpdateConfig struct {
Parallelism templatetypes.UInt64OrTemplate `yaml:"paralellism,omitempty"`
}
// Resources the resource limits and reservations
type Resources struct {
Limits *Resource `yaml:"limits,omitempty"`
Reservations *Resource `yaml:"reservations,omitempty"`
}
// Resource is a resource to be limited or reserved
type Resource struct {
NanoCPUs templatetypes.StringTemplate `yaml:"cpus,omitempty"`
MemoryBytes templatetypes.UnitBytesOrTemplate `yaml:"memory,omitempty"`
}
// RestartPolicy is the service restart policy
type RestartPolicy struct {
Condition string `yaml:"condition,omitempty"`
}
// Placement constraints for the service
type Placement struct {
Constraints *Constraints `yaml:"constraints,omitempty"`
}
// Constraints lists constraints that can be set on the service
type Constraints struct {
OperatingSystem *Constraint
Architecture *Constraint
Hostname *Constraint
MatchLabels map[string]Constraint
}
// Constraint defines a constraint and it's operator (== or !=)
type Constraint struct {
Value string
Operator string
}
// HealthCheckConfig the healthcheck configuration for a service
type HealthCheckConfig struct {
Test []string `yaml:"test,omitempty"`
Timeout templatetypes.DurationOrTemplate `yaml:"timeout,omitempty"`
Interval templatetypes.DurationOrTemplate `yaml:"interval,omitempty"`
Retries templatetypes.UInt64OrTemplate `yaml:"retries,omitempty"`
}
// ServiceVolumeConfig are references to a volume used by a service
type ServiceVolumeConfig struct {
Type string `yaml:"type,omitempty"`
Source templatetypes.StringTemplate `yaml:"source,omitempty"`
Target templatetypes.StringTemplate `yaml:"target,omitempty"`
ReadOnly templatetypes.BoolOrTemplate `yaml:"read_only,omitempty"`
}
func (s *StackSpec) clone() *StackSpec {
if s == nil {
return nil
}
result := *s
return &result
}
// StackPhase is the deployment phase of a stack
type StackPhase string
// These are valid conditions of a stack.
const (
// StackAvailable means the stack is available.
StackAvailable StackPhase = "Available"
// StackProgressing means the deployment is progressing.
StackProgressing StackPhase = "Progressing"
// StackFailure is added in a stack when one of its members fails to be created
// or deleted.
StackFailure StackPhase = "Failure"
)
// StackStatus defines the observed state of Stack
type StackStatus struct {
// Current condition of the stack.
// +optional
Phase StackPhase `yaml:"phase,omitempty" protobuf:"bytes,1,opt,name=phase,casttype=StackPhase"`
// A human readable message indicating details about the stack.
// +optional
Message string `yaml:"message,omitempty" protobuf:"bytes,5,opt,name=message"`
}
func (s *StackStatus) clone() *StackStatus {
if s == nil {
return nil
}
result := *s
return &result
}
// Clone clones a Stack
func (s *Stack) Clone() *Stack {
return s.clone()
}