Skip to content

Commit 3db4900

Browse files
committed
cleanup and use a CloseableDSLContext
1 parent fe5cd98 commit 3db4900

8 files changed

Lines changed: 148 additions & 155 deletions

File tree

src/main/java/it/aboutbits/postgresql/core/KubernetesUtil.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
import io.fabric8.kubernetes.client.KubernetesClient;
44
import it.aboutbits.postgresql.crd.connection.ClusterConnection;
5-
import jakarta.enterprise.context.RequestScoped;
65
import org.jspecify.annotations.NullMarked;
76

87
import java.util.Base64;
98

109
@NullMarked
11-
@RequestScoped
1210
public final class KubernetesUtil {
1311
public static final String SECRET_TYPE_BASIC_AUTH = "kubernetes.io/basic-auth";
1412
public static final String SECRET_DATA_BASIC_AUTH_USERNAME_KEY = "username";
@@ -88,4 +86,7 @@ public static Credentials getSecretRefCredentials(
8886
password
8987
);
9088
}
89+
90+
private KubernetesUtil() {
91+
}
9192
}

src/main/java/it/aboutbits/postgresql/core/PostgreSQLAuthenticationUtil.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import com.ongres.scram.common.StringPreparation;
44
import it.aboutbits.postgresql.crd.role.RoleSpec;
5-
import lombok.AccessLevel;
6-
import lombok.NoArgsConstructor;
75
import org.jooq.DSLContext;
86
import org.jspecify.annotations.NullMarked;
97

@@ -22,7 +20,6 @@
2220
import static it.aboutbits.postgresql.core.infrastructure.persistence.Tables.PG_AUTHID;
2321

2422
@NullMarked
25-
@NoArgsConstructor(access = AccessLevel.PRIVATE)
2623
public final class PostgreSQLAuthenticationUtil {
2724
private static final String MD5 = "MD5";
2825
private static final String SHA_256 = "SHA-256";
@@ -212,4 +209,7 @@ private static byte[] sha256(byte[] data) {
212209
throw new IllegalStateException("%s not available".formatted(SHA_256), e);
213210
}
214211
}
212+
213+
private PostgreSQLAuthenticationUtil() {
214+
}
215215
}

src/main/java/it/aboutbits/postgresql/core/PostgreSQLContextFactory.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,10 @@
33
import io.fabric8.kubernetes.client.KubernetesClient;
44
import it.aboutbits.postgresql.crd.connection.ClusterConnection;
55
import jakarta.enterprise.context.ApplicationScoped;
6-
import org.jooq.DSLContext;
7-
import org.jooq.SQLDialect;
6+
import org.jooq.CloseableDSLContext;
87
import org.jooq.impl.DSL;
98
import org.jspecify.annotations.NullMarked;
109

11-
import java.sql.DriverManager;
12-
import java.sql.SQLException;
1310
import java.util.Properties;
1411

1512
@NullMarked
@@ -24,7 +21,7 @@ public PostgreSQLContextFactory(KubernetesClient kubernetesClient) {
2421
this.kubernetesClient = kubernetesClient;
2522
}
2623

27-
public DSLContext getDSLContext(ClusterConnection clusterConnection) throws SQLException {
24+
public CloseableDSLContext getDSLContext(ClusterConnection clusterConnection) {
2825
var credentials = KubernetesUtil.getSecretRefCredentials(
2926
kubernetesClient,
3027
clusterConnection
@@ -55,14 +52,9 @@ public DSLContext getDSLContext(ClusterConnection clusterConnection) throws SQLE
5552
);
5653
}
5754

58-
var connection = DriverManager.getConnection(
55+
return DSL.using(
5956
jdbcUrl,
6057
properties
6158
);
62-
63-
return DSL.using(
64-
connection,
65-
SQLDialect.POSTGRES
66-
);
6759
}
6860
}

src/main/java/it/aboutbits/postgresql/crd/connection/ClusterConnectionReconciler.java

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@
99
import it.aboutbits.postgresql.core.PostgreSQLContextFactory;
1010
import lombok.RequiredArgsConstructor;
1111
import lombok.extern.slf4j.Slf4j;
12-
import org.jooq.DSLContext;
13-
import org.jooq.exception.DataAccessException;
1412
import org.jspecify.annotations.NonNull;
1513

16-
import java.sql.SQLException;
17-
1814
@Slf4j
1915
@RequiredArgsConstructor
2016
public class ClusterConnectionReconciler
@@ -29,26 +25,13 @@ public UpdateControl<ClusterConnection> reconcile(
2925
) {
3026
var status = initializeStatus(resource);
3127

32-
DSLContext dsl;
33-
try {
34-
dsl = contextFactory.getDSLContext(resource);
35-
} catch (SQLException e) {
36-
log.error("Failed to create DSL context", e);
37-
38-
return handleError(
39-
resource,
40-
status,
41-
e
42-
);
43-
}
44-
45-
try {
28+
try (var dsl = contextFactory.getDSLContext(resource)) {
4629
var version = dsl.fetchSingle("select version()").into(String.class);
4730

4831
status.setPhase(CRPhase.READY).setMessage(version);
4932

5033
return UpdateControl.patchStatus(resource);
51-
} catch (DataAccessException e) {
34+
} catch (Exception e) {
5235
log.error("Failed to check database connectivity", e);
5336

5437
return handleError(

src/main/java/it/aboutbits/postgresql/crd/role/RoleReconciler.java

Lines changed: 117 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
import it.aboutbits.postgresql.core.PostgreSQLContextFactory;
2020
import lombok.RequiredArgsConstructor;
2121
import lombok.extern.slf4j.Slf4j;
22+
import org.jooq.DSLContext;
2223
import org.jspecify.annotations.NonNull;
2324

24-
import java.sql.SQLException;
2525
import java.util.List;
2626
import java.util.concurrent.TimeUnit;
2727
import java.util.stream.Collectors;
@@ -94,104 +94,17 @@ public UpdateControl<Role> reconcile(
9494

9595
UpdateControl<Role> updateControl;
9696

97-
try {
97+
try (var dsl = contextFactory.getDSLContext(clusterConnection)) {
9898
// Run everything in a single transaction
99-
updateControl = contextFactory.getDSLContext(
100-
clusterConnection
101-
).transactionResult(cfg -> {
102-
// Get the transactional DSL context
103-
var tx = cfg.dsl();
104-
105-
// Create and return the role if it doesn't exist yet
106-
if (!RoleUtil.roleExists(tx, spec)) {
107-
log.info(
108-
"Creating Role [resource={}/{}]",
109-
namespace,
110-
name
111-
);
112-
113-
RoleUtil.createRole(
114-
tx,
115-
spec,
99+
updateControl = dsl.transactionResult(
100+
cfg -> reconcileInTransaction(
101+
cfg.dsl(),
102+
resource,
103+
status,
116104
password
117-
);
118-
119-
status.setPhase(CRPhase.READY)
120-
.setMessage(null);
121-
122-
return UpdateControl.patchStatus(resource);
123-
}
124-
125-
// When there is NOLOGIN, we set no password
126-
var passwordMatches = true;
127-
var roleLoginMatches = RoleUtil.roleLoginMatches(tx, spec);
128-
var currentFlags = RoleUtil.fetchCurrentFlags(tx, spec);
129-
var flagsMatch = expectedFlags.equals(currentFlags);
130-
var commentMatches = RoleUtil.roleCommentMatches(tx, spec);
131-
132-
if (loginExpected) {
133-
passwordMatches = PostgreSQLAuthenticationUtil.passwordMatches(
134-
tx,
135-
spec,
136-
password
137-
);
138-
}
139-
140-
if (roleLoginMatches && passwordMatches && flagsMatch && commentMatches) {
141-
log.info(
142-
"Role up-to-date [resource={}/{}]",
143-
namespace,
144-
name
145-
);
146-
147-
return UpdateControl.noUpdate();
148-
}
149-
150-
var changePassword = loginExpected && !passwordMatches;
151-
152-
log.info(
153-
"Updating Role [resource={}/{}]",
154-
namespace,
155-
name
156-
);
157-
158-
if (!roleLoginMatches || !passwordMatches || !flagsMatch) {
159-
RoleUtil.alterRole(
160-
tx,
161-
spec,
162-
changePassword,
163-
password
164-
);
165-
}
166-
167-
if (!flagsMatch) {
168-
log.info(
169-
"Updating Role membership [resource={}/{}]",
170-
namespace,
171-
name
172-
);
173-
174-
RoleUtil.reconcileRoleMembership(
175-
tx,
176-
spec,
177-
expectedFlags,
178-
currentFlags
179-
);
180-
}
181-
182-
if (!commentMatches) {
183-
RoleUtil.updateComment(
184-
tx,
185-
spec
186-
);
187-
}
188-
189-
status.setPhase(CRPhase.READY)
190-
.setMessage(null);
191-
192-
return UpdateControl.patchStatus(resource);
193-
});
194-
} catch (SQLException e) {
105+
)
106+
);
107+
} catch (Exception e) {
195108
return handleError(
196109
resource,
197110
status,
@@ -235,6 +148,111 @@ public List<EventSource<?, Role>> prepareEventSources(EventSourceContext<Role> c
235148
return List.of(secretEventSource);
236149
}
237150

151+
private UpdateControl<Role> reconcileInTransaction(
152+
DSLContext tx,
153+
Role resource,
154+
CRStatus status,
155+
String password
156+
) {
157+
var name = resource.getMetadata().getName();
158+
var namespace = resource.getMetadata().getNamespace();
159+
160+
var spec = resource.getSpec();
161+
var expectedFlags = spec.getFlags();
162+
163+
// Create and return the role if it doesn't exist yet
164+
if (!RoleUtil.roleExists(tx, spec)) {
165+
log.info(
166+
"Creating Role [resource={}/{}]",
167+
namespace,
168+
name
169+
);
170+
171+
RoleUtil.createRole(
172+
tx,
173+
spec,
174+
password
175+
);
176+
177+
status.setPhase(CRPhase.READY)
178+
.setMessage(null);
179+
180+
return UpdateControl.patchStatus(resource);
181+
}
182+
183+
// When there is NOLOGIN, we set no password
184+
var passwordMatches = true;
185+
var roleLoginMatches = RoleUtil.roleLoginMatches(tx, spec);
186+
var currentFlags = RoleUtil.fetchCurrentFlags(tx, spec);
187+
var flagsMatch = expectedFlags.equals(currentFlags);
188+
var commentMatches = RoleUtil.roleCommentMatches(tx, spec);
189+
190+
var passwordSecretRef = spec.getPasswordSecretRef();
191+
var loginExpected = passwordSecretRef != null;
192+
193+
if (loginExpected) {
194+
passwordMatches = PostgreSQLAuthenticationUtil.passwordMatches(
195+
tx,
196+
spec,
197+
password
198+
);
199+
}
200+
201+
if (roleLoginMatches && passwordMatches && flagsMatch && commentMatches) {
202+
log.info(
203+
"Role up-to-date [resource={}/{}]",
204+
namespace,
205+
name
206+
);
207+
208+
return UpdateControl.noUpdate();
209+
}
210+
211+
var changePassword = loginExpected && !passwordMatches;
212+
213+
log.info(
214+
"Updating Role [resource={}/{}]",
215+
namespace,
216+
name
217+
);
218+
219+
if (!roleLoginMatches || !passwordMatches || !flagsMatch) {
220+
RoleUtil.alterRole(
221+
tx,
222+
spec,
223+
changePassword,
224+
password
225+
);
226+
}
227+
228+
if (!flagsMatch) {
229+
log.info(
230+
"Updating Role membership [resource={}/{}]",
231+
namespace,
232+
name
233+
);
234+
235+
RoleUtil.reconcileRoleMembership(
236+
tx,
237+
spec,
238+
expectedFlags,
239+
currentFlags
240+
);
241+
}
242+
243+
if (!commentMatches) {
244+
RoleUtil.updateComment(
245+
tx,
246+
spec
247+
);
248+
}
249+
250+
status.setPhase(CRPhase.READY)
251+
.setMessage(null);
252+
253+
return UpdateControl.patchStatus(resource);
254+
}
255+
238256
@Override
239257
protected @NonNull CRStatus newStatus() {
240258
return new CRStatus();
@@ -257,7 +275,7 @@ private boolean isReferencedBy(
257275
var refName = ref.getName();
258276
var refNamespace = getResourceNamespaceOrOwn(role, ref.getNamespace());
259277

260-
return refName.equals(secret.getMetadata().getName()) &&
261-
refNamespace.equals(secret.getMetadata().getNamespace());
278+
return refName.equals(secret.getMetadata().getName())
279+
&& refNamespace.equals(secret.getMetadata().getNamespace());
262280
}
263281
}

0 commit comments

Comments
 (0)