Removes knownPods class.

This commit is contained in:
benbierens 2023-10-31 14:48:16 +01:00
parent ac07327d77
commit a6f7bc2393
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
4 changed files with 25 additions and 42 deletions

View File

@ -9,15 +9,14 @@ namespace KubernetesWorkflow
{ {
private readonly ILog log; private readonly ILog log;
private readonly K8sCluster cluster; private readonly K8sCluster cluster;
private readonly KnownK8sPods knownPods;
private readonly WorkflowNumberSource workflowNumberSource; private readonly WorkflowNumberSource workflowNumberSource;
private readonly K8sClient client; 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.log = log;
this.cluster = cluster; this.cluster = cluster;
this.knownPods = knownPods;
this.workflowNumberSource = workflowNumberSource; this.workflowNumberSource = workflowNumberSource;
client = new K8sClient(cluster.GetK8sClientConfig()); client = new K8sClient(cluster.GetK8sClientConfig());
@ -34,9 +33,13 @@ namespace KubernetesWorkflow
log.Debug(); log.Debug();
EnsureNamespace(); 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 (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()); return new RunningPod(cluster, podInfo, deploymentName, serviceName, servicePortsMap.ToArray());
} }
@ -299,7 +302,7 @@ namespace KubernetesWorkflow
#region Deployment management #region Deployment management
private string CreateDeployment(ContainerRecipe[] containerRecipes, ILocation location) private string CreateDeployment(ContainerRecipe[] containerRecipes, ILocation location, string podLabel)
{ {
var deploymentSpec = new V1Deployment var deploymentSpec = new V1Deployment
{ {
@ -316,7 +319,7 @@ namespace KubernetesWorkflow
{ {
Metadata = new V1ObjectMeta Metadata = new V1ObjectMeta
{ {
Labels = GetSelector(containerRecipes), Labels = GetSelector(containerRecipes, podLabel),
Annotations = GetAnnotations(containerRecipes) Annotations = GetAnnotations(containerRecipes)
}, },
Spec = new V1PodSpec Spec = new V1PodSpec
@ -363,6 +366,13 @@ namespace KubernetesWorkflow
return containerRecipes.First().PodLabels.GetLabels(); return containerRecipes.First().PodLabels.GetLabels();
} }
private IDictionary<string, string> GetSelector(ContainerRecipe[] containerRecipes, string podLabel)
{
var labels = containerRecipes.First().PodLabels.Clone();
labels.Add(podLabelKey, podLabel);
return labels.GetLabels();
}
private IDictionary<string, string> GetRunnerNamespaceSelector() private IDictionary<string, string> GetRunnerNamespaceSelector()
{ {
return new Dictionary<string, string> { { "kubernetes.io/metadata.name", "default" } }; return new Dictionary<string, string> { { "kubernetes.io/metadata.name", "default" } };
@ -751,22 +761,15 @@ namespace KubernetesWorkflow
return new CrashWatcher(log, cluster.GetK8sClientConfig(), K8sNamespace, container); 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 name = pod.Name();
var ip = pod.Status.PodIP;
var newPods = pods.Where(p => !knownPods.Contains(p.Name())).ToArray(); var k8sNodeName = pod.Spec.NodeName;
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;
if (string.IsNullOrEmpty(name)) throw new InvalidOperationException("Invalid pod name received. Test infra failure."); 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."); if (string.IsNullOrEmpty(ip)) throw new InvalidOperationException("Invalid pod IP received. Test infra failure.");
knownPods.Add(name);
return new PodInfo(name, ip, k8sNodeName); return new PodInfo(name, ip, k8sNodeName);
} }
} }

View File

@ -1,17 +0,0 @@
namespace KubernetesWorkflow
{
public class KnownK8sPods
{
private readonly List<string> knownActivePodNames = new List<string>();
public bool Contains(string name)
{
return knownActivePodNames.Contains(name);
}
public void Add(string name)
{
knownActivePodNames.Add(name);
}
}
}

View File

@ -22,17 +22,15 @@ namespace KubernetesWorkflow
private readonly ILog log; private readonly ILog log;
private readonly WorkflowNumberSource numberSource; private readonly WorkflowNumberSource numberSource;
private readonly K8sCluster cluster; private readonly K8sCluster cluster;
private readonly KnownK8sPods knownK8SPods;
private readonly string k8sNamespace; private readonly string k8sNamespace;
private readonly RecipeComponentFactory componentFactory = new RecipeComponentFactory(); private readonly RecipeComponentFactory componentFactory = new RecipeComponentFactory();
private readonly LocationProvider locationProvider; 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.log = log;
this.numberSource = numberSource; this.numberSource = numberSource;
this.cluster = cluster; this.cluster = cluster;
this.knownK8SPods = knownK8SPods;
this.k8sNamespace = k8sNamespace; this.k8sNamespace = k8sNamespace;
locationProvider = new LocationProvider(log, K8s); locationProvider = new LocationProvider(log, K8s);
@ -196,7 +194,7 @@ namespace KubernetesWorkflow
{ {
try try
{ {
var controller = new K8sController(log, cluster, knownK8SPods, numberSource, k8sNamespace); var controller = new K8sController(log, cluster, numberSource, k8sNamespace);
action(controller); action(controller);
controller.Dispose(); controller.Dispose();
} }
@ -211,7 +209,7 @@ namespace KubernetesWorkflow
{ {
try try
{ {
var controller = new K8sController(log, cluster, knownK8SPods, numberSource, k8sNamespace); var controller = new K8sController(log, cluster, numberSource, k8sNamespace);
var result = action(controller); var result = action(controller);
controller.Dispose(); controller.Dispose();
return result; return result;

View File

@ -7,7 +7,6 @@ namespace KubernetesWorkflow
{ {
private readonly NumberSource numberSource = new NumberSource(0); private readonly NumberSource numberSource = new NumberSource(0);
private readonly NumberSource containerNumberSource = new NumberSource(0); private readonly NumberSource containerNumberSource = new NumberSource(0);
private readonly KnownK8sPods knownPods = new KnownK8sPods();
private readonly K8sCluster cluster; private readonly K8sCluster cluster;
private readonly ILog log; private readonly ILog log;
private readonly Configuration configuration; private readonly Configuration configuration;
@ -26,7 +25,7 @@ namespace KubernetesWorkflow
var workflowNumberSource = new WorkflowNumberSource(numberSource.GetNextNumber(), var workflowNumberSource = new WorkflowNumberSource(numberSource.GetNextNumber(),
containerNumberSource); containerNumberSource);
return new StartupWorkflow(log, workflowNumberSource, cluster, knownPods, GetNamespace(namespaceOverride)); return new StartupWorkflow(log, workflowNumberSource, cluster, GetNamespace(namespaceOverride));
} }
private string GetNamespace(string? namespaceOverride) private string GetNamespace(string? namespaceOverride)