forked from VirtusLab/jenkins-operator
/
smtp.go
134 lines (110 loc) · 3.83 KB
/
smtp.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
package smtp
import (
"context"
"crypto/tls"
"fmt"
"strings"
"github.com/jenkinsci/kubernetes-operator/api/v1alpha2"
"github.com/jenkinsci/kubernetes-operator/pkg/notifications/event"
"github.com/jenkinsci/kubernetes-operator/pkg/notifications/provider"
"github.com/pkg/errors"
"gopkg.in/gomail.v2"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
)
const (
mailSubject = "Jenkins Operator Notification"
infoColor = "blue"
warningColor = "red"
defaultColor = "gray"
content = `
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head></head>
<body>
<h1 style="background-color: %s; color: white; padding: 3px 10px;">%s</h1>
<h3>%s</h3>
<table>
<tr>
<td><b>CR name:</b></td>
<td>%s</td>
</tr>
<tr>
<td><b>Phase:</b></td>
<td>%s</td>
</tr>
</table>
<h6 style="font-size: 11px; color: grey; margin-top: 15px;">Powered by Jenkins Operator <3</h6>
</body>
</html>`
)
// SMTP is Simple Mail Transport Protocol used for sending emails.
type SMTP struct {
k8sClient k8sclient.Client
config v1alpha2.Notification
}
// New returns instance of SMTP.
func New(k8sClient k8sclient.Client, config v1alpha2.Notification) *SMTP {
return &SMTP{k8sClient: k8sClient, config: config}
}
func (s SMTP) generateMessage(e event.Event) *gomail.Message {
var statusMessage strings.Builder
var reasons string
if s.config.Verbose {
reasons = strings.TrimRight(strings.Join(e.Reason.Verbose(), "</li><li>"), "<li>")
} else {
reasons = strings.TrimRight(strings.Join(e.Reason.Short(), "</li><li>"), "<li>")
}
statusMessage.WriteString("<ul><li>")
statusMessage.WriteString(reasons)
statusMessage.WriteString("</ul>")
htmlMessage := fmt.Sprintf(content, s.getStatusColor(e.Level), provider.NotificationTitle(e), statusMessage.String(), e.Jenkins.Name, e.Phase)
message := gomail.NewMessage()
message.SetHeader("From", s.config.SMTP.From)
message.SetHeader("To", s.config.SMTP.To)
message.SetHeader("Subject", mailSubject)
message.SetBody("text/html", htmlMessage)
return message
}
// Send is function for sending notification by SMTP server.
func (s SMTP) Send(e event.Event) error {
usernameSecret := &corev1.Secret{}
passwordSecret := &corev1.Secret{}
usernameSelector := s.config.SMTP.UsernameSecretKeySelector
passwordSelector := s.config.SMTP.PasswordSecretKeySelector
err := s.k8sClient.Get(context.TODO(), types.NamespacedName{Name: usernameSelector.Name, Namespace: e.Jenkins.Namespace}, usernameSecret)
if err != nil {
return err
}
err = s.k8sClient.Get(context.TODO(), types.NamespacedName{Name: passwordSelector.Name, Namespace: e.Jenkins.Namespace}, passwordSecret)
if err != nil {
return err
}
usernameSecretValue := string(usernameSecret.Data[usernameSelector.Key])
if usernameSecretValue == "" {
return errors.Errorf("SMTP username is empty in secret '%s/%s[%s]", e.Jenkins.Namespace, usernameSelector.Name, usernameSelector.Key)
}
passwordSecretValue := string(passwordSecret.Data[passwordSelector.Key])
if passwordSecretValue == "" {
return errors.Errorf("SMTP password is empty in secret '%s/%s[%s]", e.Jenkins.Namespace, passwordSelector.Name, passwordSelector.Key)
}
mailer := gomail.NewDialer(s.config.SMTP.Server, s.config.SMTP.Port, usernameSecretValue, passwordSecretValue)
mailer.TLSConfig = &tls.Config{InsecureSkipVerify: s.config.SMTP.TLSInsecureSkipVerify}
message := s.generateMessage(e)
if err := mailer.DialAndSend(message); err != nil {
return err
}
return nil
}
func (s SMTP) getStatusColor(logLevel v1alpha2.NotificationLevel) event.StatusColor {
switch logLevel {
case v1alpha2.NotificationLevelInfo:
return infoColor
case v1alpha2.NotificationLevelWarning:
return warningColor
default:
return defaultColor
}
}