Skip to content

Commit 0656964

Browse files
committed
feat: include the node name of each Pod in the SGCluster status
1 parent f6b40b7 commit 0656964

6 files changed

Lines changed: 52 additions & 7 deletions

File tree

stackgres-k8s/src/cluster-controller/src/main/java/io/stackgres/cluster/controller/ClusterControllerReconciliator.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.util.ArrayList;
99
import java.util.List;
10+
import java.util.Objects;
1011
import java.util.Optional;
1112

1213
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@@ -45,6 +46,7 @@ public class ClusterControllerReconciliator
4546
private final PatroniBackupFailoverRestartReconciliator patroniBackupFailoverRestartReconciliator;
4647
private final ClusterControllerPropertyContext propertyContext;
4748
private final String podName;
49+
private final String nodeName;
4850

4951
@Inject
5052
public ClusterControllerReconciliator(Parameters parameters) {
@@ -63,6 +65,8 @@ public ClusterControllerReconciliator(Parameters parameters) {
6365
this.propertyContext = parameters.propertyContext;
6466
this.podName = parameters.propertyContext
6567
.getString(ClusterControllerProperty.CLUSTER_CONTROLLER_POD_NAME);
68+
this.nodeName = parameters.propertyContext
69+
.getString(ClusterControllerProperty.CLUSTER_CONTROLLER_NODE_NAME);
6670
}
6771

6872
public ClusterControllerReconciliator() {
@@ -82,6 +86,7 @@ public ClusterControllerReconciliator() {
8286
this.patroniBackupFailoverRestartReconciliator = null;
8387
this.propertyContext = null;
8488
this.podName = null;
89+
this.nodeName = null;
8590
}
8691

8792
@SuppressFBWarnings(value = "REC_CATCH_EXCEPTION",
@@ -90,13 +95,19 @@ public ClusterControllerReconciliator() {
9095
public ReconciliationResult<Void> reconcile(KubernetesClient client,
9196
StackGresClusterContext context) throws Exception {
9297
final StackGresCluster cluster = context.getCluster();
93-
final boolean podStatusMissing = Optional.ofNullable(cluster.getStatus())
98+
final Optional<StackGresClusterPodStatus> foundPodStatus =
99+
Optional.ofNullable(cluster.getStatus())
94100
.map(StackGresClusterStatus::getPodStatuses)
95101
.stream()
96102
.flatMap(List::stream)
97-
.map(StackGresClusterPodStatus::getName)
98-
.noneMatch(podName::equals);
99-
if (podStatusMissing) {
103+
.filter(podStatus -> Objects.equals(podName, podStatus.getName()))
104+
.findFirst();
105+
final boolean nodeNameChanged;
106+
if (foundPodStatus.isPresent()) {
107+
nodeNameChanged = Objects.equals(foundPodStatus.get().getNodeName(), nodeName);
108+
foundPodStatus.get().setNodeName(nodeName);
109+
} else {
110+
nodeNameChanged = true;
100111
if (cluster.getStatus() == null) {
101112
cluster.setStatus(new StackGresClusterStatus());
102113
}
@@ -105,6 +116,7 @@ public ReconciliationResult<Void> reconcile(KubernetesClient client,
105116
}
106117
StackGresClusterPodStatus podStatus = new StackGresClusterPodStatus();
107118
podStatus.setName(podName);
119+
podStatus.setNodeName(nodeName);
108120
podStatus.setPrimary(false);
109121
podStatus.setPendingRestart(false);
110122
cluster.getStatus().getPodStatuses().add(podStatus);
@@ -131,7 +143,8 @@ public ReconciliationResult<Void> reconcile(KubernetesClient client,
131143
ReconciliationResult<Void> patroniBackupFailoverRestartReconciliatorResult =
132144
patroniBackupFailoverRestartReconciliator.reconcile(client, context);
133145

134-
if (podStatusMissing
146+
if (foundPodStatus.isEmpty()
147+
|| nodeNameChanged
135148
|| postgresBootstrapReconciliatorResult.result().orElse(false)
136149
|| extensionReconciliationResult.result().orElse(false)
137150
|| patroniReconciliationResult.result().orElse(false)) {

stackgres-k8s/src/common/src/main/java/io/stackgres/common/ClusterControllerProperty.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public enum ClusterControllerProperty implements StackGresPropertyReader {
1313
CLUSTER_NAME("stackgres.clusterName"),
1414
CLUSTER_CONTROLLER_POD_NAME("stackgres.clusterControllerPodName"),
1515
CLUSTER_CONTROLLER_POD_IP("stackgres.clusterControllerPodIp"),
16+
CLUSTER_CONTROLLER_NODE_NAME("stackgres.clusterControllerNodeName"),
1617
CLUSTER_CONTROLLER_EXTENSIONS_REPOSITORY_URLS(
1718
"stackgres.clusterControllerExtensionsRepositoryUrls"),
1819
CLUSTER_CONTROLLER_SKIP_OVERWRITE_SHARED_LIBRARIES(

stackgres-k8s/src/common/src/main/java/io/stackgres/common/crd/sgcluster/StackGresClusterPodStatus.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class StackGresClusterPodStatus {
2828

2929
private String name;
3030

31+
private String nodeName;
32+
3133
private Integer replicationGroup;
3234

3335
private Boolean primary;
@@ -53,6 +55,14 @@ public void setName(String name) {
5355
this.name = name;
5456
}
5557

58+
public String getNodeName() {
59+
return nodeName;
60+
}
61+
62+
public void setNodeName(String nodeName) {
63+
this.nodeName = nodeName;
64+
}
65+
5666
public Integer getReplicationGroup() {
5767
return replicationGroup;
5868
}
@@ -88,7 +98,7 @@ public void setInstalledPostgresExtensions(
8898

8999
@Override
90100
public int hashCode() {
91-
return Objects.hash(installedPostgresExtensions, name, pendingRestart, primary,
101+
return Objects.hash(installedPostgresExtensions, name, nodeName, pendingRestart, primary,
92102
replicationGroup);
93103
}
94104

@@ -102,7 +112,8 @@ public boolean equals(Object obj) {
102112
}
103113
StackGresClusterPodStatus other = (StackGresClusterPodStatus) obj;
104114
return Objects.equals(installedPostgresExtensions, other.installedPostgresExtensions)
105-
&& Objects.equals(name, other.name) && Objects.equals(pendingRestart, other.pendingRestart)
115+
&& Objects.equals(name, other.name) && Objects.equals(nodeName, other.nodeName)
116+
&& Objects.equals(pendingRestart, other.pendingRestart)
106117
&& Objects.equals(primary, other.primary)
107118
&& Objects.equals(replicationGroup, other.replicationGroup);
108119
}

stackgres-k8s/src/common/src/main/resources/crds/SGCluster.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2140,6 +2140,9 @@ spec:
21402140
name:
21412141
type: string
21422142
description: The name of the pod.
2143+
nodeName:
2144+
type: string
2145+
description: The node name where the pod is running.
21432146
replicationGroup:
21442147
type: integer
21452148
description: Indicates the replication group this Pod belongs to.

stackgres-k8s/src/operator/src/main/java/io/stackgres/operator/conciliation/factory/cluster/sidecars/controller/ClusterController.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ public Container getContainer(ClusterContainerContext context) {
101101
.build())
102102
.build(),
103103
new EnvVarBuilder()
104+
.withName(ClusterControllerProperty.CLUSTER_CONTROLLER_NODE_NAME
105+
.getEnvironmentVariableName())
106+
.withValueFrom(new EnvVarSourceBuilder()
107+
.withFieldRef(new ObjectFieldSelector("v1", "spec.nodeName"))
108+
.build())
109+
.build(),
110+
new EnvVarBuilder()
104111
.withName(ClusterControllerProperty.CLUSTER_CONTROLLER_EXTENSIONS_REPOSITORY_URLS
105112
.getEnvironmentVariableName())
106113
.withValue(OperatorProperty.EXTENSIONS_REPOSITORY_URLS

stackgres-k8s/src/restapi/src/main/java/io/stackgres/apiweb/dto/cluster/ClusterPodStatus.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public class ClusterPodStatus {
1717

1818
private String name;
1919

20+
private String nodeName;
21+
2022
private Integer replicationGroup;
2123

2224
private Boolean primary;
@@ -33,6 +35,14 @@ public void setName(String name) {
3335
this.name = name;
3436
}
3537

38+
public String getNodeName() {
39+
return nodeName;
40+
}
41+
42+
public void setNodeName(String nodeName) {
43+
this.nodeName = nodeName;
44+
}
45+
3646
public Integer getReplicationGroup() {
3747
return replicationGroup;
3848
}

0 commit comments

Comments
 (0)