Skip to content

Commit 8261485

Browse files
committed
Custom TLS for Exporter (Encryption Only)
With this change we allow users to bring custom certificates and enable TLS for the exporter. This will be an opt-in feature, PGO will not automatically generate certs like it does for some other features. You can enable TLS by using the following spec fields: spec: monitoring: pgmonitor: exporter: customTLSSecret: name: hippo.tls Once TLS is enabled in the exporter, you can configure your Prometheus instance to scrape over https.
1 parent f87a5b4 commit 8261485

File tree

21 files changed

+693
-24
lines changed

21 files changed

+693
-24
lines changed

bin/crunchy-postgres-exporter/start.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,10 @@ sed -i \
239239
/tmp/queries.yml
240240

241241
PG_OPTIONS="--extend.query-path=${QUERY_DIR?}/queries.yml --web.listen-address=:${POSTGRES_EXPORTER_PORT}"
242+
if [[ -v WEB_CONFIG_DIR ]]; then
243+
# TODO (jmckulk): define path not dir
244+
PG_OPTIONS+=" --web.config.file=${WEB_CONFIG_DIR}/web-config.yml"
245+
fi
242246

243247
echo_info "Starting postgres-exporter.."
244248
DATA_SOURCE_URI="${EXPORTER_PG_HOST}:${EXPORTER_PG_PORT}/${EXPORTER_PG_DATABASE}?${EXPORTER_PG_PARAMS}" DATA_SOURCE_USER="${EXPORTER_PG_USER}" DATA_SOURCE_PASS="${EXPORTER_PG_PASSWORD}" ${PG_EXP_HOME?}/postgres_exporter ${PG_OPTIONS?} >>/dev/stdout 2>&1 &

build/crd/todos.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
- op: copy
5353
from: /work
5454
path: /spec/versions/0/schema/openAPIV3Schema/properties/spec/properties/monitoring/properties/pgmonitor/properties/exporter/properties/configuration/items/properties/secret/properties/name/description
55+
- op: copy
56+
from: /work
57+
path: /spec/versions/0/schema/openAPIV3Schema/properties/spec/properties/monitoring/properties/pgmonitor/properties/exporter/properties/customTLSSecret/properties/name/description
5558
- op: copy
5659
from: /work
5760
path: /spec/versions/0/schema/openAPIV3Schema/properties/spec/properties/proxy/properties/pgBouncer/properties/config/properties/files/items/properties/configMap/properties/name/description

config/crd/bases/postgres-operator.crunchydata.com_postgresclusters.yaml

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9845,10 +9845,10 @@ spec:
98459845
configuration:
98469846
description: 'Projected volumes containing custom PostgreSQL
98479847
Exporter configuration. Currently supports the customization
9848-
of PostgreSQL Exporter queries. If a "queries.yaml"
9849-
file is detected in any volume projected using this
9850-
field, it will be loaded using the "extend.query-path"
9851-
flag: https://github.com/prometheus-community/postgres_exporter#flags
9848+
of PostgreSQL Exporter queries. If a "queries.yml" file
9849+
is detected in any volume projected using this field,
9850+
it will be loaded using the "extend.query-path" flag:
9851+
https://github.com/prometheus-community/postgres_exporter#flags
98529852
Changing the values of field causes PostgreSQL and the
98539853
exporter to restart.'
98549854
items:
@@ -10091,6 +10091,60 @@ spec:
1009110091
type: object
1009210092
type: object
1009310093
type: array
10094+
customTLSSecret:
10095+
description: Projected secret containing custom TLS certificates
10096+
to encrypt output from the exporter web server
10097+
properties:
10098+
items:
10099+
description: items if unspecified, each key-value
10100+
pair in the Data field of the referenced Secret
10101+
will be projected into the volume as a file whose
10102+
name is the key and content is the value. If specified,
10103+
the listed keys will be projected into the specified
10104+
paths, and unlisted keys will not be present. If
10105+
a key is specified which is not present in the Secret,
10106+
the volume setup will error unless it is marked
10107+
optional. Paths must be relative and may not contain
10108+
the '..' path or start with '..'.
10109+
items:
10110+
description: Maps a string key to a path within
10111+
a volume.
10112+
properties:
10113+
key:
10114+
description: key is the key to project.
10115+
type: string
10116+
mode:
10117+
description: 'mode is Optional: mode bits used
10118+
to set permissions on this file. Must be an
10119+
octal value between 0000 and 0777 or a decimal
10120+
value between 0 and 511. YAML accepts both
10121+
octal and decimal values, JSON requires decimal
10122+
values for mode bits. If not specified, the
10123+
volume defaultMode will be used. This might
10124+
be in conflict with other options that affect
10125+
the file mode, like fsGroup, and the result
10126+
can be other mode bits set.'
10127+
format: int32
10128+
type: integer
10129+
path:
10130+
description: path is the relative path of the
10131+
file to map the key to. May not be an absolute
10132+
path. May not contain the path element '..'.
10133+
May not start with the string '..'.
10134+
type: string
10135+
required:
10136+
- key
10137+
- path
10138+
type: object
10139+
type: array
10140+
name:
10141+
description: 'Name of the referent. More info: https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#names'
10142+
type: string
10143+
optional:
10144+
description: optional field specify whether the Secret
10145+
or its key must be defined
10146+
type: boolean
10147+
type: object
1009410148
image:
1009510149
description: The image name to use for crunchy-postgres-exporter
1009610150
containers. The image may also be set using the RELATED_IMAGE_PGEXPORTER

docs/content/references/crd.md

Lines changed: 80 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/content/tutorial/monitoring.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,32 @@ PGO will detect the change and add the Exporter sidecar to all Postgres Pods tha
4040
cluster. PGO will also do the work to allow the Exporter to connect to the database and gather
4141
metrics that can be accessed using the [PGO Monitoring] stack.
4242

43+
### Configuring TLS Encryption for the Exporter
44+
45+
PGO allows you to configure the exporter sidecar to use TLS encryption. If you provide a custom TLS
46+
Secret via the exporter spec:
47+
48+
```
49+
monitoring:
50+
pgmonitor:
51+
exporter:
52+
customTLSSecret:
53+
name: hippo.tls
54+
```
55+
56+
Like other custom TLS Secrets that can be configured with PGO, the Secret will need to be created in
57+
the same Namespace as your PostgresCluster. It should also contain the TLS key (`tls.key`) and TLS
58+
certificate (`tls.crt`) needed to enable encryption.
59+
60+
```
61+
data:
62+
tls.crt: <value>
63+
tls.key: <value>
64+
```
65+
66+
After you configure TLS for the exporter, you will need to update your Prometheus deployment to use
67+
TLS, and your connection to the exporter will be encrypted. Check out the [Prometheus] documentation
68+
for more information on configuring TLS for [Prometheus].
4369

4470
## Accessing the Metrics
4571

hack/create-todo-patch.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ directory=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
1717
crd_build_dir="$directory"/../build/crd
1818

1919
# Generate a Kustomize patch file for removing any TODOs we inherit from the Kubernetes API.
20-
# Right now there are one TODO in our CRD. This script focuses on removing the specific TODO
20+
# Right now there is one TODO in our CRD. This script focuses on removing the specific TODO
2121
# anywhere they are found in the CRD.
2222

2323
# The TODO comes from the following:

internal/controller/postgrescluster/controller.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ func (r *Reconciler) Reconcile(
171171
primaryService *corev1.Service
172172
rootCA *pki.RootCertificateAuthority
173173
monitoringSecret *corev1.Secret
174+
exporterWebConfig *corev1.ConfigMap
174175
err error
175176
)
176177

@@ -304,11 +305,14 @@ func (r *Reconciler) Reconcile(
304305
if err == nil {
305306
monitoringSecret, err = r.reconcileMonitoringSecret(ctx, cluster)
306307
}
308+
if err == nil {
309+
exporterWebConfig, err = r.reconcileExporterWebConfig(ctx, cluster)
310+
}
307311
if err == nil {
308312
err = r.reconcileInstanceSets(
309313
ctx, cluster, clusterConfigMap, clusterReplicationSecret,
310314
rootCA, clusterPodService, instanceServiceAccount, instances,
311-
patroniLeaderService, primaryCertificate, clusterVolumes)
315+
patroniLeaderService, primaryCertificate, clusterVolumes, exporterWebConfig)
312316
}
313317

314318
if err == nil {

internal/controller/postgrescluster/instance.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ func (r *Reconciler) reconcileInstanceSets(
502502
patroniLeaderService *corev1.Service,
503503
primaryCertificate *corev1.SecretProjection,
504504
clusterVolumes []corev1.PersistentVolumeClaim,
505+
exporterWebConfig *corev1.ConfigMap,
505506
) error {
506507

507508
// Go through the observed instances and check if a primary has been determined.
@@ -538,7 +539,7 @@ func (r *Reconciler) reconcileInstanceSets(
538539
rootCA, clusterPodService, instanceServiceAccount,
539540
patroniLeaderService, primaryCertificate,
540541
findAvailableInstanceNames(*set, instances, clusterVolumes),
541-
numInstancePods, clusterVolumes)
542+
numInstancePods, clusterVolumes, exporterWebConfig)
542543

543544
if err == nil {
544545
err = r.reconcileInstanceSetPodDisruptionBudget(ctx, cluster, set)
@@ -976,6 +977,7 @@ func (r *Reconciler) scaleUpInstances(
976977
availableInstanceNames []string,
977978
numInstancePods int,
978979
clusterVolumes []corev1.PersistentVolumeClaim,
980+
exporterWebConfig *corev1.ConfigMap,
979981
) ([]*appsv1.StatefulSet, error) {
980982
log := logging.FromContext(ctx)
981983

@@ -1019,7 +1021,7 @@ func (r *Reconciler) scaleUpInstances(
10191021
clusterConfigMap, clusterReplicationSecret,
10201022
rootCA, clusterPodService, instanceServiceAccount,
10211023
patroniLeaderService, primaryCertificate, instances[i],
1022-
numInstancePods, clusterVolumes,
1024+
numInstancePods, clusterVolumes, exporterWebConfig,
10231025
)
10241026
}
10251027
if err == nil {
@@ -1048,6 +1050,7 @@ func (r *Reconciler) reconcileInstance(
10481050
instance *appsv1.StatefulSet,
10491051
numInstancePods int,
10501052
clusterVolumes []corev1.PersistentVolumeClaim,
1053+
exporterWebConfig *corev1.ConfigMap,
10511054
) error {
10521055
log := logging.FromContext(ctx).WithValues("instance", instance.Name)
10531056
ctx = logging.NewContext(ctx, log)
@@ -1100,7 +1103,7 @@ func (r *Reconciler) reconcileInstance(
11001103

11011104
// Add pgMonitor resources to the instance Pod spec
11021105
if err == nil {
1103-
err = addPGMonitorToInstancePodSpec(cluster, &instance.Spec.Template)
1106+
err = addPGMonitorToInstancePodSpec(cluster, &instance.Spec.Template, exporterWebConfig)
11041107
}
11051108

11061109
// add nss_wrapper init container and add nss_wrapper env vars to the database and pgbackrest

0 commit comments

Comments
 (0)