/
monitor_pod.go
400 lines (345 loc) · 12.3 KB
/
monitor_pod.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package build
import (
"context"
"fmt"
"os"
"time"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/remotecommand"
ctrl "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
v1 "github.com/apache/camel-k/v2/pkg/apis/camel/v1"
"github.com/apache/camel-k/v2/pkg/platform"
"github.com/apache/camel-k/v2/pkg/util/kubernetes"
"github.com/apache/camel-k/v2/pkg/util/kubernetes/log"
)
const timeoutAnnotation = "camel.apache.org/timeout"
func newMonitorPodAction(reader ctrl.Reader) Action {
return &monitorPodAction{
reader: reader,
}
}
type monitorPodAction struct {
baseAction
reader ctrl.Reader
}
// Name returns a common name of the action.
func (action *monitorPodAction) Name() string {
return "monitor-pod"
}
// CanHandle tells whether this action can handle the build.
func (action *monitorPodAction) CanHandle(build *v1.Build) bool {
return build.Status.Phase == v1.BuildPhasePending || build.Status.Phase == v1.BuildPhaseRunning
}
// Handle handles the builds.
func (action *monitorPodAction) Handle(ctx context.Context, build *v1.Build) (*v1.Build, error) {
pod, err := getBuilderPod(ctx, action.reader, build)
if err != nil {
return nil, err
}
if pod == nil {
switch build.Status.Phase {
case v1.BuildPhasePending:
pod = newBuildPod(ctx, action.client, build)
// If the Builder Pod is in the Build namespace, we can set the ownership to it. If not (global operator mode)
// we set the ownership to the Operator Pod instead
var owner metav1.Object
owner = build
if build.Namespace != pod.Namespace {
operatorPod := platform.GetOperatorPod(ctx, action.client, pod.Namespace)
if operatorPod != nil {
owner = operatorPod
}
}
if err = controllerutil.SetControllerReference(owner, pod, action.client.GetScheme()); err != nil {
return nil, err
}
if err = action.client.Create(ctx, pod); err != nil {
return nil, fmt.Errorf("cannot create build pod: %w", err)
}
case v1.BuildPhaseRunning:
// Emulate context cancellation
build.Status.Phase = v1.BuildPhaseInterrupted
build.Status.Error = "Pod deleted"
monitorFinishedBuild(build)
return build, nil
}
}
switch pod.Status.Phase {
case corev1.PodPending, corev1.PodRunning:
// Pod remains in pending phase when init containers execute
if action.isPodScheduled(pod) {
build.Status.Phase = v1.BuildPhaseRunning
}
if time.Since(build.Status.StartedAt.Time) > build.Spec.Timeout.Duration {
// Patch the Pod with an annotation, to identify termination signal
// has been sent because the Build has timed out
if err = action.addTimeoutAnnotation(ctx, pod, metav1.Now()); err != nil {
return nil, err
}
// Send SIGTERM signal to running containers
if err = action.sigterm(ctx, pod); err != nil {
// Requeue
return nil, err
}
monitorFinishedBuild(build)
} else {
// Monitor running state of the build - this may have been done already by the schedule action but the build monitor is idempotent
// We do this here to potentially restore the running build state in the monitor in case of an operator restart
monitorRunningBuild(build)
}
case corev1.PodSucceeded:
build.Status.Phase = v1.BuildPhaseSucceeded
// Remove the annotation in case the Build succeeded, between
// the timeout deadline and the termination signal.
if err = action.removeTimeoutAnnotation(ctx, pod); err != nil {
return nil, err
}
finishedAt := action.getTerminatedTime(pod)
duration := finishedAt.Sub(build.Status.StartedAt.Time)
build.Status.Duration = duration.String()
action.setConditionsFromTerminationMessages(ctx, pod, &build.Status)
monitorFinishedBuild(build)
buildCreator := kubernetes.GetCamelCreator(build)
// Account for the Build metrics
observeBuildResult(build, build.Status.Phase, buildCreator, duration)
build.Status.Image = publishTaskImageName(build.Spec.Tasks)
// operator supported publishing tasks should provide the digest in the builder command process execution
if !operatorSupportedPublishingStrategy(build.Spec.Tasks) {
build.Status.Digest = publishTaskDigest(build.Spec.Tasks, pod.Status.ContainerStatuses)
if build.Status.Digest == "" {
// Likely to happen for users provided publishing tasks and not providing the digest image among statuses
build.Status.Phase = v1.BuildPhaseError
build.Status.SetCondition(
"ImageDigestAvailable",
corev1.ConditionFalse,
"ImageDigestAvailable",
fmt.Sprintf(
"%s publishing task completed but no digest is available in container status. Make sure that the process successfully push the image to the registry and write its digest to /dev/termination-log",
publishTaskName(build.Spec.Tasks),
),
)
}
}
case corev1.PodFailed:
phase := v1.BuildPhaseFailed
message := fmt.Sprintf("Builder Pod %s failed (see conditions for more details)", pod.Name)
if pod.DeletionTimestamp != nil {
phase = v1.BuildPhaseInterrupted
message = fmt.Sprintf("Builder Pod %s deleted", pod.Name)
} else if _, ok := pod.GetAnnotations()[timeoutAnnotation]; ok {
message = fmt.Sprintf("Builder Pod %s timeout", pod.Name)
}
// Do not override errored build
if build.Status.Phase == v1.BuildPhaseError {
phase = v1.BuildPhaseError
}
build.Status.Phase = phase
build.Status.Error = message
finishedAt := action.getTerminatedTime(pod)
duration := finishedAt.Sub(build.Status.StartedAt.Time)
build.Status.Duration = duration.String()
action.setConditionsFromTerminationMessages(ctx, pod, &build.Status)
monitorFinishedBuild(build)
buildCreator := kubernetes.GetCamelCreator(build)
// Account for the Build metrics
observeBuildResult(build, build.Status.Phase, buildCreator, duration)
}
return build, nil
}
func (action *monitorPodAction) isPodScheduled(pod *corev1.Pod) bool {
for _, condition := range pod.Status.Conditions {
if condition.Type == corev1.PodScheduled && condition.Status == corev1.ConditionTrue {
return true
}
}
return false
}
func (action *monitorPodAction) sigterm(ctx context.Context, pod *corev1.Pod) error {
var containers []corev1.ContainerStatus
containers = append(containers, pod.Status.InitContainerStatuses...)
containers = append(containers, pod.Status.ContainerStatuses...)
for _, container := range containers {
if container.State.Running == nil {
continue
}
r := action.client.CoreV1().RESTClient().Post().
Resource("pods").
Namespace(pod.Namespace).
Name(pod.Name).
SubResource("exec").
Param("container", container.Name)
r.VersionedParams(&corev1.PodExecOptions{
Container: container.Name,
Command: []string{"/bin/bash", "-c", "kill -SIGTERM 1"},
Stdout: true,
Stderr: true,
TTY: false,
}, scheme.ParameterCodec)
exec, err := remotecommand.NewSPDYExecutor(action.client.GetConfig(), "POST", r.URL())
if err != nil {
return err
}
err = exec.StreamWithContext(ctx, remotecommand.StreamOptions{
Stdout: os.Stdout,
Stderr: os.Stderr,
Tty: false,
})
if err != nil {
return err
}
}
return nil
}
func (action *monitorPodAction) addTimeoutAnnotation(ctx context.Context, pod *corev1.Pod, time metav1.Time) error {
if _, ok := pod.GetAnnotations()[timeoutAnnotation]; ok {
return nil
}
return action.patchPod(ctx, pod, func(p *corev1.Pod) {
if p.GetAnnotations() != nil {
p.GetAnnotations()[timeoutAnnotation] = time.String()
} else {
p.SetAnnotations(map[string]string{
timeoutAnnotation: time.String(),
})
}
})
}
func (action *monitorPodAction) removeTimeoutAnnotation(ctx context.Context, pod *corev1.Pod) error {
if _, ok := pod.GetAnnotations()[timeoutAnnotation]; !ok {
return nil
}
return action.patchPod(ctx, pod, func(p *corev1.Pod) {
delete(p.GetAnnotations(), timeoutAnnotation)
})
}
func (action *monitorPodAction) patchPod(ctx context.Context, pod *corev1.Pod, mutate func(*corev1.Pod)) error {
target := pod.DeepCopy()
mutate(target)
if err := action.client.Patch(ctx, target, ctrl.MergeFrom(pod)); err != nil {
return err
}
*pod = *target
return nil
}
func (action *monitorPodAction) getTerminatedTime(pod *corev1.Pod) metav1.Time {
var finishedAt metav1.Time
var containers []corev1.ContainerStatus
containers = append(containers, pod.Status.InitContainerStatuses...)
containers = append(containers, pod.Status.ContainerStatuses...)
for _, container := range containers {
if container.State.Terminated == nil {
// The container has not run
continue
}
if t := container.State.Terminated.FinishedAt; finishedAt.IsZero() || t.After(finishedAt.Time) {
finishedAt = t
}
}
return finishedAt
}
// setConditionsFromTerminationMessages sets a condition for all those containers which have been terminated (successfully or not).
func (action *monitorPodAction) setConditionsFromTerminationMessages(ctx context.Context, pod *corev1.Pod, buildStatus *v1.BuildStatus) {
var containers []corev1.ContainerStatus
containers = append(containers, pod.Status.InitContainerStatuses...)
containers = append(containers, pod.Status.ContainerStatuses...)
for _, container := range containers {
if t := container.State.Terminated; t != nil {
var err error
terminationMessage := t.Message
// Dynamic condition type (it depends on each container name)
containerConditionType := v1.BuildConditionType(fmt.Sprintf("Container%sSucceeded", container.Name))
containerSucceeded := corev1.ConditionTrue
if t.ExitCode != 0 {
containerSucceeded = corev1.ConditionFalse
}
if terminationMessage == "" {
// TODO we can make it a user variable !?
var maxLines int64 = 10
logOptions := corev1.PodLogOptions{
Container: container.Name,
TailLines: &maxLines,
}
terminationMessage, err = log.DumpLog(ctx, action.client, pod, logOptions)
if err != nil {
action.L.Errorf(err, "Dumping log for %s container in %s Pod failed", container.Name, pod.Name)
terminationMessage = fmt.Sprintf(
"Operator was not able to retrieve the error message, please, check the container %s log directly from %s Pod",
container.Name,
pod.Name,
)
}
}
terminationReason := fmt.Sprintf("%s (%d)", t.Reason, t.ExitCode)
buildStatus.SetCondition(containerConditionType, containerSucceeded, terminationReason, terminationMessage)
}
}
}
// we expect that the last task is any of the supported publishing task
// or a custom user task.
func publishTask(tasks []v1.Task) *v1.Task {
if len(tasks) > 0 {
return &tasks[len(tasks)-1]
}
return nil
}
func publishTaskImageName(tasks []v1.Task) string {
t := publishTask(tasks)
if t == nil {
return ""
}
switch {
case t.Custom != nil:
return t.Custom.PublishingImage
case t.Spectrum != nil:
return t.Spectrum.Image
case t.Jib != nil:
return t.Jib.Image
}
return ""
}
func publishTaskName(tasks []v1.Task) string {
t := publishTask(tasks)
if t == nil {
return ""
}
switch {
case t.Custom != nil:
return t.Custom.Name
case t.Spectrum != nil:
return t.Spectrum.Name
case t.Jib != nil:
return t.Jib.Name
}
return ""
}
func publishTaskDigest(tasks []v1.Task, cntStates []corev1.ContainerStatus) string {
taskName := publishTaskName(tasks)
// Reconcile image digest from build container status if available
for _, container := range cntStates {
if container.Name == taskName {
return container.State.Terminated.Message
}
}
return ""
}
func operatorSupportedPublishingStrategy(tasks []v1.Task) bool {
taskName := publishTaskName(tasks)
return taskName == "jib" || taskName == "spectrum" || taskName == "s2i"
}