Skip to content
Open
15 changes: 13 additions & 2 deletions pkg/psmdb/tls/certmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tls

import (
"context"
"fmt"
"regexp"
"time"

Expand Down Expand Up @@ -118,7 +119,12 @@ func (c *certManagerController) DeleteDeprecatedIssuerIfExists(ctx context.Conte

func (c *certManagerController) createOrUpdate(ctx context.Context, cr *api.PerconaServerMongoDB, obj client.Object) (util.ApplyStatus, error) {
if err := controllerutil.SetControllerReference(cr, obj, c.scheme); err != nil {
return "", errors.Wrap(err, "set controller reference")
switch errors.Cause(err).(type) {
case *controllerutil.AlreadyOwnedError:
fmt.Sprintf("%s", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we return error here?

default:
return "", errors.Wrap(err, "set controller reference")
}
}

status, err := util.Apply(ctx, c.cl, obj)
Expand Down Expand Up @@ -314,7 +320,12 @@ func (c *certManagerController) WaitForCerts(ctx context.Context, cr *api.Percon
continue
}
if err = controllerutil.SetControllerReference(cr, secret, c.scheme); err != nil {
return errors.Wrap(err, "set controller reference")
switch errors.Cause(err).(type) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gkech wdyt of this errors.Cause maybe we should check with errors.Is?

Copy link
Contributor

@gkech gkech Mar 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes it is better @egegunes

@Demch1k let's use errors.Is and also, let's drop switch since it is not needed, so the following for all cases.

if err = controllerutil.SetControllerReference(cr, secret, c.scheme); err != nil {
if errors.Is(err, &controllerutil.AlreadyOwnedError{}) {
	return errors.Wrap(err, "set owner reference")
}
return errors.Wrap(err, "set controller reference")
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Demch1k any updates on this one?

case *controllerutil.AlreadyOwnedError:
fmt.Sprintf("%s", err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we return error here?

default:
return errors.Wrap(err, "set controller reference")
}
}
if err = c.cl.Update(ctx, secret); err != nil {
return errors.Wrap(err, "failed to update secret")
Expand Down
Loading