-
Notifications
You must be signed in to change notification settings - Fork 11
/
metallb.go
85 lines (71 loc) · 2.77 KB
/
metallb.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
package healthcheck
import (
"context"
"fmt"
"strconv"
"github.com/gardener/gardener/extensions/pkg/controller/healthcheck"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
"github.com/go-logr/logr"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
)
// MetalLBHealthChecker contains all the information for the MetalLB HealthCheck
type MetalLBHealthChecker struct {
logger logr.Logger
shootClient client.Client
}
// CheckMetalLB is a healthCheck function to check MetalLBs
func CheckMetalLB() healthcheck.HealthCheck {
return &MetalLBHealthChecker{}
}
// shootClient injects the shoot client
func (healthChecker *MetalLBHealthChecker) InjectShootClient(shootClient client.Client) {
healthChecker.shootClient = shootClient
}
// SetLoggerSuffix injects the logger
func (healthChecker *MetalLBHealthChecker) SetLoggerSuffix(provider, extension string) {
healthChecker.logger = log.Log.WithName(fmt.Sprintf("%s-%s-healthcheck-metallb", provider, extension))
}
// DeepCopy clones the healthCheck struct by making a copy and returning the pointer to that new copy
func (healthChecker *MetalLBHealthChecker) DeepCopy() healthcheck.HealthCheck {
copy := *healthChecker
return ©
}
// Check executes the health check
func (healthChecker *MetalLBHealthChecker) Check(ctx context.Context, request types.NamespacedName) (*healthcheck.SingleCheckResult, error) {
health := &v1.ConfigMap{}
if err := healthChecker.shootClient.Get(ctx, client.ObjectKey{Namespace: "metallb-system", Name: "health"}, health); err != nil {
err := fmt.Errorf("check metallb health configmap failed. Unable to retrieve 'health' in namespace 'metallb-system': %w", err)
healthChecker.logger.Error(err, "Health check failed")
return nil, err
}
if isHealthy, err := IsHealthy(health); !isHealthy {
healthChecker.logger.Error(err, "Health check failed")
return &healthcheck.SingleCheckResult{
Status: gardencorev1beta1.ConditionFalse,
Detail: err.Error(),
}, nil
}
return &healthcheck.SingleCheckResult{
Status: gardencorev1beta1.ConditionTrue,
}, nil
}
func IsHealthy(health *v1.ConfigMap) (bool, error) {
isLoaded, err := strconv.ParseBool(health.Data["configLoaded"])
if err != nil {
return false, fmt.Errorf("unable to parse value from metallb health configmap: %w", err)
}
if !isLoaded {
return false, fmt.Errorf("metallb configmap is not loaded")
}
isStale, err := strconv.ParseBool(health.Data["configStale"])
if err != nil {
return false, fmt.Errorf("unable to parse value from metallb health configmap: %w", err)
}
if isStale {
return false, fmt.Errorf("metallb configmap is stale / erroneous, next speaker reload may interrupt workload traffic")
}
return true, nil
}