Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reduce/cleanup logging #21

Merged
merged 1 commit into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 17 additions & 21 deletions internal/controller/alias_controller.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package controller

import (
"bytes"
"context"
"encoding/json"
"errors"
Expand Down Expand Up @@ -48,7 +47,7 @@ type AliasReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *AliasReconciler) Reconcile(ctx context.Context, alias *operatorv1alpha1.Alias) (ctrl.Result, error) {
logr := log.FromContext(ctx, "alias", alias.Name)
logr := log.FromContext(ctx, "namespace", alias.Namespace, "alias", alias.Name)

aliasOriginal := alias.DeepCopy()

Expand Down Expand Up @@ -79,21 +78,12 @@ func (r *AliasReconciler) Reconcile(ctx context.Context, alias *operatorv1alpha1
}

func (r *AliasReconciler) reconcile(ctx context.Context, alias *operatorv1alpha1.Alias) (ctrl.Result, error) {
logr := log.FromContext(ctx, "alias", alias.Name)
logr := log.FromContext(ctx, "namespace", alias.Namespace, "alias", alias.Name)

if r.ApiClient == nil {
api, err := mailu.NewClient(r.ApiURL, mailu.WithRequestEditorFn(func(ctx context.Context, req *http.Request) error {
req.Header.Add("Authorization", "Bearer "+r.ApiToken)
return nil
}), mailu.WithRequestEditorFn(func(ctx context.Context, req *http.Request) error {
// TODO: make this optional
body := make([]byte, 0)
if req.Body != nil {
body, _ = io.ReadAll(req.Body) //nolint:errcheck
req.Body = io.NopCloser(bytes.NewBuffer(body))
}
logr.Info(fmt.Sprintf("request %s %s: %s", req.Method, req.URL.Path, body))
return nil
}))
if err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -129,8 +119,7 @@ func (r *AliasReconciler) reconcile(ctx context.Context, alias *operatorv1alpha1
}

func (r *AliasReconciler) create(ctx context.Context, alias *operatorv1alpha1.Alias) (ctrl.Result, error) {
logr := log.FromContext(ctx, "alias", alias.Name)
logr.Info("create alias")
logr := log.FromContext(ctx, "namespace", alias.Namespace, "alias", alias.Name)

retry, err := r.createAlias(ctx, alias)
if err != nil {
Expand All @@ -143,14 +132,16 @@ func (r *AliasReconciler) create(ctx context.Context, alias *operatorv1alpha1.Al
return ctrl.Result{}, err
}

meta.SetStatusCondition(&alias.Status.Conditions, getAliasReadyCondition(metav1.ConditionTrue, "Created", "Alias created in MailU"))
if !retry {
meta.SetStatusCondition(&alias.Status.Conditions, getAliasReadyCondition(metav1.ConditionTrue, "Created", "Alias created in MailU"))
logr.Info("created alias")
}

return ctrl.Result{Requeue: retry}, nil
}

func (r *AliasReconciler) update(ctx context.Context, alias *operatorv1alpha1.Alias, apiAlias *mailu.Alias) (ctrl.Result, error) {
logr := log.FromContext(ctx, "alias", alias.Name)
logr.Info("update alias")
logr := log.FromContext(ctx, "namespace", alias.Namespace, "alias", alias.Name)

newAlias := mailu.Alias{
Email: alias.Spec.Name + "@" + alias.Spec.Domain,
Expand All @@ -166,7 +157,6 @@ func (r *AliasReconciler) update(ctx context.Context, alias *operatorv1alpha1.Al

if reflect.DeepEqual(jsonNew, jsonOld) {
meta.SetStatusCondition(&alias.Status.Conditions, getAliasReadyCondition(metav1.ConditionTrue, "Updated", "Alias updated in MailU"))
logr.Info("no update needed")
return ctrl.Result{}, nil
}

Expand All @@ -181,14 +171,16 @@ func (r *AliasReconciler) update(ctx context.Context, alias *operatorv1alpha1.Al
return ctrl.Result{}, err
}

meta.SetStatusCondition(&alias.Status.Conditions, getAliasReadyCondition(metav1.ConditionTrue, "Updated", "Alias updated in MailU"))
if !retry {
logr.Info("updated alias")
meta.SetStatusCondition(&alias.Status.Conditions, getAliasReadyCondition(metav1.ConditionTrue, "Updated", "Alias updated in MailU"))
}

return ctrl.Result{Requeue: retry}, nil
}

func (r *AliasReconciler) delete(ctx context.Context, alias *operatorv1alpha1.Alias) (ctrl.Result, error) {
logr := log.FromContext(ctx, "alias", alias.Name)
logr.Info("delete alias")
logr := log.FromContext(ctx, "namespace", alias.Namespace, "alias", alias.Name)

retry, err := r.deleteAlias(ctx, alias)
if err != nil {
Expand All @@ -201,6 +193,10 @@ func (r *AliasReconciler) delete(ctx context.Context, alias *operatorv1alpha1.Al
return ctrl.Result{}, err
}

if !retry {
logr.Info("deleted alias")
}

return ctrl.Result{Requeue: retry}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/controller/alias_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ var _ = Describe("Alias Controller", func() {

Expect(resAfterReconciliation.GetFinalizers()).To(HaveLen(1))
Expect(resAfterReconciliation.Status.Conditions).To(HaveLen(1))
Expect(meta.IsStatusConditionTrue(resAfterReconciliation.Status.Conditions, AliasConditionTypeReady)).To(BeTrue())
Expect(meta.IsStatusConditionTrue(resAfterReconciliation.Status.Conditions, AliasConditionTypeReady)).To(BeFalse())
Expect(result.Requeue).To(BeTrue())
})
})
Expand Down
38 changes: 17 additions & 21 deletions internal/controller/domain_controller.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package controller

import (
"bytes"
"context"
"encoding/json"
"errors"
Expand Down Expand Up @@ -48,7 +47,7 @@ type DomainReconciler struct {
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/[email protected]/pkg/reconcile
func (r *DomainReconciler) Reconcile(ctx context.Context, domain *operatorv1alpha1.Domain) (ctrl.Result, error) {
logr := log.FromContext(ctx, "domain", domain.Name)
logr := log.FromContext(ctx, "namespace", domain.Namespace, "domain", domain.Name)

domainOriginal := domain.DeepCopy()

Expand Down Expand Up @@ -79,21 +78,12 @@ func (r *DomainReconciler) Reconcile(ctx context.Context, domain *operatorv1alph
}

func (r *DomainReconciler) reconcile(ctx context.Context, domain *operatorv1alpha1.Domain) (ctrl.Result, error) {
logr := log.FromContext(ctx, "domain", domain.Name)
logr := log.FromContext(ctx, "namespace", domain.Namespace, "domain", domain.Name)

if r.ApiClient == nil {
api, err := mailu.NewClient(r.ApiURL, mailu.WithRequestEditorFn(func(ctx context.Context, req *http.Request) error {
req.Header.Add("Authorization", "Bearer "+r.ApiToken)
return nil
}), mailu.WithRequestEditorFn(func(ctx context.Context, req *http.Request) error {
// TODO: make this optional
body := make([]byte, 0)
if req.Body != nil {
body, _ = io.ReadAll(req.Body) //nolint:errcheck
req.Body = io.NopCloser(bytes.NewBuffer(body))
}
logr.Info(fmt.Sprintf("request %s %s: %s", req.Method, req.URL.Path, body))
return nil
}))
if err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -129,8 +119,7 @@ func (r *DomainReconciler) reconcile(ctx context.Context, domain *operatorv1alph
}

func (r *DomainReconciler) create(ctx context.Context, domain *operatorv1alpha1.Domain) (ctrl.Result, error) {
logr := log.FromContext(ctx, "domain", domain.Name)
logr.Info("create domain")
logr := log.FromContext(ctx, "namespace", domain.Namespace, "domain", domain.Name)

retry, err := r.createDomain(ctx, domain)
if err != nil {
Expand All @@ -143,14 +132,16 @@ func (r *DomainReconciler) create(ctx context.Context, domain *operatorv1alpha1.
return ctrl.Result{}, err
}

meta.SetStatusCondition(&domain.Status.Conditions, getDomainReadyCondition(metav1.ConditionTrue, "Created", "Domain created in MailU"))
if !retry {
meta.SetStatusCondition(&domain.Status.Conditions, getDomainReadyCondition(metav1.ConditionTrue, "Created", "Domain created in MailU"))
logr.Info("created domain")
}

return ctrl.Result{Requeue: retry}, nil
}

func (r *DomainReconciler) update(ctx context.Context, domain *operatorv1alpha1.Domain, apiDomain *mailu.Domain) (ctrl.Result, error) {
logr := log.FromContext(ctx, "domain", domain.Name)
logr.Info("update domain")
logr := log.FromContext(ctx, "namespace", domain.Namespace, "domain", domain.Name)

newDomain := mailu.Domain{
Name: domain.Spec.Name,
Expand All @@ -169,7 +160,6 @@ func (r *DomainReconciler) update(ctx context.Context, domain *operatorv1alpha1.

if reflect.DeepEqual(jsonNew, jsonOld) {
meta.SetStatusCondition(&domain.Status.Conditions, getDomainReadyCondition(metav1.ConditionTrue, "Updated", "Domain updated in MailU"))
logr.Info("no update needed")
return ctrl.Result{}, nil
}

Expand All @@ -184,14 +174,16 @@ func (r *DomainReconciler) update(ctx context.Context, domain *operatorv1alpha1.
return ctrl.Result{}, err
}

meta.SetStatusCondition(&domain.Status.Conditions, getDomainReadyCondition(metav1.ConditionTrue, "Updated", "Domain updated in MailU"))
if !retry {
meta.SetStatusCondition(&domain.Status.Conditions, getDomainReadyCondition(metav1.ConditionTrue, "Updated", "Domain updated in MailU"))
logr.Info("updated domain")
}

return ctrl.Result{Requeue: retry}, nil
}

func (r *DomainReconciler) delete(ctx context.Context, domain *operatorv1alpha1.Domain) (ctrl.Result, error) {
logr := log.FromContext(ctx, "domain", domain.Name)
logr.Info("delete domain")
logr := log.FromContext(ctx, "namespace", domain.Namespace, "domain", domain.Name)

retry, err := r.deleteDomain(ctx, domain)
if err != nil {
Expand All @@ -204,6 +196,10 @@ func (r *DomainReconciler) delete(ctx context.Context, domain *operatorv1alpha1.
return ctrl.Result{}, err
}

if !retry {
logr.Info("deleted domain")
}

return ctrl.Result{Requeue: retry}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/controller/domain_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ var _ = Describe("Domain Controller", func() {

Expect(resAfterReconciliation.GetFinalizers()).To(HaveLen(1))
Expect(resAfterReconciliation.Status.Conditions).To(HaveLen(1))
Expect(meta.IsStatusConditionTrue(resAfterReconciliation.Status.Conditions, DomainConditionTypeReady)).To(BeTrue())
Expect(meta.IsStatusConditionTrue(resAfterReconciliation.Status.Conditions, DomainConditionTypeReady)).To(BeFalse())
Expect(result.Requeue).To(BeTrue())
})
})
Expand Down
34 changes: 15 additions & 19 deletions internal/controller/user_controller.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package controller

import (
"bytes"
"context"
"encoding/json"
"errors"
Expand Down Expand Up @@ -90,15 +89,6 @@ func (r *UserReconciler) reconcile(ctx context.Context, user *operatorv1alpha1.U
api, err := mailu.NewClient(r.ApiURL, mailu.WithRequestEditorFn(func(ctx context.Context, req *http.Request) error {
req.Header.Add("Authorization", "Bearer "+r.ApiToken)
return nil
}), mailu.WithRequestEditorFn(func(ctx context.Context, req *http.Request) error {
// TODO: make this optional
body := make([]byte, 0)
if req.Body != nil {
body, _ = io.ReadAll(req.Body) //nolint:errcheck
req.Body = io.NopCloser(bytes.NewBuffer(body))
}
logr.Info(fmt.Sprintf("request %s %s: %s", req.Method, req.URL.Path, body))
return nil
}))
if err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -134,8 +124,7 @@ func (r *UserReconciler) reconcile(ctx context.Context, user *operatorv1alpha1.U
}

func (r *UserReconciler) create(ctx context.Context, user *operatorv1alpha1.User) (ctrl.Result, error) {
logr := log.FromContext(ctx, "user", user.Name)
logr.Info("create user")
logr := log.FromContext(ctx, "namespace", user.Namespace, "user", user.Name)

retry, err := r.createUser(ctx, user)
if err != nil {
Expand All @@ -148,14 +137,16 @@ func (r *UserReconciler) create(ctx context.Context, user *operatorv1alpha1.User
return ctrl.Result{}, err
}

meta.SetStatusCondition(&user.Status.Conditions, getUserReadyCondition(metav1.ConditionTrue, "Created", "User created in MailU"))
if !retry {
meta.SetStatusCondition(&user.Status.Conditions, getUserReadyCondition(metav1.ConditionTrue, "Created", "User created in MailU"))
logr.Info("created user")
}

return ctrl.Result{Requeue: retry}, nil
}

func (r *UserReconciler) update(ctx context.Context, user *operatorv1alpha1.User, apiUser *mailu.User) (ctrl.Result, error) {
logr := log.FromContext(ctx, "user", user.Name)
logr.Info("update user")
logr := log.FromContext(ctx, "namespace", user.Namespace, "user", user.Name)

newUser, err := r.userFromSpec(user.Spec)
if err != nil {
Expand All @@ -174,7 +165,6 @@ func (r *UserReconciler) update(ctx context.Context, user *operatorv1alpha1.User

if reflect.DeepEqual(jsonNew, jsonOld) {
meta.SetStatusCondition(&user.Status.Conditions, getUserReadyCondition(metav1.ConditionTrue, "Updated", "User updated in MailU"))
logr.Info("no update needed")
return ctrl.Result{}, nil
}

Expand All @@ -189,14 +179,16 @@ func (r *UserReconciler) update(ctx context.Context, user *operatorv1alpha1.User
return ctrl.Result{}, err
}

meta.SetStatusCondition(&user.Status.Conditions, getUserReadyCondition(metav1.ConditionTrue, "Updated", "User updated in MailU"))
if !retry {
meta.SetStatusCondition(&user.Status.Conditions, getUserReadyCondition(metav1.ConditionTrue, "Updated", "User updated in MailU"))
logr.Info("updated user")
}

return ctrl.Result{Requeue: retry}, nil
}

func (r *UserReconciler) delete(ctx context.Context, user *operatorv1alpha1.User) (ctrl.Result, error) {
logr := log.FromContext(ctx, "user", user.Name)
logr.Info("delete user")
logr := log.FromContext(ctx, "namespace", user.Namespace, "user", user.Name)

retry, err := r.deleteUser(ctx, user)
if err != nil {
Expand All @@ -209,6 +201,10 @@ func (r *UserReconciler) delete(ctx context.Context, user *operatorv1alpha1.User
return ctrl.Result{}, err
}

if !retry {
logr.Info("deleted user")
}

return ctrl.Result{Requeue: retry}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/controller/user_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ var _ = Describe("User Controller", func() {

Expect(resAfterReconciliation.GetFinalizers()).To(HaveLen(1))
Expect(resAfterReconciliation.Status.Conditions).To(HaveLen(1))
Expect(meta.IsStatusConditionTrue(resAfterReconciliation.Status.Conditions, UserConditionTypeReady)).To(BeTrue())
Expect(meta.IsStatusConditionTrue(resAfterReconciliation.Status.Conditions, UserConditionTypeReady)).To(BeFalse())
Expect(result.Requeue).To(BeTrue())
})
})
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ var _ = Describe("controller", Ordered, func() {
var err error

// projectimage stores the name of the image used in the example
var projectimage = "example.com/mailu-operator:v0.0.1"
var projectimage = "example.com/mailu-operator:e2e"

By("building the manager(Operator) image")
cmd := exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectimage))
Expand Down
Loading