Skip to content

K8SPG-873: add env and envFrom fields to backups and restores#1320

Open
pooknull wants to merge 12 commits intomainfrom
K8SPG-873
Open

K8SPG-873: add env and envFrom fields to backups and restores#1320
pooknull wants to merge 12 commits intomainfrom
K8SPG-873

Conversation

@pooknull
Copy link
Contributor

@pooknull pooknull commented Oct 13, 2025

K8SPG-873 Powered by Pull Request Badge

https://perconadev.atlassian.net/browse/K8SPG-873

DESCRIPTION

This PR adds .containerOptions.env and .containerOptions.envFrom fields to our backup and restore resources

CHECKLIST

Jira

  • Is the Jira ticket created and referenced properly?
  • Does the Jira ticket have the proper statuses for documentation (Needs Doc) and QA (Needs QA)?
  • Does the Jira ticket link to the proper milestone (Fix Version field)?

Tests

  • Is an E2E test/test case added for the new feature/change?
  • Are unit tests added where appropriate?

Config/Logging/Testability

  • Are all needed new/changed options added to default YAML files?
  • Are all needed new/changed options added to the Helm Chart?
  • Did we add proper logging messages for operator actions?
  • Did we ensure compatibility with the previous version or cluster upgrade process?
  • Does the change support oldest and newest supported PG version?
  • Does the change support oldest and newest supported Kubernetes version?

@pooknull pooknull marked this pull request as ready for review October 14, 2025 10:14
egegunes
egegunes previously approved these changes Oct 29, 2025
egegunes
egegunes previously approved these changes Nov 6, 2025
@hors hors requested a review from mayankshah1607 January 8, 2026 09:46
@it-percona-cla
Copy link

it-percona-cla commented Jan 8, 2026

CLA assistant check
All committers have signed the CLA.

hors
hors previously approved these changes Feb 11, 2026
}

// K8SPG-833
if cluster.CompareVersion("2.8.0") >= 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we change this compare version? I guess it is going to be releases with 2.9

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@egegunes egegunes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please check @gkech's comment about version check

Copilot AI review requested due to automatic review settings February 12, 2026 13:09
@pooknull pooknull requested a review from egegunes February 12, 2026 13:09
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds support for specifying environment variables (env / envFrom) for backup and restore workloads by introducing a containerOptions block on Percona backup/restore CRs and plumbing corresponding env/envFrom fields into the generated pgBackRest Job specs.

Changes:

  • Add containerOptions.env / containerOptions.envFrom to PerconaPGBackup and PerconaPGRestore specs (API + CRDs + examples).
  • Add Env / EnvFrom fields to Crunchy PGBackRestManualBackup and PostgresClusterDataSource types and propagate them into pgBackRest backup/restore Jobs.
  • Add/update deepcopy generation and controller tests validating env/envFrom precedence and version gating.

Reviewed changes

Copilot reviewed 15 out of 22 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
pkg/apis/postgres-operator.crunchydata.com/v1beta1/zz_generated.deepcopy.go DeepCopy updates for new env/envFrom fields in Crunchy types.
pkg/apis/postgres-operator.crunchydata.com/v1beta1/postgrescluster_types.go Adds Env/EnvFrom to PostgresClusterDataSource (restore datasource).
pkg/apis/postgres-operator.crunchydata.com/v1beta1/pgbackrest_types.go Adds Env/EnvFrom to PGBackRestManualBackup.
pkg/apis/pgv2.percona.com/v2/zz_generated.deepcopy.go Adds deepcopy support for new ContainerOptions and ensures it is copied in backup/restore specs.
pkg/apis/pgv2.percona.com/v2/perconapgrestore_types.go Adds containerOptions to restore spec.
pkg/apis/pgv2.percona.com/v2/perconapgcluster_types.go Introduces ContainerOptions type and extends secret indexing to include manual/restore envFrom secrets.
pkg/apis/pgv2.percona.com/v2/perconapgbackup_types.go Adds containerOptions to backup spec.
percona/controller/pgrestore/controller.go Plumbs containerOptions env/envFrom into cluster restore spec when starting restore.
percona/controller/pgbackup/controller.go Plumbs containerOptions env/envFrom into cluster manual-backup spec when starting backup.
internal/controller/postgrescluster/pgbackrest_test.go Adds test coverage for env/envFrom behavior and version gating in backup/restore job generation.
internal/controller/postgrescluster/pgbackrest.go Implements env/envFrom injection (with manual/restore override) into generated pgBackRest Jobs.
deploy/restore.yaml Documents containerOptions usage for restores (example).
deploy/crd.yaml Updates bundled CRDs to include new schema fields.
deploy/backup.yaml Documents containerOptions usage for backups (example).
config/crd/bases/postgres-operator.crunchydata.com_postgresclusters.yaml Updates Crunchy PostgresCluster CRD base schema for new env/envFrom fields.
config/crd/bases/pgv2.percona.com_perconapgclusters.yaml Updates Percona PG CRD base schema for new env/envFrom-related schema blocks.
build/crd/percona/generated/pgv2.percona.com_perconapgrestores.yaml Generated CRD includes containerOptions schema for restores.
build/crd/percona/generated/pgv2.percona.com_perconapgclusters.yaml Generated CRD updates reflecting new schema blocks.
build/crd/percona/generated/pgv2.percona.com_perconapgbackups.yaml Generated CRD includes containerOptions schema for backups.
build/crd/crunchy/generated/postgres-operator.crunchydata.com_postgresclusters.yaml Generated Crunchy CRD includes new env/envFrom schema for manual/restore.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@egegunes egegunes added this to the v2.9.0 milestone Feb 12, 2026
egegunes
egegunes previously approved these changes Feb 13, 2026
gkech
gkech previously approved these changes Feb 13, 2026
@pooknull pooknull dismissed stale reviews from gkech and egegunes via 93555f8 February 13, 2026 10:33
Copilot AI review requested due to automatic review settings February 13, 2026 10:38
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 15 out of 22 changed files in this pull request and generated 5 comments.

Comment on lines +336 to +338
// K8SPG-873
Env []corev1.EnvVar `json:"env,omitempty"`
// K8SPG-873
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Problem: The new Env/EnvFrom fields have only a ticket reference comment and are missing +optional/descriptive doc comments.
  2. Why it matters: Controller-gen will surface this as unhelpful OpenAPI/CRD field descriptions (e.g. just "K8SPG-873"), reducing usability of kubectl explain and generated docs.
  3. Fix: Add proper field documentation and // +optional markers consistent with surrounding fields (e.g. describe that these env vars are added to the restore Job container).
Suggested change
// K8SPG-873
Env []corev1.EnvVar `json:"env,omitempty"`
// K8SPG-873
// Additional environment variables to set on the pgBackRest restore Job container.
// +optional
Env []corev1.EnvVar `json:"env,omitempty"`
// Additional sources of environment variables for the pgBackRest restore Job container.
// +optional

Copilot uses AI. Check for mistakes.
Comment on lines +222 to +224
// K8SPG-873
Env []corev1.EnvVar `json:"env,omitempty"`
// K8SPG-873
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Problem: The new Env/EnvFrom fields in PGBackRestManualBackup only include a ticket reference and omit +optional and descriptive doc comments.
  2. Why it matters: This generates unhelpful CRD schema descriptions and makes it unclear how these env settings are used (and by which job type).
  3. Fix: Add clear doc comments (e.g., "Environment variables to add to the manual backup Job container") and // +optional markers, consistent with other fields in this API type.
Suggested change
// K8SPG-873
Env []corev1.EnvVar `json:"env,omitempty"`
// K8SPG-873
// Env defines environment variables to add to the manual backup Job container.
// +optional
Env []corev1.EnvVar `json:"env,omitempty"`
// EnvFrom specifies sources for environment variables to add to the manual backup Job container.
// +optional

Copilot uses AI. Check for mistakes.
Comment on lines +949 to 963
if postgresCluster.CompareVersion("2.9.0") >= 0 {
for i := range jobSpec.Template.Spec.Containers {
pgbackrest := postgresCluster.Spec.Backups.PGBackRest

if pgbackrest.Manual != nil && len(pgbackrest.Manual.Env) != 0 {
jobSpec.Template.Spec.Containers[i].Env = append(jobSpec.Template.Spec.Containers[i].Env, pgbackrest.Manual.Env...)
} else if len(pgbackrest.Env) != 0 {
jobSpec.Template.Spec.Containers[i].Env = append(jobSpec.Template.Spec.Containers[i].Env, pgbackrest.Env...)
}

if pgbackrest.Manual != nil && len(pgbackrest.Manual.EnvFrom) != 0 {
jobSpec.Template.Spec.Containers[i].EnvFrom = append(jobSpec.Template.Spec.Containers[i].EnvFrom, pgbackrest.Manual.EnvFrom...)
} else if len(pgbackrest.EnvFrom) != 0 {
jobSpec.Template.Spec.Containers[i].EnvFrom = append(jobSpec.Template.Spec.Containers[i].EnvFrom, pgbackrest.EnvFrom...)
}
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Problem: generateBackupJobSpecIntent applies pgbackrest.Manual.Env/EnvFrom to all backup jobs whenever .spec.backups.pgbackrest.manual is set, including replica-create and scheduled CronJob backups.
  2. Why it matters: This makes manual-only environment configuration leak into other backup job types, which can change behavior and credentials unexpectedly.
  3. Fix: Only apply .manual.env/.manual.envFrom when generating a manual backup job (e.g., gate on labels[naming.LabelPGBackRestBackup] == string(naming.BackupManual)); otherwise fall back to .pgbackrest.env/.pgbackrest.envFrom.

Copilot uses AI. Check for mistakes.
jobSpec.Template.Spec.Containers[i].EnvFrom = append(jobSpec.Template.Spec.Containers[i].EnvFrom, postgresCluster.Spec.Backups.PGBackRest.EnvFrom...)
}
// K8SPG-833
if postgresCluster.CompareVersion("2.9.0") >= 0 {
Copy link

Copilot AI Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Problem: The version gate for applying pgBackRest Env/EnvFrom in backup jobs was changed to CompareVersion("2.9.0"), but other pgBackRest env injection paths still use 2.8.0 for K8SPG-833.
  2. Why it matters: This is inconsistent and may disable previously supported .spec.backups.pgbackrest.env/envFrom for clusters labeled 2.8.x.
  3. Fix: Align the version threshold with the rest of the codebase (or separate the gate: keep global pgBackRest env at 2.8.0 and gate the new manual override independently).
Suggested change
if postgresCluster.CompareVersion("2.9.0") >= 0 {
if postgresCluster.CompareVersion("2.8.0") >= 0 {

Copilot uses AI. Check for mistakes.
@JNKPercona
Copy link
Collaborator

Test Name Result Time
backup-enable-disable passed 00:06:32
builtin-extensions passed 00:05:09
custom-envs passed 00:18:23
custom-extensions passed 00:15:18
custom-tls passed 00:05:55
database-init-sql passed 00:02:29
demand-backup passed 00:23:57
finalizers passed 00:04:09
init-deploy passed 00:02:40
huge-pages passed 00:03:05
monitoring passed 00:07:24
monitoring-pmm3 passed 00:07:59
one-pod passed 00:05:55
operator-self-healing passed 00:08:34
pitr passed 00:11:37
scaling passed 00:05:05
scheduled-backup passed 00:26:11
self-healing passed 00:09:05
sidecars passed 00:02:43
standby-pgbackrest passed 00:13:42
standby-streaming passed 00:09:39
start-from-backup passed 00:10:47
tablespaces passed 00:07:03
telemetry-transfer passed 00:03:43
upgrade-consistency passed 00:06:22
upgrade-minor passed 00:05:28
users passed 00:04:38
Summary Value
Tests Run 27/27
Job Duration 01:18:13
Total Test Time 03:53:46

commit: 38fc9f3
image: perconalab/percona-postgresql-operator:PR-1320-38fc9f335

Copy link
Member

@mayankshah1607 mayankshah1607 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please resolve conflicts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants