From 55343ff5071ddc22b10fac50c1d2b2369e22d807 Mon Sep 17 00:00:00 2001 From: Aline Abler Date: Wed, 19 Apr 2023 17:55:48 +0200 Subject: [PATCH 1/2] Implement email notifications on BillingEntity update --- apis/billing/v1/billingentity_types.go | 7 ++ apis/user/v1/invitation_types.go | 3 +- .../billing/odoostorage/odoo/odoo8/odoo8.go | 10 +- apiserver/billing/odoostorage/update.go | 10 ++ config/rbac/controller/role.yaml | 23 ++++ controller.go | 113 ++++++++++++++++-- controllers/billingentity_email_cronjob.go | 112 +++++++++++++++++ .../billingentity_email_cronjob_test.go | 101 ++++++++++++++++ controllers/email_metrics.go | 21 ++++ controllers/invitation_email_controller.go | 24 +--- .../invitation_email_controller_test.go | 6 +- go.mod | 15 ++- go.sum | 38 ++++++ mailsenders/mail_senders.go | 18 ++- mailsenders/renderer.go | 12 +- mailsenders/renderer_test.go | 4 +- 16 files changed, 457 insertions(+), 60 deletions(-) create mode 100644 controllers/billingentity_email_cronjob.go create mode 100644 controllers/billingentity_email_cronjob_test.go create mode 100644 controllers/email_metrics.go diff --git a/apis/billing/v1/billingentity_types.go b/apis/billing/v1/billingentity_types.go index d8a41a91..047545c5 100644 --- a/apis/billing/v1/billingentity_types.go +++ b/apis/billing/v1/billingentity_types.go @@ -7,6 +7,13 @@ import ( "sigs.k8s.io/apiserver-runtime/pkg/builder/resource" ) +const ( + // ConditionEmailSent is set when the update notification email has been sent + ConditionEmailSent = "EmailSent" + ConditionReasonSendFailed = "SendFailed" + ConditionReasonUpdated = "Updated" +) + // +kubebuilder:object:root=true // BillingEntity is a representation of an APPUiO Cloud BillingEntity diff --git a/apis/user/v1/invitation_types.go b/apis/user/v1/invitation_types.go index fa9ef096..e36afcf9 100644 --- a/apis/user/v1/invitation_types.go +++ b/apis/user/v1/invitation_types.go @@ -16,7 +16,8 @@ const ( // ConditionRedeemed is set when the invitation has been redeemed ConditionRedeemed = "Redeemed" // ConditionEmailSent is set when the invitation email has been sent - ConditionEmailSent = "EmailSent" + ConditionEmailSent = "EmailSent" + ConditionReasonSendFailed = "SendFailed" ) // +kubebuilder:object:root=true diff --git a/apiserver/billing/odoostorage/odoo/odoo8/odoo8.go b/apiserver/billing/odoostorage/odoo/odoo8/odoo8.go index f1938b32..eec9144a 100644 --- a/apiserver/billing/odoostorage/odoo/odoo8/odoo8.go +++ b/apiserver/billing/odoostorage/odoo/odoo8/odoo8.go @@ -48,10 +48,10 @@ var ( "email", "phone", - "x_control_api_meta_status", ) accountingContactUpdateAllowedFields = newSet( "x_invoice_contact", + "x_control_api_meta_status", "email", ) ) @@ -291,10 +291,12 @@ func mapPartnersToBillingEntity(ctx context.Context, company model.Partner, acco name := odooIDToK8sID(accounting.ID) var status billingv1.BillingEntityStatus - err := json.Unmarshal([]byte(accounting.Status.Value), &status) + if accounting.Status.Value != "" { + err := json.Unmarshal([]byte(accounting.Status.Value), &status) - if err != nil { - l.Error(err, "Could not unmarshal BillingEntityStatus", "billingEntityName", name, "rawStatus", accounting.Status.Value) + if err != nil { + l.Error(err, "Could not unmarshal BillingEntityStatus", "billingEntityName", name, "rawStatus", accounting.Status.Value) + } } return billingv1.BillingEntity{ diff --git a/apiserver/billing/odoostorage/update.go b/apiserver/billing/odoostorage/update.go index 9d9525cf..39d2dc40 100644 --- a/apiserver/billing/odoostorage/update.go +++ b/apiserver/billing/odoostorage/update.go @@ -3,7 +3,9 @@ package odoostorage import ( "context" "fmt" + "reflect" + apimeta "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apiserver/pkg/registry/rest" @@ -38,5 +40,13 @@ func (s *billingEntityStorage) Update(ctx context.Context, name string, objInfo } } + if !reflect.DeepEqual(newBE.Spec, oldBE.Spec) { + apimeta.SetStatusCondition(&newBE.Status.Conditions, metav1.Condition{ + Status: metav1.ConditionFalse, + Type: billingv1.ConditionEmailSent, + Reason: billingv1.ConditionReasonUpdated, + }) + } + return newBE, false, s.storage.Update(ctx, newBE) } diff --git a/config/rbac/controller/role.yaml b/config/rbac/controller/role.yaml index db2d5d69..6e0b9485 100644 --- a/config/rbac/controller/role.yaml +++ b/config/rbac/controller/role.yaml @@ -115,6 +115,14 @@ rules: - get - list - watch +- apiGroups: + - rbac.appuio.io + resources: + - billingentities/status + verbs: + - get + - patch + - update - apiGroups: - rbac.appuio.io resources: @@ -189,6 +197,21 @@ rules: - patch - update - watch +- apiGroups: + - user.appuio.io + resources: + - billingentities + verbs: + - get + - list +- apiGroups: + - user.appuio.io + resources: + - billingentities/status + verbs: + - get + - patch + - update - apiGroups: - user.appuio.io resources: diff --git a/controller.go b/controller.go index 1a386e73..2577922b 100644 --- a/controller.go +++ b/controller.go @@ -1,6 +1,7 @@ package main import ( + "context" "flag" "os" "text/template" @@ -10,6 +11,7 @@ import ( // to ensure that exec-entrypoint and run can make use of them. _ "k8s.io/client-go/plugin/pkg/client/auth" + "github.com/Masterminds/sprig/v3" "k8s.io/apimachinery/pkg/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime" clientgoscheme "k8s.io/client-go/kubernetes/scheme" @@ -19,6 +21,7 @@ import ( "sigs.k8s.io/controller-runtime/pkg/metrics" "sigs.k8s.io/controller-runtime/pkg/webhook" + "github.com/robfig/cron/v3" "github.com/spf13/cobra" billingv1 "github.com/appuio/control-api/apis/billing/v1" @@ -35,7 +38,7 @@ import ( const ( defaultInvitationEmailTemplate = `Hello developer of great software, Kubernetes engineer or fellow human, -A user of APPUiO Cloud has invited you to join them. Follow https://portal.dev/invitations/{{.Invitation.ObjectMeta.Name}}?token={{.Invitation.Status.Token}} to accept this invitation. +A user of APPUiO Cloud has invited you to join them. Follow https://portal.dev/invitations/{{.Object.ObjectMeta.Name}}?token={{.Object.Status.Token}} to accept this invitation. APPUiO Cloud is a shared Kubernetes offering based on OpenShift provided by https://vshn.ch. @@ -43,6 +46,14 @@ Unsure what to do next? Accept this invitation using the link above, login to on If you have any problems or questions, please email us at support@appuio.ch. +All the best +Your APPUiO Cloud Team` + defaultBillingEntityEmailTemplate = `Good time of day! + +A user of APPUiO Cloud has updated billing entity {{.Object.ObjectMeta.Name}} ({{.Object.Spec.Name}}). + +See https://erp.vshn.net/web#id={{ trimPrefix "be-" .Object.ObjectMeta.Name }}&view_type=form&model=res.partner&menu_id=74&action=60 for details. + All the best Your APPUiO Cloud Team` ) @@ -86,6 +97,11 @@ func ControllerCommand() *cobra.Command { invEmailMailgunUrl := cmd.Flags().String("mailgun-url", "https://api.eu.mailgun.net/v3", "API base URL for your Mailgun account") invEmailMailgunTestMode := cmd.Flags().Bool("mailgun-test-mode", false, "If set, do not actually send e-mails") + billingEntityEmailBodyTemplate := cmd.Flags().String("billingentity-email-body-template", defaultBillingEntityEmailTemplate, "Body for billing entity modification update mails") + billingEntityEmailRecipient := cmd.Flags().String("billingentity-email-recipient", "", "Recipient e-mail address for billing entity modification update mails") + billingEntityEmailSubject := cmd.Flags().String("billingentity-email-subject", "An APPUiO Billing Entity has been updated", "Subject for billing entity modification update mails") + billingEntityCronInterval := cmd.Flags().String("billingentity-email-cron-interval", "@every 1m", "Cron interval for how frequently billing entity update e-mails are sent") + cmd.Run = func(*cobra.Command, []string) { scheme := runtime.NewScheme() setupLog := ctrl.Log.WithName("setup") @@ -100,29 +116,59 @@ func ControllerCommand() *cobra.Command { ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts))) ctx := ctrl.SetupSignalHandler() - bt, err := template.New("emailBody").Parse(*emailBodyTemplate) + bt, err := template.New("emailBody").Funcs(sprig.FuncMap()).Parse(*emailBodyTemplate) if err != nil { - setupLog.Error(err, "Failed to parse email body template") + setupLog.Error(err, "Failed to parse email body template for invitations") os.Exit(1) } - bodyRenderer := &mailsenders.InvitationRenderer{Template: bt} + bet, err := template.New("emailBody").Funcs(sprig.FuncMap()).Parse(*billingEntityEmailBodyTemplate) + if err != nil { + setupLog.Error(err, "Failed to parse email body template for billing entity e-mails") + os.Exit(1) + } + invitationBodyRenderer := &mailsenders.Renderer{Template: bt} + billingEntityBodyRenderer := &mailsenders.Renderer{Template: bet} - var mailSender mailsenders.MailSender + var invMailSender mailsenders.MailSender + var beMailSender mailsenders.MailSender if *invEmailBackend == "mailgun" { b := mailsenders.NewMailgunSender( *invEmailMailgunDomain, *invEmailMailgunToken, *invEmailMailgunUrl, *invEmailSender, - bodyRenderer, + invitationBodyRenderer, *invEmailSubject, *invEmailMailgunTestMode, ) - mailSender = &b + invMailSender = &b + if *billingEntityEmailRecipient != "" { + be := mailsenders.NewMailgunSender( + *invEmailMailgunDomain, + *invEmailMailgunToken, + *invEmailMailgunUrl, + *invEmailSender, + billingEntityBodyRenderer, + *billingEntityEmailSubject, + *invEmailMailgunTestMode, + ) + beMailSender = &be + } else { + // fall back to stdout if no recipient e-mail is given + beMailSender = &mailsenders.StdoutSender{ + Subject: *billingEntityEmailSubject, + Body: billingEntityBodyRenderer, + } + } + invMailSender = &b } else { - mailSender = &mailsenders.StdoutSender{ + invMailSender = &mailsenders.StdoutSender{ Subject: *invEmailSubject, - Body: bodyRenderer, + Body: invitationBodyRenderer, + } + beMailSender = &mailsenders.StdoutSender{ + Subject: *billingEntityEmailSubject, + Body: billingEntityBodyRenderer, } } @@ -135,7 +181,7 @@ func ControllerCommand() *cobra.Command { *invTokenValidFor, *redeemedInvitationTTL, *invEmailBaseRetryDelay, - mailSender, + invMailSender, ctrl.Options{ Scheme: scheme, MetricsBindAddress: *metricsAddr, @@ -150,11 +196,23 @@ func ControllerCommand() *cobra.Command { os.Exit(1) } + cron, err := setupCron( + ctx, + *billingEntityCronInterval, + mgr, + beMailSender, + *billingEntityEmailRecipient, + ) + + cron.Start() + setupLog.Info("starting manager") if err := mgr.Start(ctx); err != nil { setupLog.Error(err, "problem running manager") os.Exit(1) } + setupLog.Info("Stopping...") + <-cron.Stop().Done() } return cmd @@ -283,3 +341,38 @@ func setupManager( } return mgr, err } + +func setupCron( + ctx context.Context, + crontab string, + mgr ctrl.Manager, + beMailSender mailsenders.MailSender, + beMailRecipient string, +) (*cron.Cron, error) { + + bemail := controllers.NewBillingEntityEmailCronJob( + mgr.GetClient(), + mgr.GetEventRecorderFor("invitation-email-controller"), + mgr.GetScheme(), + beMailSender, + beMailRecipient, + ) + + metrics.Registry.MustRegister(bemail.GetMetrics()) + syncLog := ctrl.Log.WithName("cron") + + c := cron.New() + _, err := c.AddFunc(crontab, func() { + err := bemail.Run(ctx) + + if err == nil { + return + } + syncLog.Error(err, "Error during periodic job") + + }) + if err != nil { + return nil, err + } + return c, nil +} diff --git a/controllers/billingentity_email_cronjob.go b/controllers/billingentity_email_cronjob.go new file mode 100644 index 00000000..fc2334b5 --- /dev/null +++ b/controllers/billingentity_email_cronjob.go @@ -0,0 +1,112 @@ +package controllers + +import ( + "context" + "fmt" + + "go.uber.org/multierr" + apimeta "k8s.io/apimachinery/pkg/api/meta" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/client-go/tools/record" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/log" + + "github.com/appuio/control-api/mailsenders" + "github.com/prometheus/client_golang/prometheus" + + billingv1 "github.com/appuio/control-api/apis/billing/v1" +) + +// BillingEntityEmailCronJob periodically checks billing entities and sends notification emails if appropriate +type BillingEntityEmailCronJob struct { + client.Client + + Recorder record.EventRecorder + Scheme *runtime.Scheme + + MailSender mailsenders.MailSender + + mailRecipientAddress string + + failureCounter prometheus.Counter + successCounter prometheus.Counter +} + +func NewBillingEntityEmailCronJob(client client.Client, eventRecorder record.EventRecorder, scheme *runtime.Scheme, mailSender mailsenders.MailSender, MailRecipientAddress string) BillingEntityEmailCronJob { + return BillingEntityEmailCronJob{ + Client: client, + Recorder: eventRecorder, + MailSender: mailSender, + mailRecipientAddress: MailRecipientAddress, + failureCounter: newFailureCounter("control_api_billingentity_emails"), + successCounter: newSuccessCounter("control_api_billingentity_emails"), + } + +} + +func (r *BillingEntityEmailCronJob) GetMetrics() prometheus.Collector { + reg := prometheus.NewRegistry() + reg.MustRegister(r.failureCounter) + reg.MustRegister(r.successCounter) + return reg +} + +//+kubebuilder:rbac:groups="rbac.appuio.io",resources=billingentities,verbs=get;list +//+kubebuilder:rbac:groups="user.appuio.io",resources=billingentities,verbs=get;list +//+kubebuilder:rbac:groups="rbac.appuio.io",resources=billingentities/status,verbs=get;update;patch +//+kubebuilder:rbac:groups="user.appuio.io",resources=billingentities/status,verbs=get;update;patch + +// Run lists all BillingEntity resources and sends notification emails if needed. +func (r *BillingEntityEmailCronJob) Run(ctx context.Context) error { + log := log.FromContext(ctx) + log.V(1).Info("Sending Billing Entity E-Mails...") + + list := &billingv1.BillingEntityList{} + + err := r.Client.List(ctx, list) + + if err != nil { + return err + } + + var errors []error + for _, be := range list.Items { + if apimeta.FindStatusCondition(be.Status.Conditions, billingv1.ConditionEmailSent) == nil || apimeta.IsStatusConditionFalse(be.Status.Conditions, billingv1.ConditionEmailSent) { + err = r.sendEmailAndUpdateStatus(ctx, be) + if err != nil { + errors = append(errors, err) + } + } + } + return multierr.Combine(errors...) +} + +func (r *BillingEntityEmailCronJob) sendEmailAndUpdateStatus(ctx context.Context, be billingv1.BillingEntity) error { + log := log.FromContext(ctx) + id, err := r.MailSender.Send(ctx, r.mailRecipientAddress, be) + if err != nil { + log.V(0).Error(err, "Error in e-mail backend") + r.failureCounter.Add(1) + apimeta.SetStatusCondition(&be.Status.Conditions, metav1.Condition{ + Type: billingv1.ConditionEmailSent, + Status: metav1.ConditionFalse, + Reason: billingv1.ConditionReasonSendFailed, + Message: err.Error(), + }) + } else { + r.successCounter.Add(1) + + var message string + if id != "" { + message = fmt.Sprintf("Message ID: %s", id) + } + apimeta.SetStatusCondition(&be.Status.Conditions, metav1.Condition{ + Type: billingv1.ConditionEmailSent, + Status: metav1.ConditionTrue, + Message: message, + }) + } + + return r.Client.Update(ctx, &be) +} diff --git a/controllers/billingentity_email_cronjob_test.go b/controllers/billingentity_email_cronjob_test.go new file mode 100644 index 00000000..53f887ad --- /dev/null +++ b/controllers/billingentity_email_cronjob_test.go @@ -0,0 +1,101 @@ +package controllers_test + +import ( + "context" + "errors" + "testing" + + "github.com/stretchr/testify/require" + apimeta "k8s.io/apimachinery/pkg/api/meta" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/tools/record" + "sigs.k8s.io/controller-runtime/pkg/client" + + billingv1 "github.com/appuio/control-api/apis/billing/v1" + userv1 "github.com/appuio/control-api/apis/user/v1" + . "github.com/appuio/control-api/controllers" +) + +type FailingBESender struct{} +type BESenderWithConstantId struct{} + +func (f *FailingBESender) Send(context.Context, string, any) (string, error) { + return "", errors.New("Err0r") +} + +func (s *BESenderWithConstantId) Send(context.Context, string, any) (string, error) { + return "ID10", nil +} + +func Test_BillingEntityEmailCronJob_Sending_Success(t *testing.T) { + ctx := context.Background() + + subject := baseBillingEntity() + + c := prepareTest(t, subject) + + j := billingEntityCronJob(c) + + err := j.Run(ctx) + require.NoError(t, err) + + require.NoError(t, c.Get(ctx, client.ObjectKeyFromObject(subject), subject)) + require.True(t, apimeta.IsStatusConditionTrue(subject.Status.Conditions, userv1.ConditionEmailSent)) + condition := apimeta.FindStatusCondition(subject.Status.Conditions, userv1.ConditionEmailSent) + require.Equal(t, "Message ID: ID10", condition.Message) +} + +func Test_BillingEntityEmailCronJob_Sending_Failure(t *testing.T) { + ctx := context.Background() + + subject := baseBillingEntity() + + c := prepareTest(t, subject) + + j := billingEntityCronJobWithFailingSender(c) + + err := j.Run(ctx) + require.NoError(t, err) + + require.NoError(t, c.Get(ctx, client.ObjectKeyFromObject(subject), subject)) + require.False(t, apimeta.IsStatusConditionTrue(subject.Status.Conditions, userv1.ConditionEmailSent)) + condition := apimeta.FindStatusCondition(subject.Status.Conditions, userv1.ConditionEmailSent) + require.NotNil(t, condition) + require.Equal(t, userv1.ConditionReasonSendFailed, condition.Reason) +} + +func billingEntityCronJob(c client.WithWatch) *BillingEntityEmailCronJob { + r := NewBillingEntityEmailCronJob( + c, + record.NewFakeRecorder(3), + c.Scheme(), + &SenderWithConstantId{}, + "foo@example.com", + ) + return &r +} + +func billingEntityCronJobWithFailingSender(c client.WithWatch) *BillingEntityEmailCronJob { + r := NewBillingEntityEmailCronJob( + c, + record.NewFakeRecorder(3), + c.Scheme(), + &FailingSender{}, + "foo@example.com", + ) + return &r +} + +func baseBillingEntity() *billingv1.BillingEntity { + return &billingv1.BillingEntity{ + ObjectMeta: metav1.ObjectMeta{ + Name: "be-111", + }, + Spec: billingv1.BillingEntitySpec{ + Name: "myCompany", + }, + Status: billingv1.BillingEntityStatus{ + Conditions: []metav1.Condition{}, + }, + } +} diff --git a/controllers/email_metrics.go b/controllers/email_metrics.go new file mode 100644 index 00000000..564e05b7 --- /dev/null +++ b/controllers/email_metrics.go @@ -0,0 +1,21 @@ +package controllers + +import ( + "github.com/prometheus/client_golang/prometheus" +) + +func newSuccessCounter(subsystem string) prometheus.Counter { + return prometheus.NewCounter(prometheus.CounterOpts{ + Subsystem: subsystem, + Name: "sent_success_total", + Help: "Total number of successfully sent invitation e-mails", + }) +} + +func newFailureCounter(subsystem string) prometheus.Counter { + return prometheus.NewCounter(prometheus.CounterOpts{ + Subsystem: subsystem, + Name: "sent_failed_total", + Help: "Total number of invitation e-mails which failed to send", + }) +} diff --git a/controllers/invitation_email_controller.go b/controllers/invitation_email_controller.go index 1d22be07..89df4997 100644 --- a/controllers/invitation_email_controller.go +++ b/controllers/invitation_email_controller.go @@ -23,8 +23,6 @@ import ( userv1 "github.com/appuio/control-api/apis/user/v1" ) -const ReasonSendFailed = "SendFailed" - // InvitationEmailReconciler reconciles invitations and sends invitation emails if appropriate type InvitationEmailReconciler struct { client.Client @@ -46,28 +44,12 @@ func NewInvitationEmailReconciler(client client.Client, eventRecorder record.Eve Scheme: scheme, MailSender: mailSender, BaseRetryDelay: baseRetryDelay, - failureCounter: newFailureCounter(), - successCounter: newSuccessCounter(), + failureCounter: newFailureCounter("control_api_invitation_emails"), + successCounter: newSuccessCounter("control_api_invitation_emails"), } } -func newSuccessCounter() prometheus.Counter { - return prometheus.NewCounter(prometheus.CounterOpts{ - Subsystem: "control_api_invitation_emails", - Name: "sent_success_total", - Help: "Total number of successfully sent invitation e-mails", - }) -} - -func newFailureCounter() prometheus.Counter { - return prometheus.NewCounter(prometheus.CounterOpts{ - Subsystem: "control_api_invitation_emails", - Name: "sent_failed_total", - Help: "Total number of invitation e-mails which failed to send", - }) -} - func (r *InvitationEmailReconciler) GetMetrics() prometheus.Collector { reg := prometheus.NewRegistry() reg.MustRegister(r.failureCounter) @@ -110,7 +92,7 @@ func (r *InvitationEmailReconciler) Reconcile(ctx context.Context, req ctrl.Requ apimeta.SetStatusCondition(&inv.Status.Conditions, metav1.Condition{ Type: userv1.ConditionEmailSent, Status: metav1.ConditionFalse, - Reason: ReasonSendFailed, + Reason: userv1.ConditionReasonSendFailed, Message: err.Error(), }) return ctrl.Result{}, multierr.Append(err, r.Client.Status().Update(ctx, &inv)) diff --git a/controllers/invitation_email_controller_test.go b/controllers/invitation_email_controller_test.go index ae7f8a6e..3bbc68ec 100644 --- a/controllers/invitation_email_controller_test.go +++ b/controllers/invitation_email_controller_test.go @@ -22,11 +22,11 @@ import ( type FailingSender struct{} type SenderWithConstantId struct{} -func (f *FailingSender) Send(context.Context, string, userv1.Invitation) (string, error) { +func (f *FailingSender) Send(context.Context, string, any) (string, error) { return "", errors.New("Err0r") } -func (s *SenderWithConstantId) Send(context.Context, string, userv1.Invitation) (string, error) { +func (s *SenderWithConstantId) Send(context.Context, string, any) (string, error) { return "ID10", nil } @@ -70,7 +70,7 @@ func Test_InvitationEmailReconciler_Reconcile_WithSendingFailure_Success(t *test require.False(t, apimeta.IsStatusConditionTrue(subject.Status.Conditions, userv1.ConditionEmailSent)) condition := apimeta.FindStatusCondition(subject.Status.Conditions, userv1.ConditionEmailSent) require.NotNil(t, condition) - require.Equal(t, ReasonSendFailed, condition.Reason) + require.Equal(t, userv1.ConditionReasonSendFailed, condition.Reason) } func Test_InvitationEmailReconciler_Reconcile_MetricsCorrect(t *testing.T) { diff --git a/go.mod b/go.mod index 2cb68fdb..ddf4c823 100644 --- a/go.mod +++ b/go.mod @@ -3,10 +3,12 @@ module github.com/appuio/control-api go 1.20 require ( + github.com/Masterminds/sprig/v3 v3.2.3 github.com/go-logr/zapr v1.2.3 github.com/golang/mock v1.6.0 github.com/google/uuid v1.3.0 github.com/prometheus/client_golang v1.15.0 + github.com/robfig/cron/v3 v3.0.1 github.com/spf13/cobra v1.7.0 github.com/stretchr/testify v1.8.2 go.uber.org/multierr v1.11.0 @@ -24,7 +26,16 @@ require ( sigs.k8s.io/kustomize/kustomize/v5 v5.0.0 ) -require github.com/rogpeppe/go-internal v1.10.0 // indirect +require ( + github.com/Masterminds/goutils v1.1.1 // indirect + github.com/Masterminds/semver/v3 v3.2.0 // indirect + github.com/huandu/xstrings v1.3.3 // indirect + github.com/mitchellh/copystructure v1.0.0 // indirect + github.com/mitchellh/reflectwalk v1.0.0 // indirect + github.com/rogpeppe/go-internal v1.10.0 // indirect + github.com/shopspring/decimal v1.2.0 // indirect + github.com/spf13/cast v1.5.0 // indirect +) require ( github.com/BurntSushi/toml v1.2.1 // indirect @@ -46,7 +57,7 @@ require ( github.com/felixge/httpsnoop v1.0.3 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect github.com/go-errors/errors v1.4.2 // indirect - github.com/go-logr/logr v1.2.3 // indirect + github.com/go-logr/logr v1.2.3 github.com/go-logr/stdr v1.2.2 // indirect github.com/go-openapi/jsonpointer v0.19.6 // indirect github.com/go-openapi/jsonreference v0.20.1 // indirect diff --git a/go.sum b/go.sum index 9ffe617b..7124a106 100644 --- a/go.sum +++ b/go.sum @@ -39,6 +39,13 @@ github.com/BurntSushi/toml v1.0.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbi github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak= github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= +github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI= +github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU= +github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww= +github.com/Masterminds/semver/v3 v3.2.0 h1:3MEsd0SM6jqZojhjLWWeBY+Kcjy9i6MQAeY7YgDP83g= +github.com/Masterminds/semver/v3 v3.2.0/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ= +github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj9n6YA= +github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM= github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I= github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= @@ -228,6 +235,7 @@ github.com/google/safetext v0.0.0-20230103122802-988f86758b18 h1:oK/beJ0agU7tyst github.com/google/safetext v0.0.0-20230103122802-988f86758b18/go.mod h1:mJNEy0r5YPHC7ChQffpOszlGB4L1iqjXWpIEKcFpr9s= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -246,7 +254,10 @@ github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.0 h1:1JYBfzqrWPcCclBwxFCPAou9n+q github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.0/go.mod h1:YDZoGHuwE+ov0c8smSH49WLF3F2LaWnYYuDVd+EWrc0= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/huandu/xstrings v1.3.3 h1:/Gcsuc1x8JVbJ9/rlye4xZnVAbEkGauT8lbebqcQws4= +github.com/huandu/xstrings v1.3.3/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= +github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk= github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= @@ -294,6 +305,10 @@ github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/ github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= +github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ= +github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= +github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY= +github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -344,18 +359,25 @@ github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4O github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJfhI= github.com/prometheus/procfs v0.9.0/go.mod h1:+pB4zwohETzFnmlpe6yd2lSc+0/46IYZRB/chUwxUZY= +github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs= +github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= +github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ= +github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0= github.com/soheilhy/cmux v0.1.5 h1:jjzc5WVemNEDTLwv9tlmemhC73tI08BNOIGwBOo10Js= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= +github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= +github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w= +github.com/spf13/cast v1.5.0/go.mod h1:SpXXQ5YoyJw6s3/6cMTQuxvgRl3PCJiyaX9p6b155UU= github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g= github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I= github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0= @@ -390,6 +412,7 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.etcd.io/bbolt v1.3.6 h1:/ecaJf0sk1l4l6V4awd65v2C3ILy7MSj+s/x1ADCIMU= go.etcd.io/etcd v0.0.0-20200513171258-e048e166ab9c h1:/RwRVN9EdXAVtdHxP7Ndn/tfmM9/goiwU0QTnLBgS4w= go.etcd.io/etcd/api/v3 v3.5.6 h1:Cy2qx3npLcYqTKqGJzMypnMv2tiRyifZJ17BlWIWA7A= @@ -448,6 +471,8 @@ golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= golang.org/x/crypto v0.5.0 h1:U/0M97KRkSFvyD/3FSmdP5W5swImpNgle/EHFhOsQPE= golang.org/x/crypto v0.5.0/go.mod h1:NK/OQwhpMQP3MwtdjgLlYHnH9ebylxKWv3e0fK+mkQU= golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= @@ -484,6 +509,7 @@ golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzB golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA= golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= @@ -515,8 +541,11 @@ golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81R golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g= golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= @@ -538,6 +567,7 @@ golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -582,13 +612,18 @@ golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.0.0-20220526004731-065cf7ba2467/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -598,6 +633,8 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= @@ -650,6 +687,7 @@ golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.5.0 h1:+bSpV5HIeWkuvgaMfI3UmKRThoTA5ODJTUd8T17NO+4= golang.org/x/tools v0.5.0/go.mod h1:N+Kgy78s5I24c24dU8OfWNEotWjutIs8SnJvn5IDq+k= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= diff --git a/mailsenders/mail_senders.go b/mailsenders/mail_senders.go index 9e301862..bba0b2d2 100644 --- a/mailsenders/mail_senders.go +++ b/mailsenders/mail_senders.go @@ -6,12 +6,10 @@ import ( "strings" "github.com/mailgun/mailgun-go/v4" - - userv1 "github.com/appuio/control-api/apis/user/v1" ) type MailSender interface { - Send(ctx context.Context, recipient string, inv userv1.Invitation) (id string, err error) + Send(ctx context.Context, recipient string, obj any) (id string, err error) } var _ MailSender = &MailgunSender{} @@ -24,7 +22,7 @@ type MailgunSender struct { UseTestMode bool Subject string - Body *InvitationRenderer + Body *Renderer } var _ MailSender = &StdoutSender{} @@ -32,11 +30,11 @@ var _ MailSender = &StdoutSender{} // StdoutSender is a MailSender that logs the e-mail to stdout. type StdoutSender struct { Subject string - Body *InvitationRenderer + Body *Renderer } -func (s *StdoutSender) Send(ctx context.Context, recipient string, inv userv1.Invitation) (string, error) { - body, err := s.Body.Render(inv) +func (s *StdoutSender) Send(ctx context.Context, recipient string, obj any) (string, error) { + body, err := s.Body.Render(obj) if err != nil { return "", err } @@ -47,7 +45,7 @@ func (s *StdoutSender) Send(ctx context.Context, recipient string, inv userv1.In return "", nil } -func NewMailgunSender(domain string, token string, baseUrl string, senderAddress string, body *InvitationRenderer, subject string, useTestMode bool) MailgunSender { +func NewMailgunSender(domain string, token string, baseUrl string, senderAddress string, body *Renderer, subject string, useTestMode bool) MailgunSender { mg := mailgun.NewMailgun(domain, token) mg.SetAPIBase(baseUrl) return MailgunSender{ @@ -59,8 +57,8 @@ func NewMailgunSender(domain string, token string, baseUrl string, senderAddress } } -func (m *MailgunSender) Send(ctx context.Context, recipient string, inv userv1.Invitation) (string, error) { - body, err := m.Body.Render(inv) +func (m *MailgunSender) Send(ctx context.Context, recipient string, obj any) (string, error) { + body, err := m.Body.Render(obj) if err != nil { return "", err } diff --git a/mailsenders/renderer.go b/mailsenders/renderer.go index e6e923f3..84cf91c1 100644 --- a/mailsenders/renderer.go +++ b/mailsenders/renderer.go @@ -4,21 +4,19 @@ import ( "fmt" "strings" "text/template" - - userv1 "github.com/appuio/control-api/apis/user/v1" ) -type InvitationRenderer struct { +type Renderer struct { Template *template.Template } -type templateData struct { - Invitation userv1.Invitation +type TemplateData struct { + Object any } -func (ir InvitationRenderer) Render(inv userv1.Invitation) (string, error) { +func (ir Renderer) Render(obj any) (string, error) { body := &strings.Builder{} - err := ir.Template.Execute(body, templateData{Invitation: inv}) + err := ir.Template.Execute(body, TemplateData{Object: obj}) if err != nil { return "", fmt.Errorf("failed to render e-mail body: %w", err) } diff --git a/mailsenders/renderer_test.go b/mailsenders/renderer_test.go index 462e391f..4cd11200 100644 --- a/mailsenders/renderer_test.go +++ b/mailsenders/renderer_test.go @@ -11,10 +11,10 @@ import ( ) func Test_InvitationRenderer_Render(t *testing.T) { - tm, err := template.New("test").Parse("Hi {{.Invitation.Spec.Email}}, get your token: {{.Invitation.Status.Token}}") + tm, err := template.New("test").Parse("Hi {{.Object.Spec.Email}}, get your token: {{.Object.Status.Token}}") assert.NoError(t, err) - subject := mailsenders.InvitationRenderer{Template: tm} + subject := mailsenders.Renderer{Template: tm} rendered, err := subject.Render(userv1.Invitation{ Spec: userv1.InvitationSpec{ Email: "test@example.com", From d80c68d60a0955bc3502aa49f66dcb50c34f7d47 Mon Sep 17 00:00:00 2001 From: Aline Abler Date: Fri, 21 Apr 2023 10:49:45 +0200 Subject: [PATCH 2/2] Add test for prometheus metrics in BillingEntity Email Cronjob --- Makefile | 2 +- controller.go | 2 +- .../billingentity_email_cronjob_test.go | 52 +++++++++++++++++++ controllers/email_metrics.go | 4 +- .../invitation_email_controller_test.go | 8 +-- 5 files changed, 60 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 6600dbff..47fd4b37 100644 --- a/Makefile +++ b/Makefile @@ -72,7 +72,7 @@ run-api: build ## Starts control api apiserver against the current Kubernetes cl .PHONY: run-controller run-controller: build ## Starts control api controller against the current Kubernetes cluster (based on your local config) $(localenv_make) webhook-certs/tls.key - $(BIN_FILENAME) controller --username-prefix "appuio#" --webhook-cert-dir=./local-env/webhook-certs --webhook-port=9444 --zap-log-level debug + $(BIN_FILENAME) controller --username-prefix "appuio#" --webhook-cert-dir=./local-env/webhook-certs --webhook-port=9444 --zap-log-level debug --billingentity-email-cron-interval "@every 1m" .PHONY: local-env local-env-setup: ## Setup local kind-based dev environment diff --git a/controller.go b/controller.go index 2577922b..6041939d 100644 --- a/controller.go +++ b/controller.go @@ -100,7 +100,7 @@ func ControllerCommand() *cobra.Command { billingEntityEmailBodyTemplate := cmd.Flags().String("billingentity-email-body-template", defaultBillingEntityEmailTemplate, "Body for billing entity modification update mails") billingEntityEmailRecipient := cmd.Flags().String("billingentity-email-recipient", "", "Recipient e-mail address for billing entity modification update mails") billingEntityEmailSubject := cmd.Flags().String("billingentity-email-subject", "An APPUiO Billing Entity has been updated", "Subject for billing entity modification update mails") - billingEntityCronInterval := cmd.Flags().String("billingentity-email-cron-interval", "@every 1m", "Cron interval for how frequently billing entity update e-mails are sent") + billingEntityCronInterval := cmd.Flags().String("billingentity-email-cron-interval", "@every 1h", "Cron interval for how frequently billing entity update e-mails are sent") cmd.Run = func(*cobra.Command, []string) { scheme := runtime.NewScheme() diff --git a/controllers/billingentity_email_cronjob_test.go b/controllers/billingentity_email_cronjob_test.go index 53f887ad..e30815a5 100644 --- a/controllers/billingentity_email_cronjob_test.go +++ b/controllers/billingentity_email_cronjob_test.go @@ -3,8 +3,11 @@ package controllers_test import ( "context" "errors" + "strings" "testing" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/testutil" "github.com/stretchr/testify/require" apimeta "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -74,6 +77,55 @@ func billingEntityCronJob(c client.WithWatch) *BillingEntityEmailCronJob { ) return &r } +func Test_BillingEntityEmailCronJob_MetricsCorrect(t *testing.T) { + ctx := context.Background() + + subject := baseBillingEntity() + + c := prepareTest(t, subject) + + j := billingEntityCronJob(c) + + err := j.Run(ctx) + require.NoError(t, err) + + reg := prometheus.NewRegistry() + reg.MustRegister(j.GetMetrics()) + require.NoError(t, testutil.CollectAndCompare(reg, strings.NewReader(` +# HELP control_api_billingentity_emails_sent_failed_total Total number of e-mails which failed to send +# TYPE control_api_billingentity_emails_sent_failed_total counter +control_api_billingentity_emails_sent_failed_total 0 +# HELP control_api_billingentity_emails_sent_success_total Total number of successfully sent e-mails +# TYPE control_api_billingentity_emails_sent_success_total counter +control_api_billingentity_emails_sent_success_total 1 +`), + )) +} + +func Test_BillingEntityEmailCronJob_WithSendingFailure_MetricsCorrect(t *testing.T) { + ctx := context.Background() + + subject := baseBillingEntity() + + c := prepareTest(t, subject) + + j := billingEntityCronJobWithFailingSender(c) + + err := j.Run(ctx) + require.NoError(t, err) + + reg := prometheus.NewRegistry() + reg.MustRegister(j.GetMetrics()) + require.NoError(t, testutil.CollectAndCompare(reg, strings.NewReader(` +# HELP control_api_billingentity_emails_sent_failed_total Total number of e-mails which failed to send +# TYPE control_api_billingentity_emails_sent_failed_total counter +control_api_billingentity_emails_sent_failed_total 1 +# HELP control_api_billingentity_emails_sent_success_total Total number of successfully sent e-mails +# TYPE control_api_billingentity_emails_sent_success_total counter +control_api_billingentity_emails_sent_success_total 0 +`), + )) +} func billingEntityCronJobWithFailingSender(c client.WithWatch) *BillingEntityEmailCronJob { r := NewBillingEntityEmailCronJob( diff --git a/controllers/email_metrics.go b/controllers/email_metrics.go index 564e05b7..4f5d500c 100644 --- a/controllers/email_metrics.go +++ b/controllers/email_metrics.go @@ -8,7 +8,7 @@ func newSuccessCounter(subsystem string) prometheus.Counter { return prometheus.NewCounter(prometheus.CounterOpts{ Subsystem: subsystem, Name: "sent_success_total", - Help: "Total number of successfully sent invitation e-mails", + Help: "Total number of successfully sent e-mails", }) } @@ -16,6 +16,6 @@ func newFailureCounter(subsystem string) prometheus.Counter { return prometheus.NewCounter(prometheus.CounterOpts{ Subsystem: subsystem, Name: "sent_failed_total", - Help: "Total number of invitation e-mails which failed to send", + Help: "Total number of e-mails which failed to send", }) } diff --git a/controllers/invitation_email_controller_test.go b/controllers/invitation_email_controller_test.go index 3bbc68ec..3c2f744c 100644 --- a/controllers/invitation_email_controller_test.go +++ b/controllers/invitation_email_controller_test.go @@ -91,10 +91,10 @@ func Test_InvitationEmailReconciler_Reconcile_MetricsCorrect(t *testing.T) { reg := prometheus.NewRegistry() reg.MustRegister(r.GetMetrics()) require.NoError(t, testutil.CollectAndCompare(reg, strings.NewReader(` -# HELP control_api_invitation_emails_sent_failed_total Total number of invitation e-mails which failed to send +# HELP control_api_invitation_emails_sent_failed_total Total number of e-mails which failed to send # TYPE control_api_invitation_emails_sent_failed_total counter control_api_invitation_emails_sent_failed_total 0 -# HELP control_api_invitation_emails_sent_success_total Total number of successfully sent invitation e-mails +# HELP control_api_invitation_emails_sent_success_total Total number of successfully sent e-mails # TYPE control_api_invitation_emails_sent_success_total counter control_api_invitation_emails_sent_success_total 1 `), @@ -119,10 +119,10 @@ func Test_InvitationEmailReconciler_Reconcile_WithSendingFailure_MetricsCorrect( reg := prometheus.NewRegistry() reg.MustRegister(r.GetMetrics()) require.NoError(t, testutil.CollectAndCompare(reg, strings.NewReader(` -# HELP control_api_invitation_emails_sent_failed_total Total number of invitation e-mails which failed to send +# HELP control_api_invitation_emails_sent_failed_total Total number of e-mails which failed to send # TYPE control_api_invitation_emails_sent_failed_total counter control_api_invitation_emails_sent_failed_total 1 -# HELP control_api_invitation_emails_sent_success_total Total number of successfully sent invitation e-mails +# HELP control_api_invitation_emails_sent_success_total Total number of successfully sent e-mails # TYPE control_api_invitation_emails_sent_success_total counter control_api_invitation_emails_sent_success_total 0 `),