forked from benkeil/check-k8s
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheck-secrets.go
61 lines (52 loc) · 1.6 KB
/
check-secrets.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
package main
import (
"fmt"
"io"
icinga "github.com/SedoTech/icinga-checks-library"
"github.com/SedoTech/check-k8s/pkg/checks/secrets"
"github.com/SedoTech/check-k8s/pkg/environment"
"github.com/SedoTech/check-k8s/pkg/kube"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
)
type (
checkSecretsCmd struct {
out io.Writer
Client kubernetes.Interface
Namespace string
SecretsDefined []string
}
)
func newCheckSecretsCmd(settings environment.EnvSettings, out io.Writer) *cobra.Command {
c := &checkSecretsCmd{out: out}
cmd := &cobra.Command{
Use: "secrets",
Short: "check if a k8s secret resource exists",
SilenceUsage: false,
Args: cobra.NoArgs,
PreRun: func(cmd *cobra.Command, args []string) {
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 endpoint")
cmd.Flags().StringSliceVarP(&c.SecretsDefined, "string", "s", []string{}, "the secrets to check if they are exists (comma separated list)")
cmd.MarkPersistentFlagRequired("namespace")
cmd.MarkFlagRequired("string")
return cmd
}
func (c *checkSecretsCmd) run() {
checkSecrets := secrets.NewCheckSecrets(c.Client, c.Namespace)
result := checkSecrets.CheckExists(
secrets.CheckExistsOptions{
SecretsDefined: c.SecretsDefined,
},
)
result.Exit()
}