forked from benkeil/check-k8s
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck-statefulset-container-defined.go
64 lines (55 loc) · 1.95 KB
/
check-statefulset-container-defined.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/statefulset"
"github.com/SedoTech/check-k8s/pkg/environment"
"github.com/SedoTech/check-k8s/pkg/kube"
"github.com/spf13/cobra"
)
type (
checkStatefulSetContainerDefinedCmd struct {
out io.Writer
Client kubernetes.Interface
Name string
Namespace string
Result string
ContainerDefined []string
}
)
func newCheckStatefulSetContainerDefinedCmd(settings environment.EnvSettings, out io.Writer) *cobra.Command {
c := &checkStatefulSetContainerDefinedCmd{out: out}
cmd := &cobra.Command{
Use: "containerDefined",
Short: "check if a k8s StatefulSet has a list of containers defined",
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 StatefulSet")
cmd.Flags().StringVarP(&c.Result, "result", "r", "CRITICAL", "the result state if the check fails")
cmd.Flags().StringSliceVarP(&c.ContainerDefined, "string", "s", []string{}, "the containers to check if they are present (comma separated list)")
cmd.MarkPersistentFlagRequired("namespace")
return cmd
}
func (c *checkStatefulSetContainerDefinedCmd) run() {
checkStatefulSet := statefulset.NewCheckStatefulSet(c.Client, c.Name, c.Namespace)
result := checkStatefulSet.CheckContainerDefined(
statefulset.CheckContainerDefinedOptions{
Result: c.Result,
ContainerDefined: c.ContainerDefined,
})
result.Exit()
}