/
duros.go
100 lines (83 loc) · 3.22 KB
/
duros.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
package healthcheck
import (
"context"
"fmt"
"strings"
"github.com/gardener/gardener/extensions/pkg/controller/healthcheck"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"github.com/go-logr/logr"
durosv1 "github.com/metal-stack/duros-controller/api/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
)
// DurosHealthChecker contains all the information for the Duros HealthCheck
type DurosHealthChecker struct {
logger logr.Logger
seedClient client.Client
durosResourceName string
}
// CheckDuros is a healthCheck function to check Duross
func CheckDuros(durosResourceName string) healthcheck.HealthCheck {
return &DurosHealthChecker{
durosResourceName: durosResourceName,
}
}
// InjectSeedClient injects the seed client
func (healthChecker *DurosHealthChecker) InjectSeedClient(seedClient client.Client) {
healthChecker.seedClient = seedClient
}
// SetLoggerSuffix injects the logger
func (healthChecker *DurosHealthChecker) SetLoggerSuffix(provider, extension string) {
healthChecker.logger = log.Log.WithName(fmt.Sprintf("%s-%s-healthcheck-duros", provider, extension))
}
// DeepCopy clones the healthCheck struct by making a copy and returning the pointer to that new copy
func (healthChecker *DurosHealthChecker) DeepCopy() healthcheck.HealthCheck {
copy := *healthChecker
return ©
}
// Check executes the health check
func (healthChecker *DurosHealthChecker) Check(ctx context.Context, request types.NamespacedName) (*healthcheck.SingleCheckResult, error) {
duros := &durosv1.Duros{}
if err := healthChecker.seedClient.Get(ctx, client.ObjectKey{Namespace: request.Namespace, Name: healthChecker.durosResourceName}, duros); err != nil {
if apierrors.IsNotFound(err) {
// we skip the health check when there is no duros resource deployed
return &healthcheck.SingleCheckResult{
Status: gardencorev1beta1.ConditionTrue,
}, nil
}
err := fmt.Errorf("check duros resource failed. Unable to retrieve duros resource '%s' in namespace '%s': %v", healthChecker.durosResourceName, request.Namespace, err)
healthChecker.logger.Error(err, "Health check failed")
return nil, err
}
if isHealthy, reason, err := DurosIsHealthy(duros); !isHealthy {
healthChecker.logger.Error(err, "Health check failed")
return &healthcheck.SingleCheckResult{
Status: gardencorev1beta1.ConditionFalse,
Detail: err.Error(),
Reason: *reason,
}, nil
}
return &healthcheck.SingleCheckResult{
Status: gardencorev1beta1.ConditionTrue,
}, nil
}
func DurosIsHealthy(duros *durosv1.Duros) (bool, *string, error) {
reason := "DurosUnhealthy"
if duros == nil {
return false, &reason, fmt.Errorf("duros resource not deployed")
}
var problems []string
for _, r := range duros.Status.ManagedResourceStatuses {
if r.State == durosv1.HealthStateRunning {
continue
}
problems = append(problems, fmt.Sprintf("%s is not running because: %s", r.Name, r.Description))
}
if len(problems) > 0 {
err := fmt.Errorf("duros resource %s in namespace %s is unhealthy: %v", duros.Name, duros.Namespace, strings.Join(problems, ", "))
return false, &reason, err
}
return true, nil, nil
}