Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using RestEase;
using SmoothSailing;
using WireMock.Client;
using System.Text;

namespace GrpcTestKit.TestConnectors.Kubernetes;

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

const int maxConfigMapSizeBytes = 512 * 1024; // 0.5 MB
var configMaps = new List<object>();
var currentConfigMapFiles = new List<object>();
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<object>();
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 }}
{{ end }}
{{- end }}
16 changes: 10 additions & 6 deletions src/GrpcTestKit/charts/grpcmockserver/templates/Pod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
name: {{$.Release.Name}}-protodrive-{{ .index }}
{{- end }}
Loading