/
liveness.go
276 lines (225 loc) · 9.7 KB
/
liveness.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
/*
Copyright 2023. projectsveltos.io. All rights reserved.
Licensed 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 controllers
import (
"context"
"fmt"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
configv1alpha1 "github.com/projectsveltos/addon-controller/api/v1alpha1"
libsveltosv1alpha1 "github.com/projectsveltos/libsveltos/api/v1alpha1"
"github.com/projectsveltos/libsveltos/lib/clusterproxy"
logs "github.com/projectsveltos/libsveltos/lib/logsettings"
)
// evaluateLivenessCheck evaluates specific liveness check for a cluster.
// Return values:
// - bool indicating whether liveness check is passing
// - bool indicating if liveness check changed state since last evaluation
// - message is a human consumable information
// - an error if any occurs
func evaluateLivenessCheck(ctx context.Context, c client.Client, clusterNamespace, clusterName string,
clusterType libsveltosv1alpha1.ClusterType, chc *libsveltosv1alpha1.ClusterHealthCheck,
livenessCheck *libsveltosv1alpha1.LivenessCheck, logger logr.Logger) (passing, statusChanged bool, message string, err error) {
logger = logger.WithValues("livenesscheck", fmt.Sprintf("%s:%s", livenessCheck.Type, livenessCheck.Name))
logger.V(logs.LogDebug).Info("evaluate liveness check type")
switch livenessCheck.Type {
case libsveltosv1alpha1.LivenessTypeAddons:
passing, err = evaluateLivenessCheckAddOns(ctx, c, clusterNamespace, clusterName, clusterType,
chc, livenessCheck, logger)
case libsveltosv1alpha1.LivenessTypeHealthCheck:
passing, message, err = evaluateLivenessCheckHealthCheck(ctx, c, clusterNamespace, clusterName, clusterType,
livenessCheck, logger)
default:
logger.V(logs.LogInfo).Info("no verification registered for liveness check")
panic(1)
}
if err != nil {
logger.V(logs.LogInfo).Info("failed to evalute liveness check")
return
}
statusChanged = hasLivenessCheckStatusChange(chc, clusterNamespace, clusterName, clusterType,
livenessCheck, passing, message)
return
}
// evaluateLivenessCheckHealthCheck evaluates status reported in corresponding HealthCheckReport.
// Return values:
// - bool indicating if any add-on deployment changed state since last evaluation
// - human consumable message
// - an error if any occurs
func evaluateLivenessCheckHealthCheck(ctx context.Context, c client.Client, clusterNamespace, clusterName string,
clusterType libsveltosv1alpha1.ClusterType, livenessCheck *libsveltosv1alpha1.LivenessCheck,
logger logr.Logger) (allHealthy bool, message string, err error) {
message = ""
allHealthy = true
if livenessCheck.LivenessSourceRef == nil {
return false, "", nil
}
var healthCheckReportList *libsveltosv1alpha1.HealthCheckReportList
healthCheckReportList, err = fetchHealthCheckReports(ctx, c, clusterNamespace,
clusterName, livenessCheck.LivenessSourceRef.Name, clusterType)
if err != nil {
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to fetch healthCheckReports: %v", err))
return false, "", err
}
if len(healthCheckReportList.Items) == 0 {
logger.V(logs.LogInfo).Info("did not find healthCheckReport")
return false, "", err
}
for i := range healthCheckReportList.Items {
hcr := &healthCheckReportList.Items[i]
if hcr.DeletionTimestamp.IsZero() {
msg, isHealthy := isStatusHealthy(hcr)
if !isHealthy {
allHealthy = false
}
message += msg
}
}
return allHealthy, message, nil
}
// evaluateLivenessCheckAddOns evaluates whether all add-ons are deployed or not.
// Return values:
// - bool indicating if any add-on deployment changed state since last evaluation
// - an error if any occurs
func evaluateLivenessCheckAddOns(ctx context.Context, c client.Client, clusterNamespace, clusterName string,
clusterType libsveltosv1alpha1.ClusterType, chc *libsveltosv1alpha1.ClusterHealthCheck,
livenessCheck *libsveltosv1alpha1.LivenessCheck, logger logr.Logger) (bool, error) {
clusterSummaries, err := fetchClusterSummaries(ctx, c, clusterNamespace, clusterName, clusterType)
if err != nil {
logger.V(logs.LogInfo).Info(fmt.Sprintf("failed to fetch clustersummmaries: %v", err))
return false, err
}
deployed := true
for i := range clusterSummaries.Items {
cs := &clusterSummaries.Items[i]
if cs.DeletionTimestamp.IsZero() && !areAddonsDeployed(&clusterSummaries.Items[i]) {
deployed = false
}
}
return deployed, nil
}
// hasLivenessCheckStatusChange returns true if the status for this liveness check has changed since last evaluation
func hasLivenessCheckStatusChange(chc *libsveltosv1alpha1.ClusterHealthCheck, clusterNamespace, clusterName string,
clusterType libsveltosv1alpha1.ClusterType, livenessCheck *libsveltosv1alpha1.LivenessCheck,
passing bool, message string) bool {
for i := range chc.Status.ClusterConditions {
cc := &chc.Status.ClusterConditions[i]
if isClusterConditionForCluster(cc, clusterNamespace, clusterName, clusterType) {
previousStatus := getLivenessCheckStatus(cc, livenessCheck)
if previousStatus == nil {
// No previous status found
return true
}
return hasStatusChanged(previousStatus, passing, message)
}
}
// No previous status found
return true
}
func hasStatusChanged(previousStatus *libsveltosv1alpha1.Condition, passing bool, message string) bool {
// if currently liveness check is passing
if passing {
// No change only if previous status was also conditionTrue
return previousStatus.Status != corev1.ConditionTrue
}
// No change only if previous status was conditionFalse
return previousStatus.Message != message ||
previousStatus.Status != corev1.ConditionFalse
}
// getLivenessCheckStatus returns the liveness check status if ever set before. Nil otherwise
func getLivenessCheckStatus(cc *libsveltosv1alpha1.ClusterCondition, livenessCheck *libsveltosv1alpha1.LivenessCheck,
) *libsveltosv1alpha1.Condition {
livenessCheckType := getConditionType(livenessCheck)
for i := range cc.Conditions {
condition := cc.Conditions[i]
if condition.Type == libsveltosv1alpha1.ConditionType(livenessCheckType) &&
condition.Name == livenessCheck.Name {
return &condition
}
}
return nil
}
// isClusterConditionForCluster returns true if the ClusterCondition is for the cluster clusterType, clusterNamespace,
// clusterName
func isClusterConditionForCluster(cc *libsveltosv1alpha1.ClusterCondition, clusterNamespace, clusterName string,
clusterType libsveltosv1alpha1.ClusterType) bool {
return cc.ClusterInfo.Cluster.Namespace == clusterNamespace &&
cc.ClusterInfo.Cluster.Name == clusterName &&
clusterproxy.GetClusterType(&cc.ClusterInfo.Cluster) == clusterType
}
// fetchClusterSummaries returns all ClusterSummaries currently existing for a given cluster
func fetchClusterSummaries(ctx context.Context, c client.Client, clusterNamespace, clusterName string,
clusterType libsveltosv1alpha1.ClusterType) (*configv1alpha1.ClusterSummaryList, error) {
// Fecth all ClusterSummary for this Cluster
listOptions := []client.ListOption{
client.InNamespace(clusterNamespace),
client.MatchingLabels{
configv1alpha1.ClusterNameLabel: clusterName,
configv1alpha1.ClusterTypeLabel: string(clusterType),
},
}
clusterSummaryList := &configv1alpha1.ClusterSummaryList{}
err := c.List(ctx, clusterSummaryList, listOptions...)
return clusterSummaryList, err
}
// areAddonsDeployed returns whether all add-ons referenced by a ClusterSummary instance are deployed
// or not.
func areAddonsDeployed(clusterSummary *configv1alpha1.ClusterSummary) bool {
for i := range clusterSummary.Status.FeatureSummaries {
fs := clusterSummary.Status.FeatureSummaries[i]
if fs.Status != configv1alpha1.FeatureStatus(libsveltosv1alpha1.SveltosStatusProvisioned) {
return false
}
}
return true
}
// isStatusHealthy returns whether state is Healthy.
func isStatusHealthy(hcr *libsveltosv1alpha1.HealthCheckReport) (string, bool) {
var message string
isAllHealthy := true
for i := range hcr.Spec.ResourceStatuses {
rs := hcr.Spec.ResourceStatuses[i]
if rs.HealthStatus != libsveltosv1alpha1.HealthStatusHealthy {
isAllHealthy = false
message += fmt.Sprintf("%s: %s/%s status is %s \n",
rs.ObjectRef.Kind, rs.ObjectRef.Namespace, rs.ObjectRef.Name, rs.HealthStatus)
if rs.Message != "" {
message += fmt.Sprintf("Message: %s \n", rs.Message)
}
}
}
return message, isAllHealthy
}
// fetchHealthCheckReports returns healthCheckReports for given HealthCheck in a given cluster
func fetchHealthCheckReports(ctx context.Context, c client.Client, clusterNamespace, clusterName, healthCheckName string,
clusterType libsveltosv1alpha1.ClusterType) (*libsveltosv1alpha1.HealthCheckReportList, error) {
labels := libsveltosv1alpha1.GetHealthCheckReportLabels(healthCheckName, clusterName, &clusterType)
// Fecth all ClusterSummary for this Cluster
listOptions := []client.ListOption{
client.InNamespace(clusterNamespace),
client.MatchingLabels(labels),
}
healthCheckReportList := &libsveltosv1alpha1.HealthCheckReportList{}
err := c.List(ctx, healthCheckReportList, listOptions...)
return healthCheckReportList, err
}
func getConditionType(livenessCheck *libsveltosv1alpha1.LivenessCheck) string {
return fmt.Sprintf("%s:%s", string(livenessCheck.Type), livenessCheck.Name)
}
func getConditionStatus(passing bool) corev1.ConditionStatus {
if passing {
return corev1.ConditionTrue
}
return corev1.ConditionFalse
}