From 01ec6f7d8b7f8d779435b888e3910ae5c0615c0b Mon Sep 17 00:00:00 2001 From: benbierens Date: Thu, 10 Aug 2023 11:25:22 +0200 Subject: [PATCH] sets up additional pod labels --- CodexNetDeployer/Deployer.cs | 2 +- ContinuousTests/K8sFactory.cs | 2 +- DistTestCore/DistTest.cs | 15 ++++----------- DistTestCore/TestLifecycle.cs | 13 +++++++++++-- KubernetesWorkflow/PodLabels.cs | 22 ++++++++++++++++++++-- KubernetesWorkflow/StartupWorkflow.cs | 21 ++++++--------------- KubernetesWorkflow/WorkflowCreator.cs | 15 +++++---------- 7 files changed, 48 insertions(+), 42 deletions(-) diff --git a/CodexNetDeployer/Deployer.cs b/CodexNetDeployer/Deployer.cs index dda9cd8..32e08c7 100644 --- a/CodexNetDeployer/Deployer.cs +++ b/CodexNetDeployer/Deployer.cs @@ -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 codexContainers) diff --git a/ContinuousTests/K8sFactory.cs b/ContinuousTests/K8sFactory.cs index 7695a76..5627941 100644 --- a/ContinuousTests/K8sFactory.cs +++ b/ContinuousTests/K8sFactory.cs @@ -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) diff --git a/DistTestCore/DistTest.cs b/DistTestCore/DistTest.cs index e05c566..48ae884 100644 --- a/DistTestCore/DistTest.cs +++ b/DistTestCore/DistTest.cs @@ -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(); diff --git a/DistTestCore/TestLifecycle.cs b/DistTestCore/TestLifecycle.cs index 26c0a5a..cc98ec5 100644 --- a/DistTestCore/TestLifecycle.cs +++ b/DistTestCore/TestLifecycle.cs @@ -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; + } } } diff --git a/KubernetesWorkflow/PodLabels.cs b/KubernetesWorkflow/PodLabels.cs index 8c444bf..e1097d5 100644 --- a/KubernetesWorkflow/PodLabels.cs +++ b/KubernetesWorkflow/PodLabels.cs @@ -1,10 +1,28 @@ -namespace KubernetesWorkflow +using Logging; + +namespace KubernetesWorkflow { public class PodLabels { private readonly Dictionary labels = new Dictionary(); - 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()); } diff --git a/KubernetesWorkflow/StartupWorkflow.cs b/KubernetesWorkflow/StartupWorkflow.cs index 67f757f..185980b 100644 --- a/KubernetesWorkflow/StartupWorkflow.cs +++ b/KubernetesWorkflow/StartupWorkflow.cs @@ -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 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(Func action) - { - return K8s(action, new PodLabels()); - } - - private T K8s(Func action, PodLabels podLabels) { var controller = new K8sController(log, cluster, knownK8SPods, numberSource, testNamespace, podLabels); var result = action(controller); diff --git a/KubernetesWorkflow/WorkflowCreator.cs b/KubernetesWorkflow/WorkflowCreator.cs index 2672f0f..afc515f 100644 --- a/KubernetesWorkflow/WorkflowCreator.cs +++ b/KubernetesWorkflow/WorkflowCreator.cs @@ -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); } } }