forked from benkeil/check-k8s
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck-endpoints.go
68 lines (59 loc) · 2.08 KB
/
check-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
package main
import (
"fmt"
"io"
icinga "github.com/SedoTech/icinga-checks-library"
"github.com/SedoTech/check-k8s/pkg/checks/endpoints"
"github.com/SedoTech/check-k8s/pkg/environment"
"github.com/SedoTech/check-k8s/pkg/kube"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
)
type (
checkEndpointsCmd struct {
out io.Writer
Client kubernetes.Interface
Name string
Namespace string
AvailableAddressesWarning string
AvailableAddressesCritical string
}
)
func newCheckEndpointsCmd(settings environment.EnvSettings, out io.Writer) *cobra.Command {
c := &checkEndpointsCmd{out: out}
cmd := &cobra.Command{
Use: "endpoint",
Short: "check if a k8s endpoint resource is healthy",
SilenceUsage: false,
Args: NameArgs(),
PreRun: func(cmd *cobra.Command, args []string) {
c.Name = args[0]
client, err := kube.GetKubeClient(settings.KubeContext)
if err != nil {
icinga.NewResult("GetKubeClient", icinga.ServiceStatusUnknown, fmt.Sprintf("can't get client: %v", err)).Exit()
}
c.Client = client
},
Run: func(cmd *cobra.Command, args []string) {
c.run()
},
}
cmd.AddCommand(
newCheckEndpointsAvailableAddressesCmd(settings, out),
)
cmd.PersistentFlags().StringVarP(&c.Namespace, "namespace", "n", "", "the namespace of the endpoint")
cmd.Flags().StringVar(&c.AvailableAddressesWarning, "availableAddressesWarning", "2:", "warning threshold for minimum available replicas")
cmd.Flags().StringVar(&c.AvailableAddressesCritical, "availableAdressesCritical", "1:", "critical threshold for minimum available replicas")
cmd.MarkPersistentFlagRequired("namespace")
return cmd
}
func (c *checkEndpointsCmd) run() {
checkEndpoints := endpoints.NewCheckEndpoints(c.Client, c.Name, c.Namespace)
results := checkEndpoints.CheckAll(endpoints.CheckAllOptions{
CheckAvailableAddressesOptions: endpoints.CheckAvailableAddressesOptions{
ThresholdWarning: c.AvailableAddressesWarning,
ThresholdCritical: c.AvailableAddressesCritical,
},
})
results.Exit()
}