Skip to content
3 changes: 3 additions & 0 deletions pkg/controller/perconaservermongodbrestore/physical.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,9 @@ func (r *ReconcilePerconaServerMongoDBRestore) updateStatefulSetForPhysicalResto
cluster.Spec.ImagePullPolicy,
cmd,
)
if cluster.CompareVersion("1.23.0") >= 0 && cluster.Spec.InitContainerSecurityContext != nil {
Comment thread
egegunes marked this conversation as resolved.
Comment thread
egegunes marked this conversation as resolved.
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

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

The version gate for applying InitContainerSecurityContext to pbm-init is >= 1.23.0, but the same field is already applied to other init containers for >= 1.14.0 (see pkg/psmdb/init.go where init.SecurityContext = cr.Spec.InitContainerSecurityContext is guarded by CompareVersion("1.14.0")). With the current 1.23.0 threshold, clusters running physical restore with spec.crVersion between 1.14.0 and 1.22.x can still miss the init container security context and continue to fail PSA restricted admission. Consider lowering/removing this version guard to match the existing init-container behavior so the setting is applied consistently across supported CR versions.

Suggested change
if cluster.CompareVersion("1.23.0") >= 0 && cluster.Spec.InitContainerSecurityContext != nil {
if cluster.CompareVersion("1.14.0") >= 0 && cluster.Spec.InitContainerSecurityContext != nil {

Copilot uses AI. Check for mistakes.
pbmInit.SecurityContext = cluster.Spec.InitContainerSecurityContext
}
sts.Spec.Template.Spec.InitContainers = append(sts.Spec.Template.Spec.InitContainers, pbmInit)

pbmIdx := -1
Expand Down
92 changes: 92 additions & 0 deletions pkg/controller/perconaservermongodbrestore/physical_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,95 @@ func TestUpdateStatefulSetForPhysicalRestore(t *testing.T) {
assert.Equal(t, "PBM_MONGODB_URI", lastEnvVar.Name)
assert.Equal(t, expectedURI, lastEnvVar.Value)
}

func TestUpdateStatefulSetForPhysicalRestoreSecurityContext(t *testing.T) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Instead of having a whole new test for only asserting the security context, mostly the same as the existing, more generic test, why don't we incorporate the SC assertion in the existing?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have removed the new test that I added and added the additional CSP assertions into the existing one so there's minimal changes now.

ctx := context.Background()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since go 1.24, we can use t.Context() instead of context.Background()


nonRoot := true
allowPrivEsc := false
cluster := &psmdbv1.PerconaServerMongoDB{
ObjectMeta: metav1.ObjectMeta{
Name: "my-cluster",
Namespace: "default",
},
Spec: psmdbv1.PerconaServerMongoDBSpec{
CRVersion: version.Version(),
Comment thread
shajia-deshaw marked this conversation as resolved.
Outdated
Backup: psmdbv1.BackupSpec{
Image: "percona/percona-backup-mongodb:latest",
},
ImagePullPolicy: corev1.PullIfNotPresent,
Secrets: &psmdbv1.SecretsSpec{
Users: "users-secret",
SSL: "ssl-secret",
},
InitContainerSecurityContext: &corev1.SecurityContext{
RunAsNonRoot: &nonRoot,
AllowPrivilegeEscalation: &allowPrivEsc,
},
},
}

sts := &appsv1.StatefulSet{
ObjectMeta: metav1.ObjectMeta{
Name: "my-cluster-rs0",
Namespace: "default",
},
Spec: appsv1.StatefulSetSpec{
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{"app": "my-cluster"},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{"app": "my-cluster"},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "mongod",
Image: "percona/percona-server-mongodb:latest",
},
{
Name: naming.ContainerBackupAgent,
Image: "percona/percona-backup-agent:latest",
},
},
},
},
},
}

secretTLS := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: cluster.Spec.Secrets.SSL,
Namespace: cluster.Namespace,
},
Data: map[string][]byte{
"ca.crt": {},
"tls.crt": {},
"tls.key": {},
},
}

r := fakeReconciler(cluster, sts, secretTLS)
namespacedName := types.NamespacedName{
Name: sts.Name,
Namespace: sts.Namespace,
}

err := r.updateStatefulSetForPhysicalRestore(ctx, cluster, namespacedName, 27017)
assert.NoError(t, err)

updatedSTS := &appsv1.StatefulSet{}
err = r.client.Get(ctx, namespacedName, updatedSTS)
assert.NoError(t, err)

var pbmInit *corev1.Container
for i := range updatedSTS.Spec.Template.Spec.InitContainers {
if updatedSTS.Spec.Template.Spec.InitContainers[i].Name == "pbm-init" {
pbmInit = &updatedSTS.Spec.Template.Spec.InitContainers[i]
break
}
}
assert.NotNil(t, pbmInit)
assert.Equal(t, cluster.Spec.InitContainerSecurityContext, pbmInit.SecurityContext)
}
Loading