Skip to content

Allow specifying sidecars and env vars for the repo host #1080

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions internal/controller/postgrescluster/pgbackrest.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,25 @@ func (r *Reconciler) generateRepoHostIntent(ctx context.Context, postgresCluster
}
sizeLimit := getTMPSizeLimit(repo.Labels[naming.LabelVersion], resources)

// Apply custom environment variables to the pgBackRest repo host container if specified
if repoHost := postgresCluster.Spec.Backups.PGBackRest.RepoHost; repoHost != nil && len(repoHost.Env) > 0 {
for i, container := range repo.Spec.Template.Spec.Containers {
if container.Name == naming.PGBackRestRepoContainerName {
repo.Spec.Template.Spec.Containers[i].Env = append(
repo.Spec.Template.Spec.Containers[i].Env,
repoHost.Env...)
break
}
}
}

// Add any custom sidecar containers to the repo host pod if specified
if repoHost := postgresCluster.Spec.Backups.PGBackRest.RepoHost; repoHost != nil && len(repoHost.Containers) > 0 {
repo.Spec.Template.Spec.Containers = append(
repo.Spec.Template.Spec.Containers,
repoHost.Containers...)
}

addTMPEmptyDir(&repo.Spec.Template, sizeLimit)

// set ownership references
Expand Down
78 changes: 78 additions & 0 deletions internal/controller/postgrescluster/pgbackrest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2739,6 +2739,84 @@ func TestGenerateRepoHostIntent(t *testing.T) {
assert.NilError(t, err)
assert.Equal(t, *sts.Spec.Replicas, int32(0))
})

t.Run("With custom environment variables", func(t *testing.T) {
cluster := &v1beta1.PostgresCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "testcluster",
Labels: map[string]string{
naming.LabelVersion: "2.4.0",
},
},
Spec: v1beta1.PostgresClusterSpec{
Backups: v1beta1.Backups{
PGBackRest: v1beta1.PGBackRestArchive{
RepoHost: &v1beta1.PGBackRestRepoHost{
Env: []corev1.EnvVar{
{
Name: "TEST_ENV_VAR",
Value: "test-value",
},
},
},
},
},
},
}
sts, err := r.generateRepoHostIntent(ctx, cluster, "", &RepoResources{}, &observedInstances{})
assert.NilError(t, err)

// Find pgbackrest container and check for env var
var found bool
for _, container := range sts.Spec.Template.Spec.Containers {
if container.Name == naming.PGBackRestRepoContainerName {
for _, env := range container.Env {
if env.Name == "TEST_ENV_VAR" && env.Value == "test-value" {
found = true
break
}
}
}
}
assert.Assert(t, found, "expected environment variable TEST_ENV_VAR not found")
})

t.Run("With custom sidecar containers", func(t *testing.T) {
cluster := &v1beta1.PostgresCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "testcluster",
Labels: map[string]string{
naming.LabelVersion: "2.4.0",
},
},
Spec: v1beta1.PostgresClusterSpec{
Backups: v1beta1.Backups{
PGBackRest: v1beta1.PGBackRestArchive{
RepoHost: &v1beta1.PGBackRestRepoHost{
Containers: []corev1.Container{
{
Name: "sidecar-test",
Image: "test-image:latest",
},
},
},
},
},
},
}
sts, err := r.generateRepoHostIntent(ctx, cluster, "", &RepoResources{}, &observedInstances{})
assert.NilError(t, err)

// Check sidecar container is added
var found bool
for _, container := range sts.Spec.Template.Spec.Containers {
if container.Name == "sidecar-test" {
found = true
break
}
}
assert.Assert(t, found, "expected sidecar container 'sidecar-test' not found")
})
}

func TestGenerateRestoreJobIntent(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,15 @@ type PGBackRestRepoHost struct {
// +optional
TopologySpreadConstraints []corev1.TopologySpreadConstraint `json:"topologySpreadConstraints,omitempty"`

// Custom sidecar containers for the pgBackRest repo host pod. Changing this
// value causes the repo host to restart.
// +optional
Containers []corev1.Container `json:"containers,omitempty"`

// Environment variables to be set in the pgBackRest repo host container.
// +optional
Env []corev1.EnvVar `json:"env,omitempty"`
Copy link
Collaborator

Choose a reason for hiding this comment

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

Good day, @jsierles. What do you think about using Secret for this need? We have it for our PXC operator https://github.com/percona/percona-xtradb-cluster-operator/blob/main/deploy/cr.yaml#L472 I think it is better because env vars can have sensitive information and it is not good to set values via CR.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Additionally, we can add a separate secret option for each component (not just for the pgBackRest repo host) as we have in PXCO. This could be useful for you in the future as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That seems fine. In our case we're not setting sensitive information, but we can change it to use secrets.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It is ok, we can wait. I just want to be sure that you don't have any questions/blockers. Thank you.


// ConfigMap containing custom SSH configuration.
// Deprecated: Repository hosts use mTLS for encryption, authentication, and authorization.
// +optional
Expand Down
Loading