Skip to content

Commit

Permalink
Tiny refactor for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
evyatarmeged committed May 6, 2024
1 parent 2f1e8e6 commit 0ce55c2
Showing 1 changed file with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,23 @@ const (
OtterizeIntentsClientIndexNameField = "spec.service.name"
)

type Reconciler struct {
type DatabaseUserReconciler struct {
client client.Client
scheme *runtime.Scheme
recorder record.EventRecorder
serviceIdResolver *serviceidresolver.Resolver
}

func NewReconciler(client client.Client, scheme *runtime.Scheme, eventRecorder record.EventRecorder, serviceIdResolver *serviceidresolver.Resolver) *Reconciler {
return &Reconciler{
func NewReconciler(client client.Client, scheme *runtime.Scheme, eventRecorder record.EventRecorder, serviceIdResolver *serviceidresolver.Resolver) *DatabaseUserReconciler {
return &DatabaseUserReconciler{
client: client,
scheme: scheme,
serviceIdResolver: serviceIdResolver,
recorder: eventRecorder,
}
}

func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *DatabaseUserReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
WithOptions(controller.Options{RecoverPanic: lo.ToPtr(true)}).
For(&otterizev1alpha3.ClientIntents{}).
Expand All @@ -62,7 +62,7 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
Complete(r)
}

func (r *Reconciler) mapPodToClientIntents(ctx context.Context, obj client.Object) []reconcile.Request {
func (r *DatabaseUserReconciler) mapPodToClientIntents(ctx context.Context, obj client.Object) []reconcile.Request {
requests := make([]reconcile.Request, 0)
pod := obj.(*v1.Pod)
if !pod.DeletionTimestamp.IsZero() || isPodStatusDone(pod) {
Expand Down Expand Up @@ -102,7 +102,7 @@ func isPodStatusDone(pod *v1.Pod) bool {
return pod.Status.Phase == v1.PodSucceeded || pod.Status.Phase == v1.PodFailed
}

func (r *Reconciler) mapPGServerConfToClientIntents(ctx context.Context, obj client.Object) []reconcile.Request {
func (r *DatabaseUserReconciler) mapPGServerConfToClientIntents(ctx context.Context, obj client.Object) []reconcile.Request {
pgServerConf := obj.(*otterizev1alpha3.PostgreSQLServerConfig)
logrus.Infof("Enqueueing client intents for PostgreSQLServerConfig change %s", pgServerConf.Name)

Expand Down Expand Up @@ -130,7 +130,9 @@ func (r *Reconciler) mapPGServerConfToClientIntents(ctx context.Context, obj cli
return requests
}

func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
func (r *DatabaseUserReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
// The credentials operator doesn't have any cleanup to do, like removing permissions or deleting users
// If a namespace is being deleted, we can stop reconciliation and let the intents operator handle everything else
terminating, err := r.isNamespaceTerminating(ctx, req.Namespace)
if err != nil {
return ctrl.Result{}, errors.Wrap(err)
Expand Down Expand Up @@ -182,7 +184,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
return ctrl.Result{}, nil
}

func (r *Reconciler) createPostgresUserForWorkload(
func (r *DatabaseUserReconciler) createPostgresUserForWorkload(
ctx context.Context,
pgConfigurator *postgres.PostgresConfigurator,
pgUsername string,
Expand All @@ -201,7 +203,7 @@ func (r *Reconciler) createPostgresUserForWorkload(
return nil
}

func (r *Reconciler) getPostgresUserForWorkload(ctx context.Context, clientName, namespace string) (string, error) {
func (r *DatabaseUserReconciler) getPostgresUserForWorkload(ctx context.Context, clientName, namespace string) (string, error) {
clusterUID, err := clusterutils.GetClusterUID(ctx)
if err != nil {
return "", errors.Wrap(err)
Expand All @@ -210,7 +212,7 @@ func (r *Reconciler) getPostgresUserForWorkload(ctx context.Context, clientName,
return clusterutils.KubernetesToPostgresName(username), nil
}

func (r *Reconciler) fetchWorkloadPassword(ctx context.Context, clientIntents otterizev1alpha3.ClientIntents) (string, error) {
func (r *DatabaseUserReconciler) fetchWorkloadPassword(ctx context.Context, clientIntents otterizev1alpha3.ClientIntents) (string, error) {
pod, err := r.serviceIdResolver.ResolveClientIntentToPod(ctx, clientIntents)
if err != nil {
return "", errors.Wrap(err)
Expand All @@ -231,7 +233,7 @@ func (r *Reconciler) fetchWorkloadPassword(ctx context.Context, clientIntents ot
return string(secret.Data["password"]), nil
}

func (r *Reconciler) InitIntentsClientIndices(mgr ctrl.Manager) error {
func (r *DatabaseUserReconciler) InitIntentsClientIndices(mgr ctrl.Manager) error {
err := mgr.GetCache().IndexField(
context.Background(),
&otterizev1alpha3.ClientIntents{}, OtterizeIntentsClientIndexNameField,
Expand All @@ -246,7 +248,7 @@ func (r *Reconciler) InitIntentsClientIndices(mgr ctrl.Manager) error {
return nil
}

func (r *Reconciler) InitIntentsDatabaseServerIndices(mgr ctrl.Manager) error {
func (r *DatabaseUserReconciler) InitIntentsDatabaseServerIndices(mgr ctrl.Manager) error {
err := mgr.GetCache().IndexField(
context.Background(),
&otterizev1alpha3.ClientIntents{},
Expand All @@ -271,7 +273,7 @@ func (r *Reconciler) InitIntentsDatabaseServerIndices(mgr ctrl.Manager) error {
return nil
}

func (r *Reconciler) handleDBUserCreation(
func (r *DatabaseUserReconciler) handleDBUserCreation(
ctx context.Context,
intents otterizev1alpha3.ClientIntents,
pgConfigurator *postgres.PostgresConfigurator,
Expand Down Expand Up @@ -311,10 +313,7 @@ func (r *Reconciler) handleDBUserCreation(
return nil
}

func (r *Reconciler) isNamespaceTerminating(ctx context.Context, namespace string) (bool, error) {
// The credentials operator doesn't have any cleanup to do, like removing permissions or deleting users
// If a namespace is being deleted, we can stop reconciliation and let the intents operator handle everything else

func (r *DatabaseUserReconciler) isNamespaceTerminating(ctx context.Context, namespace string) (bool, error) {
ns := v1.Namespace{}
if err := r.client.Get(ctx, types.NamespacedName{Name: namespace}, &ns); err != nil {
return false, errors.Wrap(err)
Expand Down

0 comments on commit 0ce55c2

Please sign in to comment.