sets up additional pod labels

This commit is contained in:
benbierens 2023-08-10 11:25:22 +02:00
parent 17673e8fa5
commit 01ec6f7d8b
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
7 changed files with 48 additions and 42 deletions

View File

@ -72,7 +72,7 @@ namespace CodexNetDeployer
k8sNamespacePrefix: config.KubeNamespace
);
return new TestLifecycle(log, lifecycleConfig, timeset);
return new TestLifecycle(log, lifecycleConfig, timeset, config.TestsTypePodLabel);
}
private RunningContainer? StartMetricsService(TestLifecycle lifecycle, CodexSetup setup, List<RunningContainer> codexContainers)

View File

@ -20,7 +20,7 @@ namespace ContinuousTests
k8sNamespacePrefix: customNamespace
);
return new TestLifecycle(log, lifecycleConfig, timeSet);
return new TestLifecycle(log, lifecycleConfig, timeSet, "continuous-tests");
}
private static string? GetKubeConfig(string kubeConfigFile)

View File

@ -13,6 +13,7 @@ namespace DistTestCore
[Parallelizable(ParallelScope.All)]
public abstract class DistTest
{
private const string TestsType = "dist-tests";
private readonly Configuration configuration = new Configuration();
private readonly Assembly[] testAssemblies;
private readonly FixtureLog fixtureLog;
@ -52,7 +53,7 @@ namespace DistTestCore
{
Stopwatch.Measure(fixtureLog, "Global setup", () =>
{
var wc = new WorkflowCreator(fixtureLog, configuration.GetK8sConfiguration(GetTimeSet()), "dist-tests");
var wc = new WorkflowCreator(fixtureLog, configuration.GetK8sConfiguration(GetTimeSet()), new PodLabels(TestsType, "null"));
wc.CreateWorkflow().DeleteAllResources();
});
}
@ -195,7 +196,7 @@ namespace DistTestCore
{
lock (lifecycleLock)
{
lifecycles.Add(testName, new TestLifecycle(fixtureLog.CreateTestLog(), configuration, GetTimeSet()));
lifecycles.Add(testName, new TestLifecycle(fixtureLog.CreateTestLog(), configuration, GetTimeSet(), TestsType));
}
});
}
@ -206,7 +207,7 @@ namespace DistTestCore
var testResult = GetTestResult();
var testDuration = lifecycle.GetTestDuration();
fixtureLog.Log($"{GetCurrentTestName()} = {testResult} ({testDuration})");
statusLog.ConcludeTest(testResult, testDuration, GetCodexId(lifecycle));
statusLog.ConcludeTest(testResult, testDuration, lifecycle.GetCodexId());
Stopwatch.Measure(fixtureLog, $"Teardown for {GetCurrentTestName()}", () =>
{
lifecycle.Log.EndTest();
@ -216,14 +217,6 @@ namespace DistTestCore
});
}
private static string GetCodexId(TestLifecycle lifecycle)
{
var v = lifecycle.CodexVersion;
if (v == null) return new CodexContainerRecipe().Image;
if (v.version != "untagged build") return v.version;
return v.revision;
}
private ITimeSet GetTimeSet()
{
if (ShouldUseLongTimeouts()) return new LongTimeSet();

View File

@ -10,13 +10,14 @@ namespace DistTestCore
{
private readonly DateTime testStart;
public TestLifecycle(BaseLog log, Configuration configuration, ITimeSet timeSet)
public TestLifecycle(BaseLog log, Configuration configuration, ITimeSet timeSet, string testsType)
{
Log = log;
Configuration = configuration;
TimeSet = timeSet;
WorkflowCreator = new WorkflowCreator(log, configuration.GetK8sConfiguration(timeSet), "dist-tests");
var podLabels = new PodLabels(testsType, GetCodexId());
WorkflowCreator = new WorkflowCreator(log, configuration.GetK8sConfiguration(timeSet), podLabels);
FileManager = new FileManager(Log, configuration);
CodexStarter = new CodexStarter(this);
@ -66,5 +67,13 @@ namespace DistTestCore
{
if (CodexVersion == null) CodexVersion = version;
}
public string GetCodexId()
{
var v = CodexVersion;
if (v == null) return new CodexContainerRecipe().Image;
if (v.version != "untagged build") return v.version;
return v.revision;
}
}
}

View File

@ -1,10 +1,28 @@
namespace KubernetesWorkflow
using Logging;
namespace KubernetesWorkflow
{
public class PodLabels
{
private readonly Dictionary<string, string> labels = new Dictionary<string, string>();
public void Add(string key, string value)
public PodLabels(string testsType, string codexId)
{
Add("tests-type", testsType);
Add("runid", NameUtils.GetRunId());
Add("testid", NameUtils.GetTestId());
Add("category", NameUtils.GetCategoryName());
Add("codexid", codexId);
Add("fixturename", NameUtils.GetRawFixtureName());
Add("testname", NameUtils.GetTestMethodName());
}
public void AddAppName(string appName)
{
Add("app", appName);
}
private void Add(string key, string value)
{
labels.Add(key, value.ToLowerInvariant());
}

View File

@ -10,26 +10,22 @@ namespace KubernetesWorkflow
private readonly K8sCluster cluster;
private readonly KnownK8sPods knownK8SPods;
private readonly string testNamespace;
private readonly string testsType;
private readonly PodLabels podLabels;
private readonly RecipeComponentFactory componentFactory = new RecipeComponentFactory();
internal StartupWorkflow(BaseLog log, WorkflowNumberSource numberSource, K8sCluster cluster, KnownK8sPods knownK8SPods, string testNamespace, string testsType)
internal StartupWorkflow(BaseLog log, WorkflowNumberSource numberSource, K8sCluster cluster, KnownK8sPods knownK8SPods, string testNamespace, PodLabels podLabels)
{
this.log = log;
this.numberSource = numberSource;
this.cluster = cluster;
this.knownK8SPods = knownK8SPods;
this.testNamespace = testNamespace;
this.testsType = testsType;
this.podLabels = podLabels;
}
public RunningContainers Start(int numberOfContainers, Location location, ContainerRecipeFactory recipeFactory, StartupConfig startupConfig)
{
var podLabels = new PodLabels();
podLabels.Add("runid", NameUtils.GetRunId());
podLabels.Add("tests-type", testsType);
podLabels.Add("app", recipeFactory.AppName);
podLabels.AddAppName(recipeFactory.AppName);
return K8s(controller =>
{
@ -38,7 +34,7 @@ namespace KubernetesWorkflow
var runningPod = controller.BringOnline(recipes, location);
return new RunningContainers(startupConfig, runningPod, CreateContainers(runningPod, recipes, startupConfig));
}, podLabels);
});
}
public void Stop(RunningContainers runningContainers)
@ -155,17 +151,12 @@ namespace KubernetesWorkflow
private void K8s(Action<K8sController> action)
{
var controller = new K8sController(log, cluster, knownK8SPods, numberSource, testNamespace, new PodLabels());
var controller = new K8sController(log, cluster, knownK8SPods, numberSource, testNamespace, podLabels);
action(controller);
controller.Dispose();
}
private T K8s<T>(Func<K8sController, T> action)
{
return K8s(action, new PodLabels());
}
private T K8s<T>(Func<K8sController, T> action, PodLabels podLabels)
{
var controller = new K8sController(log, cluster, knownK8SPods, numberSource, testNamespace, podLabels);
var result = action(controller);

View File

@ -10,20 +10,15 @@ namespace KubernetesWorkflow
private readonly KnownK8sPods knownPods = new KnownK8sPods();
private readonly K8sCluster cluster;
private readonly BaseLog log;
private readonly string testsType;
private readonly PodLabels podLabels;
private readonly string testNamespace;
public WorkflowCreator(BaseLog log, Configuration configuration, string testsType)
: this(log, configuration, testsType, Guid.NewGuid().ToString().ToLowerInvariant())
{
}
public WorkflowCreator(BaseLog log, Configuration configuration, string testsType, string testNamespacePostfix)
public WorkflowCreator(BaseLog log, Configuration configuration, PodLabels podLabels)
{
cluster = new K8sCluster(configuration);
this.log = log;
this.testsType = testsType;
testNamespace = testNamespacePostfix;
this.podLabels = podLabels;
testNamespace = Guid.NewGuid().ToString().ToLowerInvariant();
}
public StartupWorkflow CreateWorkflow()
@ -31,7 +26,7 @@ namespace KubernetesWorkflow
var workflowNumberSource = new WorkflowNumberSource(numberSource.GetNextNumber(),
containerNumberSource);
return new StartupWorkflow(log, workflowNumberSource, cluster, knownPods, testNamespace, testsType);
return new StartupWorkflow(log, workflowNumberSource, cluster, knownPods, testNamespace, podLabels);
}
}
}