-
Notifications
You must be signed in to change notification settings - Fork 0
/
healthscope.go
433 lines (384 loc) · 14.5 KB
/
healthscope.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
Copyright 2020 The Crossplane Authors.
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 healthscope
import (
"context"
"encoding/json"
"fmt"
"reflect"
"sort"
"strconv"
"strings"
"time"
"github.com/pkg/errors"
apps "k8s.io/api/apps/v1"
core "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/xishengcai/oam/apis/core/v1alpha2"
corev1alpha2 "github.com/xishengcai/oam/apis/core/v1alpha2"
"github.com/xishengcai/oam/pkg/oam"
runtimev1alpha1 "github.com/crossplane/crossplane-runtime/apis/core/v1alpha1"
)
const (
infoFmtUnknownWorkload = "APIVersion %v Kind %v workload is unknown for HealthScope "
infoFmtReady = "Ready:%d/%d "
infoFmtNoChildRes = "cannot get child resource references of workload %v"
errHealthCheck = "error occurs in health check "
errGetVersioningWorkloads = "error occurs when get versioning peer workloads refs"
defaultTimeout = 10 * time.Second
)
// HealthStatus represents health status strings.
type HealthStatus = v1alpha2.HealthStatus
const (
// StatusHealthy represents healthy status.
StatusHealthy = v1alpha2.StatusHealthy
// StatusUnhealthy represents unhealthy status.
StatusUnhealthy = v1alpha2.StatusUnhealthy
// StatusUnknown represents unknown status.
StatusUnknown = v1alpha2.StatusUnknown
)
var (
kindContainerizedWorkload = corev1alpha2.ContainerizedWorkloadKind
kindDeployment = reflect.TypeOf(apps.Deployment{}).Name()
kindService = reflect.TypeOf(core.Service{}).Name()
kindStatefulSet = reflect.TypeOf(apps.StatefulSet{}).Name()
kindDaemonSet = reflect.TypeOf(apps.DaemonSet{}).Name()
)
// WorkloadHealthCondition holds health status of any resource
type WorkloadHealthCondition = v1alpha2.WorkloadHealthCondition
// ScopeHealthCondition holds health condition of a scope
type ScopeHealthCondition = v1alpha2.ScopeHealthCondition
// A WorloadHealthChecker checks health status of specified resource
// and saves status into an HealthCondition object.
type WorloadHealthChecker interface {
Check(context.Context, client.Client, runtimev1alpha1.TypedReference, string) *WorkloadHealthCondition
}
// WorkloadHealthCheckFn checks health status of specified resource
// and saves status into an HealthCondition object.
type WorkloadHealthCheckFn func(context.Context, client.Client, runtimev1alpha1.TypedReference, string) *WorkloadHealthCondition
// Check the health status of specified resource
func (fn WorkloadHealthCheckFn) Check(ctx context.Context, c client.Client, tr runtimev1alpha1.TypedReference, ns string) *WorkloadHealthCondition {
r := fn(ctx, c, tr, ns)
if r == nil {
return r
}
// check all workloads of a version-enabled component
peerRefs, err := getVersioningPeerWorkloadRefs(ctx, c, tr, ns)
if err != nil {
r.HealthStatus = StatusUnhealthy
r.Diagnosis = fmt.Sprintf("%s %s:%s",
r.Diagnosis,
errGetVersioningWorkloads,
err.Error())
return r
}
if len(peerRefs) > 0 {
var peerHCs PeerHealthConditions
for _, peerRef := range peerRefs {
if peerHC := fn(ctx, c, peerRef, ns); peerHC != nil {
peerHCs = append(peerHCs, *peerHC.DeepCopy())
}
}
peerHCs.MergePeerWorkloadsConditions(r)
}
return r
}
// CheckContainerziedWorkloadHealth check health condition of ContainerizedWorkload
func CheckContainerziedWorkloadHealth(ctx context.Context, c client.Client, ref runtimev1alpha1.TypedReference, namespace string) *WorkloadHealthCondition {
if ref.GroupVersionKind() != corev1alpha2.SchemeGroupVersion.WithKind(kindContainerizedWorkload) {
return nil
}
r := &WorkloadHealthCondition{
HealthStatus: StatusHealthy,
TargetWorkload: ref,
}
cwObj := corev1alpha2.ContainerizedWorkload{}
cwObj.SetGroupVersionKind(corev1alpha2.SchemeGroupVersion.WithKind(kindContainerizedWorkload))
if err := c.Get(ctx, types.NamespacedName{Namespace: namespace, Name: ref.Name}, &cwObj); err != nil {
r.HealthStatus = StatusUnhealthy
r.Diagnosis = errors.Wrap(err, errHealthCheck).Error()
return r
}
r.ComponentName = getComponentNameFromLabel(&cwObj)
r.TargetWorkload.UID = cwObj.GetUID()
childRefs := cwObj.Status.Resources
updateChildResourcesCondition(ctx, c, namespace, r, ref, childRefs)
return r
}
func updateChildResourcesCondition(ctx context.Context, c client.Client, namespace string, r *WorkloadHealthCondition, ref runtimev1alpha1.TypedReference, childRefs []runtimev1alpha1.TypedReference) {
subConditions := []*WorkloadHealthCondition{}
if len(childRefs) != 2 {
// one deployment and one svc are required by containerizedworkload
r.Diagnosis = fmt.Sprintf(infoFmtNoChildRes, ref.Name)
r.HealthStatus = StatusUnhealthy
return
}
for _, childRef := range childRefs {
switch childRef.Kind {
case kindDeployment:
// reuse Deployment health checker
childCondition := CheckDeploymentHealth(ctx, c, childRef, namespace)
subConditions = append(subConditions, childCondition)
case kindService:
childCondition := &WorkloadHealthCondition{
TargetWorkload: childRef,
HealthStatus: StatusHealthy,
}
o := unstructured.Unstructured{}
o.SetAPIVersion(childRef.APIVersion)
o.SetKind(childRef.Kind)
if err := c.Get(ctx, types.NamespacedName{Namespace: namespace, Name: childRef.Name}, &o); err != nil {
childCondition.HealthStatus = StatusUnhealthy
childCondition.Diagnosis = errors.Wrap(err, errHealthCheck).Error()
}
subConditions = append(subConditions, childCondition)
}
}
for _, sc := range subConditions {
if sc.HealthStatus != StatusHealthy {
r.HealthStatus = StatusUnhealthy
}
r.Diagnosis = fmt.Sprintf("%s%s", r.Diagnosis, sc.Diagnosis)
}
}
// CheckDeploymentHealth checks health condition of Deployment
func CheckDeploymentHealth(ctx context.Context, client client.Client, ref runtimev1alpha1.TypedReference, namespace string) *WorkloadHealthCondition {
if ref.GroupVersionKind() != apps.SchemeGroupVersion.WithKind(kindDeployment) {
return nil
}
r := &WorkloadHealthCondition{
HealthStatus: StatusUnhealthy,
TargetWorkload: ref,
}
deployment := apps.Deployment{}
deployment.SetGroupVersionKind(apps.SchemeGroupVersion.WithKind(kindDeployment))
deploymentRef := types.NamespacedName{Namespace: namespace, Name: ref.Name}
if err := client.Get(ctx, deploymentRef, &deployment); err != nil {
r.Diagnosis = errors.Wrap(err, errHealthCheck).Error()
return r
}
r.ComponentName = getComponentNameFromLabel(&deployment)
r.TargetWorkload.UID = deployment.GetUID()
requiredReplicas := int32(0)
if deployment.Spec.Replicas != nil {
requiredReplicas = *deployment.Spec.Replicas
}
r.Diagnosis = fmt.Sprintf(infoFmtReady, deployment.Status.ReadyReplicas, requiredReplicas)
// Health criteria
if deployment.Status.ReadyReplicas != requiredReplicas {
return r
}
r.HealthStatus = StatusHealthy
return r
}
// CheckStatefulsetHealth checks health condition of StatefulSet
func CheckStatefulsetHealth(ctx context.Context, client client.Client, ref runtimev1alpha1.TypedReference, namespace string) *WorkloadHealthCondition {
if ref.GroupVersionKind() != apps.SchemeGroupVersion.WithKind(kindStatefulSet) {
return nil
}
r := &WorkloadHealthCondition{
HealthStatus: StatusUnhealthy,
TargetWorkload: ref,
}
statefulset := apps.StatefulSet{}
statefulset.APIVersion = ref.APIVersion
statefulset.Kind = ref.Kind
nk := types.NamespacedName{Namespace: namespace, Name: ref.Name}
if err := client.Get(ctx, nk, &statefulset); err != nil {
r.Diagnosis = errors.Wrap(err, errHealthCheck).Error()
return r
}
r.ComponentName = getComponentNameFromLabel(&statefulset)
r.TargetWorkload.UID = statefulset.GetUID()
requiredReplicas := int32(0)
if statefulset.Spec.Replicas != nil {
requiredReplicas = *statefulset.Spec.Replicas
}
r.Diagnosis = fmt.Sprintf(infoFmtReady, statefulset.Status.ReadyReplicas, requiredReplicas)
// Health criteria
if statefulset.Status.ReadyReplicas != requiredReplicas {
return r
}
r.HealthStatus = StatusHealthy
return r
}
// CheckDaemonsetHealth checks health condition of DaemonSet
func CheckDaemonsetHealth(ctx context.Context, client client.Client, ref runtimev1alpha1.TypedReference, namespace string) *WorkloadHealthCondition {
if ref.GroupVersionKind() != apps.SchemeGroupVersion.WithKind(kindDaemonSet) {
return nil
}
r := &WorkloadHealthCondition{
HealthStatus: StatusUnhealthy,
TargetWorkload: ref,
}
daemonset := apps.DaemonSet{}
daemonset.APIVersion = ref.APIVersion
daemonset.Kind = ref.Kind
nk := types.NamespacedName{Namespace: namespace, Name: ref.Name}
if err := client.Get(ctx, nk, &daemonset); err != nil {
r.Diagnosis = errors.Wrap(err, errHealthCheck).Error()
return r
}
r.ComponentName = getComponentNameFromLabel(&daemonset)
r.TargetWorkload.UID = daemonset.GetUID()
r.Diagnosis = fmt.Sprintf(infoFmtReady, daemonset.Status.NumberReady, daemonset.Status.DesiredNumberScheduled)
// Health criteria
if daemonset.Status.NumberUnavailable != 0 {
return r
}
r.HealthStatus = StatusHealthy
return r
}
// CheckByHealthCheckTrait checks health condition through HealthCheckTrait.
func CheckByHealthCheckTrait(ctx context.Context, c client.Client, wlRef runtimev1alpha1.TypedReference, ns string) *WorkloadHealthCondition {
// TODO(roywang) implement HealthCheckTrait feature
return nil
}
// CheckUnknownWorkload handles unknown type workloads.
func CheckUnknownWorkload(ctx context.Context, c client.Client, wlRef runtimev1alpha1.TypedReference, ns string) *WorkloadHealthCondition {
healthCondition := &WorkloadHealthCondition{
TargetWorkload: wlRef,
HealthStatus: StatusUnknown,
Diagnosis: fmt.Sprintf(infoFmtUnknownWorkload, wlRef.APIVersion, wlRef.Kind),
}
wl := &unstructured.Unstructured{}
wl.SetGroupVersionKind(wlRef.GroupVersionKind())
if err := c.Get(ctx, client.ObjectKey{Namespace: ns, Name: wlRef.Name}, wl); err != nil {
healthCondition.Diagnosis = errors.Wrap(err, errHealthCheck).Error()
return healthCondition
}
healthCondition.ComponentName = getComponentNameFromLabel(wl)
// for unknown workloads, just show status instead of precise diagnosis
wlStatus, _, _ := unstructured.NestedMap(wl.UnstructuredContent(), "status")
wlStatusR, err := json.Marshal(wlStatus)
if err != nil {
healthCondition.Diagnosis = errors.Wrap(err, errHealthCheck).Error()
return healthCondition
}
healthCondition.WorkloadStatus = string(wlStatusR)
return healthCondition
}
func getComponentNameFromLabel(o metav1.Object) string {
if o == nil {
return ""
}
compName, exist := o.GetLabels()[oam.LabelAppComponent]
if !exist {
compName = ""
}
return compName
}
func getAppConfigNameFromLabel(o metav1.Object) string {
if o == nil {
return ""
}
appName, exist := o.GetLabels()[oam.LabelAppName]
if !exist {
appName = ""
}
return appName
}
func getVersioningPeerWorkloadRefs(ctx context.Context, c client.Reader, wlRef runtimev1alpha1.TypedReference, ns string) ([]runtimev1alpha1.TypedReference, error) {
o := &unstructured.Unstructured{}
o.SetGroupVersionKind(wlRef.GroupVersionKind())
if err := c.Get(ctx, client.ObjectKey{Namespace: ns, Name: wlRef.Name}, o); err != nil {
return nil, err
}
compName := getComponentNameFromLabel(o)
appName := getAppConfigNameFromLabel(o)
if compName == "" || appName == "" {
// if missing these lables, cannot get peer workloads
return nil, nil
}
peerRefs := []runtimev1alpha1.TypedReference{}
l := &unstructured.UnstructuredList{}
l.SetGroupVersionKind(wlRef.GroupVersionKind())
opts := []client.ListOption{
client.InNamespace(ns),
client.MatchingLabels{
oam.LabelAppComponent: compName,
oam.LabelAppName: appName},
}
if err := c.List(ctx, l, opts...); err != nil {
return nil, err
}
for _, obj := range l.Items {
if obj.GetName() == o.GetName() {
continue
}
tmpRef := runtimev1alpha1.TypedReference{}
tmpRef.SetGroupVersionKind(obj.GroupVersionKind())
tmpRef.Name = obj.GetName()
peerRefs = append(peerRefs, tmpRef)
}
return peerRefs, nil
}
// PeerHealthConditions refers to a slice of health condition of worloads
// belonging to one version-enabled component
type PeerHealthConditions []WorkloadHealthCondition
func (p PeerHealthConditions) Len() int { return len(p) }
func (p PeerHealthConditions) Less(i, j int) bool {
// sort by revision number in descending order
return extractRevision(p[i].TargetWorkload.Name) > extractRevision(p[j].TargetWorkload.Name)
}
func (p PeerHealthConditions) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// exract revision number from revision name in format: <comp-name>-v<revision number>
// any non-qualified format should return 0
func extractRevision(c string) int {
i, _ := strconv.ParseInt(c[strings.LastIndex(c, "v")+1:], 10, 0)
return int(i)
}
// MergePeerWorkloadsConditions merge health conditions of all peer workloads into basic
func (p PeerHealthConditions) MergePeerWorkloadsConditions(basic *WorkloadHealthCondition) {
if basic == nil || len(p) == 0 {
return
}
// copy to keep idempotent
peerHCs := make(PeerHealthConditions, len(p))
copy(peerHCs, p)
peerHCs = append(peerHCs, *basic.DeepCopy())
// sort by revision number in descending order
sort.Sort(peerHCs)
for _, peerHC := range peerHCs {
if peerHC.HealthStatus == StatusUnhealthy {
// if ANY peer workload is unhealthy
// then the overall condition is unhealthy
basic.HealthStatus = StatusUnhealthy
}
}
// re-format diagnosis/workloadStatus to show multiple workloads'
if basic.HealthStatus == StatusUnknown {
basic.WorkloadStatus = fmt.Sprintf("%s:%s", peerHCs[0].TargetWorkload.Name, peerHCs[0].WorkloadStatus)
for _, peerHC := range peerHCs[1:] {
basic.WorkloadStatus = fmt.Sprintf("%s %s:%s",
basic.WorkloadStatus,
peerHC.TargetWorkload.Name,
peerHC.WorkloadStatus)
}
} else {
basic.Diagnosis = fmt.Sprintf("%s:%s", peerHCs[0].TargetWorkload.Name, peerHCs[0].Diagnosis)
for i, peerHC := range peerHCs[1:] {
if i > 0 && peerHC.Diagnosis == fmt.Sprintf(infoFmtReady, 0, 0) {
// skip timeworn ones
continue
}
basic.Diagnosis = fmt.Sprintf("%s %s:%s",
basic.Diagnosis,
peerHC.TargetWorkload.Name,
peerHC.Diagnosis)
}
}
}