Skip to content

Commit 47ae40f

Browse files
committed
Merge remote-tracking branch 'origin' into lb-kill-switch
2 parents be3a00b + bd2e6e2 commit 47ae40f

File tree

6 files changed

+150
-379
lines changed

6 files changed

+150
-379
lines changed

api/v1/postgres_types.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -700,10 +700,11 @@ func (p *Postgres) ToUnstructuredZalandoPostgresql(z *zalando.Postgresql, c *cor
700700
// finally, overwrite the (special to us) shared buffer parameter
701701
setSharedBufferSize(z.Spec.PostgresqlParam.Parameters, p.Spec.Size.SharedBuffer)
702702

703-
z.Spec.Resources.ResourceRequests.CPU = p.Spec.Size.CPU
704-
z.Spec.Resources.ResourceRequests.Memory = p.Spec.Size.Memory
705-
z.Spec.Resources.ResourceLimits.CPU = p.Spec.Size.CPU
706-
z.Spec.Resources.ResourceLimits.Memory = p.Spec.Size.Memory
703+
z.Spec.Resources = &zalando.Resources{}
704+
z.Spec.Resources.ResourceRequests.CPU = pointer.String(p.Spec.Size.CPU)
705+
z.Spec.Resources.ResourceRequests.Memory = pointer.String(p.Spec.Size.Memory)
706+
z.Spec.Resources.ResourceLimits.CPU = pointer.String(p.Spec.Size.CPU)
707+
z.Spec.Resources.ResourceLimits.Memory = pointer.String(p.Spec.Size.Memory)
707708
z.Spec.TeamID = p.generateTeamID()
708709
z.Spec.Volume.Size = p.Spec.Size.StorageSize
709710
z.Spec.Volume.StorageClass = sc
@@ -790,12 +791,9 @@ func (p *Postgres) ToUnstructuredZalandoPostgresql(z *zalando.Postgresql, c *cor
790791
} else {
791792
// overwrite connection info
792793
z.Spec.StandbyCluster = &zalando.StandbyDescription{
793-
StandbyMethod: StandbyMethod,
794-
StandbyHost: p.Spec.PostgresConnection.ConnectionIP,
795-
StandbyPort: strconv.FormatInt(int64(p.Spec.PostgresConnection.ConnectionPort), 10),
796-
StandbySecretName: "standby." + p.ToPeripheralResourceName() + ".credentials",
797-
S3WalPath: "",
798-
StandbyApplicationName: p.ObjectMeta.Name,
794+
StandbyHost: p.Spec.PostgresConnection.ConnectionIP,
795+
StandbyPort: strconv.FormatInt(int64(p.Spec.PostgresConnection.ConnectionPort), 10),
796+
// S3WalPath: "",
799797
}
800798
}
801799

controllers/postgres_controller.go

Lines changed: 116 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,19 @@ import (
5656
const (
5757
postgresExporterServiceName string = "postgres-exporter"
5858
postgresExporterServicePortName string = "metrics"
59+
postgresExporterServicePortKeyName string = "postgres-exporter-service-port"
60+
postgresExporterServiceTargetPortKeyName string = "postgres-exporter-service-target-port"
61+
walGExporterServicePortName string = "backup-metrics"
62+
walGExporterServicePortKeyName string = "wal-g-exporter-service-port"
63+
walGExporterServiceTargetPortKeyName string = "wal-g-exporter-service-target-port"
5964
postgresExporterServiceTenantAnnotationName string = pg.TenantLabelName
6065
postgresExporterServiceProjectIDAnnotationName string = pg.ProjectIDLabelName
6166
storageEncryptionKeyName string = "storage-encryption-key"
6267
storageEncryptionKeyFinalizerName string = "postgres.database.fits.cloud/secret-finalizer"
6368
walGEncryptionSecretNamePostfix string = "-walg-encryption"
6469
walGEncryptionSecretKeyName string = "key"
70+
podMonitorName string = "patroni"
71+
podMonitorPort string = "8008"
6572
initDBName string = "postgres-initdb"
6673
initDBSQLDummy string = `SELECT 'NOOP';`
6774
debugLogLevel int = 1
@@ -102,6 +109,7 @@ type PostgresReconciler struct {
102109
EnableCustomTLSCert bool
103110
TLSClusterIssuer string
104111
TLSSubDomain string
112+
EnableWalGExporter bool
105113
}
106114

107115
type PatroniStandbyCluster struct {
@@ -300,6 +308,12 @@ func (r *PostgresReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
300308
return ctrl.Result{}, fmt.Errorf("error while creating sidecars servicemonitor %v: %w", namespace, err)
301309
}
302310

311+
// Add pod monitor
312+
if err := r.createOrUpdatePatroniPodMonitor(ctx, namespace, instance); err != nil {
313+
r.recorder.Eventf(instance, "Warning", "Error", "failed to create podmonitor: %v", err)
314+
return ctrl.Result{}, fmt.Errorf("error while creating podmonitor %v: %w", namespace, err)
315+
}
316+
303317
// Make sure the storage secret exist, if necessary
304318
if err := r.ensureStorageEncryptionSecret(log, ctx, instance); err != nil {
305319
r.recorder.Eventf(instance, "Warning", "Error", "failed to create storage secret: %v", err)
@@ -1473,16 +1487,28 @@ func (r *PostgresReconciler) createOrUpdateNetPol(ctx context.Context, instance
14731487

14741488
// createOrUpdateExporterSidecarServices ensures the necessary services to access the sidecars exist
14751489
func (r *PostgresReconciler) createOrUpdateExporterSidecarServices(log logr.Logger, ctx context.Context, namespace string, c *corev1.ConfigMap, in *pg.Postgres) error {
1476-
exporterServicePort, error := strconv.ParseInt(c.Data["postgres-exporter-service-port"], 10, 32)
1490+
pesPort, error := strconv.ParseInt(c.Data[postgresExporterServicePortKeyName], 10, 32)
1491+
if error != nil {
1492+
log.Error(error, "postgres-exporter-service-port could not be parsed to int32, falling back to default value")
1493+
pesPort = 9187
1494+
}
1495+
1496+
pesTargetPort, error := strconv.ParseInt(c.Data[postgresExporterServiceTargetPortKeyName], 10, 32)
1497+
if error != nil {
1498+
log.Error(error, "postgres-exporter-service-target-port could not be parsed to int32, falling back to default value")
1499+
pesTargetPort = pesPort
1500+
}
1501+
1502+
wgesPort, error := strconv.ParseInt(c.Data[walGExporterServicePortKeyName], 10, 32)
14771503
if error != nil {
1478-
// todo log error
1479-
exporterServicePort = 9187
1504+
log.Error(error, "wal-g-exporter-service-port could not be parsed to int32, falling back to default value")
1505+
pesPort = 9351
14801506
}
14811507

1482-
exporterServiceTargetPort, error := strconv.ParseInt(c.Data["postgres-exporter-service-target-port"], 10, 32)
1508+
wgesTargetPort, error := strconv.ParseInt(c.Data[walGExporterServiceTargetPortKeyName], 10, 32)
14831509
if error != nil {
1484-
// todo log error
1485-
exporterServiceTargetPort = exporterServicePort
1510+
log.Error(error, "wal-g-exporter-service-target-port could not be parsed to int32, falling back to default value")
1511+
pesTargetPort = pesPort
14861512
}
14871513

14881514
labels := map[string]string{
@@ -1506,11 +1532,20 @@ func (r *PostgresReconciler) createOrUpdateExporterSidecarServices(log logr.Logg
15061532
pes.Spec.Ports = []corev1.ServicePort{
15071533
{
15081534
Name: postgresExporterServicePortName,
1509-
Port: int32(exporterServicePort), //nolint
1535+
Port: int32(pesPort), //nolint
15101536
Protocol: corev1.ProtocolTCP,
1511-
TargetPort: intstr.FromInt(int(exporterServiceTargetPort)),
1537+
TargetPort: intstr.FromInt(int(pesTargetPort)),
15121538
},
15131539
}
1540+
if r.EnableWalGExporter {
1541+
wgesp := corev1.ServicePort{
1542+
Name: walGExporterServicePortName,
1543+
Port: int32(wgesPort), //nolint
1544+
Protocol: corev1.ProtocolTCP,
1545+
TargetPort: intstr.FromInt(int(wgesTargetPort)),
1546+
}
1547+
pes.Spec.Ports = append(pes.Spec.Ports, wgesp)
1548+
}
15141549
selector := map[string]string{
15151550
pg.ApplicationLabelName: pg.ApplicationLabelValue,
15161551
}
@@ -1586,6 +1621,12 @@ func (r *PostgresReconciler) createOrUpdateExporterSidecarServiceMonitor(log log
15861621
Port: postgresExporterServicePortName,
15871622
},
15881623
}
1624+
if r.EnableWalGExporter {
1625+
wgesme := coreosv1.Endpoint{
1626+
Port: walGExporterServicePortName,
1627+
}
1628+
pesm.Spec.Endpoints = append(pesm.Spec.Endpoints, wgesme)
1629+
}
15891630
pesm.Spec.NamespaceSelector = coreosv1.NamespaceSelector{
15901631
MatchNames: []string{namespace},
15911632
}
@@ -1596,6 +1637,10 @@ func (r *PostgresReconciler) createOrUpdateExporterSidecarServiceMonitor(log log
15961637
pesm.Spec.Selector = metav1.LabelSelector{
15971638
MatchLabels: matchLabels,
15981639
}
1640+
pesm.Spec.TargetLabels = []string{
1641+
"postgres_partition_id=" + in.Spec.PartitionID,
1642+
"is_primary=" + strconv.FormatBool(in.IsReplicationPrimaryOrStandalone()),
1643+
}
15991644

16001645
// try to fetch any existing postgres-exporter service
16011646
ns := types.NamespacedName{
@@ -1623,6 +1668,69 @@ func (r *PostgresReconciler) createOrUpdateExporterSidecarServiceMonitor(log log
16231668
return nil
16241669
}
16251670

1671+
// createOrUpdatePatroniPodMonitor ensures the servicemonitors for the sidecars exist
1672+
func (r *PostgresReconciler) createOrUpdatePatroniPodMonitor(ctx context.Context, namespace string, in *pg.Postgres) error {
1673+
log := r.Log.WithValues("namespace", namespace)
1674+
1675+
labels := map[string]string{
1676+
"app": "postgres-exporter",
1677+
"release": "prometheus",
1678+
}
1679+
1680+
annotations := map[string]string{
1681+
postgresExporterServiceTenantAnnotationName: in.Spec.Tenant,
1682+
postgresExporterServiceProjectIDAnnotationName: in.Spec.ProjectID,
1683+
}
1684+
1685+
pm := &coreosv1.PodMonitor{
1686+
ObjectMeta: metav1.ObjectMeta{
1687+
Name: podMonitorName,
1688+
Namespace: namespace,
1689+
Labels: labels,
1690+
Annotations: annotations,
1691+
},
1692+
}
1693+
1694+
pm.Spec.PodMetricsEndpoints = []coreosv1.PodMetricsEndpoint{
1695+
{
1696+
Port: podMonitorPort,
1697+
},
1698+
}
1699+
pm.Spec.NamespaceSelector = coreosv1.NamespaceSelector{
1700+
MatchNames: []string{namespace},
1701+
}
1702+
matchLabels := map[string]string{
1703+
"application": "spilo",
1704+
}
1705+
pm.Spec.Selector = metav1.LabelSelector{
1706+
MatchLabels: matchLabels,
1707+
}
1708+
1709+
// try to fetch any existing podmonitor
1710+
ns := types.NamespacedName{
1711+
Namespace: namespace,
1712+
Name: podMonitorName,
1713+
}
1714+
old := &coreosv1.PodMonitor{}
1715+
if err := r.SvcClient.Get(ctx, ns, old); err == nil {
1716+
// Copy the resource version
1717+
pm.ObjectMeta.ResourceVersion = old.ObjectMeta.ResourceVersion
1718+
if err := r.SvcClient.Update(ctx, pm); err != nil {
1719+
return fmt.Errorf("error while updating the podmonitor: %w", err)
1720+
}
1721+
log.Info("pod monitor updated")
1722+
return nil
1723+
}
1724+
1725+
// local podmonitor does not exist, creating it
1726+
if err := r.SvcClient.Create(ctx, pm); err != nil {
1727+
return fmt.Errorf("error while creating the podmonitor: %w", err)
1728+
}
1729+
log.Info("podmonitor created")
1730+
1731+
return nil
1732+
}
1733+
16261734
func (r *PostgresReconciler) deleteExporterSidecarService(log logr.Logger, ctx context.Context, namespace string) error {
16271735
s := &corev1.Service{
16281736
ObjectMeta: metav1.ObjectMeta{

go.mod

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/onsi/gomega v1.27.10
1313
github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring v0.56.2
1414
github.com/spf13/viper v1.12.0
15-
github.com/zalando/postgres-operator v1.7.0
15+
github.com/zalando/postgres-operator v1.11.0
1616
k8s.io/api v0.28.9
1717
k8s.io/apiextensions-apiserver v0.28.9
1818
k8s.io/apimachinery v0.28.9
@@ -73,15 +73,15 @@ require (
7373
go.uber.org/multierr v1.11.0 // indirect
7474
go.uber.org/zap v1.25.0 // indirect
7575
golang.org/x/crypto v0.21.0 // indirect
76-
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect
76+
golang.org/x/exp v0.0.0-20240112132812-db7319d0e0e3 // indirect
7777
golang.org/x/mod v0.14.0 // indirect
7878
golang.org/x/net v0.23.0 // indirect
7979
golang.org/x/oauth2 v0.8.0 // indirect
8080
golang.org/x/sys v0.18.0 // indirect
8181
golang.org/x/term v0.18.0 // indirect
8282
golang.org/x/text v0.14.0 // indirect
8383
golang.org/x/time v0.3.0 // indirect
84-
golang.org/x/tools v0.16.1 // indirect
84+
golang.org/x/tools v0.17.0 // indirect
8585
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
8686
google.golang.org/appengine v1.6.7 // indirect
8787
google.golang.org/protobuf v1.33.0 // indirect
@@ -98,5 +98,3 @@ require (
9898
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
9999
sigs.k8s.io/yaml v1.3.0 // indirect
100100
)
101-
102-
replace github.com/zalando/postgres-operator v1.7.0 => github.com/ermajn/postgres-operator v1.0.1-0.20211123085256-711648b7fdde

0 commit comments

Comments
 (0)