-
Notifications
You must be signed in to change notification settings - Fork 0
/
controller.go
161 lines (124 loc) · 3.29 KB
/
controller.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"crypto/x509"
"encoding/pem"
"net/smtp"
"strconv"
"time"
log "github.com/sirupsen/logrus"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
type certInfo struct {
Namespace string
CertificateName string
NotAfter time.Time
}
//Controller type handles the control loop
type Controller struct {
Config
KubeClient *kubernetes.Clientset
}
//NewController creates a new controller with the config
func NewController(cfg *Config) (Controller, error) {
ctrl := Controller{}
kubeConfig, err := buildConfig(cfg)
if err != nil {
return ctrl, err
}
ctrl.Config = *cfg
ctrl.KubeClient = kubeConfig
return ctrl, nil
}
func buildConfig(cfg *Config) (*kubernetes.Clientset, error) {
config, err := clientcmd.BuildConfigFromFlags("", cfg.KubeConfig)
if err != nil {
return nil, err
}
client, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, err
}
return client, nil
}
// Run starts the execution loop
func (ctrl Controller) Run(stopChan <-chan struct{}) {
ticker := time.NewTicker(ctrl.Config.Interval)
defer ticker.Stop()
for {
log.Info("Starting certificate check...")
err := ctrl.RunOnce()
if err != nil {
log.Error(err)
}
select {
case <-ticker.C:
case <-stopChan:
log.Error("Terminating main controller loop")
return
}
}
}
// RunOnce executes one iteration of the cert check
func (ctrl Controller) RunOnce() error {
log.Info("Retrieving tls secrets...")
nl, err := ctrl.KubeClient.CoreV1().Namespaces().List(metav1.ListOptions{})
if err != nil {
return err
}
expired := []certInfo{}
threshold := time.Now().AddDate(0, 0, ctrl.Config.WarningDays)
for _, n := range nl.Items {
namespace := n.ObjectMeta.Name
sl, err := ctrl.KubeClient.CoreV1().Secrets(namespace).List(metav1.ListOptions{
FieldSelector: "type=kubernetes.io/tls",
})
if err != nil {
return err
}
for _, sec := range sl.Items {
block, _ := pem.Decode(sec.Data["tls.crt"])
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return err
}
if cert.NotAfter.Before(threshold) {
ec := certInfo{
Namespace: namespace,
CertificateName: sec.ObjectMeta.Name,
NotAfter: cert.NotAfter}
expired = append(expired, ec)
}
}
}
//If there's no expiring certs, return
if len(expired) == 0 {
log.Info("No cert alerts detected")
return nil
}
log.Info("Sending notifications")
err = ctrl.notify(expired)
if err != nil {
return err
}
return nil
}
func (ctrl Controller) notify(expiredInfo []certInfo) error {
auth := smtp.PlainAuth("", ctrl.Config.SMTPUsername, ctrl.Config.SMTPPassword, ctrl.Config.SMTPHost)
to := []string{ctrl.Config.NotifyEmailAddr}
// Build the message
msgStr := "To: " + ctrl.Config.NotifyEmailAddr + "\r\n" +
"Subject: Warning: Certificates Expiring Soon " + " - " + ctrl.Config.ClusterName + "\r\n" +
"\r\n"
for _, ci := range expiredInfo {
log.Infof("%v", ci)
msgStr += "ns=" + ci.Namespace + " cert=" + ci.CertificateName + " expires=" + ci.NotAfter.String() + "\r\n"
}
log.Info("sending alert")
err := smtp.SendMail(ctrl.SMTPHost+":"+strconv.Itoa(ctrl.SMTPPort), auth, ctrl.Config.SenderEmailAddr, to, []byte(msgStr))
if err != nil {
return err
}
return nil
}