Skip to content

Commit 8e3873f

Browse files
authored
fix(deployment): pass --maxmemory to Redis instance (#6737)
1 parent c6badf5 commit 8e3873f

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

deployment/utils/k8s.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const memoryMultipliers: Record<string, number> = {
2+
k: 1000,
3+
M: 1000 ** 2,
4+
G: 1000 ** 3,
5+
T: 1000 ** 4,
6+
P: 1000 ** 5,
7+
E: 1000 ** 6,
8+
Ki: 1024,
9+
Mi: 1024 ** 2,
10+
Gi: 1024 ** 3,
11+
Ti: 1024 ** 4,
12+
Pi: 1024 ** 5,
13+
Ei: 1024 ** 6,
14+
} as const;
15+
16+
export function memoryParser(input: string): number {
17+
const unitMatch = input.match(/^([0-9]+)([A-Za-z]{1,2})$/);
18+
if (unitMatch) {
19+
return parseInt(unitMatch[1], 10) * memoryMultipliers[unitMatch[2]];
20+
}
21+
22+
return parseInt(input, 10);
23+
}

deployment/utils/redis.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as k8s from '@pulumi/kubernetes';
22
import * as kx from '@pulumi/kubernetesx';
33
import { Output } from '@pulumi/pulumi';
4+
import { memoryParser } from './k8s';
45
import { getLocalComposeConfig } from './local-config';
56
import { normalizeEnv, PodBuilder } from './pod-builder';
67

@@ -16,9 +17,13 @@ export class Redis {
1617
},
1718
) {}
1819

19-
deploy({ limits }: { limits: k8s.types.input.core.v1.ResourceRequirements['limits'] }) {
20+
deploy(input: { limits: { memory: string; cpu: string } }) {
2021
const redisService = getLocalComposeConfig().service('redis');
2122
const name = 'redis-store';
23+
const limits: k8s.types.input.core.v1.ResourceRequirements['limits'] = {
24+
memory: input.limits.memory,
25+
cpu: input.limits.cpu,
26+
};
2227

2328
const env: k8s.types.input.core.v1.EnvVar[] = normalizeEnv(this.options.env ?? {}).concat([
2429
{
@@ -76,6 +81,9 @@ export class Redis {
7681
},
7782
];
7883

84+
const memoryInBytes = memoryParser(input.limits.memory) * 0.8; // Redis recommends 80%
85+
const memoryInMegabytes = Math.floor(memoryInBytes / 1024 / 1024);
86+
7987
const pb = new PodBuilder({
8088
restartPolicy: 'Always',
8189
containers: [
@@ -97,6 +105,10 @@ export class Redis {
97105
command: ['/bin/sh', '/scripts/liveness.sh'],
98106
},
99107
},
108+
// Note: this is needed, otherwise local config is not loaded at all
109+
command: ['/opt/bitnami/scripts/redis/entrypoint.sh'],
110+
// This is where we can pass actual flags to the bitnami/redis runtime
111+
args: ['/opt/bitnami/scripts/redis/run.sh', `--maxmemory ${memoryInMegabytes}mb`],
100112
readinessProbe: {
101113
initialDelaySeconds: 5,
102114
periodSeconds: 8,

0 commit comments

Comments
 (0)