diff --git a/src/GrpcTestKit/TestConnectors/Kubernetes/TestChartGrpcMockServerConnector.cs b/src/GrpcTestKit/TestConnectors/Kubernetes/TestChartGrpcMockServerConnector.cs index dec45da..869aaf6 100644 --- a/src/GrpcTestKit/TestConnectors/Kubernetes/TestChartGrpcMockServerConnector.cs +++ b/src/GrpcTestKit/TestConnectors/Kubernetes/TestChartGrpcMockServerConnector.cs @@ -1,6 +1,7 @@ using RestEase; using SmoothSailing; using WireMock.Client; +using System.Text; namespace GrpcTestKit.TestConnectors.Kubernetes; @@ -25,17 +26,60 @@ public async Task Install() { var allProtoFile = Directory.EnumerateFiles(_protoDirectory, "*.proto", SearchOption.AllDirectories); + const int maxConfigMapSizeBytes = 512 * 1024; // 0.5 MB + var configMaps = new List(); + var currentConfigMapFiles = new List(); + var currentConfigMapSize = 0; + var configMapIndex = 0; + + foreach (var protoFile in allProtoFile) + { + var content = File.ReadAllText(protoFile); + var path = protoFile.Remove(0, _protoDirectory.Length).Replace("\\", "/").Trim('/'); + var key = Guid.NewGuid().ToString("N"); + + // Estimate size (content + some overhead for YAML structure) + var estimatedSize = Encoding.UTF8.GetByteCount(content) + Encoding.UTF8.GetByteCount(key) + Encoding.UTF8.GetByteCount(path) + 100; + + // If adding this file would exceed the limit and we already have files, start a new ConfigMap + if (currentConfigMapFiles.Count > 0 && currentConfigMapSize + estimatedSize > maxConfigMapSizeBytes) + { + configMaps.Add(new + { + index = configMapIndex, + files = currentConfigMapFiles.ToArray() + }); + + currentConfigMapFiles = new List(); + currentConfigMapSize = 0; + configMapIndex++; + } + + currentConfigMapFiles.Add(new + { + key = key, + path = path, + content = content + }); + currentConfigMapSize += estimatedSize; + } + + // Add the last ConfigMap if it has any files + if (currentConfigMapFiles.Count > 0) + { + configMaps.Add(new + { + index = configMapIndex, + files = currentConfigMapFiles.ToArray() + }); + } + var overrides = new { dockerImage = _settings.DockerImage, grpcPort = _settings.GrpcPort, stubbingPort = _settings.StubbingPort, - protoFiles = allProtoFile.Select(x => new - { - key = Guid.NewGuid().ToString("N"), - path = x.Remove(0, _protoDirectory.Length).Replace("\\", "/").Trim('/'), - content = File.ReadAllText(x) - }).ToArray() + configMaps = configMaps.ToArray() }; _release = await _chartInstaller.Install(_chart, _settings.ReleaseName, overrides, context: _settings.Context); diff --git a/src/GrpcTestKit/charts/grpcmockserver/templates/ConfigMap.yaml b/src/GrpcTestKit/charts/grpcmockserver/templates/ConfigMap.yaml index 6de8bb9..046a22a 100644 --- a/src/GrpcTestKit/charts/grpcmockserver/templates/ConfigMap.yaml +++ b/src/GrpcTestKit/charts/grpcmockserver/templates/ConfigMap.yaml @@ -1,9 +1,12 @@ +{{- range .Values.configMaps }} +--- apiVersion: v1 kind: ConfigMap metadata: - name: {{.Release.Name}}-protodrive + name: {{$.Release.Name}}-protodrive-{{ .index }} data: -{{ range .Values.protoFiles }} +{{ range .files }} {{ .key }}: | {{ .content | indent 4 }} -{{ end }} \ No newline at end of file +{{ end }} +{{- end }} diff --git a/src/GrpcTestKit/charts/grpcmockserver/templates/Pod.yaml b/src/GrpcTestKit/charts/grpcmockserver/templates/Pod.yaml index d4606fe..b7f8411 100644 --- a/src/GrpcTestKit/charts/grpcmockserver/templates/Pod.yaml +++ b/src/GrpcTestKit/charts/grpcmockserver/templates/Pod.yaml @@ -22,12 +22,16 @@ spec: periodSeconds: 10 timeoutSeconds: 10 volumeMounts: -{{- range .Values.protoFiles }} - - name: protodrive - mountPath: {{ printf "/protos/%s" (.path) }} +{{- range $configMap := .Values.configMaps }} +{{- range $configMap.files }} + - name: protodrive-{{ $configMap.index }} + mountPath: {{ printf "/protos/%s" .path }} subPath: {{ .key }} -{{ end }} +{{- end }} +{{- end }} volumes: - - name: protodrive +{{- range .Values.configMaps }} + - name: protodrive-{{ .index }} configMap: - name: {{.Release.Name}}-protodrive \ No newline at end of file + name: {{$.Release.Name}}-protodrive-{{ .index }} +{{- end }}