-
Notifications
You must be signed in to change notification settings - Fork 182
K8SPSMDB-1595: set container security context for pbm-init container
#2283
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
Changes from 2 commits
1a755ae
e0d14ee
04a6c69
0ae8abc
032e032
e32dd8b
7533db1
73ca379
ddfd1cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -409,6 +409,9 @@ func (r *ReconcilePerconaServerMongoDBRestore) updateStatefulSetForPhysicalResto | |||||
| cluster.Spec.ImagePullPolicy, | ||||||
| cmd, | ||||||
| ) | ||||||
| if cluster.CompareVersion("1.23.0") >= 0 && cluster.Spec.InitContainerSecurityContext != nil { | ||||||
|
egegunes marked this conversation as resolved.
|
||||||
| if cluster.CompareVersion("1.23.0") >= 0 && cluster.Spec.InitContainerSecurityContext != nil { | |
| if cluster.CompareVersion("1.14.0") >= 0 && cluster.Spec.InitContainerSecurityContext != nil { |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since go 1.24, we can use t.Context() instead of |
||
|
|
||
| nonRoot := true | ||
| allowPrivEsc := false | ||
| cluster := &psmdbv1.PerconaServerMongoDB{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "my-cluster", | ||
| Namespace: "default", | ||
| }, | ||
| Spec: psmdbv1.PerconaServerMongoDBSpec{ | ||
| CRVersion: version.Version(), | ||
|
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) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.