forked from benkeil/check-k8s
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathendpoints.go
72 lines (58 loc) · 2.48 KB
/
endpoints.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
package endpoints
import (
"fmt"
"github.com/SedoTech/icinga-checks-library"
"k8s.io/client-go/kubernetes"
"github.com/SedoTech/check-k8s/pkg/checks/api"
)
type (
// CheckEndpoints interface to check a deployment
CheckEndpoints interface {
CheckAvailableAddresses(CheckAvailableAddressesOptions) icinga.Result
CheckAll(CheckAllOptions) icinga.Results
}
checkEndpointsImpl struct {
Client kubernetes.Interface
Name string
Namespace string
}
)
// NewCheckEndpoints creates a new instance of CheckEndpoints
func NewCheckEndpoints(client kubernetes.Interface, name string, namespace string) CheckEndpoints {
return &checkEndpointsImpl{Client: client, Name: name, Namespace: namespace}
}
// CheckAllOptions contains options needed to run all deployment checks
type CheckAllOptions struct {
Client kubernetes.Interface
CheckAvailableAddressesOptions CheckAvailableAddressesOptions
}
// CheckAll runs all tests and returns an instance of ServiceCheckResults
func (c *checkEndpointsImpl) CheckAll(options CheckAllOptions) icinga.Results {
results := icinga.NewResults()
results.Add(c.CheckAvailableAddresses(options.CheckAvailableAddressesOptions))
return results
}
// CheckAvailableAddressesOptions contains options needed to run CheckAvailableAddresses check
type CheckAvailableAddressesOptions struct {
ThresholdWarning string
ThresholdCritical string
}
// CheckAvailableAddresses checks if the deployment has a minimum of available replicas
func (c *checkEndpointsImpl) CheckAvailableAddresses(options CheckAvailableAddressesOptions) icinga.Result {
name := "Endpoints.AvailableAddresses"
statusCheck, err := icinga.NewStatusCheck(options.ThresholdWarning, options.ThresholdCritical)
if err != nil {
return icinga.NewResult(name, icinga.ServiceStatusUnknown, fmt.Sprintf("can't check status: %v", err))
}
endpoints, err := api.GetEndpoints(c.Client, api.GetEndpointsOptions{Name: c.Name, Namespace: c.Namespace})
if err != nil {
return icinga.NewResult("GetEndpoints", icinga.ServiceStatusUnknown, fmt.Sprintf("cant't get endpoint: %v", err))
}
if len(endpoints.Subsets) != 1 {
return icinga.NewResult("GetEndpoints", icinga.ServiceStatusUnknown, fmt.Sprintf("endpoint has %d subsets, dont know what to do", len(endpoints.Subsets)))
}
addresses := len(endpoints.Subsets[0].Addresses)
status := statusCheck.CheckInt(addresses)
message := fmt.Sprintf("endpoint has %v available address(ess)", addresses)
return icinga.NewResult(name, status, message)
}