Skip to content

Commit

Permalink
Create warning event when postgres version is EOL.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsessler7 committed Nov 5, 2024
1 parent 561c650 commit b17d8f3
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
8 changes: 8 additions & 0 deletions internal/controller/postgrescluster/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ func (r *Reconciler) Reconcile(
return runtime.ErrorWithBackoff(err)
}
}
// Issue Warning Event if postgres version is EOL according to PostgreSQL:
// https://www.postgresql.org/support/versioning/
currentTime := time.Now()
if postgres.ReleaseIsFinal(cluster.Spec.PostgresVersion, currentTime) {
r.Recorder.Eventf(cluster, corev1.EventTypeWarning, "EndOfLifePostgresVersion",
"Postgres Version %v is End-Of-Life. We recommend upgrading to the latest supported version.",
cluster.Spec.PostgresVersion)
}

if cluster.Spec.Standby != nil &&
cluster.Spec.Standby.Enabled &&
Expand Down
62 changes: 62 additions & 0 deletions internal/controller/postgrescluster/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,4 +556,66 @@ spec:
Expect(instance.Spec.Replicas).To(PointTo(BeEquivalentTo(1)))
})
})

Context("Postgres version EOL", func() {
var cluster *v1beta1.PostgresCluster

BeforeEach(func() {
cluster = create(`
metadata:
name: old-postgres
spec:
postgresVersion: 11
image: postgres
instances:
- name: instance1
dataVolumeClaimSpec:
accessModes:
- "ReadWriteMany"
resources:
requests:
storage: 1Gi
backups:
pgbackrest:
image: pgbackrest
repos:
- name: repo1
volume:
volumeClaimSpec:
accessModes:
- "ReadWriteOnce"
resources:
requests:
storage: 1Gi
`)
Expect(reconcile(cluster)).To(BeZero())
})

AfterEach(func() {
ctx := context.Background()

if cluster != nil {
Expect(client.IgnoreNotFound(
suite.Client.Delete(ctx, cluster),
)).To(Succeed())

// Remove finalizers, if any, so the namespace can terminate.
Expect(client.IgnoreNotFound(
suite.Client.Patch(ctx, cluster, client.RawPatch(
client.Merge.Type(), []byte(`{"metadata":{"finalizers":[]}}`))),
)).To(Succeed())
}
})

Specify("Postgres EOL Warning Event", func() {
existing := &v1beta1.PostgresCluster{}
Expect(suite.Client.Get(
context.Background(), client.ObjectKeyFromObject(cluster), existing,
)).To(Succeed())

event, ok := <-test.Recorder.Events
Expect(ok).To(BeTrue())
Expect(event).To(ContainSubstring("End-Of-Life."))
})
})
})
27 changes: 27 additions & 0 deletions internal/postgres/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"fmt"
"strings"
"time"

corev1 "k8s.io/api/core/v1"

Expand Down Expand Up @@ -425,3 +426,29 @@ chmod +x /tmp/pg_rewind_tde.sh

return append([]string{"bash", "-ceu", "--", script, "startup"}, args...)
}

// ReleaseIsFinal returns whether or not t is definitively past the final
// scheduled release of a Postgres version.
// See: https://www.postgresql.org/support/versioning/
func ReleaseIsFinal(majorVersion int, t time.Time) bool {
switch majorVersion {
case 10:
return t.After(time.Date(2022, time.November+1, 10, 0, 0, 0, 0, time.UTC))
case 11:
return t.After(time.Date(2023, time.November+1, +9, 0, 0, 0, 0, time.UTC))
case 12:
return t.After(time.Date(2024, time.November+1, 14, 0, 0, 0, 0, time.UTC))
case 13:
return t.After(time.Date(2025, time.November+1, 13, 0, 0, 0, 0, time.UTC))
case 14:
return t.After(time.Date(2026, time.November+1, 12, 0, 0, 0, 0, time.UTC))
case 15:
return t.After(time.Date(2027, time.November+1, 11, 0, 0, 0, 0, time.UTC))
case 16:
return t.After(time.Date(2028, time.November+1, +9, 0, 0, 0, 0, time.UTC))
case 17:
return t.After(time.Date(2029, time.November+1, +8, 0, 0, 0, 0, time.UTC))
}

return false
}
23 changes: 23 additions & 0 deletions internal/postgres/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"path/filepath"
"strings"
"testing"
"time"

"gotest.tools/v3/assert"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -506,3 +507,25 @@ EOF
chmod +x /tmp/pg_rewind_tde.sh`))
})
}

func TestReleaseIsFinal(t *testing.T) {
// On November 4th, 2024, PG 10 and 11 were EOL and 12-17 were supported.
testDate, err := time.Parse("2006-Jan-02", "2024-Nov-04")
assert.NilError(t, err)
assert.Check(t, ReleaseIsFinal(10, testDate))
assert.Check(t, ReleaseIsFinal(11, testDate))
assert.Check(t, !ReleaseIsFinal(12, testDate))
assert.Check(t, !ReleaseIsFinal(13, testDate))
assert.Check(t, !ReleaseIsFinal(14, testDate))
assert.Check(t, !ReleaseIsFinal(15, testDate))
assert.Check(t, !ReleaseIsFinal(16, testDate))
assert.Check(t, !ReleaseIsFinal(17, testDate))

// On December 15th, 2024 we alert that PG 12 is EOL
testDate = testDate.AddDate(0, 1, 11)
assert.Check(t, ReleaseIsFinal(12, testDate))

// ReleaseIsFinal covers PG versions 10 and greater. Any version not covered
// by the case statement in ReleaseIsFinal returns false
assert.Check(t, !ReleaseIsFinal(1, testDate))
}

0 comments on commit b17d8f3

Please sign in to comment.