Skip to content

K8SPG-460: Mount hugepage volumes #1230

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
32 changes: 27 additions & 5 deletions internal/postgres/huge_pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
package postgres

import (
"strings"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"

Expand All @@ -24,12 +22,12 @@ func SetHugePages(cluster *v1beta1.PostgresCluster, pgParameters *Parameters) {
}
}

// This helper function checks to see if a huge_pages value greater than zero has
// This helper function checks to see if a hugepages-2Mi value greater than zero has
// been set in any of the PostgresCluster's instances' resource specs
func HugePagesRequested(cluster *v1beta1.PostgresCluster) bool {
func HugePages2MiRequested(cluster *v1beta1.PostgresCluster) bool {
for _, instance := range cluster.Spec.InstanceSets {
for resourceName := range instance.Resources.Limits {
if strings.HasPrefix(resourceName.String(), corev1.ResourceHugePagesPrefix) {
if resourceName == corev1.ResourceHugePagesPrefix+"2Mi" {
resourceQuantity := instance.Resources.Limits.Name(resourceName, resource.BinarySI)

if resourceQuantity != nil && resourceQuantity.Value() > 0 {
Expand All @@ -41,3 +39,27 @@ func HugePagesRequested(cluster *v1beta1.PostgresCluster) bool {

return false
}

// This helper function checks to see if a hugepages-1Gi value greater than zero has
// been set in any of the PostgresCluster's instances' resource specs
func HugePages1GiRequested(cluster *v1beta1.PostgresCluster) bool {
for _, instance := range cluster.Spec.InstanceSets {
for resourceName := range instance.Resources.Limits {
if resourceName == corev1.ResourceHugePagesPrefix+"1Gi" {
resourceQuantity := instance.Resources.Limits.Name(resourceName, resource.BinarySI)

if resourceQuantity != nil && resourceQuantity.Value() > 0 {
return true
}
}
}
}

return false
}

// This helper function checks to see if a huge_pages value greater than zero has
// been set in any of the PostgresCluster's instances' resource specs
func HugePagesRequested(cluster *v1beta1.PostgresCluster) bool {
return HugePages2MiRequested(cluster) || HugePages1GiRequested(cluster)
}
38 changes: 38 additions & 0 deletions internal/postgres/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,22 @@ func InstancePod(ctx context.Context,
dataVolumeMount,
downwardAPIVolumeMount,
}

if HugePages2MiRequested(inCluster) {
dbContainerMounts = append(dbContainerMounts, corev1.VolumeMount{

Name: "hugepage-2mi",
MountPath: "/hugepages-2Mi",
})
}

if HugePages1GiRequested(inCluster) {
dbContainerMounts = append(dbContainerMounts, corev1.VolumeMount{
Name: "hugepage-1gi",
MountPath: "/hugepages-1Gi",
})
}

dbContainerMounts = append(dbContainerMounts, inInstanceSpec.VolumeMounts...)

container := corev1.Container{
Expand Down Expand Up @@ -221,6 +237,28 @@ func InstancePod(ctx context.Context,
downwardAPIVolume,
}

if HugePages2MiRequested(inCluster) {
outInstancePod.Volumes = append(outInstancePod.Volumes, corev1.Volume{
Name: "hugepage-2mi",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{
Medium: corev1.StorageMediumHugePagesPrefix + "2Mi",
},
},
})
}

if HugePages1GiRequested(inCluster) {
outInstancePod.Volumes = append(outInstancePod.Volumes, corev1.Volume{
Name: "hugepage-1gi",
VolumeSource: corev1.VolumeSource{
EmptyDir: &corev1.EmptyDirVolumeSource{
Medium: corev1.StorageMediumHugePagesPrefix + "1Gi",
},
},
})
}

// If `TablespaceVolumes` FeatureGate is enabled, `inTablespaceVolumes` may not be nil.
// In that case, add any tablespace volumes to the pod, and
// add volumeMounts to the database and startup containers
Expand Down
276 changes: 276 additions & 0 deletions internal/postgres/reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,282 @@ volumes:
assert.DeepEqual(t, pod.InitContainers[0].Command[4:],
[]string{"startup", "11", "/pgwal/pg11_wal", "/pgdata/pgbackrest/log"})
})

t.Run("WithHugepages2Mi", func(t *testing.T) {
clusterWithHugepages2Mi := cluster.DeepCopy()
clusterWithHugepages2Mi.Spec.InstanceSets = []v1beta1.PostgresInstanceSetSpec{
{
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("4Gi"),
corev1.ResourceHugePagesPrefix + "2Mi": resource.MustParse("2Gi"),
},
},
},
}

pod := new(corev1.PodSpec)
InstancePod(ctx, clusterWithHugepages2Mi, instance,
serverSecretProjection, clientSecretProjection, dataVolume, nil, nil, pod)

assert.Assert(t, cmp.MarshalMatches(pod.Volumes, `
- name: cert-volume
projected:
defaultMode: 384
sources:
- secret:
items:
- key: tls.crt
path: tls.crt
- key: tls.key
path: tls.key
- key: ca.crt
path: ca.crt
name: srv-secret
- secret:
items:
- key: tls.crt
path: replication/tls.crt
- key: tls.key
path: replication/tls.key
name: repl-secret
- name: postgres-data
persistentVolumeClaim:
claimName: datavol
- downwardAPI:
items:
- path: cpu_limit
resourceFieldRef:
containerName: database
divisor: 1m
resource: limits.cpu
- path: cpu_request
resourceFieldRef:
containerName: database
divisor: 1m
resource: requests.cpu
- path: mem_limit
resourceFieldRef:
containerName: database
divisor: 1Mi
resource: limits.memory
- path: mem_request
resourceFieldRef:
containerName: database
divisor: 1Mi
resource: requests.memory
- fieldRef:
apiVersion: v1
fieldPath: metadata.labels
path: labels
- fieldRef:
apiVersion: v1
fieldPath: metadata.annotations
path: annotations
name: database-containerinfo
- emptyDir:
medium: HugePages-2Mi
name: hugepage-2mi
`), "expected HugePages-2Mi volume")

assert.Assert(t, cmp.MarshalMatches(pod.Containers[0].VolumeMounts, `
- mountPath: /pgconf/tls
name: cert-volume
readOnly: true
- mountPath: /pgdata
name: postgres-data
- mountPath: /etc/database-containerinfo
name: database-containerinfo
readOnly: true
- mountPath: /hugepages-2Mi
name: hugepage-2mi`), "expected hugepage mount in %q container", pod.Containers[0].Name)
})

t.Run("WithHugepages1Gi", func(t *testing.T) {
clusterWithHugepages1Gi := cluster.DeepCopy()
clusterWithHugepages1Gi.Spec.InstanceSets = []v1beta1.PostgresInstanceSetSpec{
{
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("4Gi"),
corev1.ResourceHugePagesPrefix + "1Gi": resource.MustParse("2Gi"),
},
},
},
}

pod := new(corev1.PodSpec)
InstancePod(ctx, clusterWithHugepages1Gi, instance,
serverSecretProjection, clientSecretProjection, dataVolume, nil, nil, pod)

assert.Assert(t, cmp.MarshalMatches(pod.Volumes, `
- name: cert-volume
projected:
defaultMode: 384
sources:
- secret:
items:
- key: tls.crt
path: tls.crt
- key: tls.key
path: tls.key
- key: ca.crt
path: ca.crt
name: srv-secret
- secret:
items:
- key: tls.crt
path: replication/tls.crt
- key: tls.key
path: replication/tls.key
name: repl-secret
- name: postgres-data
persistentVolumeClaim:
claimName: datavol
- downwardAPI:
items:
- path: cpu_limit
resourceFieldRef:
containerName: database
divisor: 1m
resource: limits.cpu
- path: cpu_request
resourceFieldRef:
containerName: database
divisor: 1m
resource: requests.cpu
- path: mem_limit
resourceFieldRef:
containerName: database
divisor: 1Mi
resource: limits.memory
- path: mem_request
resourceFieldRef:
containerName: database
divisor: 1Mi
resource: requests.memory
- fieldRef:
apiVersion: v1
fieldPath: metadata.labels
path: labels
- fieldRef:
apiVersion: v1
fieldPath: metadata.annotations
path: annotations
name: database-containerinfo
- emptyDir:
medium: HugePages-1Gi
name: hugepage-1gi
`), "expected HugePages-1Gi volume")

assert.Assert(t, cmp.MarshalMatches(pod.Containers[0].VolumeMounts, `
- mountPath: /pgconf/tls
name: cert-volume
readOnly: true
- mountPath: /pgdata
name: postgres-data
- mountPath: /etc/database-containerinfo
name: database-containerinfo
readOnly: true
- mountPath: /hugepages-1Gi
name: hugepage-1gi`), "expected hugepage mount in %q container", pod.Containers[0].Name)
})

t.Run("WithHugepages", func(t *testing.T) {
clusterWithHugepages := cluster.DeepCopy()
clusterWithHugepages.Spec.InstanceSets = []v1beta1.PostgresInstanceSetSpec{
{
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
corev1.ResourceMemory: resource.MustParse("4Gi"),
corev1.ResourceHugePagesPrefix + "2Mi": resource.MustParse("2Gi"),
corev1.ResourceHugePagesPrefix + "1Gi": resource.MustParse("2Gi"),
},
},
},
}

pod := new(corev1.PodSpec)
InstancePod(ctx, clusterWithHugepages, instance,
serverSecretProjection, clientSecretProjection, dataVolume, nil, nil, pod)

assert.Assert(t, cmp.MarshalMatches(pod.Volumes, `
- name: cert-volume
projected:
defaultMode: 384
sources:
- secret:
items:
- key: tls.crt
path: tls.crt
- key: tls.key
path: tls.key
- key: ca.crt
path: ca.crt
name: srv-secret
- secret:
items:
- key: tls.crt
path: replication/tls.crt
- key: tls.key
path: replication/tls.key
name: repl-secret
- name: postgres-data
persistentVolumeClaim:
claimName: datavol
- downwardAPI:
items:
- path: cpu_limit
resourceFieldRef:
containerName: database
divisor: 1m
resource: limits.cpu
- path: cpu_request
resourceFieldRef:
containerName: database
divisor: 1m
resource: requests.cpu
- path: mem_limit
resourceFieldRef:
containerName: database
divisor: 1Mi
resource: limits.memory
- path: mem_request
resourceFieldRef:
containerName: database
divisor: 1Mi
resource: requests.memory
- fieldRef:
apiVersion: v1
fieldPath: metadata.labels
path: labels
- fieldRef:
apiVersion: v1
fieldPath: metadata.annotations
path: annotations
name: database-containerinfo
- emptyDir:
medium: HugePages-2Mi
name: hugepage-2mi
- emptyDir:
medium: HugePages-1Gi
name: hugepage-1gi
`), "expected HugePages-2Mi and HugePages-1Gi volumes")

assert.Assert(t, cmp.MarshalMatches(pod.Containers[0].VolumeMounts, `
- mountPath: /pgconf/tls
name: cert-volume
readOnly: true
- mountPath: /pgdata
name: postgres-data
- mountPath: /etc/database-containerinfo
name: database-containerinfo
readOnly: true
- mountPath: /hugepages-2Mi
name: hugepage-2mi
- mountPath: /hugepages-1Gi
name: hugepage-1gi`), "expected hugepage mounts in %q container", pod.Containers[0].Name)
})
}

func TestPodSecurityContext(t *testing.T) {
Expand Down
Loading