Skip to content

Commit d841252

Browse files
committed
Generate multiple config maps with proto to handle size limitations
1 parent 7dc90e9 commit d841252

File tree

3 files changed

+66
-15
lines changed

3 files changed

+66
-15
lines changed

src/GrpcTestKit/TestConnectors/Kubernetes/TestChartGrpcMockServerConnector.cs

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using RestEase;
22
using SmoothSailing;
33
using WireMock.Client;
4+
using System.Text;
45

56
namespace GrpcTestKit.TestConnectors.Kubernetes;
67

@@ -25,17 +26,60 @@ public async Task<GrpcMockServerConnectionInfo> Install()
2526
{
2627
var allProtoFile = Directory.EnumerateFiles(_protoDirectory, "*.proto", SearchOption.AllDirectories);
2728

29+
const int maxConfigMapSizeBytes = 512 * 1024; // 0.5 MB
30+
var configMaps = new List<object>();
31+
var currentConfigMapFiles = new List<object>();
32+
var currentConfigMapSize = 0;
33+
var configMapIndex = 0;
34+
35+
foreach (var protoFile in allProtoFile)
36+
{
37+
var content = File.ReadAllText(protoFile);
38+
var path = protoFile.Remove(0, _protoDirectory.Length).Replace("\\", "/").Trim('/');
39+
var key = Guid.NewGuid().ToString("N");
40+
41+
// Estimate size (content + some overhead for YAML structure)
42+
var estimatedSize = Encoding.UTF8.GetByteCount(content) + Encoding.UTF8.GetByteCount(key) + Encoding.UTF8.GetByteCount(path) + 100;
43+
44+
// If adding this file would exceed the limit and we already have files, start a new ConfigMap
45+
if (currentConfigMapFiles.Count > 0 && currentConfigMapSize + estimatedSize > maxConfigMapSizeBytes)
46+
{
47+
configMaps.Add(new
48+
{
49+
index = configMapIndex,
50+
files = currentConfigMapFiles.ToArray()
51+
});
52+
53+
currentConfigMapFiles = new List<object>();
54+
currentConfigMapSize = 0;
55+
configMapIndex++;
56+
}
57+
58+
currentConfigMapFiles.Add(new
59+
{
60+
key = key,
61+
path = path,
62+
content = content
63+
});
64+
currentConfigMapSize += estimatedSize;
65+
}
66+
67+
// Add the last ConfigMap if it has any files
68+
if (currentConfigMapFiles.Count > 0)
69+
{
70+
configMaps.Add(new
71+
{
72+
index = configMapIndex,
73+
files = currentConfigMapFiles.ToArray()
74+
});
75+
}
76+
2877
var overrides = new
2978
{
3079
dockerImage = _settings.DockerImage,
3180
grpcPort = _settings.GrpcPort,
3281
stubbingPort = _settings.StubbingPort,
33-
protoFiles = allProtoFile.Select(x => new
34-
{
35-
key = Guid.NewGuid().ToString("N"),
36-
path = x.Remove(0, _protoDirectory.Length).Replace("\\", "/").Trim('/'),
37-
content = File.ReadAllText(x)
38-
}).ToArray()
82+
configMaps = configMaps.ToArray()
3983
};
4084

4185
_release = await _chartInstaller.Install(_chart, _settings.ReleaseName, overrides, context: _settings.Context);
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
{{- range .Values.configMaps }}
2+
---
13
apiVersion: v1
24
kind: ConfigMap
35
metadata:
4-
name: {{.Release.Name}}-protodrive
6+
name: {{$.Release.Name}}-protodrive-{{ .index }}
57
data:
6-
{{ range .Values.protoFiles }}
8+
{{ range .files }}
79
{{ .key }}: |
810
{{ .content | indent 4 }}
9-
{{ end }}
11+
{{ end }}
12+
{{- end }}

src/GrpcTestKit/charts/grpcmockserver/templates/Pod.yaml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,16 @@ spec:
2222
periodSeconds: 10
2323
timeoutSeconds: 10
2424
volumeMounts:
25-
{{- range .Values.protoFiles }}
26-
- name: protodrive
27-
mountPath: {{ printf "/protos/%s" (.path) }}
25+
{{- range $configMap := .Values.configMaps }}
26+
{{- range $configMap.files }}
27+
- name: protodrive-{{ $configMap.index }}
28+
mountPath: {{ printf "/protos/%s" .path }}
2829
subPath: {{ .key }}
29-
{{ end }}
30+
{{- end }}
31+
{{- end }}
3032
volumes:
31-
- name: protodrive
33+
{{- range .Values.configMaps }}
34+
- name: protodrive-{{ .index }}
3235
configMap:
33-
name: {{.Release.Name}}-protodrive
36+
name: {{$.Release.Name}}-protodrive-{{ .index }}
37+
{{- end }}

0 commit comments

Comments
 (0)