This repository has been archived by the owner on Nov 3, 2022. It is now read-only.
/
status.go
290 lines (251 loc) · 8.81 KB
/
status.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
/*
* Copyright (c) 2021-2022.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0.
*
* If a copy of the MPL was not distributed with
* this file, You can obtain one at
*
* http://mozilla.org/MPL/2.0/
*/
package controllers
import (
"context"
"fmt"
"time"
lt "github.com/artilleryio/artillery-operator/api/v1alpha1"
"github.com/artilleryio/artillery-operator/internal/telemetry"
"github.com/go-logr/logr"
"github.com/thoas/go-funk"
v1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/duration"
"k8s.io/client-go/util/retry"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
// ObservedStatus defines expected observed statuses for a LoadTest.
type ObservedStatus uint
const (
LoadTestInactive ObservedStatus = iota
LoadTestActive
LoadTestCompleted
)
// updateStatus updates the LoadTestStatus based on the status of created LoadTest objects.
// This includes updates to,
// - Conditions
// - Status properties
// - PrinterColumns
func (r *LoadTestReconciler) updateStatus(ctx context.Context, v *lt.LoadTest, logger logr.Logger) (*reconcile.Result, error) {
found := &v1.Job{}
err := r.Get(ctx, types.NamespacedName{
Name: v.Name,
Namespace: v.Namespace,
}, found)
if err != nil {
// The job may not have been created yet, so requeue
return &ctrl.Result{RequeueAfter: 5 * time.Second}, err
}
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
if err := configureStatus(ctx, v, r, found, logger); err != nil {
return err
}
return r.Status().Update(ctx, v)
})
if err != nil {
return &ctrl.Result{}, err
}
// status updated successfully
return nil, nil
}
// configureStatus configures, and if need be, broadcasts the LoadTest status
// based on observed status of the created Job object.
func configureStatus(
ctx context.Context,
v *lt.LoadTest,
r *LoadTestReconciler,
job *v1.Job,
logger logr.Logger,
) error {
observedStatus := observedStatus(job.Status)
configureStatesAndPrinterColumns(v, job)
setConditions(v, observedStatus)
if err := broadcastIfActiveOrCompleted(ctx, v, r, observedStatus, logger); err != nil {
return err
}
// Configuration should always happen after any events are published.
configureStartupAndCompletion(v, observedStatus)
return nil
}
// observedStatus relays a load test's observed status from its related Job.
func observedStatus(s v1.JobStatus) ObservedStatus {
switch {
case jobConditionsActive(s) && (s.Active > (s.Succeeded + s.Failed)):
return LoadTestActive
case jobConditionsCompleted(s) && (s.Active == 0 && (s.Succeeded > 0 || s.Failed > 0)):
return LoadTestCompleted
default:
return LoadTestInactive
}
}
// jobConditionsActive use Job conditions to conclude if a job is active.
func jobConditionsActive(s v1.JobStatus) bool {
suspended, complete, failed := jobConditions(s)
running := suspended == false && complete == false && failed == false
return s.StartTime != nil && running
}
// jobConditionsCompleted use Job conditions to conclude if a job has completed.
func jobConditionsCompleted(s v1.JobStatus) bool {
suspended, complete, failed := jobConditions(s)
completed := suspended == false && complete == true && failed == false
return s.StartTime != nil && s.CompletionTime != nil && completed
}
// jobConditions returns whether a Job is suspended, complete or failed by traversing all found Conditions.
func jobConditions(s v1.JobStatus) (suspended bool, complete bool, failed bool) {
for _, c := range s.Conditions {
suspended = c.Type == v1.JobSuspended
complete = c.Type == v1.JobComplete
failed = c.Type == v1.JobFailed
}
return
}
// setConditions sets the LoadTest's Status Conditions based on
// the provided observed state.
func setConditions(v *lt.LoadTest, o ObservedStatus) {
var progressing lt.LoadTestCondition
var completed lt.LoadTestCondition
conditionsMap := conditionsMap(v.Status.Conditions)
progressing = conditionsMap[lt.LoadTestProgressing]
switch o {
case LoadTestInactive:
progressing.Status = corev1.ConditionUnknown
case LoadTestActive:
progressing.Status = corev1.ConditionTrue
case LoadTestCompleted:
progressing.Status = corev1.ConditionFalse
completed = lt.LoadTestCondition{
Type: lt.LoadTestCompleted,
Status: corev1.ConditionTrue,
LastTransitionTime: metav1.Now(),
LastProbeTime: metav1.Now(),
}
conditionsMap[lt.LoadTestCompleted] = completed
}
progressing.LastProbeTime = metav1.Now()
conditionsMap[lt.LoadTestProgressing] = progressing
v.Status.Conditions = funk.Map(conditionsMap, func(key lt.LoadTestConditionType, val lt.LoadTestCondition) lt.LoadTestCondition {
return val
}).([]lt.LoadTestCondition)
}
// configureStatesAndPrinterColumns configures LoadTestStatus properties
// and printer column values.
// For printercolumns see:
// https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#additional-printer-columns
func configureStatesAndPrinterColumns(v *lt.LoadTest, job *v1.Job) {
configureStates(v, job)
configurePrinterColumns(v, job)
}
func configureStates(v *lt.LoadTest, job *v1.Job) {
v.Status.Active = job.Status.Active
v.Status.Succeeded = job.Status.Succeeded
v.Status.Failed = job.Status.Failed
}
func configurePrinterColumns(v *lt.LoadTest, job *v1.Job) {
v.Status.Duration = loadTestDuration(v.Status)
v.Status.Completions = loadTestCompletions(v.Status, job.Spec.Completions, job.Spec.Parallelism)
v.Status.Image = job.Spec.Template.Spec.Containers[0].Image
}
func conditionsMap(conditions []lt.LoadTestCondition) map[lt.LoadTestConditionType]lt.LoadTestCondition {
out := funk.ToMap(conditions, "Type").(map[lt.LoadTestConditionType]lt.LoadTestCondition)
if _, ok := out[lt.LoadTestProgressing]; !ok {
out[lt.LoadTestProgressing] = lt.LoadTestCondition{
Type: lt.LoadTestProgressing,
LastTransitionTime: metav1.Now(),
}
}
return out
}
// configureStartupAndCompletion configures the start and completion time of a LoadTest
// based on the provided observed state.
func configureStartupAndCompletion(v *lt.LoadTest, o ObservedStatus) {
switch o {
case LoadTestActive:
if v.Status.StartTime == nil {
now := metav1.Now()
v.Status.StartTime = &now
}
case LoadTestCompleted:
if v.Status.CompletionTime == nil {
now := metav1.Now()
v.Status.CompletionTime = &now
}
}
}
// broadcastIfActiveOrCompleted broadcasts informational events to mark that a LoadTest has started or completed.
func broadcastIfActiveOrCompleted(ctx context.Context, v *lt.LoadTest, r *LoadTestReconciler, o ObservedStatus, logger logr.Logger) error {
switch {
case o == LoadTestActive && v.Status.StartTime == nil:
podList, err := getPods(ctx, v, r.Client)
if err != nil {
return err
}
for _, pod := range podList.Items {
r.Recorder.Eventf(v, "Normal", "Running", "Running Load Test worker pod: %s", pod.Name)
}
telemetry.TelemeterActive(v, r.TelemetryClient, r.TelemetryConfig, logger)
case o == LoadTestCompleted && v.Status.CompletionTime == nil:
msg := "Load Test completed"
if v.Status.Failed > 0 {
r.Recorder.Event(v, "Warning", "Failed", fmt.Sprintf("%s with failed workers", msg))
} else {
r.Recorder.Event(v, "Normal", "Completed", msg)
}
telemetry.TelemeterCompletion(v, r.TelemetryClient, r.TelemetryConfig, logger)
}
return nil
}
// getPods returns all the worker Pods for a given LoadTest.
func getPods(ctx context.Context, v *lt.LoadTest, ctl client.Client) (*corev1.PodList, error) {
podList := &corev1.PodList{}
listOpts := []client.ListOption{
client.InNamespace(v.Namespace),
client.MatchingLabels(labels(v, "loadtest-worker")),
}
if err := ctl.List(ctx, podList, listOpts...); err != nil {
return nil, err
}
return podList, nil
}
// loadTestDuration returns a formatted LoadTest duration.
func loadTestDuration(status lt.LoadTestStatus) string {
var d string
switch {
case status.StartTime == nil:
case status.CompletionTime == nil:
d = duration.HumanDuration(time.Since(status.StartTime.Time))
default:
d = duration.HumanDuration(status.CompletionTime.Sub(status.StartTime.Time))
}
return d
}
// loadTestCompletions returns a formatted value of how many LoadTest workers
// have completed from the total of all workers, e.g. 1/4.
// It takes worker parallelism into account.
func loadTestCompletions(status lt.LoadTestStatus, jobCompletions, jobParallelism *int32) string {
if jobCompletions != nil {
return fmt.Sprintf("%d/%d", status.Succeeded, *jobCompletions)
}
parallelism := int32(0)
if jobParallelism != nil {
parallelism = *jobParallelism
}
if parallelism > 1 {
return fmt.Sprintf("%d/1 of %d", status.Succeeded, parallelism)
} else {
return fmt.Sprintf("%d/1", status.Succeeded)
}
}