Skip to content

Commit

Permalink
Merge pull request #265 from gianlucam76/mail
Browse files Browse the repository at this point in the history
(feat) add support to send notification via SMTP
  • Loading branch information
gianlucam76 authored Jan 5, 2025
2 parents bc3622e + 9f5d3ef commit 6a11b81
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
22 changes: 19 additions & 3 deletions controllers/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package controllers

import (
"context"
"errors"
"fmt"
"strconv"
"strings"
Expand All @@ -36,6 +35,7 @@ import (

libsveltosv1beta1 "github.com/projectsveltos/libsveltos/api/v1beta1"
logs "github.com/projectsveltos/libsveltos/lib/logsettings"
sveltosnotifications "github.com/projectsveltos/libsveltos/lib/notifications"
)

type slackInfo struct {
Expand Down Expand Up @@ -85,8 +85,7 @@ func sendNotification(ctx context.Context, c client.Client, clusterNamespace, cl
case libsveltosv1beta1.NotificationTypeTelegram:
err = sendTelegramNotification(ctx, c, clusterNamespace, clusterName, clusterType, n, conditions, logger)
case libsveltosv1beta1.NotificationTypeSMTP:
// TODO: add SMTP support
err = errors.New("not supported yet")
err = sendSMTPNotification(ctx, c, clusterNamespace, clusterName, clusterType, n, conditions, logger)
default:
logger.V(logs.LogInfo).Info("no handler registered for notification")
panic(1)
Expand Down Expand Up @@ -282,6 +281,23 @@ func sendTelegramNotification(ctx context.Context, c client.Client, clusterNames
return err
}

func sendSMTPNotification(ctx context.Context, c client.Client, clusterNamespace, clusterName string,
clusterType libsveltosv1beta1.ClusterType, n *libsveltosv1beta1.Notification, conditions []libsveltosv1beta1.Condition,
logger logr.Logger) error {

mailer, err := sveltosnotifications.NewMailer(ctx, c, n)
if err != nil {
return err
}

l := logger.WithValues("notification", n.Name)
l.V(logs.LogInfo).Info("send smtp message")

message, _ := getNotificationMessage(clusterNamespace, clusterName, clusterType, conditions, logger)

return mailer.SendMail("Sveltos Notification", message, false, nil)
}

func getNotificationMessage(clusterNamespace, clusterName string, clusterType libsveltosv1beta1.ClusterType,
conditions []libsveltosv1beta1.Condition, logger logr.Logger) (string, bool) {

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/onsi/gomega v1.36.2
github.com/pkg/errors v0.9.1
github.com/projectsveltos/addon-controller v0.44.0
github.com/projectsveltos/libsveltos v0.44.1-0.20250103101813-2ae46c8fe0f5
github.com/projectsveltos/libsveltos v0.44.1-0.20250105090719-c7e096a8998f
github.com/prometheus/client_golang v1.20.5
github.com/slack-go/slack v0.15.0
github.com/spf13/pflag v1.0.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRI
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/projectsveltos/addon-controller v0.44.0 h1:WMEWHlPulFGKdq96P1sUu1E+3XXLuSNNVW3SA+yr20U=
github.com/projectsveltos/addon-controller v0.44.0/go.mod h1:4GjQZyxvgNvD0joYN0l0eZDPauo0m299ZG4X9zilNJs=
github.com/projectsveltos/libsveltos v0.44.1-0.20250103101813-2ae46c8fe0f5 h1:4AH8uyJ005axlyi9GbqBzeZqEZvqdNGzl+NG/1Xi1g8=
github.com/projectsveltos/libsveltos v0.44.1-0.20250103101813-2ae46c8fe0f5/go.mod h1:ygOskqy32UUcH9P0Ygpei3oaNcyrcWWSQ+e4OxRX6QA=
github.com/projectsveltos/libsveltos v0.44.1-0.20250105090719-c7e096a8998f h1:SVqsw6s+PNQfOtOnZefvNXeaRl5VRWNF8FGle07+Mjw=
github.com/projectsveltos/libsveltos v0.44.1-0.20250105090719-c7e096a8998f/go.mod h1:ygOskqy32UUcH9P0Ygpei3oaNcyrcWWSQ+e4OxRX6QA=
github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+bR9r+8l63Y=
github.com/prometheus/client_golang v1.20.5/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
Expand Down

0 comments on commit 6a11b81

Please sign in to comment.