forked from fluxcd/flux
/
release.go
144 lines (122 loc) · 3.62 KB
/
release.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
package flux
import (
"sort"
"time"
"fmt"
"github.com/weaveworks/flux/guid"
)
// ReleaseKind says whether a release is to be planned only, or planned then executed
type ReleaseKind string
const (
ReleaseKindPlan ReleaseKind = "plan"
ReleaseKindExecute = "execute"
)
func ParseReleaseKind(s string) (ReleaseKind, error) {
switch s {
case string(ReleaseKindPlan):
return ReleaseKindPlan, nil
case string(ReleaseKindExecute):
return ReleaseKindExecute, nil
default:
return "", ErrInvalidReleaseKind
}
}
const (
ReleaseStatusPending ServiceReleaseStatus = "pending"
ReleaseStatusSuccess ServiceReleaseStatus = "success"
ReleaseStatusFailed ServiceReleaseStatus = "failed"
ReleaseStatusSkipped ServiceReleaseStatus = "skipped"
ReleaseStatusIgnored ServiceReleaseStatus = "ignored"
ReleaseStatusUnknown ServiceReleaseStatus = "unknown"
)
type ServiceReleaseStatus string
type ReleaseID string
func NewReleaseID() ReleaseID {
return ReleaseID(guid.New())
}
// How did this release get triggered?
type ReleaseCause struct {
Message string
User string
}
const UserAutomated = "<automated>"
// Release describes a release
type Release struct {
ID ReleaseID `json:"id"`
CreatedAt time.Time `json:"createdAt"`
StartedAt time.Time `json:"startedAt"`
EndedAt time.Time `json:"endedAt"`
Done bool `json:"done"`
Priority int `json:"priority"`
Status ServiceReleaseStatus `json:"status"`
Log []string `json:"log"`
Cause ReleaseCause `json:"cause"`
Spec ReleaseSpec `json:"spec"`
Result ReleaseResult `json:"result"`
}
// NB: these get sent from fluxctl, so we have to maintain the json format of
// this. Eugh.
type ReleaseSpec struct {
ServiceSpecs []ServiceSpec
ImageSpec ImageSpec
Kind ReleaseKind
Excludes []ServiceID
}
// ReleaseType gives a one-word description of the release, mainly
// useful for labelling metrics or log messages.
func (s ReleaseSpec) ReleaseType() string {
switch {
case s.ImageSpec == ImageSpecLatest:
return "latest_images"
case s.ImageSpec == ImageSpecNone:
return "config_only"
default:
return "specific_image"
}
}
type ReleaseResult map[ServiceID]ServiceResult
func (r ReleaseResult) ServiceIDs() []string {
var result []string
for id := range r {
result = append(result, string(id))
}
sort.StringSlice(result).Sort()
return result
}
func (r ReleaseResult) ImageIDs() []string {
images := map[ImageID]struct{}{}
for _, serviceResult := range r {
for _, containerResult := range serviceResult.PerContainer {
images[containerResult.Target] = struct{}{}
}
}
var result []string
for image := range images {
result = append(result, image.String())
}
sort.StringSlice(result).Sort()
return result
}
// Error returns the error for this release (if any)
// TODO: should we concat them here? or what if there are multiple?
func (r ReleaseResult) Error() string {
for _, serviceResult := range r {
if serviceResult.Error != "" {
return serviceResult.Error
}
}
return ""
}
type ServiceResult struct {
Status ServiceReleaseStatus // summary of what happened, e.g., "incomplete", "ignored", "success"
Error string `json:",omitempty"` // error if there was one finding the service (e.g., it doesn't exist in repo)
PerContainer []ContainerUpdate // what happened with each container
}
func (fr ServiceResult) Msg(id ServiceID) string {
return fmt.Sprintf("%s service %s as it is %s", fr.Status, id.String(), fr.Error)
}
type ContainerUpdate struct {
Container string
Current ImageID
Target ImageID
}