-
Notifications
You must be signed in to change notification settings - Fork 2
feat: update spec when db versions change #377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a9b49a3
fix: use most recently fetched primary instance id
jason-lynch 0724bb3
test: add brief sleep to cancel task test
jason-lynch 0254f55
feat: improve ds.PgEdgeVersion initializers
jason-lynch 8c1698f
feat: add conditions to etcd txn
jason-lynch b4597d1
feat: update host in host monitor
jason-lynch 534521a
chore: remove empty file
jason-lynch 1c51ca6
feat: record versions for replica instances
jason-lynch 8ee115b
feat: add service method to reconcile db versions
jason-lynch 03d9eae
feat: add databases monitor
jason-lynch 5f77185
test: add cluster test for external db upgrades
jason-lynch ee2de5a
chore: add 'old' pgedge repos to dev-lima
jason-lynch 4936e66
docs: update configuration doc
jason-lynch fb8d6eb
docs: minor postgres version upgrades for systemd
jason-lynch 7b5327f
docs: changelog entries
jason-lynch 6a5b26f
fix: reconcile versions improvements
jason-lynch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| kind: Added | ||
| body: Added a feature to enable manual Postgres minor version updates in systemd clusters. The Control Plane will now update its copy of the database spec when it detects changes to an instance's Postgres or Spock version. | ||
| time: 2026-05-04T15:02:35.045407-04:00 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| kind: Changed | ||
| body: Changed the instance monitoring system to query the Postgres and Spock versions for replica instances and report them in the databases API. | ||
| time: 2026-05-04T15:23:48.324604-04:00 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,292 @@ | ||
| //go:build cluster_test | ||
|
|
||
| package clustertest | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| controlplane "github.com/pgEdge/control-plane/api/apiv1/gen/control_plane" | ||
| "github.com/pgEdge/control-plane/client" | ||
| ) | ||
|
|
||
| func TestExternalUpgrade(t *testing.T) { | ||
| // Tests that the control plane updates its records when the user upgrades | ||
| // a database outside of our API. | ||
| t.Parallel() | ||
| ctx := t.Context() | ||
|
|
||
| const ( | ||
| startPostgresVersion string = "18.2" | ||
| upgradePostgresVersion string = "18.3" | ||
| upgradeImage string = "ghcr.io/pgedge/pgedge-postgres:18.3-spock5.0.6-standard-1" | ||
| spockVersion string = "5" | ||
| sleepDuration time.Duration = 5 * time.Second | ||
| ) | ||
|
|
||
| // Helper functions | ||
| assertSpecVersions := func(t *testing.T, spec *controlplane.DatabaseSpec, expectedSpecVersion string, expectedNodeVersions map[string]string) { | ||
| t.Helper() | ||
|
|
||
| actualNodeVersions := make(map[string]string, len(spec.Nodes)) | ||
| for _, node := range spec.Nodes { | ||
| var version string | ||
| if node.PostgresVersion != nil { | ||
| version = *node.PostgresVersion | ||
| } | ||
| actualNodeVersions[node.Name] = version | ||
| } | ||
| var actualSpecVersion string | ||
| if spec.PostgresVersion != nil { | ||
| actualSpecVersion = *spec.PostgresVersion | ||
| } | ||
| require.Equal(t, expectedSpecVersion, actualSpecVersion) | ||
| require.Equal(t, expectedNodeVersions, actualNodeVersions) | ||
| } | ||
| assertInstanceVersions := func(t *testing.T, instances []*controlplane.Instance, expectedNodeHostVersions map[string]map[string]string) { | ||
| t.Helper() | ||
|
|
||
| actualNodeHostVersions := map[string]map[string]string{} | ||
| for _, instance := range instances { | ||
| require.Equal(t, client.InstanceStateAvailable, instance.State) | ||
|
|
||
| if _, ok := actualNodeHostVersions[instance.NodeName]; !ok { | ||
| actualNodeHostVersions[instance.NodeName] = map[string]string{} | ||
| } | ||
| var version string | ||
| if instance.Postgres.Version != nil { | ||
| version = *instance.Postgres.Version | ||
| } | ||
| actualNodeHostVersions[instance.NodeName][instance.HostID] = version | ||
| } | ||
| require.Equal(t, expectedNodeHostVersions, actualNodeHostVersions) | ||
| } | ||
| upgradeService := func(t *testing.T, databaseID, nodeName, hostID string) { | ||
| t.Helper() | ||
|
|
||
| tLogf(t, "upgrading %s %s instance", nodeName, hostID) | ||
|
|
||
| ctx, cancel := context.WithTimeout(t.Context(), 30*time.Second) | ||
| defer cancel() | ||
|
|
||
| serviceName := dockerCmd(t, ctx, | ||
| "service", | ||
| "ls", | ||
| fmt.Sprintf("--filter=label=pgedge.database.id=%s", databaseID), | ||
| fmt.Sprintf("--filter=label=pgedge.node.name=%s", nodeName), | ||
| fmt.Sprintf("--filter=label=pgedge.host.id=%s", hostID), | ||
| "--format={{.Name}}", | ||
| ) | ||
| require.NotEmpty(t, serviceName) | ||
| dockerCmd(t, ctx, | ||
| "service", | ||
| "update", | ||
| fmt.Sprintf("--image=%s", upgradeImage), | ||
| // disabling healthchecks to speed up startup time | ||
| "--no-healthcheck", | ||
| serviceName, | ||
| ) | ||
| } | ||
|
|
||
| env := map[string]string{ | ||
| "PGEDGE_DATABASES_MONITOR_INTERVAL_SECONDS": "3", | ||
| } | ||
| cluster := NewCluster(t, ClusterConfig{ | ||
| Hosts: []HostConfig{ | ||
| {ID: "host-1", ExtraEnv: env}, | ||
| {ID: "host-2", ExtraEnv: env}, | ||
| {ID: "host-3", ExtraEnv: env}, | ||
| }, | ||
| }) | ||
| cluster.Init(t) | ||
|
|
||
| spec := &controlplane.DatabaseSpec{ | ||
| DatabaseName: "test_upgrade", | ||
| PostgresVersion: pointerTo(startPostgresVersion), | ||
| SpockVersion: pointerTo(spockVersion), | ||
| Nodes: []*controlplane.DatabaseNodeSpec{ | ||
| { | ||
| Name: "n1", | ||
| HostIds: []controlplane.Identifier{"host-1", "host-2"}, | ||
| }, | ||
| { | ||
| Name: "n2", | ||
| HostIds: []controlplane.Identifier{"host-3"}, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| tLog(t, "creating database") | ||
|
|
||
| createResp, err := cluster.Client().CreateDatabase(ctx, &controlplane.CreateDatabaseRequest{ | ||
| Spec: spec, | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| databaseID := createResp.Database.ID | ||
|
|
||
| t.Cleanup(func() { | ||
| // Use a new context for cleanup operations since t.Context is canceled. | ||
| ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second) | ||
| defer cancel() | ||
|
|
||
| if testConfig.skipCleanup { | ||
| tLogf(t, "skipping cleanup for database '%s'", databaseID) | ||
| return | ||
| } | ||
|
|
||
| tLogf(t, "cleaning up database '%s'", databaseID) | ||
|
|
||
| resp, err := cluster.Client().DeleteDatabase(ctx, &controlplane.DeleteDatabasePayload{ | ||
| DatabaseID: databaseID, | ||
| }) | ||
| if err != nil { | ||
| tLogf(t, "failed to delete database '%s': %v", databaseID, err) | ||
| return | ||
| } | ||
|
|
||
| tLog(t, "waiting for database deletion to complete") | ||
|
|
||
| err = waitForTaskComplete(ctx, cluster.Client(), databaseID, resp.Task.TaskID, time.Minute) | ||
| if err != nil { | ||
| tLogf(t, "failed while waiting for database deletion '%s'", databaseID) | ||
| return | ||
| } | ||
| }) | ||
|
|
||
| tLog(t, "waiting for database creation to complete") | ||
|
|
||
| err = waitForTaskComplete(ctx, cluster.Client(), databaseID, createResp.Task.TaskID, 3*time.Minute) | ||
| require.NoError(t, err) | ||
|
|
||
| tLog(t, "sleeping to allow instance monitor interval to complete") | ||
|
|
||
| time.Sleep(sleepDuration) | ||
|
|
||
| tLogf(t, "asserting that all instances and spec versions are %s", startPostgresVersion) | ||
|
|
||
| db, err := cluster.Client().GetDatabase(ctx, &controlplane.GetDatabasePayload{ | ||
| DatabaseID: databaseID, | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| assertSpecVersions(t, db.Spec, startPostgresVersion, map[string]string{ | ||
| "n1": "", | ||
| "n2": "", | ||
| }) | ||
| assertInstanceVersions(t, db.Instances, map[string]map[string]string{ | ||
| "n1": map[string]string{ | ||
| "host-1": startPostgresVersion, | ||
| "host-2": startPostgresVersion, | ||
| }, | ||
| "n2": map[string]string{ | ||
| "host-3": startPostgresVersion, | ||
| }, | ||
| }) | ||
|
|
||
| tLog(t, "getting database docker service names") | ||
|
|
||
| upgradeService(t, string(databaseID), "n1", "host-2") | ||
| upgradeService(t, string(databaseID), "n2", "host-3") | ||
|
|
||
| tLog(t, "sleeping to allow instance monitor interval and version reconciliation to complete") | ||
|
|
||
| time.Sleep(sleepDuration) | ||
|
|
||
| tLogf(t, "asserting that n2 is %s in the spec and that the n1-host-2 and n2-host-3 instances are %s", upgradePostgresVersion, upgradePostgresVersion) | ||
|
|
||
| db, err = cluster.Client().GetDatabase(ctx, &controlplane.GetDatabasePayload{ | ||
| DatabaseID: databaseID, | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| assertSpecVersions(t, db.Spec, startPostgresVersion, map[string]string{ | ||
| "n1": "", | ||
| "n2": upgradePostgresVersion, | ||
| }) | ||
| assertInstanceVersions(t, db.Instances, map[string]map[string]string{ | ||
| "n1": map[string]string{ | ||
| "host-1": startPostgresVersion, | ||
| "host-2": upgradePostgresVersion, | ||
| }, | ||
| "n2": map[string]string{ | ||
| "host-3": upgradePostgresVersion, | ||
| }, | ||
| }) | ||
|
|
||
| upgradeService(t, string(databaseID), "n1", "host-1") | ||
|
|
||
| tLog(t, "sleeping to allow monitor interval and version reconciliation to complete") | ||
|
|
||
| time.Sleep(sleepDuration) | ||
|
|
||
| tLogf(t, "asserting the top-level version is %s and that all instances are %s", upgradePostgresVersion, upgradePostgresVersion) | ||
|
|
||
| db, err = cluster.Client().GetDatabase(ctx, &controlplane.GetDatabasePayload{ | ||
| DatabaseID: databaseID, | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| assertSpecVersions(t, db.Spec, upgradePostgresVersion, map[string]string{ | ||
| "n1": "", | ||
| "n2": "", | ||
| }) | ||
| assertInstanceVersions(t, db.Instances, map[string]map[string]string{ | ||
| "n1": map[string]string{ | ||
| "host-1": upgradePostgresVersion, | ||
| "host-2": upgradePostgresVersion, | ||
| }, | ||
| "n2": map[string]string{ | ||
| "host-3": upgradePostgresVersion, | ||
| }, | ||
| }) | ||
|
|
||
| tLog(t, "performing a no-op update") | ||
|
|
||
| // We still expect to see some resource updates in the logs because the | ||
| // version number shows up in a few resources states. This does trigger a | ||
| // patroni reload in Swarm databases, which eats up time, but no actual | ||
| // changes should occur. | ||
|
|
||
| updateResp, err := cluster.Client().UpdateDatabase(ctx, &controlplane.UpdateDatabasePayload{ | ||
| DatabaseID: databaseID, | ||
| Request: &controlplane.UpdateDatabaseRequest{ | ||
| Spec: db.Spec, | ||
| }, | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| tLog(t, "waiting for database update to complete") | ||
|
|
||
| err = waitForTaskComplete(ctx, cluster.Client(), databaseID, updateResp.Task.TaskID, 3*time.Minute) | ||
| require.NoError(t, err) | ||
|
|
||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| tLog(t, "sleeping to allow instance monitor interval to complete") | ||
|
|
||
| time.Sleep(sleepDuration) | ||
|
|
||
| tLog(t, "asserting that top-level versions have not changed") | ||
|
|
||
| db, err = cluster.Client().GetDatabase(ctx, &controlplane.GetDatabasePayload{ | ||
| DatabaseID: databaseID, | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| assertSpecVersions(t, db.Spec, upgradePostgresVersion, map[string]string{ | ||
| "n1": "", | ||
| "n2": "", | ||
| }) | ||
| assertInstanceVersions(t, db.Instances, map[string]map[string]string{ | ||
| "n1": map[string]string{ | ||
| "host-1": upgradePostgresVersion, | ||
| "host-2": upgradePostgresVersion, | ||
| }, | ||
| "n2": map[string]string{ | ||
| "host-3": upgradePostgresVersion, | ||
| }, | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.