/
pipelineRun.go
319 lines (287 loc) · 9.79 KB
/
pipelineRun.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package k8s
import (
"fmt"
"net/url"
"time"
api "github.com/SAP/stewardci-core/pkg/apis/steward/v1alpha1"
stewardv1alpha1 "github.com/SAP/stewardci-core/pkg/client/clientset/versioned/typed/steward/v1alpha1"
utils "github.com/SAP/stewardci-core/pkg/utils"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/retry"
klog "k8s.io/klog/v2"
)
// PipelineRun is a wrapper for the K8s PipelineRun resource
type PipelineRun interface {
fmt.Stringer
GetStatus() *api.PipelineStatus
GetSpec() *api.PipelineSpec
GetName() string
GetKey() string
GetRunNamespace() string
GetNamespace() string
GetPipelineRepoServerURL() (string, error)
HasDeletionTimestamp() bool
AddFinalizer() error
DeleteFinalizerIfExists() error
UpdateState(api.State) (*api.StateItem, error)
UpdateResult(api.Result) error
UpdateContainer(*corev1.ContainerState) error
StoreErrorAsMessage(error, string) error
UpdateRunNamespace(string) error
UpdateMessage(string) error
}
type pipelineRun struct {
client stewardv1alpha1.PipelineRunInterface
apiObj *api.PipelineRun
copied bool
}
// NewPipelineRun creates a managed pipeline run object.
// If a factory is provided a new version of the pipelinerun is fetched.
// All changes are done on the fetched object.
// If no pipeline run can be found matching the apiObj, nil,nil is returned.
// An error is only returned if a Get for the pipelinerun returns an error other than a NotFound error.
// If you call with factory nil you can only use the Get* functions
// If you use functions changing the pipeline run without factroy set you will get an error.
// The provided PipelineRun object is never modified and copied as late as possible.
func NewPipelineRun(apiObj *api.PipelineRun, factory ClientFactory) (PipelineRun, error) {
if factory == nil {
return &pipelineRun{
apiObj: apiObj,
copied: false,
}, nil
}
client := factory.StewardV1alpha1().PipelineRuns(apiObj.GetNamespace())
obj, err := client.Get(apiObj.GetName(), metav1.GetOptions{})
if err != nil {
if k8serrors.IsNotFound(err) {
return nil, nil
}
return nil, err
}
return &pipelineRun{
apiObj: obj,
copied: true,
client: client,
}, nil
}
// GetRunNamespace returns the namespace in which the build takes place
func (r *pipelineRun) GetRunNamespace() string {
return r.apiObj.Status.Namespace
}
// GetKey returns the key of the pipelineRun
func (r *pipelineRun) GetKey() string {
key, _ := cache.MetaNamespaceKeyFunc(r.apiObj)
return key
}
// GetNamespace returns the namespace of the underlying pipelineRun object
func (r *pipelineRun) GetNamespace() string {
return r.apiObj.GetNamespace()
}
// GetPipelineRepoServerURL returns the server hosting the Jenkinsfile repository
func (r *pipelineRun) GetPipelineRepoServerURL() (string, error) {
urlString := r.GetSpec().JenkinsFile.URL
repoURL, err := url.Parse(urlString)
if err != nil {
return "", errors.Wrapf(err, "value %q of field spec.jenkinsFile.url is invalid [%s]", urlString, r.String())
}
if !(repoURL.Scheme == "http") && !(repoURL.Scheme == "https") {
return "", fmt.Errorf("value %q of field spec.jenkinsFile.url is invalid [%s]: scheme not supported: %q", urlString, r.String(), repoURL.Scheme)
}
return fmt.Sprintf("%s://%s", repoURL.Scheme, repoURL.Host), nil
}
func (r *pipelineRun) GetName() string {
return r.apiObj.GetName()
}
// GetStatus return the Status
// the returned PipelineStatus MUST NOT be modified
// use the prodided Update* functions instead
func (r *pipelineRun) GetStatus() *api.PipelineStatus {
return &r.apiObj.Status
}
// GetSpec return the spec part of the PipelineRun resource
// the returned PipelineSpec MUST NOT be modified
func (r *pipelineRun) GetSpec() *api.PipelineSpec {
return &r.apiObj.Spec
}
// UpdateState set end time of current (defined) state (A) and store it to the history.
// if no current state is defined a new state (A) with cretiontime of the pipelinerun as start time is created.
// It also creates a new current state (B) with start time.
// Returns the state details of state A
func (r *pipelineRun) UpdateState(state api.State) (*api.StateItem, error) {
r.ensureCopy()
klog.V(3).Infof("Update State to %s [%s]", state, r.String())
now := metav1.Now()
oldstate := r.finishCurrentState()
newState := api.StateItem{State: state, StartedAt: now}
if state == api.StateFinished {
newState.FinishedAt = now
}
return oldstate, r.changeStatusAndUpdateSafely(func() {
r.apiObj.Status.StateDetails = newState
r.apiObj.Status.State = state
})
}
// String returns the full qualified name of the pipeline run
func (r *pipelineRun) String() string {
return fmt.Sprintf("PipelineRun{name: %s, namespace: %s, state: %s}", r.GetName(), r.GetNamespace(), string(r.GetStatus().State))
}
func (r *pipelineRun) finishCurrentState() *api.StateItem {
r.ensureCopy()
state := r.apiObj.Status.StateDetails
now := metav1.Now()
if state.State == api.StateUndefined {
state.State = api.StateNew
state.StartedAt = r.apiObj.ObjectMeta.CreationTimestamp
r.apiObj.Status.StartedAt = &now
}
state.FinishedAt = now
his := r.apiObj.Status.StateHistory
his = append(his, state)
r.apiObj.Status.StateHistory = his
r.apiObj.Status.StateDetails = state
return &state
}
// UpdateResult of the pipeline run
func (r *pipelineRun) UpdateResult(result api.Result) error {
r.ensureCopy()
return r.changeStatusAndUpdateSafely(func() {
r.apiObj.Status.Result = result
now := metav1.Now()
r.apiObj.Status.FinishedAt = &now
})
}
// UpdateContainer ...
func (r *pipelineRun) UpdateContainer(c *corev1.ContainerState) error {
if c == nil {
return nil
}
r.ensureCopy()
return r.changeStatusAndUpdateSafely(func() {
r.apiObj.Status.Container = *c
})
}
// StoreErrorAsMessage stores the error as message in the status
func (r *pipelineRun) StoreErrorAsMessage(err error, message string) error {
if err != nil {
text := fmt.Sprintf("ERROR: %s [%s]: %s", utils.Trim(message), r.String(), err.Error())
klog.V(3).Infof(text)
return r.UpdateMessage(text)
}
return nil
}
// UpdateMessage stores string as message in the status
func (r *pipelineRun) UpdateMessage(message string) error {
r.ensureCopy()
return r.changeStatusAndUpdateSafely(func() {
old := r.apiObj.Status.Message
if old != "" {
his := r.apiObj.Status.History
his = append(his, old)
r.apiObj.Status.History = his
}
r.apiObj.Status.Message = utils.Trim(message)
r.apiObj.Status.MessageShort = utils.ShortenMessage(message, 100)
})
}
// UpdateRunNamespace overrides the namespace in which the builds happens
func (r *pipelineRun) UpdateRunNamespace(ns string) error {
r.ensureCopy()
return r.changeStatusAndUpdateSafely(func() {
r.apiObj.Status.Namespace = ns
})
}
//HasDeletionTimestamp returns true if deletion timestamp is set
func (r *pipelineRun) HasDeletionTimestamp() bool {
return !r.apiObj.ObjectMeta.DeletionTimestamp.IsZero()
}
// AddFinalizer adds a finalizer to pipeline run
func (r *pipelineRun) AddFinalizer() error {
changed, finalizerList := utils.AddStringIfMissing(r.apiObj.ObjectMeta.Finalizers, FinalizerName)
if changed {
r.updateFinalizers(finalizerList)
}
return nil
}
// DeleteFinalizerIfExists deletes a finalizer from pipeline run
func (r *pipelineRun) DeleteFinalizerIfExists() error {
changed, finalizerList := utils.RemoveString(r.apiObj.ObjectMeta.Finalizers, FinalizerName)
if changed {
return r.updateFinalizers(finalizerList)
}
return nil
}
func (r *pipelineRun) updateFinalizers(finalizerList []string) error {
if r.client == nil {
panic(fmt.Errorf("No factory provided to store updates [%s]", r.String()))
}
r.ensureCopy()
start := time.Now()
r.apiObj.ObjectMeta.Finalizers = finalizerList
result, err := r.client.Update(r.apiObj)
end := time.Now()
elapsed := end.Sub(start)
klog.V(3).Infof("finish update finalizer after %s in %s", elapsed, r.apiObj.Name)
if err != nil {
return errors.Wrap(err,
fmt.Sprintf("Failed to update finalizers [%s]", r.String()))
}
r.apiObj = result
return nil
}
// changeStatusAndUpdateSafely executes `change` and writes the
// status of the underlying PipelineRun object to storage afterwards.
// `change` is expected to mutate only the status of the underlying
// object, not more.
// In case of a conflict (object in storage is different version than
// ours), the update is retried with backoff:
// - wait
// - fetch object from storage
// - run `change`
// - write object status to storage
// After too many conflicts retrying is aborted, in which case an
// error is returned.
// Non-conflict errors are returned without retrying.
//
// Pitfall: If the underlying PipelineRun object was changed in memory
// compared to the version in storage _before calling this function_,
// that change _gets_ persisted in case there's _no_ update conflict, but
// gets _lost_ in case there _is_ an update conflict! This is hard to find
// by tests, as those typically do not encounter update conflicts.
func (r *pipelineRun) changeStatusAndUpdateSafely(change func()) error {
if r.client == nil {
panic(fmt.Errorf("No factory provided to store updates [%s]", r.String()))
}
isRetry := false
err := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
var err error
if isRetry {
new, err := r.client.Get(r.apiObj.GetName(), metav1.GetOptions{})
if err != nil {
return errors.Wrap(err,
"failed to fetch pipeline after update conflict")
}
r.apiObj = new
r.copied = true
} else {
defer func() { isRetry = true }()
}
change()
result, err := r.client.UpdateStatus(r.apiObj)
if err == nil {
r.apiObj = result
return nil
}
return err
})
return errors.Wrapf(err, "failed to update status [%s]", r.String())
}
func (r *pipelineRun) ensureCopy() {
if !r.copied {
r.apiObj = r.apiObj.DeepCopy()
r.copied = true
}
}