-
Notifications
You must be signed in to change notification settings - Fork 151
/
cluster_status_event_handler.go
154 lines (131 loc) · 4.98 KB
/
cluster_status_event_handler.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
/*
Copyright (C) 2022-2024 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package apps
import (
"context"
"time"
"golang.org/x/exp/slices"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/client-go/tools/record"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/apecloud/kubeblocks/controllers/k8score"
"github.com/apecloud/kubeblocks/pkg/constant"
intctrlutil "github.com/apecloud/kubeblocks/pkg/controllerutil"
lorryutil "github.com/apecloud/kubeblocks/pkg/lorry/util"
)
// EventTimeOut timeout of the event
const EventTimeOut = 30 * time.Second
// ClusterStatusEventHandler is the event handler for the cluster status event
type ClusterStatusEventHandler struct{}
var _ k8score.EventHandler = &ClusterStatusEventHandler{}
func init() {
k8score.EventHandlerMap["cluster-status-handler"] = &ClusterStatusEventHandler{}
}
// Handle handles the cluster status events.
func (r *ClusterStatusEventHandler) Handle(cli client.Client, reqCtx intctrlutil.RequestCtx, recorder record.EventRecorder, event *corev1.Event) error {
if event.Reason != string(lorryutil.CheckRoleOperation) {
return handleEventForClusterStatus(reqCtx.Ctx, cli, recorder, event)
}
// parse probe event message when field path is probe-role-changed-check
message := k8score.ParseProbeEventMessage(reqCtx, event)
if message == nil {
reqCtx.Log.Info("parse probe event message failed", "message", event.Message)
return nil
}
// if probe message event is checkRoleFailed, it means the cluster is abnormal, need to handle the cluster status
if message.Event == lorryutil.OperationFailed {
return handleEventForClusterStatus(reqCtx.Ctx, cli, recorder, event)
}
return nil
}
// TODO: Unified cluster event processing
// handleEventForClusterStatus handles event for cluster Warning and Failed phase
func handleEventForClusterStatus(ctx context.Context, cli client.Client, recorder record.EventRecorder, event *corev1.Event) error {
type predicateProcessor struct {
pred func() bool
processor func() error
}
nilReturnHandler := func() error { return nil }
pps := []predicateProcessor{
{
pred: func() bool {
return event.Type != corev1.EventTypeWarning ||
!isTargetKindForEvent(event)
},
processor: nilReturnHandler,
},
{
pred: func() bool {
// the error repeated several times, so we can be sure it's a real error to the cluster.
return !k8score.IsOvertimeEvent(event, EventTimeOut)
},
processor: nilReturnHandler,
},
{
// handle cluster workload error events such as pod/statefulset/deployment errors
// must be the last one
pred: func() bool {
return true
},
processor: func() error {
return handleClusterStatusByEvent(ctx, cli, recorder, event)
},
},
}
for _, pp := range pps {
if pp.pred() {
return pp.processor()
}
}
return nil
}
// handleClusterStatusByEvent handles the cluster status when warning event happened
func handleClusterStatusByEvent(ctx context.Context, cli client.Client, recorder record.EventRecorder, event *corev1.Event) error {
object, err := getEventInvolvedObject(ctx, cli, event)
if err != nil {
return err
}
return notifyClusterStatusChange(ctx, cli, recorder, object, event)
}
// getEventInvolvedObject gets event involved object for StatefulSet/Deployment/Pod workload
func getEventInvolvedObject(ctx context.Context, cli client.Client, event *corev1.Event) (client.Object, error) {
objectKey := client.ObjectKey{
Name: event.InvolvedObject.Name,
Namespace: event.InvolvedObject.Namespace,
}
var err error
// If client.object interface object is used as a parameter, it will not return an error when the object is not found.
// so we should specify the object type to get the object.
switch event.InvolvedObject.Kind {
case constant.PodKind:
pod := &corev1.Pod{}
err = cli.Get(ctx, objectKey, pod)
return pod, err
case constant.StatefulSetKind:
sts := &appsv1.StatefulSet{}
err = cli.Get(ctx, objectKey, sts)
return sts, err
case constant.DeploymentKind:
deployment := &appsv1.Deployment{}
err = cli.Get(ctx, objectKey, deployment)
return deployment, err
}
return nil, err
}
// isTargetKindForEvent checks the event involved object is one of the target resources
func isTargetKindForEvent(event *corev1.Event) bool {
return slices.Index([]string{constant.PodKind, constant.DeploymentKind, constant.StatefulSetKind}, event.InvolvedObject.Kind) != -1
}