diff --git a/controllers/notification.go b/controllers/notification.go index ee0f48a..720f6ea 100644 --- a/controllers/notification.go +++ b/controllers/notification.go @@ -18,7 +18,6 @@ package controllers import ( "context" - "errors" "fmt" "strconv" "strings" @@ -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 { @@ -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) @@ -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) { diff --git a/go.mod b/go.mod index 4923e93..fd1a29e 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index c6a611c..70a15e8 100644 --- a/go.sum +++ b/go.sum @@ -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=