1919import it .aboutbits .postgresql .core .PostgreSQLContextFactory ;
2020import lombok .RequiredArgsConstructor ;
2121import lombok .extern .slf4j .Slf4j ;
22+ import org .jooq .DSLContext ;
2223import org .jspecify .annotations .NonNull ;
2324
24- import java .sql .SQLException ;
2525import java .util .List ;
2626import java .util .concurrent .TimeUnit ;
2727import 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