Skip to content

Commit b8fa8ed

Browse files
authored
added container security context and resource requirements to managed… (#134)
@moriarity-da Thank you for the contribution!
1 parent 4e4c7a7 commit b8fa8ed

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

internal/controller/documentdb_controller.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ import (
1414
pgTime "github.com/cloudnative-pg/machinery/pkg/postgres/time"
1515
batchv1 "k8s.io/api/batch/v1"
1616
corev1 "k8s.io/api/core/v1"
17-
v1 "k8s.io/api/core/v1"
1817
rbacv1 "k8s.io/api/rbac/v1"
1918
"k8s.io/apimachinery/pkg/api/errors"
19+
"k8s.io/apimachinery/pkg/api/resource"
2020
"k8s.io/apimachinery/pkg/runtime"
2121
"k8s.io/apimachinery/pkg/types"
22+
"k8s.io/utils/ptr"
2223
ctrl "sigs.k8s.io/controller-runtime"
2324
"sigs.k8s.io/controller-runtime/pkg/client"
2425
"sigs.k8s.io/controller-runtime/pkg/log"
@@ -277,7 +278,7 @@ func Promote(ctx context.Context, cli client.Client,
277278
}
278279

279280
// Check if the Pod exist
280-
var pod v1.Pod
281+
var pod corev1.Pod
281282
err = cli.Get(ctx, client.ObjectKey{Namespace: namespace, Name: serverName}, &pod)
282283
if err != nil {
283284
return fmt.Errorf("new primary node %s not found in namespace %s: %w", serverName, namespace, err)
@@ -310,10 +311,10 @@ func (r *DocumentDBReconciler) executeSQLCommand(ctx context.Context, documentdb
310311
Namespace: namespace,
311312
},
312313
Spec: batchv1.JobSpec{
313-
Template: v1.PodTemplateSpec{
314-
Spec: v1.PodSpec{
315-
RestartPolicy: v1.RestartPolicyNever,
316-
Containers: []v1.Container{
314+
Template: corev1.PodTemplateSpec{
315+
Spec: corev1.PodSpec{
316+
RestartPolicy: corev1.RestartPolicyNever,
317+
Containers: []corev1.Container{
317318
{
318319
Name: "sql-executor",
319320
Image: documentdb.Spec.DocumentDBImage,
@@ -324,6 +325,21 @@ func (r *DocumentDBReconciler) executeSQLCommand(ctx context.Context, documentdb
324325
"-d", "postgres",
325326
"-c", sqlCommand,
326327
},
328+
Resources: corev1.ResourceRequirements{
329+
Requests: corev1.ResourceList{
330+
"cpu": resource.MustParse(util.SQL_JOB_REQUESTS_CPU),
331+
"memory": resource.MustParse(util.SQL_JOB_REQUESTS_MEMORY),
332+
},
333+
Limits: corev1.ResourceList{
334+
"cpu": resource.MustParse(util.SQL_JOB_LIMITS_CPU),
335+
"memory": resource.MustParse(util.SQL_JOB_LIMITS_MEMORY),
336+
},
337+
},
338+
SecurityContext: &corev1.SecurityContext{
339+
RunAsUser: ptr.To(int64(util.SQL_JOB_LINUX_UID)),
340+
RunAsNonRoot: ptr.To(util.SQL_JOB_RUN_AS_NON_ROOT),
341+
AllowPrivilegeEscalation: ptr.To(util.SQL_JOB_ALLOW_PRIVILEGED),
342+
},
327343
},
328344
},
329345
},

internal/utils/constants.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,13 @@ const (
4545
JSON_PATCH_OP_REPLACE = "replace"
4646
JSON_PATCH_OP_ADD = "add"
4747
JSON_PATCH_OP_REMOVE = "remove"
48+
49+
// SQL job resource requirements and container security context
50+
SQL_JOB_REQUESTS_MEMORY = "32Mi"
51+
SQL_JOB_REQUESTS_CPU = "10m"
52+
SQL_JOB_LIMITS_MEMORY = "64Mi"
53+
SQL_JOB_LIMITS_CPU = "50m"
54+
SQL_JOB_LINUX_UID = 1000
55+
SQL_JOB_RUN_AS_NON_ROOT = true
56+
SQL_JOB_ALLOW_PRIVILEGED = false
4857
)

0 commit comments

Comments
 (0)