diff --git a/go.mod b/go.mod index 71cfc4ff3..6bf633cea 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/nais/api -go 1.26.10 +go 1.26.11 tool ( github.com/99designs/gqlgen @@ -41,7 +41,7 @@ require ( github.com/nais/api/pkg/apiclient v0.0.0-20250219111538-2b76a0fd6ed9 github.com/nais/bifrost v0.0.0-20260106105449-911627ac2c61 github.com/nais/liberator v0.0.0-20261008133002-00075fb0d007 - github.com/nais/pgrator/pkg/api v0.0.0-20261106143424-e72706ac0330 + github.com/nais/pgrator/pkg/api v0.0.0-20261113135456-297e807a50e6 github.com/nais/tester v0.2.0 github.com/nais/unleasherator v0.0.0-20251216221129-efebc54203fe github.com/nais/v13s/pkg/api v0.0.0-20261105092527-4fb2d5f61e67 diff --git a/go.sum b/go.sum index f920edb98..7af8f5059 100644 --- a/go.sum +++ b/go.sum @@ -809,6 +809,8 @@ github.com/nais/liberator v0.0.0-20261008133002-00075fb0d007 h1:F3ChkijLTpjxqGpQ github.com/nais/liberator v0.0.0-20261008133002-00075fb0d007/go.mod h1:PI0BYiAkX+ike2N1ZIOns0aUkhgiNKiCmy+YGKDR2R0= github.com/nais/pgrator/pkg/api v0.0.0-20261106143424-e72706ac0330 h1:bPVghfEV8ILMO9njAlBO/L/Vd6X9UkSedsSveztueSA= github.com/nais/pgrator/pkg/api v0.0.0-20261106143424-e72706ac0330/go.mod h1:jwBmSICTVLZyeJyD6ck+LLb2V5FsgsUbuCbfbHosew0= +github.com/nais/pgrator/pkg/api v0.0.0-20261113135456-297e807a50e6 h1:7bg8b4tTjkOjpLuaLZ0zd49OUefm3RImWDgSsAxrT3w= +github.com/nais/pgrator/pkg/api v0.0.0-20261113135456-297e807a50e6/go.mod h1:uMfm7kYuZxUNDMdf9sLv7MP2Y7RvQYWcI61NEd2Ni5c= github.com/nais/tester v0.2.0 h1:lcTkDP52ddw9l5s3KC4snUP+GlUpZMUtJ3XR2vF4G0w= github.com/nais/tester v0.2.0/go.mod h1:Pp7CtcVk/NZI3z0MW7V4j8CPMG5lhKZeAV4IaJKcslo= github.com/nais/unleasherator v0.0.0-20251216221129-efebc54203fe h1:CdRVopOihru4tXVwKZjhg6C8SbPLCQYOhJKpjBZYhjg= diff --git a/integration_tests/k8s_resources/postgres_pitr/dev/pg-crud-team/source.yaml b/integration_tests/k8s_resources/postgres_pitr/dev/pg-crud-team/source.yaml new file mode 100644 index 000000000..f43eaa7f6 --- /dev/null +++ b/integration_tests/k8s_resources/postgres_pitr/dev/pg-crud-team/source.yaml @@ -0,0 +1,52 @@ +apiVersion: nais.io/v1 +kind: Postgres +metadata: + name: pitr-source + namespace: pg-crud-team + uid: pitr-source-uid +spec: + majorVersion: "18" + highAvailability: true + extensions: + - name: pg_trgm + resources: + cpu: 250m + memory: 1Gi + diskSize: 20Gi +status: + clusterName: pg-pitr-source-main + ownership: + phase: Managed + clusterUID: pitr-source-cluster-uid +--- +apiVersion: postgresql.cnpg.io/v1 +kind: Cluster +metadata: + name: pg-pitr-source-main + namespace: pg-crud-team + uid: pitr-source-cluster-uid + ownerReferences: + - apiVersion: nais.io/v1 + kind: Postgres + name: pitr-source + uid: pitr-source-uid + controller: true +spec: + plugins: + - name: barman-cloud.cloudnative-pg.io + parameters: + barmanObjectName: pitr-source-archive +status: {} +--- +apiVersion: barmancloud.cnpg.io/v1 +kind: ObjectStore +metadata: + name: pitr-source-archive + namespace: pg-crud-team +spec: + configuration: + destinationPath: gs://pitr-source-archive +status: + serverRecoveryWindow: + pg-pitr-source-main: + firstRecoverabilityPoint: "2026-09-29T00:00:00Z" diff --git a/integration_tests/postgres_pitr.lua b/integration_tests/postgres_pitr.lua new file mode 100644 index 000000000..ff0b13679 --- /dev/null +++ b/integration_tests/postgres_pitr.lua @@ -0,0 +1,100 @@ +local member = User.new("pitr-member", "pitr-member@usersen.com") +local outsider = User.new("pitr-outsider", "pitr-outsider@usersen.com") +local team = Team.new("pg-crud-team", "Postgres PITR", "#pitr") +team:addMember(member) +local otherTeam = Team.new("pg-pitr-other", "PITR source isolation", "#pitr-other") +otherTeam:addMember(member) +Helper.readK8sResources("k8s_resources/postgres_crud") +Helper.readK8sResources("k8s_resources/postgres_pitr") + +local function restore(name, source, target, extra) + return string.format([[mutation { createPostgres(input: { + teamSlug: "pg-crud-team", environmentName: "dev", name: "%s", majorVersion: "18", + recovery: { sourcePostgres: "%s", targetTime: "%s" }, %s + }) { postgres { name extensions highAvailability resources { cpu memory diskSize } + recovery { sourcePostgres targetTime completed failureMessage } } } }]], name, source, target, extra or "") +end + +Test.gql("PITR source exposes the preserved physical archive lower bound", function(t) + t.addHeader("x-user-email", member:email()) + t.query [[{ team(slug: "pg-crud-team") { environment(name: "dev") { postgres(name: "pitr-source") { earliestRestoreTime } } } }]] + t.check { data = { team = { environment = { postgres = { earliestRestoreTime = "2026-09-29T00:00:00Z" } } } } } +end) + +Test.gql("PITR uses normal create authorization", function(t) + t.addHeader("x-user-email", outsider:email()) + t.query(restore("denied-restore", "pitr-source", "2026-09-30T12:00:00Z")) + t.check { data = Null, errors = { { locations = NotNull(), path = { "createPostgres" }, message = Contains("You are authenticated") } } } +end) + +Test.gql("PITR creates an independent Postgres and inherits source configuration", function(t) + t.addHeader("x-user-email", member:email()) + t.query(restore("pitr-restored", "pitr-source", "2026-09-30T14:00:00+02:00")) + t.check { data = { createPostgres = { postgres = { + name = "pitr-restored", extensions = { "pg_trgm" }, highAvailability = true, + resources = { cpu = "250m", memory = "1Gi", diskSize = "20Gi" }, + recovery = { sourcePostgres = "pitr-source", targetTime = "2026-09-30T12:00:00Z", completed = false, failureMessage = Null }, + } } } } +end) + +Test.k8s("PITR pins source UID and physical archive identity", function(t) + t.check("nais.io/v1", "postgres", "dev", team:slug(), "pitr-restored", { + apiVersion = "nais.io/v1", + kind = "Postgres", + metadata = Ignore(), + spec = { + majorVersion = "18", + highAvailability = true, + extensions = { { name = "pg_trgm" } }, + resources = { cpu = "250m", memory = "1Gi", diskSize = "20Gi" }, + recovery = { sourcePostgres = "pitr-source", sourceUID = "pitr-source-uid", bucketName = "pitr-source-archive", serverName = "pg-pitr-source-main", targetTime = "2026-09-30T12:00:00Z" }, + }, + }) +end) + +for _, case in ipairs({ + { name = "before archive", source = "pitr-source", time = "2026-09-28T12:00:00Z", message = "earliest recoverable" }, + { name = "missing source", source = "unknown", time = "2026-09-30T12:00:00Z", message = "not found in this team and environment" }, + { name = "missing physical source", source = "branchless", time = "2026-09-30T12:00:00Z", message = "no recoverable cluster" }, + { name = "smaller disk", source = "pitr-source", time = "2026-09-30T12:00:00Z", extra = 'diskSize: "10Gi"', message = "must not be smaller" }, + { name = "missing source extension", source = "pitr-source", time = "2026-09-30T12:00:00Z", extra = "extensions: []", message = "must include source extension" }, +}) do + Test.gql("PITR rejects " .. case.name, function(t) + t.addHeader("x-user-email", member:email()) + t.query(restore("invalid-restore", case.source, case.time, case.extra)) + t.check { data = Null, errors = { { locations = NotNull(), path = { "createPostgres" }, message = Contains(case.message) } } } + end) +end + +Test.gql("PITR cannot change PostgreSQL major version", function(t) + t.addHeader("x-user-email", member:email()) + t.query [[mutation { createPostgres(input: { + teamSlug: "pg-crud-team", environmentName: "dev", name: "wrong-major", majorVersion: "17", + recovery: { sourcePostgres: "pitr-source", targetTime: "2026-09-30T12:00:00Z" } + }) { postgres { name } } }]] + t.check { data = Null, errors = { { locations = NotNull(), path = { "createPostgres" }, message = Contains("source PostgreSQL major version 18") } } } +end) + +Test.gql("PITR source lookup cannot cross environments", function(t) + t.addHeader("x-user-email", member:email()) + t.query [[mutation { createPostgres(input: { + teamSlug: "pg-crud-team", environmentName: "dev-gcp", name: "cross-env", majorVersion: "18", + recovery: { sourcePostgres: "pitr-source", targetTime: "2026-09-30T12:00:00Z" } + }) { postgres { name } } }]] + t.check { data = Null, errors = { { locations = NotNull(), path = { "createPostgres" }, message = Contains("not found in this team and environment") } } } +end) + +Test.gql("PITR creation is audited with source and UTC target", function(t) + t.addHeader("x-user-email", member:email()) + t.query [[{ team(slug: "pg-crud-team") { activityLog(filter: { activityTypes: [POSTGRES_CREATED] }) { nodes { message } } } }]] + t.check { data = { team = { activityLog = { nodes = { { message = "Requested Postgres recovery from pitr-source to 2026-09-30T12:00:00Z" } } } } } } +end) + +Test.gql("PITR source lookup cannot cross teams even for a member of both", function(t) + t.addHeader("x-user-email", member:email()) + t.query [[mutation { createPostgres(input: { + teamSlug: "pg-pitr-other", environmentName: "dev", name: "cross-team", majorVersion: "18", + recovery: { sourcePostgres: "pitr-source", targetTime: "2026-09-30T12:00:00Z" } + }) { postgres { name } } }]] + t.check { data = Null, errors = { { locations = NotNull(), path = { "createPostgres" }, message = Contains("not found in this team and environment") } } } +end) diff --git a/internal/graph/gengql/postgres.generated.go b/internal/graph/gengql/postgres.generated.go index 0f9a35e5c..41dd1aa87 100644 --- a/internal/graph/gengql/postgres.generated.go +++ b/internal/graph/gengql/postgres.generated.go @@ -33,6 +33,7 @@ type PostgresResolver interface { State(ctx context.Context, obj *postgres.Postgres) (postgres.PostgresBranchState, error) Configuration(ctx context.Context, obj *postgres.Postgres) (*postgres.PostgresConfiguration, error) EarliestRestoreTime(ctx context.Context, obj *postgres.Postgres) (*time.Time, error) + Issues(ctx context.Context, obj *postgres.Postgres, first *int, after *pagination.Cursor, last *int, before *pagination.Cursor, orderBy *issue.IssueOrder, filter *issue.ResourceIssueFilter) (*issue.IssueConnection, error) Workloads(ctx context.Context, obj *postgres.Postgres, first *int, after *pagination.Cursor, last *int, before *pagination.Cursor) (*pagination.Connection[*postgres.PostgresWorkload], error) @@ -512,6 +513,38 @@ func (ec *executionContext) fieldContext_Postgres_earliestRestoreTime(_ context. return graphql.NewScalarFieldContext("Postgres", field, true, true, errors.New("field of type Time does not have child fields")) } +func (ec *executionContext) _Postgres_recovery(ctx context.Context, field graphql.CollectedField, obj *postgres.Postgres) (ret graphql.Marshaler) { + return graphql.ResolveField( + ctx, + ec.OperationContext, + field, + func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return ec.fieldContext_Postgres_recovery(ctx, field) + }, + func(ctx context.Context) (any, error) { + return obj.Recovery, nil + }, + nil, + func(ctx context.Context, selections ast.SelectionSet, v *postgres.PostgresRecovery) graphql.Marshaler { + return ec.marshalOPostgresRecovery2ᚖgithubᚗcomᚋnaisᚋapiᚋinternalᚋpersistenceᚋpostgresᚐPostgresRecovery(ctx, selections, v) + }, + true, + false, + ) +} +func (ec *executionContext) fieldContext_Postgres_recovery(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Postgres", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return ec.childFields_PostgresRecovery(ctx, field) + }, + } + return fc, nil +} + func (ec *executionContext) _Postgres_issues(ctx context.Context, field graphql.CollectedField, obj *postgres.Postgres) (ret graphql.Marshaler) { return graphql.ResolveField( ctx, @@ -3615,6 +3648,98 @@ func (ec *executionContext) fieldContext_PostgresPersonalAccessCreatedActivityLo return graphql.NewScalarFieldContext("PostgresPersonalAccessCreatedActivityLogEntryData", field, false, false, errors.New("field of type String does not have child fields")) } +func (ec *executionContext) _PostgresRecovery_sourcePostgres(ctx context.Context, field graphql.CollectedField, obj *postgres.PostgresRecovery) (ret graphql.Marshaler) { + return graphql.ResolveField( + ctx, + ec.OperationContext, + field, + func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return ec.fieldContext_PostgresRecovery_sourcePostgres(ctx, field) + }, + func(ctx context.Context) (any, error) { + return obj.SourcePostgres, nil + }, + nil, + func(ctx context.Context, selections ast.SelectionSet, v string) graphql.Marshaler { + return ec.marshalNString2string(ctx, selections, v) + }, + true, + true, + ) +} +func (ec *executionContext) fieldContext_PostgresRecovery_sourcePostgres(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + return graphql.NewScalarFieldContext("PostgresRecovery", field, false, false, errors.New("field of type String does not have child fields")) +} + +func (ec *executionContext) _PostgresRecovery_targetTime(ctx context.Context, field graphql.CollectedField, obj *postgres.PostgresRecovery) (ret graphql.Marshaler) { + return graphql.ResolveField( + ctx, + ec.OperationContext, + field, + func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return ec.fieldContext_PostgresRecovery_targetTime(ctx, field) + }, + func(ctx context.Context) (any, error) { + return obj.TargetTime, nil + }, + nil, + func(ctx context.Context, selections ast.SelectionSet, v time.Time) graphql.Marshaler { + return ec.marshalNTime2timeᚐTime(ctx, selections, v) + }, + true, + true, + ) +} +func (ec *executionContext) fieldContext_PostgresRecovery_targetTime(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + return graphql.NewScalarFieldContext("PostgresRecovery", field, false, false, errors.New("field of type Time does not have child fields")) +} + +func (ec *executionContext) _PostgresRecovery_completed(ctx context.Context, field graphql.CollectedField, obj *postgres.PostgresRecovery) (ret graphql.Marshaler) { + return graphql.ResolveField( + ctx, + ec.OperationContext, + field, + func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return ec.fieldContext_PostgresRecovery_completed(ctx, field) + }, + func(ctx context.Context) (any, error) { + return obj.Completed, nil + }, + nil, + func(ctx context.Context, selections ast.SelectionSet, v bool) graphql.Marshaler { + return ec.marshalNBoolean2bool(ctx, selections, v) + }, + true, + true, + ) +} +func (ec *executionContext) fieldContext_PostgresRecovery_completed(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + return graphql.NewScalarFieldContext("PostgresRecovery", field, false, false, errors.New("field of type Boolean does not have child fields")) +} + +func (ec *executionContext) _PostgresRecovery_failureMessage(ctx context.Context, field graphql.CollectedField, obj *postgres.PostgresRecovery) (ret graphql.Marshaler) { + return graphql.ResolveField( + ctx, + ec.OperationContext, + field, + func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return ec.fieldContext_PostgresRecovery_failureMessage(ctx, field) + }, + func(ctx context.Context) (any, error) { + return obj.FailureMessage, nil + }, + nil, + func(ctx context.Context, selections ast.SelectionSet, v *string) graphql.Marshaler { + return ec.marshalOString2ᚖstring(ctx, selections, v) + }, + true, + false, + ) +} +func (ec *executionContext) fieldContext_PostgresRecovery_failureMessage(_ context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + return graphql.NewScalarFieldContext("PostgresRecovery", field, false, false, errors.New("field of type String does not have child fields")) +} + func (ec *executionContext) _PostgresResources_cpu(ctx context.Context, field graphql.CollectedField, obj *postgres.PostgresResources) (ret graphql.Marshaler) { return graphql.ResolveField( ctx, @@ -4589,7 +4714,7 @@ func (ec *executionContext) unmarshalInputCreatePostgresInput(ctx context.Contex asMap[k] = v } - fieldsInOrder := [...]string{"name", "environmentName", "teamSlug", "majorVersion", "extensions", "highAvailability", "cpu", "memory", "diskSize", "labels"} + fieldsInOrder := [...]string{"name", "environmentName", "teamSlug", "majorVersion", "extensions", "highAvailability", "cpu", "memory", "diskSize", "labels", "recovery"} for _, k := range fieldsInOrder { v, ok := asMap[k] if !ok { @@ -4666,6 +4791,13 @@ func (ec *executionContext) unmarshalInputCreatePostgresInput(ctx context.Contex return it, err } it.Labels = data + case "recovery": + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("recovery")) + data, err := ec.unmarshalOPostgresRecoveryInput2ᚖgithubᚗcomᚋnaisᚋapiᚋinternalᚋpersistenceᚋpostgresᚐPostgresRecoveryInput(ctx, v) + if err != nil { + return it, err + } + it.Recovery = data } } return it, nil @@ -4715,6 +4847,43 @@ func (ec *executionContext) unmarshalInputDeletePostgresInput(ctx context.Contex return it, nil } +func (ec *executionContext) unmarshalInputPostgresRecoveryInput(ctx context.Context, obj any) (postgres.PostgresRecoveryInput, error) { + var it postgres.PostgresRecoveryInput + if obj == nil { + return it, nil + } + + asMap := map[string]any{} + for k, v := range obj.(map[string]any) { + asMap[k] = v + } + + fieldsInOrder := [...]string{"sourcePostgres", "targetTime"} + for _, k := range fieldsInOrder { + v, ok := asMap[k] + if !ok { + continue + } + switch k { + case "sourcePostgres": + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("sourcePostgres")) + data, err := ec.unmarshalNString2string(ctx, v) + if err != nil { + return it, err + } + it.SourcePostgres = data + case "targetTime": + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("targetTime")) + data, err := ec.unmarshalNTime2timeᚐTime(ctx, v) + if err != nil { + return it, err + } + it.TargetTime = data + } + } + return it, nil +} + func (ec *executionContext) unmarshalInputTeamPostgresFilter(ctx context.Context, obj any) (postgres.TeamPostgresFilter, error) { var it postgres.TeamPostgresFilter if obj == nil { @@ -5168,6 +5337,8 @@ func (ec *executionContext) _Postgres(ctx context.Context, sel ast.SelectionSet, } out.Concurrently(i, func(ctx context.Context) graphql.Marshaler { return innerFunc(ctx, out) }) + case "recovery": + out.Values[i] = ec._Postgres_recovery(ctx, field, obj) case "issues": field := field @@ -6626,6 +6797,57 @@ func (ec *executionContext) _PostgresPersonalAccessCreatedActivityLogEntryData(c return out } +var postgresRecoveryImplementors = []string{"PostgresRecovery"} + +func (ec *executionContext) _PostgresRecovery(ctx context.Context, sel ast.SelectionSet, obj *postgres.PostgresRecovery) graphql.Marshaler { + fields := graphql.CollectFields(ec.OperationContext, sel, postgresRecoveryImplementors) + + out := graphql.NewFieldSet(fields) + deferred := make(map[string]*graphql.FieldSet) + for i, field := range fields { + switch field.Name { + case "__typename": + out.Values[i] = graphql.MarshalString("PostgresRecovery") + case "sourcePostgres": + out.Values[i] = ec._PostgresRecovery_sourcePostgres(ctx, field, obj) + if out.Values[i] == graphql.Null { + out.Invalids++ + } + case "targetTime": + out.Values[i] = ec._PostgresRecovery_targetTime(ctx, field, obj) + if out.Values[i] == graphql.Null { + out.Invalids++ + } + case "completed": + out.Values[i] = ec._PostgresRecovery_completed(ctx, field, obj) + if out.Values[i] == graphql.Null { + out.Invalids++ + } + case "failureMessage": + out.Values[i] = ec._PostgresRecovery_failureMessage(ctx, field, obj) + default: + panic("unknown field " + strconv.Quote(field.Name)) + } + } + out.Dispatch(ctx) + if out.Invalids > 0 { + return graphql.Null + } + + atomic.AddInt32(&ec.Deferred, int32(min(len(deferred), math.MaxInt32))) + + for label, dfs := range deferred { + ec.ProcessDeferredGroup(graphql.DeferredGroup{ + Label: label, + Path: graphql.GetPath(ctx), + FieldSet: dfs, + Context: ctx, + }) + } + + return out +} + var postgresResourcesImplementors = []string{"PostgresResources"} func (ec *executionContext) _PostgresResources(ctx context.Context, sel ast.SelectionSet, obj *postgres.PostgresResources) graphql.Marshaler { @@ -7606,6 +7828,21 @@ func (ec *executionContext) marshalOPostgresConfiguration2ᚖgithubᚗcomᚋnais return ec._PostgresConfiguration(ctx, sel, v) } +func (ec *executionContext) marshalOPostgresRecovery2ᚖgithubᚗcomᚋnaisᚋapiᚋinternalᚋpersistenceᚋpostgresᚐPostgresRecovery(ctx context.Context, sel ast.SelectionSet, v *postgres.PostgresRecovery) graphql.Marshaler { + if v == nil { + return graphql.Null + } + return ec._PostgresRecovery(ctx, sel, v) +} + +func (ec *executionContext) unmarshalOPostgresRecoveryInput2ᚖgithubᚗcomᚋnaisᚋapiᚋinternalᚋpersistenceᚋpostgresᚐPostgresRecoveryInput(ctx context.Context, v any) (*postgres.PostgresRecoveryInput, error) { + if v == nil { + return nil, nil + } + res, err := ec.unmarshalInputPostgresRecoveryInput(ctx, v) + return &res, graphql.ErrorOnPath(ctx, err) +} + func (ec *executionContext) unmarshalOTeamPostgresFilter2ᚖgithubᚗcomᚋnaisᚋapiᚋinternalᚋpersistenceᚋpostgresᚐTeamPostgresFilter(ctx context.Context, v any) (*postgres.TeamPostgresFilter, error) { if v == nil { return nil, nil diff --git a/internal/graph/gengql/root_.generated.go b/internal/graph/gengql/root_.generated.go index 5bffdc640..30aa9c5e9 100644 --- a/internal/graph/gengql/root_.generated.go +++ b/internal/graph/gengql/root_.generated.go @@ -1947,6 +1947,7 @@ type ComplexityRoot struct { Labels func(childComplexity int) int MajorVersion func(childComplexity int) int Name func(childComplexity int) int + Recovery func(childComplexity int) int Resources func(childComplexity int) int State func(childComplexity int) int Team func(childComplexity int) int @@ -2130,6 +2131,13 @@ type ComplexityRoot struct { Username func(childComplexity int) int } + PostgresRecovery struct { + Completed func(childComplexity int) int + FailureMessage func(childComplexity int) int + SourcePostgres func(childComplexity int) int + TargetTime func(childComplexity int) int + } + PostgresResources struct { CPU func(childComplexity int) int DiskSize func(childComplexity int) int @@ -12260,6 +12268,13 @@ func (e *executableSchema) Complexity(ctx context.Context, typeName, field strin return e.ComplexityRoot.Postgres.Name(childComplexity), true + case "Postgres.recovery": + if e.ComplexityRoot.Postgres.Recovery == nil { + break + } + + return e.ComplexityRoot.Postgres.Recovery(childComplexity), true + case "Postgres.resources": if e.ComplexityRoot.Postgres.Resources == nil { break @@ -13133,6 +13148,34 @@ func (e *executableSchema) Complexity(ctx context.Context, typeName, field strin return e.ComplexityRoot.PostgresPersonalAccessCreatedActivityLogEntryData.Username(childComplexity), true + case "PostgresRecovery.completed": + if e.ComplexityRoot.PostgresRecovery.Completed == nil { + break + } + + return e.ComplexityRoot.PostgresRecovery.Completed(childComplexity), true + + case "PostgresRecovery.failureMessage": + if e.ComplexityRoot.PostgresRecovery.FailureMessage == nil { + break + } + + return e.ComplexityRoot.PostgresRecovery.FailureMessage(childComplexity), true + + case "PostgresRecovery.sourcePostgres": + if e.ComplexityRoot.PostgresRecovery.SourcePostgres == nil { + break + } + + return e.ComplexityRoot.PostgresRecovery.SourcePostgres(childComplexity), true + + case "PostgresRecovery.targetTime": + if e.ComplexityRoot.PostgresRecovery.TargetTime == nil { + break + } + + return e.ComplexityRoot.PostgresRecovery.TargetTime(childComplexity), true + case "PostgresResources.cpu": if e.ComplexityRoot.PostgresResources.CPU == nil { break @@ -22860,6 +22903,7 @@ func (e *executableSchema) Exec(ctx context.Context) graphql.ResponseHandler { ec.unmarshalInputOpenSearchAccessOrder, ec.unmarshalInputOpenSearchFilter, ec.unmarshalInputOpenSearchOrder, + ec.unmarshalInputPostgresRecoveryInput, ec.unmarshalInputReconcilerConfigInput, ec.unmarshalInputRemoveConfigValueInput, ec.unmarshalInputRemoveRepositoryFromTeamInput, @@ -29615,6 +29659,8 @@ type Postgres implements Node { configuration: PostgresConfiguration "Earliest point in time covered by available backups, when known." earliestRestoreTime: Time + "Immutable recovery origin and observed bootstrap progress, if created from a backup." + recovery: PostgresRecovery "Issues reported for this database." issues( "Get the first n items in the connection." @@ -30217,6 +30263,28 @@ input CreatePostgresInput { diskSize: String "Initial user-defined labels to apply." labels: [ResourceLabelInput!] + "Initialize from an existing database in the same team and environment instead of creating an empty database." + recovery: PostgresRecoveryInput +} + +"An explicit point-in-time recovery source for a new Postgres database." +input PostgresRecoveryInput { + "Name of the source Postgres in the destination team and environment." + sourcePostgres: String! + "Restore to this explicit instant. Must be within the available archive and not in the future." + targetTime: Time! +} + +"The immutable origin and bootstrap progress of a recovered database." +type PostgresRecovery { + "Name of the source database at creation; the source may since have been deleted." + sourcePostgres: String! + "The requested point in time, not the database creation time." + targetTime: Time! + "Recovery is checkpointed and the database has a successful backup of its own." + completed: Boolean! + "A terminal recovery failure, if reported by the operator." + failureMessage: String } "Result of creating a Postgres." @@ -41042,6 +41110,8 @@ func (ec *executionContext) childFields_Postgres(ctx context.Context, field grap return ec.fieldContext_Postgres_configuration(ctx, field) case "earliestRestoreTime": return ec.fieldContext_Postgres_earliestRestoreTime(ctx, field) + case "recovery": + return ec.fieldContext_Postgres_recovery(ctx, field) case "issues": return ec.fieldContext_Postgres_issues(ctx, field) case "majorVersion": @@ -41196,6 +41266,20 @@ func (ec *executionContext) childFields_PostgresPersonalAccessCreatedActivityLog return nil, fmt.Errorf("no field named %q was found under type PostgresPersonalAccessCreatedActivityLogEntryData", field.Name) } +func (ec *executionContext) childFields_PostgresRecovery(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + switch field.Name { + case "sourcePostgres": + return ec.fieldContext_PostgresRecovery_sourcePostgres(ctx, field) + case "targetTime": + return ec.fieldContext_PostgresRecovery_targetTime(ctx, field) + case "completed": + return ec.fieldContext_PostgresRecovery_completed(ctx, field) + case "failureMessage": + return ec.fieldContext_PostgresRecovery_failureMessage(ctx, field) + } + return nil, fmt.Errorf("no field named %q was found under type PostgresRecovery", field.Name) +} + func (ec *executionContext) childFields_PostgresResources(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { switch field.Name { case "cpu": diff --git a/internal/graph/schema/postgres.graphqls b/internal/graph/schema/postgres.graphqls index 8b0fd36c7..add607d4f 100644 --- a/internal/graph/schema/postgres.graphqls +++ b/internal/graph/schema/postgres.graphqls @@ -122,6 +122,8 @@ type Postgres implements Node { configuration: PostgresConfiguration "Earliest point in time covered by available backups, when known." earliestRestoreTime: Time + "Immutable recovery origin and observed bootstrap progress, if created from a backup." + recovery: PostgresRecovery "Issues reported for this database." issues( "Get the first n items in the connection." @@ -724,6 +726,28 @@ input CreatePostgresInput { diskSize: String "Initial user-defined labels to apply." labels: [ResourceLabelInput!] + "Initialize from an existing database in the same team and environment instead of creating an empty database." + recovery: PostgresRecoveryInput +} + +"An explicit point-in-time recovery source for a new Postgres database." +input PostgresRecoveryInput { + "Name of the source Postgres in the destination team and environment." + sourcePostgres: String! + "Restore to this explicit instant. Must be within the available archive and not in the future." + targetTime: Time! +} + +"The immutable origin and bootstrap progress of a recovered database." +type PostgresRecovery { + "Name of the source database at creation; the source may since have been deleted." + sourcePostgres: String! + "The requested point in time, not the database creation time." + targetTime: Time! + "Recovery is checkpointed and the database has a successful backup of its own." + completed: Boolean! + "A terminal recovery failure, if reported by the operator." + failureMessage: String } "Result of creating a Postgres." diff --git a/internal/persistence/postgres/activitylog.go b/internal/persistence/postgres/activitylog.go index 5807d99ea..fe4336225 100644 --- a/internal/persistence/postgres/activitylog.go +++ b/internal/persistence/postgres/activitylog.go @@ -87,8 +87,18 @@ func init() { func transformPostgresActivityLogEntry(entry activitylog.GenericActivityLogEntry) (activitylog.ActivityLogEntry, error) { switch entry.Action { case activitylog.ActivityLogEntryActionCreated: + message := "Created Postgres" + if len(entry.Data) > 0 { + data, err := activitylog.UnmarshalData[PostgresCreatedActivityLogEntryData](entry) + if err != nil { + return nil, fmt.Errorf("transforming Postgres creation data: %w", err) + } + if data.Recovery != nil { + message = fmt.Sprintf("Requested Postgres recovery from %s to %s", data.Recovery.SourcePostgres, data.Recovery.TargetTime.UTC().Format(time.RFC3339)) + } + } return PostgresCreatedActivityLogEntry{ - GenericActivityLogEntry: entry.WithMessage("Created Postgres"), + GenericActivityLogEntry: entry.WithMessage(message), }, nil case activitylog.ActivityLogEntryActionUpdated: data, err := activitylog.UnmarshalData[PostgresUpdatedActivityLogEntryData](entry) @@ -195,6 +205,11 @@ type PostgresCreatedActivityLogEntry struct { activitylog.GenericActivityLogEntry } +type PostgresCreatedActivityLogEntryData struct { + Recovery *PostgresRecoveryInput `json:"recovery,omitempty"` + SourceUID string `json:"sourceUID,omitempty"` +} + type PostgresUpdatedActivityLogEntry struct { activitylog.GenericActivityLogEntry Data *PostgresUpdatedActivityLogEntryData `json:"data"` diff --git a/internal/persistence/postgres/branch_operations.go b/internal/persistence/postgres/branch_operations.go deleted file mode 100644 index 252045719..000000000 --- a/internal/persistence/postgres/branch_operations.go +++ /dev/null @@ -1,115 +0,0 @@ -package postgres - -import ( - "context" - "fmt" - "time" - - "github.com/nais/api/internal/kubernetes/watcher" - k8serrors "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/apimachinery/pkg/runtime/schema" -) - -var ( - cnpgClusterGVR = schema.GroupVersionResource{ - Group: "postgresql.cnpg.io", Version: "v1", Resource: "clusters", - } - barmanObjectStoreGVR = schema.GroupVersionResource{ - Group: "barmancloud.cnpg.io", Version: "v1", Resource: "objectstores", - } -) - -func EarliestRestoreTime(ctx context.Context, branch *PostgresBranch) (*time.Time, error) { - if branch.ClusterName == "" { - return nil, nil - } - client, err := fromContext(ctx).postgresWatcher.SystemAuthenticatedClient( - ctx, branch.EnvironmentName, watcher.WithImpersonatedClientGVR(cnpgClusterGVR), - ) - if err != nil { - return nil, err - } - clusterName := branch.clusterName() - cluster, err := client.Namespace(branch.TeamSlug.String()).Get(ctx, clusterName, metav1.GetOptions{}) - if k8serrors.IsNotFound(err) { - return nil, nil - } - if err != nil { - return nil, fmt.Errorf("getting CNPG cluster %q for Postgres branch %q: %w", clusterName, branch.Name, err) - } - - objectStoreName, err := barmanObjectStoreName(cluster) - if err != nil { - return nil, fmt.Errorf("reading Barman ObjectStore name from CNPG cluster %q: %w", clusterName, err) - } - if objectStoreName != "" { - objectStoreClient, err := fromContext(ctx).postgresWatcher.SystemAuthenticatedClient( - ctx, branch.EnvironmentName, watcher.WithImpersonatedClientGVR(barmanObjectStoreGVR), - ) - if err != nil { - return nil, err - } - objectStore, err := objectStoreClient.Namespace(branch.TeamSlug.String()).Get(ctx, objectStoreName, metav1.GetOptions{}) - if err != nil && !k8serrors.IsNotFound(err) { - return nil, fmt.Errorf("getting Barman ObjectStore %q for CNPG cluster %q: %w", objectStoreName, clusterName, err) - } - if err == nil { - earliest, err := firstRecoverabilityPoint(objectStore, clusterName) - if err != nil || earliest != nil { - return earliest, err - } - } - } - - // Older CNPG configurations publish the recovery point directly on Cluster. - value, found, err := unstructured.NestedString(cluster.Object, "status", "firstRecoverabilityPoint") - if err != nil { - return nil, fmt.Errorf("reading firstRecoverabilityPoint from CNPG cluster %q: %w", clusterName, err) - } - if !found || value == "" { - return nil, nil - } - earliest, err := time.Parse(time.RFC3339, value) - if err != nil { - return nil, fmt.Errorf("parsing firstRecoverabilityPoint %q from CNPG cluster %q: %w", value, clusterName, err) - } - return &earliest, nil -} - -func barmanObjectStoreName(cluster *unstructured.Unstructured) (string, error) { - plugins, found, err := unstructured.NestedSlice(cluster.Object, "spec", "plugins") - if err != nil || !found { - return "", err - } - for _, item := range plugins { - plugin, ok := item.(map[string]any) - if !ok { - return "", fmt.Errorf("CNPG plugin configuration has unexpected type %T", item) - } - name, found, err := unstructured.NestedString(plugin, "parameters", "barmanObjectName") - if err != nil { - return "", err - } - if found && name != "" { - return name, nil - } - } - return "", nil -} - -func firstRecoverabilityPoint(objectStore *unstructured.Unstructured, clusterName string) (*time.Time, error) { - value, found, err := unstructured.NestedString(objectStore.Object, "status", "serverRecoveryWindow", clusterName, "firstRecoverabilityPoint") - if err != nil { - return nil, err - } - if !found || value == "" { - return nil, nil - } - earliest, err := time.Parse(time.RFC3339, value) - if err != nil { - return nil, fmt.Errorf("parsing firstRecoverabilityPoint %q: %w", value, err) - } - return &earliest, nil -} diff --git a/internal/persistence/postgres/models.go b/internal/persistence/postgres/models.go index 51f1fe72a..e2ca10c1e 100644 --- a/internal/persistence/postgres/models.go +++ b/internal/persistence/postgres/models.go @@ -118,6 +118,7 @@ type Postgres struct { Resources PostgresResources `json:"resources"` Labels []*model.ResourceLabel `json:"labels"` CreatedAt time.Time `json:"createdAt"` + Recovery *PostgresRecovery `json:"recovery,omitempty"` directClusterName *string physicalState PostgresBranchState configuration *PostgresConfiguration @@ -407,6 +408,20 @@ func toPostgres(u *unstructured.Unstructured, environmentName string) (*Postgres for _, extension := range obj.Spec.Extensions { extensions = append(extensions, extension.Name) } + var recovery *PostgresRecovery + if obj.Spec.Recovery != nil { + recovery = &PostgresRecovery{SourcePostgres: obj.Spec.Recovery.SourcePostgres, TargetTime: obj.Spec.Recovery.TargetTime.Time} + if obj.Status != nil { + for _, condition := range obj.Status.Conditions { + if condition.Type == "RecoveryCompleted" && condition.Status == metav1.ConditionTrue { + recovery.Completed = obj.Status.Ownership != nil && obj.Status.Ownership.ClusterUID != "" && condition.Message == obj.Status.Ownership.ClusterUID + } + if condition.Type == "RecoveryFailed" && condition.Status == metav1.ConditionTrue { + recovery.FailureMessage = new(condition.Message) + } + } + } + } return &Postgres{ Name: obj.Name, EnvironmentName: environmentName, TeamSlug: slug.Slug(obj.Namespace), ActiveBranch: active, DesiredActiveBranch: strPtr(obj.Spec.ActiveBranch), MajorVersion: obj.Spec.MajorVersion, Extensions: extensions, HighAvailability: obj.Spec.HighAvailability, @@ -416,7 +431,7 @@ func toPostgres(u *unstructured.Unstructured, environmentName string) (*Postgres }, Labels: model.UserLabels(obj.Labels), CreatedAt: obj.CreationTimestamp.Time, directClusterName: clusterName, - physicalState: state, configuration: configuration, + physicalState: state, configuration: configuration, Recovery: recovery, }, nil } @@ -645,6 +660,21 @@ type CreatePostgresInput struct { DiskSize *string `json:"diskSize,omitempty"` Extensions []string `json:"extensions,omitempty"` Labels []*model.ResourceLabel `json:"labels,omitempty"` + Recovery *PostgresRecoveryInput `json:"recovery,omitempty"` +} + +// PostgresRecoveryInput can only select a source in the new database's team +// and environment. Physical archive identifiers are resolved by the API. +type PostgresRecoveryInput struct { + SourcePostgres string `json:"sourcePostgres"` + TargetTime time.Time `json:"targetTime"` +} + +type PostgresRecovery struct { + SourcePostgres string `json:"sourcePostgres"` + TargetTime time.Time `json:"targetTime"` + Completed bool `json:"completed"` + FailureMessage *string `json:"failureMessage,omitempty"` } type CreatePostgresPayload struct { @@ -705,6 +735,19 @@ func (i *CreatePostgresInput) ValidationErrors(_ context.Context) *validate.Vali } validateQuantities(verr, i.CPU, i.Memory, i.DiskSize) validatePostgresUserLabels(verr, i.Labels) + if i.Recovery != nil { + i.Recovery.SourcePostgres = strings.TrimSpace(i.Recovery.SourcePostgres) + if i.Recovery.SourcePostgres == "" || len(validation.IsDNS1123Subdomain(i.Recovery.SourcePostgres)) > 0 { + verr.Add("recovery.sourcePostgres", "Select a valid source database.") + } else if i.Recovery.SourcePostgres == i.Name { + verr.Add("name", "The recovered database must have a different name from its source.") + } + if i.Recovery.TargetTime.IsZero() || i.Recovery.TargetTime.After(time.Now()) { + verr.Add("recovery.targetTime", "Choose an explicit restore time no later than now.") + } else if i.Recovery.TargetTime.Nanosecond() != 0 { + verr.Add("recovery.targetTime", "Restore time must have whole-second precision.") + } + } return verr } diff --git a/internal/persistence/postgres/physical_status.go b/internal/persistence/postgres/physical_status.go index 4aabe003c..be83e8bc6 100644 --- a/internal/persistence/postgres/physical_status.go +++ b/internal/persistence/postgres/physical_status.go @@ -2,7 +2,6 @@ package postgres import ( "context" - "time" nais_io_v1 "github.com/nais/pgrator/pkg/api/v1" ) @@ -25,15 +24,6 @@ func observedBranchName(pg *Postgres) string { return nais_io_v1.DefaultBranchName } -// PostgresEarliestRestoreTime uses the same physical target as database status, -// including the preserved archive identity of an adopted main. -func PostgresEarliestRestoreTime(ctx context.Context, pg *Postgres) (*time.Time, error) { - if pg.directClusterName != nil { - return EarliestRestoreTime(ctx, mainBranchView(pg)) - } - return nil, nil -} - func mainBranchView(pg *Postgres) *PostgresBranch { clusterName := "" state := PostgresBranchStateProgressing diff --git a/internal/persistence/postgres/queries.go b/internal/persistence/postgres/queries.go index 47417b654..71cd2b46f 100644 --- a/internal/persistence/postgres/queries.go +++ b/internal/persistence/postgres/queries.go @@ -773,6 +773,9 @@ func Create(ctx context.Context, input CreatePostgresInput) (*CreatePostgresPayl pg.SetAnnotations(kubernetes.WithCommonAnnotations(nil, authz.ActorFromContext(ctx).User.Identity())) kubernetes.SetManagedByConsoleLabel(pg) setPostgresUserLabels(pg, input.Labels) + if err := resolveRecovery(ctx, client, input, pg); err != nil { + return nil, err + } if input.HighAvailability != nil { pg.Spec.HighAvailability = *input.HighAvailability } @@ -785,7 +788,9 @@ func Create(ctx context.Context, input CreatePostgresInput) (*CreatePostgresPayl return nil, err } - created, err := client.Create(ctx, obj, metav1.CreateOptions{}) + // An older CRD must reject recovery fields, never silently prune them and + // turn a restore request into an empty database. + created, err := client.Create(ctx, obj, metav1.CreateOptions{FieldValidation: metav1.FieldValidationStrict}) if err != nil { if k8serrors.IsAlreadyExists(err) { return nil, apierror.ErrAlreadyExists @@ -793,7 +798,11 @@ func Create(ctx context.Context, input CreatePostgresInput) (*CreatePostgresPayl return nil, err } - if err := logPostgresChange(ctx, activitylog.ActivityLogEntryActionCreated, input.Name, input.EnvironmentName, input.TeamSlug, nil); err != nil { + var creationData any + if input.Recovery != nil { + creationData = PostgresCreatedActivityLogEntryData{Recovery: input.Recovery, SourceUID: pg.Spec.Recovery.SourceUID} + } + if err := logPostgresChange(ctx, activitylog.ActivityLogEntryActionCreated, input.Name, input.EnvironmentName, input.TeamSlug, creationData); err != nil { return nil, err } diff --git a/internal/persistence/postgres/recovery.go b/internal/persistence/postgres/recovery.go new file mode 100644 index 000000000..c259fec90 --- /dev/null +++ b/internal/persistence/postgres/recovery.go @@ -0,0 +1,209 @@ +package postgres + +import ( + "context" + "fmt" + "slices" + "time" + + "github.com/nais/api/internal/graph/apierror" + "github.com/nais/api/internal/kubernetes" + "github.com/nais/api/internal/kubernetes/watcher" + nais_io_v1 "github.com/nais/pgrator/pkg/api/v1" + k8serrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/api/resource" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/client-go/dynamic" +) + +var ( + cnpgClusterGVR = schema.GroupVersionResource{ + Group: "postgresql.cnpg.io", Version: "v1", Resource: "clusters", + } + barmanObjectStoreGVR = schema.GroupVersionResource{ + Group: "barmancloud.cnpg.io", Version: "v1", Resource: "objectstores", + } +) + +func PostgresEarliestRestoreTime(ctx context.Context, pg *Postgres) (*time.Time, error) { + if pg.directClusterName == nil || *pg.directClusterName == "" { + return nil, nil + } + client, err := fromContext(ctx).postgresWatcher.SystemAuthenticatedClient( + ctx, pg.EnvironmentName, watcher.WithImpersonatedClientGVR(cnpgClusterGVR), + ) + if err != nil { + return nil, err + } + clusterName := *pg.directClusterName + cluster, err := client.Namespace(pg.TeamSlug.String()).Get(ctx, clusterName, metav1.GetOptions{}) + if k8serrors.IsNotFound(err) { + return nil, nil + } + if err != nil { + return nil, fmt.Errorf("getting CNPG cluster %q for Postgres %q: %w", clusterName, pg.Name, err) + } + + objectStoreName, err := barmanObjectStoreName(cluster) + if err != nil { + return nil, fmt.Errorf("reading Barman ObjectStore name from CNPG cluster %q: %w", clusterName, err) + } + if objectStoreName != "" { + objectStoreClient, err := fromContext(ctx).postgresWatcher.SystemAuthenticatedClient( + ctx, pg.EnvironmentName, watcher.WithImpersonatedClientGVR(barmanObjectStoreGVR), + ) + if err != nil { + return nil, err + } + objectStore, err := objectStoreClient.Namespace(pg.TeamSlug.String()).Get(ctx, objectStoreName, metav1.GetOptions{}) + if err != nil && !k8serrors.IsNotFound(err) { + return nil, fmt.Errorf("getting Barman ObjectStore %q for CNPG cluster %q: %w", objectStoreName, clusterName, err) + } + if err == nil { + earliest, err := firstRecoverabilityPoint(objectStore, clusterName) + if err != nil || earliest != nil { + return earliest, err + } + } + } + + // Older CNPG configurations publish the recovery point directly on Cluster. + value, found, err := unstructured.NestedString(cluster.Object, "status", "firstRecoverabilityPoint") + if err != nil { + return nil, fmt.Errorf("reading firstRecoverabilityPoint from CNPG cluster %q: %w", clusterName, err) + } + if !found || value == "" { + return nil, nil + } + earliest, err := time.Parse(time.RFC3339, value) + if err != nil { + return nil, fmt.Errorf("parsing firstRecoverabilityPoint %q from CNPG cluster %q: %w", value, clusterName, err) + } + return &earliest, nil +} + +func barmanObjectStoreName(cluster *unstructured.Unstructured) (string, error) { + plugins, found, err := unstructured.NestedSlice(cluster.Object, "spec", "plugins") + if err != nil || !found { + return "", err + } + for _, item := range plugins { + plugin, ok := item.(map[string]any) + if !ok { + return "", fmt.Errorf("CNPG plugin configuration has unexpected type %T", item) + } + name, found, err := unstructured.NestedString(plugin, "parameters", "barmanObjectName") + if err != nil { + return "", err + } + if plugin["name"] == "barman-cloud.cloudnative-pg.io" && found && name != "" { + return name, nil + } + } + return "", nil +} + +func firstRecoverabilityPoint(objectStore *unstructured.Unstructured, clusterName string) (*time.Time, error) { + value, found, err := unstructured.NestedString(objectStore.Object, "status", "serverRecoveryWindow", clusterName, "firstRecoverabilityPoint") + if err != nil { + return nil, err + } + if !found || value == "" { + return nil, nil + } + earliest, err := time.Parse(time.RFC3339, value) + if err != nil { + return nil, fmt.Errorf("parsing firstRecoverabilityPoint %q: %w", value, err) + } + return &earliest, nil +} + +// resolveRecovery never accepts bucket names or cross-team/environment selectors +// from clients. Resolve and pin the live source's UID and physical archive. +func resolveRecovery(ctx context.Context, client dynamic.ResourceInterface, input CreatePostgresInput, target *nais_io_v1.Postgres) error { + if input.Recovery == nil { + return nil + } + sourceObject, err := client.Get(ctx, input.Recovery.SourcePostgres, metav1.GetOptions{}) + if k8serrors.IsNotFound(err) { + return apierror.Errorf("Source Postgres %q not found in this team and environment.", input.Recovery.SourcePostgres) + } + if err != nil { + return err + } + source, err := kubernetes.ToConcrete[nais_io_v1.Postgres](sourceObject) + if err != nil { + return err + } + if source.UID == "" || !source.DeletionTimestamp.IsZero() || source.Status == nil || source.Status.ClusterName == "" { + return apierror.Errorf("Source Postgres is deleting or has no recoverable cluster yet.") + } + if source.Spec.MajorVersion != input.MajorVersion { + return apierror.Errorf("Recovery must use the source PostgreSQL major version %s.", source.Spec.MajorVersion) + } + if input.Extensions == nil { + target.Spec.Extensions = slices.Clone(source.Spec.Extensions) + } + for _, extension := range source.Spec.Extensions { + if !slices.Contains(target.Spec.Extensions, extension) { + return apierror.Errorf("Recovery must include source extension %q.", extension.Name) + } + } + // Unspecified resources inherit the source configuration, particularly disk + // size: the normal empty-database defaults can be too small for a restore. + target.Spec.Resources = *source.Spec.Resources.DeepCopy() + if input.DiskSize != nil && source.Spec.Resources.DiskSize.Sign() > 0 { + requested, err := resource.ParseQuantity(*input.DiskSize) + if err != nil { + return err + } + if requested.Cmp(source.Spec.Resources.DiskSize) < 0 { + return apierror.Errorf("Recovery disk size must not be smaller than the source (%s).", source.Spec.Resources.DiskSize.String()) + } + } + if input.HighAvailability == nil { + target.Spec.HighAvailability = source.Spec.HighAvailability + } + pg, err := toPostgres(sourceObject, input.EnvironmentName) + if err != nil { + return err + } + earliest, err := PostgresEarliestRestoreTime(ctx, pg) + if err != nil { + return err + } + if earliest == nil { + return apierror.Errorf("Source Postgres has no recoverable backup window yet.") + } + if input.Recovery.TargetTime.Before(*earliest) { + return apierror.Errorf("Restore time must not be before the earliest recoverable time %s.", earliest.UTC().Format(time.RFC3339)) + } + clusters, err := fromContext(ctx).postgresWatcher.SystemAuthenticatedClient(ctx, input.EnvironmentName, watcher.WithImpersonatedClientGVR(cnpgClusterGVR)) + if err != nil { + return err + } + cluster, err := clusters.Namespace(input.TeamSlug.String()).Get(ctx, source.Status.ClusterName, metav1.GetOptions{}) + if err != nil { + return err + } + if !metav1.IsControlledBy(cluster, source) || !cluster.GetDeletionTimestamp().IsZero() { + return apierror.Errorf("Source cluster does not belong to the selected Postgres or is deleting.") + } + if source.Status.Ownership == nil || source.Status.Ownership.Phase != "Managed" || source.Status.Ownership.ClusterUID == "" || source.Status.Ownership.ClusterUID != string(cluster.GetUID()) { + return apierror.Errorf("Source Postgres has no matching managed cluster checkpoint.") + } + bucket, err := barmanObjectStoreName(cluster) + if err != nil { + return err + } + if bucket == "" { + return apierror.Errorf("Source Postgres has no Barman archive.") + } + target.Spec.Recovery = &nais_io_v1.PostgresRecovery{ + SourcePostgres: source.Name, SourceUID: string(source.UID), BucketName: bucket, + ServerName: source.Status.ClusterName, TargetTime: metav1.NewTime(input.Recovery.TargetTime.UTC()), + } + return nil +} diff --git a/internal/persistence/postgres/recovery_test.go b/internal/persistence/postgres/recovery_test.go new file mode 100644 index 000000000..5d28f64bc --- /dev/null +++ b/internal/persistence/postgres/recovery_test.go @@ -0,0 +1,86 @@ +package postgres + +import ( + "encoding/json" + "strings" + "testing" + "time" + + "github.com/nais/api/internal/activitylog" + "github.com/nais/api/internal/kubernetes" + nais_io_v1 "github.com/nais/pgrator/pkg/api/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func TestRecoveryInputValidation(t *testing.T) { + for _, tt := range []struct { + name string + recovery *PostgresRecoveryInput + wantError string + }{ + {name: "empty database"}, + {name: "valid recovery", recovery: &PostgresRecoveryInput{SourcePostgres: "source", TargetTime: time.Now().UTC().Truncate(time.Second).Add(-time.Hour)}}, + {name: "source required", recovery: &PostgresRecoveryInput{TargetTime: time.Now().Add(-time.Hour).Truncate(time.Second)}, wantError: "recovery.sourcePostgres"}, + {name: "different name", recovery: &PostgresRecoveryInput{SourcePostgres: "restored", TargetTime: time.Now().Add(-time.Hour).Truncate(time.Second)}, wantError: "different name"}, + {name: "explicit target", recovery: &PostgresRecoveryInput{SourcePostgres: "source"}, wantError: "recovery.targetTime"}, + {name: "future target", recovery: &PostgresRecoveryInput{SourcePostgres: "source", TargetTime: time.Now().Add(time.Hour)}, wantError: "no later than now"}, + {name: "fractional target", recovery: &PostgresRecoveryInput{SourcePostgres: "source", TargetTime: time.Unix(1_750_000_000, 1)}, wantError: "whole-second"}, + } { + t.Run(tt.name, func(t *testing.T) { + input := CreatePostgresInput{Name: "restored", EnvironmentName: "dev", TeamSlug: "team", MajorVersion: "18", Recovery: tt.recovery} + err := input.ValidationErrors(t.Context()).NilIfEmpty() + if tt.wantError == "" { + if err != nil { + t.Fatal(err) + } + } else if err == nil || !strings.Contains(err.Error(), tt.wantError) { + t.Fatalf("error = %v, want %q", err, tt.wantError) + } + }) + } +} + +func TestRecoveryMetadataSurvivesWithoutSourceLookup(t *testing.T) { + for _, completed := range []bool{false, true} { + t.Run(map[bool]string{false: "pending", true: "completed"}[completed], func(t *testing.T) { + pg := &nais_io_v1.Postgres{ObjectMeta: metav1.ObjectMeta{Name: "restored", Namespace: "team"}, Spec: nais_io_v1.PostgresSpec{MajorVersion: "18", Recovery: &nais_io_v1.PostgresRecovery{SourcePostgres: "deleted-source", TargetTime: metav1.NewTime(time.Unix(1_750_000_000, 0))}}} + if completed { + pg.Status = &nais_io_v1.PostgresStatus{Ownership: &nais_io_v1.PostgresOwnership{ClusterUID: "original-cluster"}} + pg.GetStatus().SetCondition(metav1.Condition{Type: "RecoveryCompleted", Status: metav1.ConditionTrue, Message: "original-cluster"}) + } + object, err := kubernetes.ToUnstructured(pg) + if err != nil { + t.Fatal(err) + } + got, err := toPostgres(object, "dev") + if err != nil { + t.Fatal(err) + } + if got.Recovery == nil || got.Recovery.SourcePostgres != "deleted-source" || got.Recovery.Completed != completed || !got.Recovery.TargetTime.Equal(pg.Spec.Recovery.TargetTime.Time) { + t.Fatalf("lost recovery provenance: %#v", got.Recovery) + } + }) + } +} + +func TestRecoveryCreationAuditPreservesHistoricalEvents(t *testing.T) { + for _, recovery := range []*PostgresRecoveryInput{nil, {SourcePostgres: "source", TargetTime: time.Date(2026, 9, 30, 14, 0, 0, 0, time.FixedZone("CEST", 7200))}} { + entry := activitylog.GenericActivityLogEntry{Action: activitylog.ActivityLogEntryActionCreated} + want := "Created Postgres" + if recovery != nil { + var err error + entry.Data, err = json.Marshal(PostgresCreatedActivityLogEntryData{Recovery: recovery}) + if err != nil { + t.Fatal(err) + } + want = "Requested Postgres recovery from source to 2026-09-30T12:00:00Z" + } + got, err := transformPostgresActivityLogEntry(entry) + if err != nil { + t.Fatal(err) + } + if got.(PostgresCreatedActivityLogEntry).Message != want { + t.Fatalf("audit message = %q, want %q", got.(PostgresCreatedActivityLogEntry).Message, want) + } + } +} diff --git a/internal/persistence/postgres/branch_operations_test.go b/internal/persistence/postgres/recovery_window_test.go similarity index 100% rename from internal/persistence/postgres/branch_operations_test.go rename to internal/persistence/postgres/recovery_window_test.go diff --git a/mise.lock b/mise.lock index 35f721ab7..c5b5e3d2d 100644 --- a/mise.lock +++ b/mise.lock @@ -43,37 +43,37 @@ url = "https://github.com/CppCXY/EmmyLuaCodeStyle/releases/download/1.6.0/win32- url_api = "https://api.github.com/repos/CppCXY/EmmyLuaCodeStyle/releases/assets/332769162" [[tools.go]] -version = "1.26.10" +version = "1.26.11" backend = "core:go" -specifiers = ["1.26.10"] +specifiers = ["1.26.11"] [tools.go."platforms.linux-arm64"] -checksum = "sha256:3f4227ce7b206a513e794f1393adb473a07b46e94d18bd15d1c684bbb7319797" -url = "https://dl.google.com/go/go1.26.10.linux-arm64.tar.gz" +checksum = "sha256:6c2f29b458b5add0835f18b776f69c8881a6d88428f1484a6da55755e620757e" +url = "https://dl.google.com/go/go1.26.11.linux-arm64.tar.gz" [tools.go."platforms.linux-arm64-musl"] -checksum = "sha256:3f4227ce7b206a513e794f1393adb473a07b46e94d18bd15d1c684bbb7319797" -url = "https://dl.google.com/go/go1.26.10.linux-arm64.tar.gz" +checksum = "sha256:6c2f29b458b5add0835f18b776f69c8881a6d88428f1484a6da55755e620757e" +url = "https://dl.google.com/go/go1.26.11.linux-arm64.tar.gz" [tools.go."platforms.linux-x64"] -checksum = "sha256:ccfe7ae822d2c2f41f4fe7c956199967a4dac22d0ee0d2b2bfdb440aab3b20c5" -url = "https://dl.google.com/go/go1.26.10.linux-amd64.tar.gz" +checksum = "sha256:9849f4e04ee35303714e34fbc3ac8ab44ea3e448a95aa182f91e3b9b79c710ec" +url = "https://dl.google.com/go/go1.26.11.linux-amd64.tar.gz" [tools.go."platforms.linux-x64-musl"] -checksum = "sha256:ccfe7ae822d2c2f41f4fe7c956199967a4dac22d0ee0d2b2bfdb440aab3b20c5" -url = "https://dl.google.com/go/go1.26.10.linux-amd64.tar.gz" +checksum = "sha256:9849f4e04ee35303714e34fbc3ac8ab44ea3e448a95aa182f91e3b9b79c710ec" +url = "https://dl.google.com/go/go1.26.11.linux-amd64.tar.gz" [tools.go."platforms.macos-arm64"] -checksum = "sha256:0b5183492292ada170f9b561a834863808dd02edba599e4ad0e89899330020b3" -url = "https://dl.google.com/go/go1.26.10.darwin-arm64.tar.gz" +checksum = "sha256:e3a42e67a7a8acf450ebada2b074c7ca863000ca514907b7185b45e8f5cdff57" +url = "https://dl.google.com/go/go1.26.11.darwin-arm64.tar.gz" [tools.go."platforms.macos-x64"] -checksum = "sha256:e820ab780c273ae4a340b79d0f3c4313b74ee029aa3874792145d44b9f4f91da" -url = "https://dl.google.com/go/go1.26.10.darwin-amd64.tar.gz" +checksum = "sha256:394baae971332f2bc783d6e2004ae57101479bd3b06ed8209a3fe3570d4ce498" +url = "https://dl.google.com/go/go1.26.11.darwin-amd64.tar.gz" [tools.go."platforms.windows-x64"] -checksum = "sha256:7ecd2d7260e82c59bcbd7f1077a07cee2dea607295b858df67195a1e4e09629b" -url = "https://dl.google.com/go/go1.26.10.windows-amd64.zip" +checksum = "sha256:71ae086f0137e665f855a811cc119152b6a783e0bbc1b906e709f46d80141eb0" +url = "https://dl.google.com/go/go1.26.11.windows-amd64.zip" [[tools.helm]] version = "4.1.0" diff --git a/mise.toml b/mise.toml index 12cc4b18c..88d99941d 100644 --- a/mise.toml +++ b/mise.toml @@ -3,7 +3,7 @@ pin = true lockfile = true [tools] -go = "1.26.10" +go = "1.26.11" helm = "4.1.0" node = "24" protoc = "29.3"