Skip to content

Commit 0f2d99e

Browse files
committed
controllers: Refactored Postgres connection setup to utils.
This is expected to be reused for future Postgres resources.
1 parent fbccb7e commit 0f2d99e

File tree

4 files changed

+82
-40
lines changed

4 files changed

+82
-40
lines changed

controllers/postgresconfig_controller.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,10 @@ import (
3434
)
3535

3636
const (
37-
EventTypeMissingSecret string = "MissingSecret"
38-
EventTypeFailedConnectPostgres string = "FailedConnectPostgres"
39-
EventTypeSuccessConnectPostgres string = "SuccessConnectPostgres"
40-
EventTypeFailedReconcile string = "FailedReconcile"
41-
EventTypeSuccessfulReconcile string = "SuccessfulReconcile"
37+
EventTypeFailedSetupPostgresConnection string = "FailedSetupPostgresConnection"
38+
EventTypeSuccessConnectPostgres string = "SuccessConnectPostgres"
39+
EventTypeFailedReconcile string = "FailedReconcile"
40+
EventTypeSuccessfulReconcile string = "SuccessfulReconcile"
4241
)
4342

4443
var (

controllers/postgrespublication_controller.go

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@ package controllers
1919
import (
2020
"context"
2121
"fmt"
22-
"net/url"
2322

2423
"github.com/go-logr/logr"
2524
"github.com/jackc/pgx/v4"
2625
corev1 "k8s.io/api/core/v1"
2726
"k8s.io/apimachinery/pkg/runtime"
28-
"k8s.io/apimachinery/pkg/types"
2927
"k8s.io/client-go/tools/record"
3028
ctrl "sigs.k8s.io/controller-runtime"
3129
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -71,43 +69,19 @@ func (r *PostgresPublicationReconciler) Reconcile(ctx context.Context, req ctrl.
7169
return ctrl.Result{}, client.IgnoreNotFound(err)
7270
}
7371

74-
secretNamespacedName := types.NamespacedName{
75-
Name: publication.Spec.PostgresRef.SecretRef.SecretName,
76-
Namespace: publication.GetNamespace(),
77-
}
78-
secretRef := &corev1.Secret{}
79-
if err := r.Get(ctx, secretNamespacedName, secretRef); err != nil {
80-
r.recorder.Eventf(
81-
publication,
82-
corev1.EventTypeWarning,
83-
EventTypeMissingSecret,
84-
"failed to get Secret: %v",
85-
err,
86-
)
87-
return ctrl.Result{Requeue: true}, nil
88-
}
89-
90-
connURL := url.URL{
91-
Scheme: "postgres",
92-
User: url.UserPassword(
93-
string(secretRef.Data["POSTGRES_USER"]),
94-
string(secretRef.Data["POSTGRES_PASSWORD"]),
95-
),
96-
Host: fmt.Sprintf(
97-
"%s:%d",
98-
publication.Spec.PostgresRef.Host,
99-
publication.Spec.PostgresRef.Port,
100-
),
101-
RawPath: publication.Spec.PostgresRef.Database,
102-
}
103-
conn, err := pgx.Connect(ctx, connURL.String())
72+
conn, err := utils.SetupPostgresConnection(
73+
ctx,
74+
r,
75+
r.recorder,
76+
publication.Spec.PostgresRef,
77+
publication.ObjectMeta,
78+
)
10479
if err != nil {
10580
r.recorder.Eventf(
10681
publication,
10782
corev1.EventTypeWarning,
108-
EventTypeFailedConnectPostgres,
109-
"failed to connect to PostgreSQL: %v",
110-
err,
83+
EventTypeFailedSetupPostgresConnection,
84+
err.Error(),
11185
)
11286
return ctrl.Result{Requeue: true}, nil
11387
}
@@ -149,6 +123,13 @@ func (r *PostgresPublicationReconciler) Reconcile(ctx context.Context, req ctrl.
149123
return reconcileResult, nil
150124
}
151125

126+
r.recorder.Event(
127+
publication,
128+
corev1.EventTypeNormal,
129+
EventTypeSuccessfulReconcile,
130+
"successfully reconcilled publication",
131+
)
132+
152133
return ctrl.Result{}, nil
153134
}
154135

controllers/utils/errors.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package utils
2+
3+
import "fmt"
4+
5+
type ErrGetSecret struct {
6+
Err error
7+
}
8+
9+
func (e *ErrGetSecret) Error() string {
10+
return fmt.Sprintf("failed to get Secret: %s", e.Err.Error())
11+
}
12+
13+
type ErrFailedConnectPostgres struct {
14+
Err error
15+
}
16+
17+
func (e *ErrFailedConnectPostgres) Error() string {
18+
return fmt.Sprintf("failed to connect to PostgreSQL: %s", e.Err.Error())
19+
}

controllers/utils/utils.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,19 @@ package utils
22

33
import (
44
"context"
5+
"fmt"
6+
"net/url"
57

68
"github.com/go-logr/logr"
9+
"github.com/jackc/pgx/v4"
10+
corev1 "k8s.io/api/core/v1"
11+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
"k8s.io/apimachinery/pkg/types"
13+
"k8s.io/client-go/tools/record"
714
ctrl "sigs.k8s.io/controller-runtime"
15+
"sigs.k8s.io/controller-runtime/pkg/client"
16+
17+
postgresv1alpha1 "github.com/glints-dev/postgres-config-operator/api/v1alpha1"
818
)
919

1020
// contextKey represents a value that will be used as a key in Go's context API.
@@ -14,6 +24,39 @@ const (
1424
contextKeyRequestLogger contextKey = "request-logger"
1525
)
1626

27+
func SetupPostgresConnection(
28+
ctx context.Context,
29+
r client.Reader,
30+
recorder record.EventRecorder,
31+
ref postgresv1alpha1.PostgresRef,
32+
obj metav1.ObjectMeta,
33+
) (*pgx.Conn, error) {
34+
secretNamespacedName := types.NamespacedName{
35+
Name: ref.SecretRef.SecretName,
36+
Namespace: obj.GetNamespace(),
37+
}
38+
secretRef := &corev1.Secret{}
39+
if err := r.Get(ctx, secretNamespacedName, secretRef); err != nil {
40+
return nil, &ErrGetSecret{Err: err}
41+
}
42+
43+
connURL := url.URL{
44+
Scheme: "postgres",
45+
User: url.UserPassword(
46+
string(secretRef.Data["POSTGRES_USER"]),
47+
string(secretRef.Data["POSTGRES_PASSWORD"]),
48+
),
49+
Host: fmt.Sprintf("%s:%d", ref.Host, ref.Port),
50+
RawPath: ref.Database,
51+
}
52+
conn, err := pgx.Connect(ctx, connURL.String())
53+
if err != nil {
54+
return nil, &ErrFailedConnectPostgres{Err: err}
55+
}
56+
57+
return conn, nil
58+
}
59+
1760
// WithRequestLogger returns a context that has a request-scoped logger attached
1861
// to it.
1962
func WithRequestLogger(

0 commit comments

Comments
 (0)