From ec6c987ef901092fd3b18b304e721c3b7c7daed0 Mon Sep 17 00:00:00 2001 From: benbierens Date: Tue, 7 Nov 2023 15:51:28 +0100 Subject: [PATCH] determine runner location with environment variables --- Framework/KubernetesWorkflow/K8sController.cs | 9 ------ .../KubernetesWorkflow/RunnerLocationUtils.cs | 30 +++++++++++-------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/Framework/KubernetesWorkflow/K8sController.cs b/Framework/KubernetesWorkflow/K8sController.cs index 930ad1c..a8fcfa4 100644 --- a/Framework/KubernetesWorkflow/K8sController.cs +++ b/Framework/KubernetesWorkflow/K8sController.cs @@ -38,8 +38,6 @@ namespace KubernetesWorkflow var internalService = CreateInternalService(containerRecipes); var externalService = CreateExternalService(containerRecipes); - DetermineRunnerLocation(deployment); - return new StartResult(cluster, containerRecipes, deployment, internalService, externalService); } @@ -834,12 +832,5 @@ namespace KubernetesWorkflow return new PodInfo(name, ip, k8sNodeName); } - - private void DetermineRunnerLocation(RunningDeployment deployment) - { - if (RunnerLocationUtils.IsKnown()) return; - var podInfo = GetPodInfo(deployment); - RunnerLocationUtils.DetermineRunnerLocation(log, podInfo, cluster); - } } } diff --git a/Framework/KubernetesWorkflow/RunnerLocationUtils.cs b/Framework/KubernetesWorkflow/RunnerLocationUtils.cs index ca61c4c..75676ab 100644 --- a/Framework/KubernetesWorkflow/RunnerLocationUtils.cs +++ b/Framework/KubernetesWorkflow/RunnerLocationUtils.cs @@ -14,24 +14,30 @@ namespace KubernetesWorkflow { private static RunnerLocation location = RunnerLocation.Unknown; - internal static void DetermineRunnerLocation(ILog log, PodInfo info, K8sCluster cluster) - { - if (location != RunnerLocation.Unknown) return; - location = PingForLocation(info, cluster); - log.Log("Runner location set to: " + location); - } - - internal static bool IsKnown() - { - return location != RunnerLocation.Unknown; - } - internal static RunnerLocation GetRunnerLocation() { + DetermineRunnerLocation(); if (location == RunnerLocation.Unknown) throw new Exception("Runner location is unknown."); return location; } + private static void DetermineRunnerLocation()//ILog log, PodInfo info, K8sCluster cluster) + { + if (location != RunnerLocation.Unknown) return; + + var port = Environment.GetEnvironmentVariable("KUBERNETES_PORT"); + var host = Environment.GetEnvironmentVariable("KUBERNETES_SERVICE_HOST"); + + if (string.IsNullOrEmpty(port) || string.IsNullOrEmpty(host)) + { + location = RunnerLocation.ExternalToCluster; + } + else + { + location = RunnerLocation.InternalToCluster; + } + } + private static RunnerLocation PingForLocation(PodInfo podInfo, K8sCluster cluster) { if (PingHost(podInfo.Ip))