This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
release_containers.go
184 lines (163 loc) · 4.81 KB
/
release_containers.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
package update
import (
"bytes"
"context"
"errors"
"fmt"
"strings"
"github.com/go-kit/kit/log"
"github.com/weaveworks/flux/image"
"github.com/weaveworks/flux/resource"
)
var zeroImageRef = image.Ref{}
// ReleaseContainersSpec defines the spec for a `containers` manifest update.
type ReleaseContainersSpec struct {
Kind ReleaseKind
ContainerSpecs map[resource.ID][]ContainerUpdate
SkipMismatches bool
Force bool
}
// CalculateRelease computes required controller updates to satisfy this specification.
// It returns an error if any spec calculation fails unless `SkipMismatches` is true.
func (s ReleaseContainersSpec) CalculateRelease(ctx context.Context, rc ReleaseContext,
logger log.Logger) ([]*WorkloadUpdate, Result, error) {
results := Result{}
prefilter, postfilter := s.filters()
all, err := rc.SelectWorkloads(ctx, results, prefilter, postfilter)
if err != nil {
return nil, results, err
}
updates := s.workloadUpdates(results, all)
return updates, results, s.resultsError(results)
}
func (s ReleaseContainersSpec) resultsError(results Result) error {
failures := 0
successes := 0
for _, res := range results {
switch res.Status {
case ReleaseStatusFailed:
failures++
case ReleaseStatusSuccess:
successes++
}
}
if failures > 0 {
return errors.New("cannot satisfy specs")
}
if successes == 0 {
return errors.New("no changes found")
}
return nil
}
func (s ReleaseContainersSpec) filters() ([]WorkloadFilter, []WorkloadFilter) {
var rids []resource.ID
for rid := range s.ContainerSpecs {
rids = append(rids, rid)
}
pre := []WorkloadFilter{&IncludeFilter{IDs: rids}}
if !s.Force {
return pre, []WorkloadFilter{&LockedFilter{}, &IgnoreFilter{}}
}
return pre, []WorkloadFilter{}
}
func (s ReleaseContainersSpec) workloadUpdates(results Result, all []*WorkloadUpdate) []*WorkloadUpdate {
var updates []*WorkloadUpdate
for _, u := range all {
cs, err := u.Workload.ContainersOrError()
if err != nil {
results[u.ResourceID] = WorkloadResult{
Status: ReleaseStatusFailed,
Error: err.Error(),
}
continue
}
containers := map[string]resource.Container{}
for _, spec := range cs {
containers[spec.Name] = spec
}
var mismatch, notfound []string
var containerUpdates []ContainerUpdate
for _, spec := range s.ContainerSpecs[u.ResourceID] {
container, ok := containers[spec.Container]
if !ok {
notfound = append(notfound, spec.Container)
continue
}
// An empty spec for the current image skips the precondition
if spec.Current != zeroImageRef && container.Image != spec.Current {
mismatch = append(mismatch, spec.Container)
continue
}
if container.Image == spec.Target {
// Nothing to update
continue
}
containerUpdates = append(containerUpdates, spec)
}
mismatchError := fmt.Sprintf(ContainerTagMismatch, strings.Join(mismatch, ", "))
var rerr string
skippedMismatches := s.SkipMismatches && len(mismatch) > 0
switch {
case len(notfound) > 0:
// Always fail if container disappeared or was misspelled
results[u.ResourceID] = WorkloadResult{
Status: ReleaseStatusFailed,
Error: fmt.Sprintf(ContainerNotFound, strings.Join(notfound, ", ")),
}
case !s.SkipMismatches && len(mismatch) > 0:
// Only fail if we do not skip for mismatches. Otherwise we either succeed
// with partial updates or then mark it as skipped because no precondition
// fulfilled.
results[u.ResourceID] = WorkloadResult{
Status: ReleaseStatusFailed,
Error: mismatchError,
}
case len(containerUpdates) == 0:
rerr = ImageUpToDate
if skippedMismatches {
rerr = mismatchError
}
results[u.ResourceID] = WorkloadResult{
Status: ReleaseStatusSkipped,
Error: rerr,
}
default:
rerr = ""
if skippedMismatches {
// While we succeed here, we still want the client to know that some
// container mismatched.
rerr = mismatchError
}
u.Updates = containerUpdates
updates = append(updates, u)
results[u.ResourceID] = WorkloadResult{
Status: ReleaseStatusSuccess,
Error: rerr,
PerContainer: u.Updates,
}
}
}
return updates
}
func (s ReleaseContainersSpec) ReleaseKind() ReleaseKind {
return s.Kind
}
func (s ReleaseContainersSpec) ReleaseType() ReleaseType {
return "containers"
}
func (s ReleaseContainersSpec) CommitMessage(result Result) string {
var workloads []string
body := &bytes.Buffer{}
for _, res := range result.AffectedResources() {
workloads = append(workloads, res.String())
fmt.Fprintf(body, "\n%s", res)
for _, upd := range result[res].PerContainer {
fmt.Fprintf(body, "\n- %s", upd.Target)
}
fmt.Fprintln(body)
}
if err := result.Error(); err != "" {
fmt.Fprintf(body, "\n%s", result.Error())
}
return fmt.Sprintf("Update image refs in %s\n%s", strings.Join(workloads, ", "), body.String())
}