forked from fluxcd/flux
/
service.go
289 lines (240 loc) · 5.98 KB
/
service.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
278
279
280
281
282
283
284
285
286
287
288
289
package flux
import (
"fmt"
"net/http"
"sort"
"strings"
"time"
"github.com/pkg/errors"
)
const (
ServiceSpecAll = ServiceSpec("<all>")
ImageSpecLatest = ImageSpec("<all latest>")
ImageSpecNone = ImageSpec("<no updates>")
PolicyNone = Policy("")
PolicyLocked = Policy("locked")
PolicyAutomated = Policy("automated")
)
var (
ErrInvalidServiceID = errors.New("invalid service ID")
ErrInvalidReleaseKind = errors.New("invalid release kind")
)
type Token string
func (t Token) Set(req *http.Request) {
if string(t) != "" {
req.Header.Set("Authorization", fmt.Sprintf("Scope-Probe token=%s", t))
}
}
type InstanceID string
const InstanceIDHeaderKey = "X-Scope-OrgID"
const DefaultInstanceID = "<default-instance-id>"
type ServiceID string // "default/helloworld"
func (id ServiceID) String() string {
return string(id)
}
func ParseServiceID(s string) (ServiceID, error) {
toks := strings.SplitN(s, "/", 2)
if len(toks) != 2 {
return "", ErrInvalidServiceID
}
return ServiceID(s), nil
}
func MakeServiceID(namespace, service string) ServiceID {
return ServiceID(namespace + "/" + service)
}
func (id ServiceID) Components() (namespace, service string) {
toks := strings.SplitN(string(id), "/", 2)
if len(toks) != 2 {
panic("invalid service spec")
}
return toks[0], toks[1]
}
type ServiceIDSet map[ServiceID]struct{}
func (s ServiceIDSet) String() string {
var ids []string
for id, _ := range s {
ids = append(ids, string(id))
}
return "{" + strings.Join(ids, ", ") + "}"
}
func (s ServiceIDSet) Add(ids []ServiceID) {
for _, id := range ids {
s[id] = struct{}{}
}
}
func (s ServiceIDSet) Without(others ServiceIDSet) ServiceIDSet {
if s == nil || len(s) == 0 || others == nil || len(others) == 0 {
return s
}
res := ServiceIDSet{}
for id := range s {
if !others.Contains(id) {
res[id] = struct{}{}
}
}
return res
}
func (s ServiceIDSet) Contains(id ServiceID) bool {
if s == nil {
return false
}
_, ok := s[id]
return ok
}
func (s ServiceIDSet) Intersection(others ServiceIDSet) ServiceIDSet {
if s == nil {
return others
}
if others == nil {
return s
}
result := ServiceIDSet{}
for id := range s {
if _, ok := others[id]; ok {
result[id] = struct{}{}
}
}
return result
}
func (s ServiceIDSet) ToSlice() ServiceIDs {
i := 0
keys := make(ServiceIDs, len(s))
for k := range s {
keys[i] = k
i++
}
return keys
}
type ServiceIDs []ServiceID
func (p ServiceIDs) Len() int { return len(p) }
func (p ServiceIDs) Less(i, j int) bool { return string(p[i]) < string(p[j]) }
func (p ServiceIDs) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p ServiceIDs) Sort() { sort.Sort(p) }
func (ids ServiceIDs) Without(others ServiceIDSet) (res ServiceIDs) {
for _, id := range ids {
if !others.Contains(id) {
res = append(res, id)
}
}
return res
}
func (ids ServiceIDs) Contains(id ServiceID) bool {
set := ServiceIDSet{}
set.Add(ids)
return set.Contains(id)
}
func (ids ServiceIDs) Intersection(others ServiceIDSet) ServiceIDSet {
set := ServiceIDSet{}
set.Add(ids)
return set.Intersection(others)
}
type ServiceSpec string // ServiceID or "<all>"
func ParseServiceSpec(s string) (ServiceSpec, error) {
if s == string(ServiceSpecAll) {
return ServiceSpecAll, nil
}
id, err := ParseServiceID(s)
if err != nil {
return "", errors.Wrap(err, "invalid service spec")
}
return ServiceSpec(id), nil
}
func (s ServiceSpec) AsID() (ServiceID, error) {
return ParseServiceID(string(s))
}
func (s ServiceSpec) String() string {
return string(s)
}
// ImageSpec is an ImageID, or "<all latest>" (update all containers
// to the latest available), or "<no updates>" (do not update any
// images)
type ImageSpec string
func ParseImageSpec(s string) (ImageSpec, error) {
if s == string(ImageSpecLatest) || s == string(ImageSpecNone) {
return ImageSpec(s), nil
}
parts := strings.Split(s, ":")
if len(parts) != 2 || parts[1] == "" {
return "", errors.Wrap(ErrInvalidImageID, "blank tag (if you want latest, explicitly state the tag :latest)")
}
id, err := ParseImageID(s)
return ImageSpec(id.String()), err
}
func (s ImageSpec) String() string {
return string(s)
}
func (s ImageSpec) AsID() (ImageID, error) {
return ParseImageID(s.String())
}
func ImageSpecFromID(id ImageID) ImageSpec {
return ImageSpec(id.String())
}
type ImageStatus struct {
ID ServiceID
Containers []Container
}
// Policy is an string, denoting the current deployment policy of a service,
// e.g. automated, or locked.
type Policy string
func ParsePolicy(s string) Policy {
for _, p := range []Policy{
PolicyLocked,
PolicyAutomated,
} {
if s == string(p) {
return p
}
}
return PolicyNone
}
type ServiceStatus struct {
ID ServiceID
Containers []Container
Status string
Automated bool
Locked bool
}
func (s ServiceStatus) Policies() string {
var ps []string
if s.Automated {
ps = append(ps, string(PolicyAutomated))
}
if s.Locked {
ps = append(ps, string(PolicyLocked))
}
sort.Strings(ps)
return strings.Join(ps, ",")
}
type Container struct {
Name string
Current ImageDescription
Available []ImageDescription
}
type ImageDescription struct {
ID ImageID
CreatedAt *time.Time `json:",omitempty"`
}
// Ask me for more details.
type HistoryEntry struct {
Stamp *time.Time `json:",omitempty"`
Type string
Data string
Event *Event `json:",omitempty"`
}
// TODO: How similar should this be to the `get-config` result?
type Status struct {
Fluxsvc FluxsvcStatus `json:"fluxsvc" yaml:"fluxsvc"`
Fluxd FluxdStatus `json:"fluxd" yaml:"fluxd"`
Git GitStatus `json:"git" yaml:"git"`
}
type FluxsvcStatus struct {
Version string `json:"version,omitempty" yaml:"version,omitempty"`
}
type FluxdStatus struct {
Connected bool `json:"connected" yaml:"connected"`
Version string `json:"version,omitempty" yaml:"version,omitempty"`
}
type GitStatus struct {
Configured bool `json:"configured" yaml:"configured"`
Error string `json:"error,omitempty" yaml:"error,omitempty"`
}