-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c08a533
commit 5a4fece
Showing
6 changed files
with
131 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,5 @@ metadata: | |
app.kubernetes.io/managed-by: kustomize | ||
name: emailsenderconfig-sample | ||
spec: | ||
apiTokenSecretRef: "08c775409a7869f59eda96b11efdb2f5" | ||
senderEmail: "[email protected]" | ||
apiTokenSecretRef: "mailersenttoken" | ||
senderEmail: "[email protected]" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
apiVersion: v1 | ||
data: | ||
apiToken: bWxzbi44NGMxNWUzNjI1MTgyN2NkZjVkY2QyZDc4NjcyODAwN2ZmNjEzYzllZjlmMGMwM2QxZjJjNDk0YzBmYTUwNGJm | ||
kind: Secret | ||
metadata: | ||
creationTimestamp: null | ||
name: mailersenttoken |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,11 +18,17 @@ package controller | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/mailersend/mailersend-go" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/types" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/predicate" | ||
|
||
emailv1 "email-operator/api/v1" | ||
) | ||
|
@@ -47,9 +53,95 @@ type EmailReconciler struct { | |
// For more details, check Reconcile and its Result here: | ||
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile | ||
func (r *EmailReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
_ = log.FromContext(ctx) | ||
logger := log.FromContext(ctx) | ||
// Fetch the Email instance | ||
email := &emailv1.Email{} | ||
err := r.Get(ctx, req.NamespacedName, email) | ||
if err != nil { | ||
return ctrl.Result{}, client.IgnoreNotFound(err) | ||
} | ||
|
||
// TODO(user): your logic here | ||
// Log the creation or update action | ||
if email.Generation == 1 { | ||
logger.Info("Created new Email", "Email", email.Name) | ||
} else { | ||
logger.Info("Updated existing Email", "Email", email.Name) | ||
} | ||
|
||
// Fetch the EmailSenderConfig instance | ||
emailSenderConfig := &emailv1.EmailSenderConfig{} | ||
err = r.Get(ctx, types.NamespacedName{Name: email.Spec.SenderConfigRef, Namespace: req.Namespace}, emailSenderConfig) | ||
if err != nil { | ||
email.Status.DeliveryStatus = "Failed" | ||
if errors.IsNotFound(err) { | ||
email.Status.Error = fmt.Sprintf("EmailSenderConfig %s not found", email.Spec.SenderConfigRef) | ||
} else { | ||
email.Status.Error = fmt.Sprintf("Failed to get EmailSenderConfig: %v", err) | ||
} | ||
if updateErr := r.Status().Update(ctx, email); updateErr != nil { | ||
logger.Error(updateErr, "Failed to update Email status") | ||
return ctrl.Result{}, updateErr | ||
} | ||
return ctrl.Result{}, err | ||
} | ||
|
||
// Retrieve the API token from the secret | ||
secret := &corev1.Secret{} | ||
err = r.Get(ctx, types.NamespacedName{Name: emailSenderConfig.Spec.APITokenSecretRef, Namespace: req.Namespace}, secret) | ||
if err != nil { | ||
email.Status.DeliveryStatus = "Failed" | ||
email.Status.Error = fmt.Sprintf("Failed to get secret: %v", err) | ||
if updateErr := r.Status().Update(ctx, email); updateErr != nil { | ||
logger.Error(updateErr, "Failed to update Email status") | ||
return ctrl.Result{}, updateErr | ||
} | ||
return ctrl.Result{}, err | ||
} | ||
|
||
apiToken := string(secret.Data["apiToken"]) | ||
subject := email.Spec.Subject | ||
text := email.Spec.Body | ||
from := mailersend.From{ | ||
Name: emailSenderConfig.Spec.SenderEmail, | ||
Email: emailSenderConfig.Spec.SenderEmail, | ||
} | ||
recipients := []mailersend.Recipient{ | ||
{ | ||
Name: email.Spec.RecipientEmail, | ||
Email: email.Spec.RecipientEmail, | ||
}, | ||
} | ||
ms := mailersend.NewMailersend(apiToken) | ||
message := ms.Email.NewMessage() | ||
message.SetFrom(from) | ||
message.SetRecipients(recipients) | ||
message.SetSubject(subject) | ||
message.SetText(text) | ||
|
||
res, err := ms.Email.Send(ctx, message) | ||
if err != nil { | ||
email.Status.DeliveryStatus = "Failed" | ||
email.Status.Error = fmt.Sprintf("Failed to send email: %v", err) | ||
if updateErr := r.Status().Update(ctx, email); updateErr != nil { | ||
logger.Error(updateErr, "Failed to update Email status") | ||
return ctrl.Result{}, updateErr | ||
} | ||
return ctrl.Result{}, err | ||
} | ||
email.Status.DeliveryStatus = "Success" | ||
email.Status.Error = "" | ||
email.Status.MessageID = res.Header.Get("X-Message-Id") | ||
if updateErr := r.Status().Update(ctx, email); updateErr != nil { | ||
logger.Error(updateErr, "Failed to update Email status") | ||
return ctrl.Result{}, updateErr | ||
} | ||
|
||
logger.Info("Email sent successfully", | ||
"MessageID", email.Status.MessageID, | ||
"Subject", email.Spec.Subject, | ||
"From", emailSenderConfig.Spec.SenderEmail, | ||
"To", email.Spec.RecipientEmail, | ||
) | ||
|
||
return ctrl.Result{}, nil | ||
} | ||
|
@@ -58,5 +150,6 @@ func (r *EmailReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl | |
func (r *EmailReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewControllerManagedBy(mgr). | ||
For(&emailv1.Email{}). | ||
WithEventFilter(predicate.GenerationChangedPredicate{}). | ||
Complete(r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,9 +47,27 @@ type EmailSenderConfigReconciler struct { | |
// For more details, check Reconcile and its Result here: | ||
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile | ||
func (r *EmailSenderConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { | ||
_ = log.FromContext(ctx) | ||
logger := log.FromContext(ctx) | ||
// Fetch the Email instance | ||
emailSenderConfig := &emailv1.EmailSenderConfig{} | ||
err := r.Get(ctx, req.NamespacedName, emailSenderConfig) | ||
if err != nil { | ||
logger.Error(err, "Failed to get EmailSenderConfig", | ||
"EmailSenderConfig", emailSenderConfig.Name) | ||
return ctrl.Result{}, client.IgnoreNotFound(err) | ||
} | ||
// Log the creation or update action | ||
action := "Updated existing" | ||
if emailSenderConfig.Generation == 1 { | ||
action = "Created new" | ||
} | ||
|
||
// TODO(user): your logic here | ||
logger.Info(action+" EmailSenderConfig", | ||
"Name", emailSenderConfig.Name, | ||
"senderEmail", emailSenderConfig.Spec.SenderEmail, | ||
"secret", emailSenderConfig.Spec.APITokenSecretRef, | ||
"generation", emailSenderConfig.Generation, | ||
) | ||
|
||
return ctrl.Result{}, nil | ||
} | ||
|