Skip to content

Commit

Permalink
Fixing lint issues
Browse files Browse the repository at this point in the history
Signed-off-by: Rob Ulbrich <[email protected]>
  • Loading branch information
robert-ulbrich-mercedes-benz committed Jul 3, 2024
1 parent b11f2cc commit 3296a1a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 29 deletions.
4 changes: 2 additions & 2 deletions flyteadmin/pkg/async/notifications/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func GetEmailer(config runtimeInterfaces.NotificationsConfig, scope promutils.Sc
switch config.NotificationsEmailerConfig.EmailerConfig.ServiceName {
case implementations.Sendgrid:
return implementations.NewSendGridEmailer(config, scope)
case implementations.Smtp:
return implementations.NewSmtpEmailer(context.Background(), config, scope, sm)
case implementations.SMTP:
return implementations.NewSMTPEmailer(context.Background(), config, scope, sm)
default:
panic(fmt.Errorf("No matching email implementation for %s", config.NotificationsEmailerConfig.EmailerConfig.ServiceName))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ type ExternalEmailer = string

const (
Sendgrid ExternalEmailer = "sendgrid"
Smtp ExternalEmailer = "smtp"
SMTP ExternalEmailer = "smtp"
)
32 changes: 13 additions & 19 deletions flyteadmin/pkg/async/notifications/implementations/smtp_emailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ import (
"strings"
)

type SmtpEmailer struct {
type SMTPEmailer struct {
config *runtimeInterfaces.NotificationsEmailerConfig
systemMetrics emailMetrics
tlsConf *tls.Config
auth *smtp.Auth
}

func (s *SmtpEmailer) SendEmail(ctx context.Context, email admin.EmailMessage) error {
func (s *SMTPEmailer) SendEmail(ctx context.Context, email admin.EmailMessage) error {

newClient, err := smtp.Dial(s.config.EmailerConfig.SmtpServer + ":" + s.config.EmailerConfig.SmtpPort)
newClient, err := smtp.Dial(s.config.EmailerConfig.SMTPServer + ":" + s.config.EmailerConfig.SMTPPort)

if err != nil {
return s.emailError(ctx, fmt.Sprintf("Error creating email client: %s", err))
Expand Down Expand Up @@ -65,13 +65,7 @@ func (s *SmtpEmailer) SendEmail(ctx context.Context, email admin.EmailMessage) e
return s.emailError(ctx, fmt.Sprintf("Error adding email recipient: %s", err))
}

mailBody, err := createMailBody(s.config.Sender, email)

if err != nil {
return s.emailError(ctx, fmt.Sprintf("Error creating email body: %s", err))
}

_, err = writer.Write([]byte(mailBody))
_, err = writer.Write([]byte(createMailBody(s.config.Sender, email)))

if err != nil {
return s.emailError(ctx, fmt.Sprintf("Error writing mail body: %s", err))
Expand All @@ -93,13 +87,13 @@ func (s *SmtpEmailer) SendEmail(ctx context.Context, email admin.EmailMessage) e
return nil
}

func (s *SmtpEmailer) emailError(ctx context.Context, error string) error {
func (s *SMTPEmailer) emailError(ctx context.Context, error string) error {
s.systemMetrics.SendError.Inc()
logger.Error(ctx, error)
return errors.NewFlyteAdminErrorf(codes.Internal, "errors were seen while sending emails")
}

func createMailBody(emailSender string, email admin.EmailMessage) (string, error) {
func createMailBody(emailSender string, email admin.EmailMessage) string {
headerMap := make(map[string]string)
headerMap["From"] = emailSender
headerMap["To"] = strings.Join(email.RecipientsEmail, ",")
Expand All @@ -114,27 +108,27 @@ func createMailBody(emailSender string, email admin.EmailMessage) (string, error

mailMessage += "\r\n" + email.Body

return mailMessage, nil
return mailMessage
}

func NewSmtpEmailer(ctx context.Context, config runtimeInterfaces.NotificationsConfig, scope promutils.Scope, sm core.SecretManager) interfaces.Emailer {
func NewSMTPEmailer(ctx context.Context, config runtimeInterfaces.NotificationsConfig, scope promutils.Scope, sm core.SecretManager) interfaces.Emailer {
var tlsConfiguration *tls.Config
emailConf := config.NotificationsEmailerConfig.EmailerConfig

smtpPassword, err := sm.Get(ctx, emailConf.SmtpPasswordSecretName)
smtpPassword, err := sm.Get(ctx, emailConf.SMTPPasswordSecretName)
if err != nil {
logger.Debug(ctx, "No SMTP password found.")
smtpPassword = ""
}

auth := smtp.PlainAuth("", emailConf.SmtpUsername, smtpPassword, emailConf.SmtpServer)
auth := smtp.PlainAuth("", emailConf.SMTPUsername, smtpPassword, emailConf.SMTPServer)

tlsConfiguration = &tls.Config{
InsecureSkipVerify: emailConf.SmtpSkipTLSVerify,
ServerName: emailConf.SmtpServer,
InsecureSkipVerify: emailConf.SMTPSkipTLSVerify,
ServerName: emailConf.SMTPServer,
}

return &SmtpEmailer{
return &SMTPEmailer{
config: &config.NotificationsEmailerConfig,
systemMetrics: newEmailMetrics(scope.NewSubScope("smtp")),
tlsConf: tlsConfiguration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func getNotificationsEmailerConfig() interfaces.NotificationsConfig {
NotificationsProcessorConfig: interfaces.NotificationsProcessorConfig{},
NotificationsEmailerConfig: interfaces.NotificationsEmailerConfig{
EmailerConfig: interfaces.EmailServerConfig{
ServiceName: Smtp,
ServiceName: SMTP,
SmtpServer: "smtpServer",
SmtpPort: "smtpPort",
SmtpUsername: "smtpUsername",
Expand Down Expand Up @@ -54,7 +54,7 @@ func TestNewSmtpEmailer(t *testing.T) {

notificationsConfig := getNotificationsEmailerConfig()

smtpEmailer := NewSmtpEmailer(context.Background(), notificationsConfig, promutils.NewTestScope(), &secretManagerMock)
smtpEmailer := NewSMTPEmailer(context.Background(), notificationsConfig, promutils.NewTestScope(), &secretManagerMock)

assert.NotNil(t, smtpEmailer)
}
10 changes: 5 additions & 5 deletions flyteadmin/pkg/runtime/interfaces/application_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,11 +490,11 @@ type EmailServerConfig struct {
// Only one of these should be set.
APIKeyEnvVar string `json:"apiKeyEnvVar"`
APIKeyFilePath string `json:"apiKeyFilePath"`
SmtpServer string `json:"smtpServer"`
SmtpPort string `json:"smtpPort"`
SmtpSkipTLSVerify bool `json:"smtpSkipTLSVerify"`
SmtpUsername string `json:"smtpUsername"`
SmtpPasswordSecretName string `json:"smtpPasswordSecretName"`
SMTPServer string `json:"smtpServer"`
SMTPPort string `json:"smtpPort"`
SMTPSkipTLSVerify bool `json:"smtpSkipTLSVerify"`
SMTPUsername string `json:"smtpUsername"`
SMTPPasswordSecretName string `json:"smtpPasswordSecretName"`
}

// This section handles the configuration of notifications emails.
Expand Down

0 comments on commit 3296a1a

Please sign in to comment.