From a6f7bc2393ee58f2b6e6575f2cfa1a62020b7614 Mon Sep 17 00:00:00 2001 From: benbierens Date: Tue, 31 Oct 2023 14:48:16 +0100 Subject: [PATCH] Removes knownPods class. --- Framework/KubernetesWorkflow/K8sController.cs | 39 ++++++++++--------- Framework/KubernetesWorkflow/KnownK8sPods.cs | 17 -------- .../KubernetesWorkflow/StartupWorkflow.cs | 8 ++-- .../KubernetesWorkflow/WorkflowCreator.cs | 3 +- 4 files changed, 25 insertions(+), 42 deletions(-) delete mode 100644 Framework/KubernetesWorkflow/KnownK8sPods.cs diff --git a/Framework/KubernetesWorkflow/K8sController.cs b/Framework/KubernetesWorkflow/K8sController.cs index f18afdb..8054892 100644 --- a/Framework/KubernetesWorkflow/K8sController.cs +++ b/Framework/KubernetesWorkflow/K8sController.cs @@ -9,15 +9,14 @@ namespace KubernetesWorkflow { private readonly ILog log; private readonly K8sCluster cluster; - private readonly KnownK8sPods knownPods; private readonly WorkflowNumberSource workflowNumberSource; private readonly K8sClient client; + private const string podLabelKey = "pod-uuid"; - public K8sController(ILog log, K8sCluster cluster, KnownK8sPods knownPods, WorkflowNumberSource workflowNumberSource, string k8sNamespace) + public K8sController(ILog log, K8sCluster cluster, WorkflowNumberSource workflowNumberSource, string k8sNamespace) { this.log = log; this.cluster = cluster; - this.knownPods = knownPods; this.workflowNumberSource = workflowNumberSource; client = new K8sClient(cluster.GetK8sClientConfig()); @@ -34,9 +33,13 @@ namespace KubernetesWorkflow log.Debug(); EnsureNamespace(); - var deploymentName = CreateDeployment(containerRecipes, location); + var podLabel = K8sNameUtils.Format(Guid.NewGuid().ToString()); + var deploymentName = CreateDeployment(containerRecipes, location, podLabel); var (serviceName, servicePortsMap) = CreateService(containerRecipes); - var podInfo = FetchNewPod(); + + var pods = client.Run(c => c.ListNamespacedPod(K8sNamespace)); + var pod = pods.Items.Single(p => p.Labels().Any(l => l.Key == podLabelKey && l.Value == podLabel)); + var podInfo = CreatePodInfo(pod); return new RunningPod(cluster, podInfo, deploymentName, serviceName, servicePortsMap.ToArray()); } @@ -299,7 +302,7 @@ namespace KubernetesWorkflow #region Deployment management - private string CreateDeployment(ContainerRecipe[] containerRecipes, ILocation location) + private string CreateDeployment(ContainerRecipe[] containerRecipes, ILocation location, string podLabel) { var deploymentSpec = new V1Deployment { @@ -316,7 +319,7 @@ namespace KubernetesWorkflow { Metadata = new V1ObjectMeta { - Labels = GetSelector(containerRecipes), + Labels = GetSelector(containerRecipes, podLabel), Annotations = GetAnnotations(containerRecipes) }, Spec = new V1PodSpec @@ -363,6 +366,13 @@ namespace KubernetesWorkflow return containerRecipes.First().PodLabels.GetLabels(); } + private IDictionary GetSelector(ContainerRecipe[] containerRecipes, string podLabel) + { + var labels = containerRecipes.First().PodLabels.Clone(); + labels.Add(podLabelKey, podLabel); + return labels.GetLabels(); + } + private IDictionary GetRunnerNamespaceSelector() { return new Dictionary { { "kubernetes.io/metadata.name", "default" } }; @@ -751,22 +761,15 @@ namespace KubernetesWorkflow return new CrashWatcher(log, cluster.GetK8sClientConfig(), K8sNamespace, container); } - private PodInfo FetchNewPod() + private PodInfo CreatePodInfo(V1Pod pod) { - var pods = client.Run(c => c.ListNamespacedPod(K8sNamespace)).Items; - - var newPods = pods.Where(p => !knownPods.Contains(p.Name())).ToArray(); - if (newPods.Length != 1) throw new InvalidOperationException("Expected only 1 pod to be created. Test infra failure."); - - var newPod = newPods.Single(); - var name = newPod.Name(); - var ip = newPod.Status.PodIP; - var k8sNodeName = newPod.Spec.NodeName; + var name = pod.Name(); + var ip = pod.Status.PodIP; + var k8sNodeName = pod.Spec.NodeName; if (string.IsNullOrEmpty(name)) throw new InvalidOperationException("Invalid pod name received. Test infra failure."); if (string.IsNullOrEmpty(ip)) throw new InvalidOperationException("Invalid pod IP received. Test infra failure."); - knownPods.Add(name); return new PodInfo(name, ip, k8sNodeName); } } diff --git a/Framework/KubernetesWorkflow/KnownK8sPods.cs b/Framework/KubernetesWorkflow/KnownK8sPods.cs deleted file mode 100644 index 6d80eb6..0000000 --- a/Framework/KubernetesWorkflow/KnownK8sPods.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace KubernetesWorkflow -{ - public class KnownK8sPods - { - private readonly List knownActivePodNames = new List(); - - public bool Contains(string name) - { - return knownActivePodNames.Contains(name); - } - - public void Add(string name) - { - knownActivePodNames.Add(name); - } - } -} diff --git a/Framework/KubernetesWorkflow/StartupWorkflow.cs b/Framework/KubernetesWorkflow/StartupWorkflow.cs index 1cd5eb1..b34cb75 100644 --- a/Framework/KubernetesWorkflow/StartupWorkflow.cs +++ b/Framework/KubernetesWorkflow/StartupWorkflow.cs @@ -22,17 +22,15 @@ namespace KubernetesWorkflow private readonly ILog log; private readonly WorkflowNumberSource numberSource; private readonly K8sCluster cluster; - private readonly KnownK8sPods knownK8SPods; private readonly string k8sNamespace; private readonly RecipeComponentFactory componentFactory = new RecipeComponentFactory(); private readonly LocationProvider locationProvider; - internal StartupWorkflow(ILog log, WorkflowNumberSource numberSource, K8sCluster cluster, KnownK8sPods knownK8SPods, string k8sNamespace) + internal StartupWorkflow(ILog log, WorkflowNumberSource numberSource, K8sCluster cluster, string k8sNamespace) { this.log = log; this.numberSource = numberSource; this.cluster = cluster; - this.knownK8SPods = knownK8SPods; this.k8sNamespace = k8sNamespace; locationProvider = new LocationProvider(log, K8s); @@ -196,7 +194,7 @@ namespace KubernetesWorkflow { try { - var controller = new K8sController(log, cluster, knownK8SPods, numberSource, k8sNamespace); + var controller = new K8sController(log, cluster, numberSource, k8sNamespace); action(controller); controller.Dispose(); } @@ -211,7 +209,7 @@ namespace KubernetesWorkflow { try { - var controller = new K8sController(log, cluster, knownK8SPods, numberSource, k8sNamespace); + var controller = new K8sController(log, cluster, numberSource, k8sNamespace); var result = action(controller); controller.Dispose(); return result; diff --git a/Framework/KubernetesWorkflow/WorkflowCreator.cs b/Framework/KubernetesWorkflow/WorkflowCreator.cs index 213db92..d70b0bc 100644 --- a/Framework/KubernetesWorkflow/WorkflowCreator.cs +++ b/Framework/KubernetesWorkflow/WorkflowCreator.cs @@ -7,7 +7,6 @@ namespace KubernetesWorkflow { private readonly NumberSource numberSource = new NumberSource(0); private readonly NumberSource containerNumberSource = new NumberSource(0); - private readonly KnownK8sPods knownPods = new KnownK8sPods(); private readonly K8sCluster cluster; private readonly ILog log; private readonly Configuration configuration; @@ -26,7 +25,7 @@ namespace KubernetesWorkflow var workflowNumberSource = new WorkflowNumberSource(numberSource.GetNextNumber(), containerNumberSource); - return new StartupWorkflow(log, workflowNumberSource, cluster, knownPods, GetNamespace(namespaceOverride)); + return new StartupWorkflow(log, workflowNumberSource, cluster, GetNamespace(namespaceOverride)); } private string GetNamespace(string? namespaceOverride)