-
Notifications
You must be signed in to change notification settings - Fork 5
/
configmaps.go
79 lines (67 loc) · 2.33 KB
/
configmaps.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
73
74
75
76
77
78
79
package configmaps
import (
"fmt"
"github.com/benkeil/check-k8s/cmd/api"
"github.com/benkeil/check-k8s/pkg/utils"
"github.com/benkeil/icinga-checks-library"
"k8s.io/client-go/kubernetes"
)
type (
// CheckConfigMaps interface to check a deployment
CheckConfigMaps interface {
CheckExists(CheckExistsOptions) icinga.Result
}
checkConfigMapsImpl struct {
Client kubernetes.Interface
Namespace string
}
)
// NewCheckConfigMaps creates a new instance of CheckConfigMaps
func NewCheckConfigMaps(client kubernetes.Interface, name string, namespace string) CheckConfigMaps {
return &checkConfigMapsImpl{Client: client, Namespace: namespace}
}
// CheckExistsOptions contains options needed to run CheckExists check
type CheckExistsOptions struct {
ConfigMapsDefined []string
}
// CheckExists checks if the deployment has a minimum of available replicas
func (c *checkConfigMapsImpl) CheckExists(options CheckExistsOptions) icinga.Result {
name := "ConfigMaps.Exists"
// first check if all defined secrets are existent
missing := []string{}
for _, secretName := range options.ConfigMapsDefined {
_, err := api.GetConfigMap(c.Client, api.GetConfigMapOptions{Name: secretName, Namespace: c.Namespace})
if err != nil {
missing = append(missing, secretName)
}
}
// then check if there are secrets in the namespace, we forgot to check
odd := []string{}
allConfigMaps, err := api.GetConfigMaps(c.Client, api.GetConfigMapsOptions{Namespace: c.Namespace})
if err != nil {
return icinga.NewResult("GetConfigMaps", icinga.ServiceStatusUnknown, fmt.Sprintf("cant't list secrets: %v", err))
}
for _, s := range allConfigMaps.Items {
if !utils.Contains(s.Name, options.ConfigMapsDefined) {
odd = append(odd, s.Name)
}
}
// return a warning if we found secrets that are not checked
// return a critical if a secret is not installed
status := icinga.ServiceStatusOk
message := icinga.DefaultSuccessMessage
messageMissing := ""
messageOdd := ""
if len(odd) > 0 {
status = icinga.ServiceStatusWarning
messageOdd = fmt.Sprintf("installed but not checked: %v", odd)
}
if len(missing) > 0 {
status = icinga.ServiceStatusCritical
messageMissing = fmt.Sprintf("missing: %v ", missing)
}
if len(odd) > 0 || len(missing) > 0 {
message = fmt.Sprintf("%v%v", messageMissing, messageOdd)
}
return icinga.NewResult(name, status, message)
}