forked from benkeil/check-k8s
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck-deployment-available-replicas.go
64 lines (55 loc) · 1.95 KB
/
check-deployment-available-replicas.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
package main
import (
"fmt"
"io"
icinga "github.com/SedoTech/icinga-checks-library"
"k8s.io/client-go/kubernetes"
"github.com/SedoTech/check-k8s/pkg/checks/deployment"
"github.com/SedoTech/check-k8s/pkg/environment"
"github.com/SedoTech/check-k8s/pkg/kube"
"github.com/spf13/cobra"
)
type (
checkDeploymentAvailableReplicasCmd struct {
out io.Writer
Client kubernetes.Interface
Name string
Namespace string
ThresholdWarning string
ThresholdCritical string
}
)
func newCheckDeploymentAvailableReplicasCmd(settings environment.EnvSettings, out io.Writer) *cobra.Command {
c := &checkDeploymentAvailableReplicasCmd{out: out}
cmd := &cobra.Command{
Use: "availableReplicas",
Short: "check if a k8s deployment has a minimum of available replicas",
SilenceUsage: true,
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.PersistentFlags().StringVarP(&c.Namespace, "namespace", "n", "", "the namespace of the deployment")
cmd.Flags().StringVarP(&c.ThresholdCritical, "critical", "c", "2:", "critical threshold for minimum available replicas")
cmd.Flags().StringVarP(&c.ThresholdWarning, "warning", "w", "2:", "warning threshold for minimum available replicas")
cmd.MarkPersistentFlagRequired("namespace")
return cmd
}
func (c *checkDeploymentAvailableReplicasCmd) run() {
checkDeployment := deployment.NewCheckDeployment(c.Client, c.Name, c.Namespace)
result := checkDeployment.CheckAvailableReplicas(
deployment.CheckAvailableReplicasOptions{
ThresholdWarning: c.ThresholdWarning,
ThresholdCritical: c.ThresholdCritical,
})
result.Exit()
}