Skip to content

Commit fb81c36

Browse files
committed
fix some issues
1 parent 3449915 commit fb81c36

7 files changed

Lines changed: 67 additions & 18 deletions

File tree

operator/src/main/java/it/aboutbits/postgresql/core/BaseReconciler.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ public <E extends Exception> UpdateControl<CR> handleError(
9191
S status,
9292
E exception
9393
) {
94+
log.error(
95+
"Failed to reconcile resource [resource={}]",
96+
resource.getMetadata().getName(),
97+
exception
98+
);
99+
94100
status.setPhase(CRPhase.ERROR)
95101
.setMessage(exception.getMessage());
96102

operator/src/main/java/it/aboutbits/postgresql/crd/grant/Grant.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package it.aboutbits.postgresql.crd.grant;
22

33
import com.fasterxml.jackson.annotation.JsonIgnore;
4+
import io.fabric8.crd.generator.annotation.AdditionalPrinterColumn;
45
import io.fabric8.kubernetes.api.model.Namespaced;
56
import io.fabric8.kubernetes.client.CustomResource;
67
import io.fabric8.kubernetes.model.annotation.Group;
@@ -20,6 +21,31 @@
2021
@NullMarked
2122
@Version("v1")
2223
@Group("postgresql.aboutbits.it")
24+
@AdditionalPrinterColumn(
25+
name = "Name",
26+
jsonPath = ".status.name",
27+
type = AdditionalPrinterColumn.Type.STRING
28+
)
29+
@AdditionalPrinterColumn(
30+
name = "Phase",
31+
jsonPath = ".status.phase",
32+
type = AdditionalPrinterColumn.Type.STRING
33+
)
34+
@AdditionalPrinterColumn(
35+
name = "Message",
36+
jsonPath = ".status.message",
37+
type = AdditionalPrinterColumn.Type.STRING
38+
)
39+
@AdditionalPrinterColumn(
40+
name = "Since",
41+
jsonPath = ".status.lastPhaseTransitionTime",
42+
type = AdditionalPrinterColumn.Type.DATE
43+
)
44+
@AdditionalPrinterColumn(
45+
name = "Age",
46+
jsonPath = ".metadata.creationTimestamp",
47+
type = AdditionalPrinterColumn.Type.DATE
48+
)
2349
public class Grant
2450
extends CustomResource<GrantSpec, CRStatus>
2551
implements Namespaced, Named {

operator/src/main/java/it/aboutbits/postgresql/crd/grant/GrantObjectType.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package it.aboutbits.postgresql.crd.grant;
22

3+
import com.fasterxml.jackson.annotation.JsonValue;
34
import lombok.Getter;
45
import lombok.experimental.Accessors;
56
import org.jooq.Keyword;
@@ -78,6 +79,11 @@ public enum GrantObjectType {
7879
this.privilegesSet = Set.copyOf(privileges);
7980
}
8081

82+
@JsonValue
83+
public String toValue() {
84+
return name().toLowerCase(Locale.ROOT);
85+
}
86+
8187
public Keyword objectType() {
8288
return keyword(
8389
name().toLowerCase(Locale.ROOT)

operator/src/main/java/it/aboutbits/postgresql/crd/grant/GrantPrivilege.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package it.aboutbits.postgresql.crd.grant;
22

3+
import com.fasterxml.jackson.annotation.JsonValue;
34
import org.jooq.Privilege;
45
import org.jooq.impl.DSL;
56
import org.jspecify.annotations.NullMarked;
@@ -26,6 +27,11 @@ public enum GrantPrivilege {
2627
USAGE,
2728
MAINTAIN;
2829

30+
@JsonValue
31+
public String toValue() {
32+
return name().toLowerCase(Locale.ROOT);
33+
}
34+
2935
public Privilege privilege() {
3036
return DSL.privilege(
3137
name().toLowerCase(Locale.ROOT)

operator/src/main/java/it/aboutbits/postgresql/crd/grant/GrantReconciler.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,11 @@ private UpdateControl<Grant> reconcileInTransaction(
185185
}
186186

187187
// If we are not in the "ALL" mode, e.g. objects is an empty List, do explicit grants
188-
if (!isAllMode) {
188+
// We need to exclude objectType's DATABASE and SCHEMA as the CRD doesn't alllow to specify objects there
189+
if (!isAllMode
190+
|| objectType == GrantObjectType.DATABASE
191+
|| objectType == GrantObjectType.SCHEMA
192+
) {
189193
// Calculate Grants: Expected - Current
190194
var privilegesToGrant = new HashSet<>(expectedPrivileges);
191195
privilegesToGrant.removeAll(currentPrivileges);

operator/src/main/java/it/aboutbits/postgresql/crd/grant/GrantService.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import static it.aboutbits.postgresql.core.infrastructure.persistence.Tables.PG_CLASS;
2020
import static it.aboutbits.postgresql.core.infrastructure.persistence.Tables.PG_DATABASE;
2121
import static it.aboutbits.postgresql.core.infrastructure.persistence.Tables.PG_NAMESPACE;
22+
import static it.aboutbits.postgresql.crd.grant.GrantObjectType.SEQUENCE;
23+
import static it.aboutbits.postgresql.crd.grant.GrantObjectType.TABLE;
2224
import static org.jooq.impl.DSL.field;
2325
import static org.jooq.impl.DSL.noCondition;
2426
import static org.jooq.impl.DSL.query;
@@ -82,7 +84,7 @@ public Map<String, Set<GrantPrivilege>> determineCurrentObjectPrivileges(
8284
)
8385
.fetchGroups(
8486
PG_DATABASE.DATNAME,
85-
GrantPrivilege.class
87+
r -> r.get(ACLEXPLODE.PRIVILEGE_TYPE, GrantPrivilege.class)
8688
);
8789
/*
8890
* select
@@ -111,7 +113,7 @@ public Map<String, Set<GrantPrivilege>> determineCurrentObjectPrivileges(
111113
)
112114
.fetchGroups(
113115
PG_NAMESPACE.NSPNAME,
114-
GrantPrivilege.class
116+
r -> r.get(PG_NAMESPACE.NSPNAME, GrantPrivilege.class)
115117
);
116118

117119
/*
@@ -154,7 +156,7 @@ public Map<String, Set<GrantPrivilege>> determineCurrentObjectPrivileges(
154156
)
155157
.fetchGroups(
156158
PG_CLASS.RELNAME,
157-
GrantPrivilege.class
159+
r -> r.get(ACLEXPLODE.PRIVILEGE_TYPE, GrantPrivilege.class)
158160
);
159161
/*
160162
* select
@@ -192,7 +194,7 @@ public Map<String, Set<GrantPrivilege>> determineCurrentObjectPrivileges(
192194
)
193195
.fetchGroups(
194196
PG_CLASS.RELNAME,
195-
GrantPrivilege.class
197+
r -> r.get(ACLEXPLODE.PRIVILEGE_TYPE, GrantPrivilege.class)
196198
);
197199
};
198200

@@ -232,7 +234,7 @@ public Map<String, Set<GrantPrivilege>> determineCurrentObjectPrivileges(
232234
(objects.isEmpty() ? 1 : objects.size()) * spec.getPrivileges().size()
233235
);
234236

235-
var allMode = objects.isEmpty();
237+
var isAllMode = objects.isEmpty();
236238

237239
switch (objectType) {
238240
case DATABASE -> {
@@ -360,15 +362,15 @@ public Map<String, Set<GrantPrivilege>> determineCurrentObjectPrivileges(
360362
OID_DATA_TYPE,
361363
val(schema)
362364
)),
363-
allMode ? noCondition() : PG_CLASS.RELNAME.in(objects),
365+
isAllMode ? noCondition() : PG_CLASS.RELNAME.in(objects),
364366
// See https://www.postgresql.org/docs/current/catalog-pg-class.html#CATALOG-PG-CLASS
365367
PG_CLASS.RELKIND.eq(
366368
"S" // Sequence
367369
)
368370
)
369371
.fetchMap(PG_CLASS.RELNAME, isOwnerCondition);
370372

371-
if (allMode) {
373+
if (isAllMode) {
372374
objectExistenceAndOwnershipMap.putAll(existingObjectsOwner);
373375
} else {
374376
for (var object : objects) {
@@ -394,7 +396,7 @@ public void grant(
394396
DSLContext tx,
395397
Grant resource,
396398
String object,
397-
Set<GrantPrivilege> privilegesToRevoke
399+
Set<GrantPrivilege> privilegesToGrant
398400
) {
399401
var spec = resource.getSpec();
400402

@@ -407,7 +409,7 @@ public void grant(
407409
default -> quotedName(object);
408410
};
409411

410-
var privileges = privilegesToRevoke.stream()
412+
var privileges = privilegesToGrant.stream()
411413
.map(GrantPrivilege::privilege)
412414
.toList();
413415

@@ -425,7 +427,7 @@ public void grant(
425427
public void grantOnAll(
426428
DSLContext tx,
427429
Grant resource,
428-
Set<GrantPrivilege> privilegesToRevoke
430+
Set<GrantPrivilege> privilegesToGrant
429431
) {
430432
var spec = resource.getSpec();
431433

@@ -435,11 +437,11 @@ public void grantOnAll(
435437

436438
// grant <privileges> on all <objectType>s in schema <schema> to <role>;
437439
// is only supported by TABLE, SEQUENCE, FUNCTION, PROCEDURE and ROUTINE
438-
if (objectType != GrantObjectType.TABLE && objectType != GrantObjectType.SEQUENCE) {
440+
if (objectType != TABLE && objectType != SEQUENCE) {
439441
return;
440442
}
441443

442-
var privileges = privilegesToRevoke.stream()
444+
var privileges = privilegesToGrant.stream()
443445
.map(GrantPrivilege::privilege)
444446
.toList();
445447

@@ -464,10 +466,9 @@ public void revoke(
464466

465467
var schema = spec.getSchema();
466468
var role = role(spec.getRole());
467-
var objectTypeEnum = spec.getObjectType();
468-
var objectType = objectTypeEnum.objectType();
469+
var objectType = spec.getObjectType();
469470

470-
var qualifiedObject = switch (objectTypeEnum) {
471+
var qualifiedObject = switch (objectType) {
471472
case TABLE, SEQUENCE -> quotedName(schema, object);
472473
default -> quotedName(object);
473474
};
@@ -479,7 +480,7 @@ public void revoke(
479480
var statement = query(
480481
"revoke {0} on {1} {2} from {3}",
481482
SQLUtil.concatenateQueryPartsWithComma(privileges),
482-
objectType,
483+
objectType.objectType(),
483484
qualifiedObject,
484485
role
485486
);

operator/src/main/java/it/aboutbits/postgresql/crd/grant/GrantSpec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
message = "The Grant schema must be not set if objectType is 'database', for all other objectType's it is required."
2020
)
2121
@ValidationRule(
22-
value = "self.objectType in ['database', 'schema'] ? !has(self.objects) : (has(self.objects) && self.objects.size() > 0)",
22+
value = "self.objectType in ['database', 'schema'] ? !has(self.objects) : has(self.objects)",
2323
message = "The Grant objects must be not set if objectType is 'database' or 'schema', for all other objectType's it is required."
2424
)
2525
public class GrantSpec {

0 commit comments

Comments
 (0)