Eric e8b2ad014b
feat: remove usage of PVC disks (#128)
* fix: delete PVCs after stopping containers

* Didn't work, instead try to delete all PVCs just before the namespace is deleted, after all pods destroyed.

* Didn't work, force kill pods, then delete pvcs

Force kill pods, wait for them to be killed. Then remove the pvc finaliser that protects the pvc from deletion. Finally, delete the pvc. The finaliser deletion step is there in case the force kill pod times out.

* try without waiting for pods to be killed

* prevent double delete race

* remove unneeded method, improve log output in pvc deletion

* Use emptyDir ephemeral volumes instead of PVCs

* fix dist tests workflow summary

After kubeconfig was replaced with an in-cluster service account, k8sClient was returning null and thus no test summaries were being written to ConfigMaps. This change returns a default Kubeconfig for the k8sClient when one is not passed in an environment variable.

* remove PVC volume deletion since PVCs are no longer created
2026-06-15 15:50:16 +10:00

115 lines
3.7 KiB
C#

using System.Diagnostics;
using System.Reflection;
using Core;
using k8s;
using k8s.Models;
using Logging;
namespace DistTestCore
{
public class Global
{
public const string TestNamespacePrefix = "storage-";
public Configuration Configuration { get; } = new Configuration();
public Assembly[] TestAssemblies { get; }
private readonly EntryPoint globalEntryPoint;
private readonly ILog log;
private static readonly IKubernetes? k8sClient = CreateK8sClient();
private static IKubernetes? CreateK8sClient()
{
try
{
var kubeconfig = Environment.GetEnvironmentVariable("KUBECONFIG");
var config = string.IsNullOrEmpty(kubeconfig)
? KubernetesClientConfiguration.BuildDefaultConfig()
: KubernetesClientConfiguration.BuildConfigFromConfigFile(kubeconfig);
return new Kubernetes(config);
}
catch
{
return null;
}
}
public Global()
{
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
TestAssemblies = assemblies.Where(a => a.FullName!.ToLowerInvariant().Contains("test")).ToArray();
log = new ConsoleLog();
globalEntryPoint = new EntryPoint(
log,
Configuration.GetK8sConfiguration(
new DefaultK8sTimeSet(),
TestNamespacePrefix
),
Configuration.GetFileManagerFolder()
);
}
/// <summary>
/// Write test result to ConfigMap so it can be read from the workflow
/// </summary>
/// <param name="runId">RunId</param>
/// <param name="json">Json payload containing the test result</param>
public static void WriteTestResult(string runId, string json)
{
if (k8sClient == null) return;
try
{
var cm = new V1ConfigMap
{
Metadata = new V1ObjectMeta
{
Name = $"test-result-{Guid.NewGuid():N}",
NamespaceProperty = "default",
Labels = new Dictionary<string, string>
{
["runid"] = runId,
["app"] = "test-result"
}
},
Data = new Dictionary<string, string> { ["result"] = json }
};
k8sClient.CoreV1.CreateNamespacedConfigMap(cm, "default");
}
catch { }
}
public void Setup()
{
try
{
Trace.Listeners.Add(new ConsoleTraceListener());
Logging.Stopwatch.Measure(log, "Global setup", () =>
{
globalEntryPoint.Announce();
globalEntryPoint.Tools.CreateWorkflow().DeleteNamespacesStartingWith(TestNamespacePrefix, wait: true);
});
}
catch (Exception ex)
{
GlobalTestFailure.HasFailed = true;
log.Error($"Global setup cleanup failed with: {ex}");
throw;
}
}
public void TearDown()
{
globalEntryPoint.Decommission(
// There shouldn't be any of either, but clean everything up regardless.
deleteKubernetesResources: true,
deleteTrackedFiles: true,
waitTillDone: true
);
Trace.Flush();
}
}
}