Skip to content

Commit 9268c90

Browse files
committed
feat: Allow to disable Envoy
1 parent b66cf77 commit 9268c90

26 files changed

Lines changed: 314 additions & 27 deletions

stackgres-k8s/e2e/spec/no-envoy

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/bin/sh
2+
3+
. "$SPEC_PATH/abstract/patroni"
4+
5+
e2e_test_extra_hash() {
6+
"$SHELL" "$PROJECT_PATH/stackgres-k8s/ci/build/build-functions.sh" path_hash \
7+
"$(realpath --relative-to "$PROJECT_PATH" "$SPEC_PATH/abstract/patroni")"
8+
}
9+
10+
e2e_test_install() {
11+
create_or_replace_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE" "2"
12+
13+
deploy_curl_pod "$CLUSTER_NAMESPACE"
14+
15+
wait_pods_running "$CLUSTER_NAMESPACE" 3
16+
wait_cluster "$CLUSTER_NAME" "$CLUSTER_NAMESPACE"
17+
}
18+
19+
e2e_test() {
20+
run_test "Checking that all 2 ports (5432, 6432) in the patroni pods are opened and listeing for queries" ports_check
21+
22+
run_test "Checking that is possible to connect using services is working" service_check
23+
24+
run_test "Checking that envoy ports are not open" check_no_envoy
25+
26+
run_test "Check that patroni healthcheck endpoints are accesible" patroni_ports_check
27+
28+
run_test "Check that patroni management endpoints are not accesible" patroni_management_check
29+
30+
run_test "Check that patroni is not accisible directly" patroni_direct_check
31+
}
32+
33+
ports_check() {
34+
RESPONSE_5432="$(run_query -i 0 -p 5432)"
35+
RESPONSE_6432="$(run_query -i 0 -p 6432)"
36+
37+
if [ "$RESPONSE_5432" = "1" ] && [ "$RESPONSE_6432" = "1" ]
38+
then
39+
RESPONSE_5432="$(run_query -i 1 -p 5432)"
40+
RESPONSE_6432="$(run_query -i 1 -p 6432)"
41+
42+
if [ "$RESPONSE_5432" = "1" ] && [ "$RESPONSE_6432" = "1" ]
43+
then
44+
success "All ports are ok"
45+
else
46+
fail "Not all 2 ports of the replica node are working"
47+
fi
48+
else
49+
fail "Not all 2 ports of the primary node are working"
50+
fi
51+
}
52+
53+
service_check() {
54+
RESPONSE_PRIMARY="$(run_query -h "$CLUSTER_NAME" -i 1 -p 5432)"
55+
56+
if [ "$RESPONSE_PRIMARY" = "1" ]
57+
then
58+
RESPONSE_REPLICA="$(run_query -h "$CLUSTER_NAME"-replicas -i 0 -p 5432)"
59+
if [ "$RESPONSE_REPLICA" = "1" ]
60+
then
61+
success "Connections are possible using services"
62+
else
63+
fail "Cannot connect to replica db using a kubernetes service"
64+
fi
65+
else
66+
fail "Cannot connect to primary db using a kubernetes service"
67+
fi
68+
}
69+
70+
check_no_envoy() {
71+
if run_query -i 0 -p 7432 > /dev/null
72+
then
73+
fail "envoy port 7432 on primary server is open"
74+
else
75+
if run_query -i 1 -p 7432 > /dev/null
76+
then
77+
fail "envoy port 7432 on replica server is open"
78+
else
79+
success "envoy port 7432 is closed"
80+
fi
81+
fi
82+
83+
if run_query -i 0 -p 7433 > /dev/null
84+
then
85+
fail "envoy port 7433 on primary server is open"
86+
else
87+
if run_query -i 1 -p 7433 > /dev/null
88+
then
89+
fail "envoy port 7433 on replica server is open"
90+
else
91+
success "envoy port 7433 is closed"
92+
fi
93+
fi
94+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
configurations:
2+
postgresconfig:
3+
postgresql.conf:
4+
shared_buffers: '32MB'
5+
backupconfig:
6+
create: false
7+
cluster:
8+
pods:
9+
persistentVolume:
10+
size: 128Mi
11+
disableEnvoy: true
12+
managementPolicy: Parallel
13+
nonProductionOptions:
14+
disableClusterPodAntiAffinity: true

stackgres-k8s/install/helm/stackgres-cluster/templates/sgcluster.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ spec:
139139
{{- with .Values.cluster.pods.disablePostgresUtil }}
140140
disablePostgresUtil: {{ . }}
141141
{{- end }}
142+
{{- with .Values.cluster.pods.disableEnvoy }}
143+
disableEnvoy: {{ . }}
144+
{{- end }}
142145
{{- with .Values.cluster.pods.scheduling }}
143146
scheduling:
144147
{{- toYaml . | nindent 6 }}

stackgres-k8s/install/helm/stackgres-cluster/templates/sgshardedcluster.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ spec:
144144
{{- with .Values.cluster.pods.disablePostgresUtil }}
145145
disablePostgresUtil: {{ . }}
146146
{{- end }}
147+
{{- with .Values.cluster.pods.disableEnvoy }}
148+
disableEnvoy: {{ . }}
149+
{{- end }}
147150
{{- with .Values.cluster.pods.scheduling }}
148151
scheduling:
149152
{{- toYaml . | nindent 8 }}
@@ -204,6 +207,9 @@ spec:
204207
{{- with .Values.shardedCluster.shards.pods.disablePostgresUtil }}
205208
disablePostgresUtil: {{ . }}
206209
{{- end }}
210+
{{- with .Values.shardedCluster.shards.pods.disableEnvoy }}
211+
disableEnvoy: {{ . }}
212+
{{- end }}
207213
{{- with .Values.shardedCluster.shards.pods.scheduling }}
208214
scheduling:
209215
{{- toYaml . | nindent 8 }}

stackgres-k8s/install/helm/stackgres-cluster/values.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ cluster:
116116
# storageClass: "-"
117117
disableConnectionPooling: false
118118
disablePostgresUtil: false
119+
disableEnvoy: false
119120
# scheduling:
120121
# nodeSelector:
121122
# key: value

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55

66
package io.stackgres.common.crd.sgcluster;
77

8+
import java.util.List;
89
import java.util.Map;
10+
import java.util.Optional;
911

12+
import com.fasterxml.jackson.annotation.JsonIgnore;
1013
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
1114
import com.fasterxml.jackson.annotation.JsonInclude;
1215
import io.quarkus.runtime.annotations.RegisterForReflection;
@@ -26,6 +29,21 @@ public StackGresClusterPatroniDynamicConfig(Map<String, Object> m) {
2629
super(m);
2730
}
2831

32+
@JsonIgnore
33+
public Optional<JsonObject> getPostgresql() {
34+
return Optional.of(this)
35+
.filter(config -> config.hasObject("postgresql"))
36+
.map(config -> config.getObject("postgresql"));
37+
}
38+
39+
@JsonIgnore
40+
public Optional<List<String>> getPgHba() {
41+
return getPostgresql()
42+
.map(postgresql -> postgresql.get("pg_hba"))
43+
.filter(List.class::isInstance)
44+
.map(List.class::cast);
45+
}
46+
2947
public String toString() {
3048
return StackGresUtil.toPrettyYaml(this);
3149
}

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public class StackGresClusterPods {
4141

4242
private Boolean disablePostgresUtil;
4343

44+
private Boolean disableEnvoy;
45+
4446
@ValidEnum(enumClass = StackGresPodManagementPolicy.class, allowNulls = true,
4547
message = "managementPolicy must be OrderedReady or Parallel")
4648
private String managementPolicy;
@@ -109,6 +111,14 @@ public void setDisablePostgresUtil(Boolean disablePostgresUtil) {
109111
this.disablePostgresUtil = disablePostgresUtil;
110112
}
111113

114+
public Boolean getDisableEnvoy() {
115+
return disableEnvoy;
116+
}
117+
118+
public void setDisableEnvoy(Boolean disableEnvoy) {
119+
this.disableEnvoy = disableEnvoy;
120+
}
121+
112122
public StackGresClusterResources getResources() {
113123
return resources;
114124
}
@@ -175,9 +185,10 @@ public void setCustomInitVolumeMounts(Map<String, List<CustomVolumeMount>> custo
175185

176186
@Override
177187
public int hashCode() {
178-
return Objects.hash(customContainers, customInitContainers, customInitVolumeMounts, customVolumeMounts,
179-
customVolumes, disableConnectionPooling, disableMetricsExporter, disablePostgresUtil, managementPolicy,
180-
persistentVolume, resources, scheduling);
188+
return Objects.hash(customContainers, customInitContainers, customInitVolumeMounts,
189+
customVolumeMounts, customVolumes, disableConnectionPooling, disableEnvoy,
190+
disableMetricsExporter, disablePostgresUtil, managementPolicy, persistentVolume, resources,
191+
scheduling);
181192
}
182193

183194
@Override
@@ -195,10 +206,12 @@ public boolean equals(Object obj) {
195206
&& Objects.equals(customVolumeMounts, other.customVolumeMounts)
196207
&& Objects.equals(customVolumes, other.customVolumes)
197208
&& Objects.equals(disableConnectionPooling, other.disableConnectionPooling)
209+
&& Objects.equals(disableEnvoy, other.disableEnvoy)
198210
&& Objects.equals(disableMetricsExporter, other.disableMetricsExporter)
199211
&& Objects.equals(disablePostgresUtil, other.disablePostgresUtil)
200212
&& Objects.equals(managementPolicy, other.managementPolicy)
201-
&& Objects.equals(persistentVolume, other.persistentVolume) && Objects.equals(resources, other.resources)
213+
&& Objects.equals(persistentVolume, other.persistentVolume)
214+
&& Objects.equals(resources, other.resources)
202215
&& Objects.equals(scheduling, other.scheduling);
203216
}
204217

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,13 @@ spec:
819819
description: |
820820
If set to `true`, avoids creating the `postgres-util` sidecar. This sidecar contains usual Postgres administration utilities *that are not present in the main (`patroni`) container*, like `psql`. Only disable if you know what you are doing.
821821
822+
**Changing this field may require a restart.**
823+
default: false
824+
disableEnvoy:
825+
type: boolean
826+
description: |
827+
If set to `true`, avoids creating the `envoy` sidecar. This sidecar is used as the endge proxy for the cluster's Pods providing extra metrics to the monitoring layer.
828+
822829
**Changing this field may require a restart.**
823830
default: false
824831
resources:

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,12 @@ spec:
12031203
12041204
If sharding type is `shardingsphere` then this field is ignored.
12051205

1206+
**Changing this field may require a restart.**
1207+
disableEnvoy:
1208+
type: boolean
1209+
description: |
1210+
If set to `true`, avoids creating the `envoy` sidecar. This sidecar is used as the endge proxy for the cluster's Pods providing extra metrics to the monitoring layer.
1211+
12061212
**Changing this field may require a restart.**
12071213
resources:
12081214
type: object
@@ -1901,6 +1907,12 @@ spec:
19011907
description: |
19021908
If set to `true`, avoids creating the `postgres-util` sidecar. This sidecar contains usual Postgres administration utilities *that are not present in the main (`patroni`) container*, like `psql`. Only disable if you know what you are doing.
19031909
1910+
**Changing this field may require a restart.**
1911+
disableEnvoy:
1912+
type: boolean
1913+
description: |
1914+
If set to `true`, avoids creating the `envoy` sidecar. This sidecar is used as the endge proxy for the cluster's Pods providing extra metrics to the monitoring layer.
1915+
19041916
**Changing this field may require a restart.**
19051917
resources:
19061918
type: object
@@ -2194,6 +2206,12 @@ spec:
21942206
description: |
21952207
If set to `true`, avoids creating the `postgres-util` sidecar. This sidecar contains usual Postgres administration utilities *that are not present in the main (`patroni`) container*, like `psql`. Only disable if you know what you are doing.
21962208
2209+
**Changing this field may require a restart.**
2210+
disableEnvoy:
2211+
type: boolean
2212+
description: |
2213+
If set to `true`, avoids creating the `envoy` sidecar. This sidecar is used as the endge proxy for the cluster's Pods providing extra metrics to the monitoring layer.
2214+
21972215
**Changing this field may require a restart.**
21982216
resources:
21992217
type: object

stackgres-k8s/src/operator/src/main/java/io/stackgres/operator/conciliation/factory/cluster/patroni/Patroni.java

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
package io.stackgres.operator.conciliation.factory.cluster.patroni;
77

88
import static io.stackgres.common.StackGresUtil.getDefaultPullPolicy;
9+
import static io.stackgres.common.StackGresUtil.getPostgresFlavorComponent;
910

1011
import java.util.HashMap;
12+
import java.util.List;
1113
import java.util.Map;
1214
import java.util.Optional;
1315

@@ -16,6 +18,8 @@
1618
import io.fabric8.kubernetes.api.model.ConfigMapEnvSourceBuilder;
1719
import io.fabric8.kubernetes.api.model.Container;
1820
import io.fabric8.kubernetes.api.model.ContainerBuilder;
21+
import io.fabric8.kubernetes.api.model.ContainerPort;
22+
import io.fabric8.kubernetes.api.model.ContainerPortBuilder;
1923
import io.fabric8.kubernetes.api.model.EnvFromSourceBuilder;
2024
import io.fabric8.kubernetes.api.model.EnvVar;
2125
import io.fabric8.kubernetes.api.model.EnvVarBuilder;
@@ -33,8 +37,10 @@
3337
import io.stackgres.common.StackGresVolume;
3438
import io.stackgres.common.crd.sgcluster.StackGresCluster;
3539
import io.stackgres.common.crd.sgcluster.StackGresClusterInitialData;
40+
import io.stackgres.common.crd.sgcluster.StackGresClusterPods;
3641
import io.stackgres.common.crd.sgcluster.StackGresClusterRestore;
3742
import io.stackgres.common.crd.sgcluster.StackGresClusterRestoreFromBackup;
43+
import io.stackgres.common.crd.sgcluster.StackGresClusterSpec;
3844
import io.stackgres.operator.conciliation.OperatorVersionBinder;
3945
import io.stackgres.operator.conciliation.cluster.StackGresClusterContext;
4046
import io.stackgres.operator.conciliation.factory.ContainerFactory;
@@ -157,7 +163,7 @@ public Container getContainer(ClusterContainerContext context) {
157163
volumeMounts.addAll(restoreMounts.getVolumeMounts(context))
158164
);
159165

160-
return new ContainerBuilder()
166+
var containerBuilder = new ContainerBuilder()
161167
.withName(StackGresContainer.PATRONI.getName())
162168
.withImage(patroniImageName)
163169
.withCommand("/bin/sh", "-ex",
@@ -200,8 +206,59 @@ public Container getContainer(ClusterContainerContext context) {
200206
.withInitialDelaySeconds(0)
201207
.withPeriodSeconds(2)
202208
.withTimeoutSeconds(1)
203-
.build())
204-
.build();
209+
.build());
210+
if (Optional.of(cluster)
211+
.map(StackGresCluster::getSpec)
212+
.map(StackGresClusterSpec::getPods)
213+
.map(StackGresClusterPods::getDisableEnvoy)
214+
.orElse(false)) {
215+
containerBuilder.withPorts(getContainerPorts(cluster));
216+
}
217+
return containerBuilder.build();
218+
}
219+
220+
private List<ContainerPort> getContainerPorts(StackGresCluster cluster) {
221+
boolean isConnectionPoolingDisabled = Optional.of(cluster)
222+
.map(StackGresCluster::getSpec)
223+
.map(StackGresClusterSpec::getPods)
224+
.map(StackGresClusterPods::getDisableConnectionPooling)
225+
.orElse(false);
226+
if (getPostgresFlavorComponent(cluster) == StackGresComponent.BABELFISH) {
227+
return List.of(
228+
new ContainerPortBuilder()
229+
.withProtocol("TCP")
230+
.withName(EnvoyUtil.POSTGRES_PORT_NAME)
231+
.withContainerPort(isConnectionPoolingDisabled
232+
? EnvoyUtil.PG_ENTRY_PORT : EnvoyUtil.PG_POOL_PORT).build(),
233+
new ContainerPortBuilder()
234+
.withProtocol("TCP")
235+
.withName(EnvoyUtil.POSTGRES_REPLICATION_PORT_NAME)
236+
.withContainerPort(EnvoyUtil.PG_PORT).build(),
237+
new ContainerPortBuilder()
238+
.withProtocol("TCP")
239+
.withName(EnvoyUtil.BABELFISH_PORT_NAME)
240+
.withContainerPort(EnvoyUtil.BF_PORT).build(),
241+
new ContainerPortBuilder()
242+
.withName(EnvoyUtil.PATRONI_RESTAPI_PORT_NAME)
243+
.withProtocol("TCP")
244+
.withContainerPort(EnvoyUtil.PATRONI_PORT)
245+
.build());
246+
}
247+
return List.of(
248+
new ContainerPortBuilder()
249+
.withProtocol("TCP")
250+
.withName(EnvoyUtil.POSTGRES_PORT_NAME)
251+
.withContainerPort(isConnectionPoolingDisabled
252+
? EnvoyUtil.PG_ENTRY_PORT : EnvoyUtil.PG_POOL_PORT).build(),
253+
new ContainerPortBuilder()
254+
.withProtocol("TCP")
255+
.withName(EnvoyUtil.POSTGRES_REPLICATION_PORT_NAME)
256+
.withContainerPort(EnvoyUtil.PG_PORT).build(),
257+
new ContainerPortBuilder()
258+
.withName(EnvoyUtil.PATRONI_RESTAPI_PORT_NAME)
259+
.withProtocol("TCP")
260+
.withContainerPort(EnvoyUtil.PATRONI_PORT)
261+
.build());
205262
}
206263

207264
private ImmutableList<EnvVar> getEnvVars(ClusterContainerContext context) {

0 commit comments

Comments
 (0)