2023-05-31 11:34:12 +00:00
|
|
|
|
using DistTestCore.Codex;
|
|
|
|
|
using KubernetesWorkflow;
|
2023-08-11 06:39:51 +00:00
|
|
|
|
using System.Net.NetworkInformation;
|
2023-06-21 06:28:40 +00:00
|
|
|
|
using Utils;
|
2023-04-12 14:06:04 +00:00
|
|
|
|
|
|
|
|
|
namespace DistTestCore
|
|
|
|
|
{
|
|
|
|
|
public class Configuration
|
|
|
|
|
{
|
2023-06-02 07:03:46 +00:00
|
|
|
|
private readonly string? kubeConfigFile;
|
|
|
|
|
private readonly string logPath;
|
|
|
|
|
private readonly bool logDebug;
|
|
|
|
|
private readonly string dataFilesPath;
|
|
|
|
|
private readonly CodexLogLevel codexLogLevel;
|
2023-08-10 08:58:18 +00:00
|
|
|
|
private readonly string k8sNamespacePrefix;
|
2023-08-11 07:09:03 +00:00
|
|
|
|
private RunnerLocation? runnerLocation = null;
|
2023-06-02 07:03:46 +00:00
|
|
|
|
|
|
|
|
|
public Configuration()
|
|
|
|
|
{
|
|
|
|
|
kubeConfigFile = GetNullableEnvVarOrDefault("KUBECONFIG", null);
|
|
|
|
|
logPath = GetEnvVarOrDefault("LOGPATH", "CodexTestLogs");
|
2023-07-17 06:54:07 +00:00
|
|
|
|
logDebug = GetEnvVarOrDefault("LOGDEBUG", "false").ToLowerInvariant() == "true";
|
2023-06-02 07:03:46 +00:00
|
|
|
|
dataFilesPath = GetEnvVarOrDefault("DATAFILEPATH", "TestDataFiles");
|
2023-06-22 08:17:12 +00:00
|
|
|
|
codexLogLevel = ParseEnum.Parse<CodexLogLevel>(GetEnvVarOrDefault("LOGLEVEL", nameof(CodexLogLevel.Trace)));
|
2023-08-10 08:58:18 +00:00
|
|
|
|
k8sNamespacePrefix = "ct-";
|
2023-06-22 08:17:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-11 06:39:51 +00:00
|
|
|
|
public Configuration(string? kubeConfigFile, string logPath, bool logDebug, string dataFilesPath, CodexLogLevel codexLogLevel, string k8sNamespacePrefix)
|
2023-06-22 08:17:12 +00:00
|
|
|
|
{
|
|
|
|
|
this.kubeConfigFile = kubeConfigFile;
|
|
|
|
|
this.logPath = logPath;
|
|
|
|
|
this.logDebug = logDebug;
|
|
|
|
|
this.dataFilesPath = dataFilesPath;
|
|
|
|
|
this.codexLogLevel = codexLogLevel;
|
2023-08-10 08:58:18 +00:00
|
|
|
|
this.k8sNamespacePrefix = k8sNamespacePrefix;
|
2023-06-02 07:03:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-04 06:55:20 +00:00
|
|
|
|
public KubernetesWorkflow.Configuration GetK8sConfiguration(ITimeSet timeSet)
|
2023-04-12 14:06:04 +00:00
|
|
|
|
{
|
|
|
|
|
return new KubernetesWorkflow.Configuration(
|
2023-08-10 08:58:18 +00:00
|
|
|
|
k8sNamespacePrefix: k8sNamespacePrefix,
|
2023-06-02 07:03:46 +00:00
|
|
|
|
kubeConfigFile: kubeConfigFile,
|
2023-05-04 06:55:20 +00:00
|
|
|
|
operationTimeout: timeSet.K8sOperationTimeout(),
|
2023-06-02 08:04:07 +00:00
|
|
|
|
retryDelay: timeSet.WaitForK8sServiceDelay()
|
2023-04-12 14:06:04 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Logging.LogConfig GetLogConfig()
|
|
|
|
|
{
|
2023-06-02 07:03:46 +00:00
|
|
|
|
return new Logging.LogConfig(logPath, debugEnabled: logDebug);
|
2023-04-12 14:06:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetFileManagerFolder()
|
|
|
|
|
{
|
2023-06-02 07:03:46 +00:00
|
|
|
|
return dataFilesPath;
|
2023-04-12 14:06:04 +00:00
|
|
|
|
}
|
2023-05-31 11:34:12 +00:00
|
|
|
|
|
|
|
|
|
public CodexLogLevel GetCodexLogLevel()
|
|
|
|
|
{
|
2023-06-02 07:03:46 +00:00
|
|
|
|
return codexLogLevel;
|
2023-05-31 11:34:12 +00:00
|
|
|
|
}
|
2023-06-01 07:35:18 +00:00
|
|
|
|
|
2023-06-21 06:28:40 +00:00
|
|
|
|
public Address GetAddress(RunningContainer container)
|
2023-06-01 07:35:18 +00:00
|
|
|
|
{
|
2023-08-11 06:39:51 +00:00
|
|
|
|
if (runnerLocation == null)
|
|
|
|
|
{
|
|
|
|
|
runnerLocation = RunnerLocationUtils.DetermineRunnerLocation(container);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (runnerLocation == RunnerLocation.InternalToCluster)
|
2023-06-01 07:35:18 +00:00
|
|
|
|
{
|
|
|
|
|
return container.ClusterInternalAddress;
|
|
|
|
|
}
|
|
|
|
|
return container.ClusterExternalAddress;
|
|
|
|
|
}
|
2023-06-02 07:03:46 +00:00
|
|
|
|
|
|
|
|
|
private static string GetEnvVarOrDefault(string varName, string defaultValue)
|
|
|
|
|
{
|
|
|
|
|
var v = Environment.GetEnvironmentVariable(varName);
|
|
|
|
|
if (v == null) return defaultValue;
|
|
|
|
|
return v;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string? GetNullableEnvVarOrDefault(string varName, string? defaultValue)
|
|
|
|
|
{
|
|
|
|
|
var v = Environment.GetEnvironmentVariable(varName);
|
|
|
|
|
if (v == null) return defaultValue;
|
|
|
|
|
return v;
|
|
|
|
|
}
|
2023-06-01 07:35:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-11 06:39:51 +00:00
|
|
|
|
public enum RunnerLocation
|
2023-06-01 07:35:18 +00:00
|
|
|
|
{
|
|
|
|
|
ExternalToCluster,
|
|
|
|
|
InternalToCluster,
|
2023-04-12 14:06:04 +00:00
|
|
|
|
}
|
2023-08-11 06:39:51 +00:00
|
|
|
|
|
2023-08-11 07:09:03 +00:00
|
|
|
|
public static class RunnerLocationUtils
|
2023-08-11 06:39:51 +00:00
|
|
|
|
{
|
|
|
|
|
private static bool alreadyDidThat = false;
|
|
|
|
|
|
|
|
|
|
public static RunnerLocation DetermineRunnerLocation(RunningContainer container)
|
|
|
|
|
{
|
|
|
|
|
// We want to be sure we don't ping more often than strictly necessary.
|
|
|
|
|
// If we have already determined the location during this application
|
|
|
|
|
// lifetime, don't do it again.
|
|
|
|
|
if (alreadyDidThat) throw new Exception("We already did that.");
|
|
|
|
|
alreadyDidThat = true;
|
|
|
|
|
|
2023-08-11 07:09:03 +00:00
|
|
|
|
if (PingHost(container.Pod.PodInfo.Ip))
|
2023-08-11 06:39:51 +00:00
|
|
|
|
{
|
|
|
|
|
return RunnerLocation.InternalToCluster;
|
|
|
|
|
}
|
2023-08-11 07:09:03 +00:00
|
|
|
|
if (PingHost(Format(container.ClusterExternalAddress)))
|
2023-08-11 06:39:51 +00:00
|
|
|
|
{
|
|
|
|
|
return RunnerLocation.ExternalToCluster;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new Exception("Unable to determine runner location.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string Format(Address host)
|
|
|
|
|
{
|
|
|
|
|
return host.Host
|
|
|
|
|
.Replace("http://", "")
|
|
|
|
|
.Replace("https://", "");
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-11 07:09:03 +00:00
|
|
|
|
private static bool PingHost(string host)
|
2023-08-11 06:39:51 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var pinger = new Ping();
|
2023-08-11 07:09:03 +00:00
|
|
|
|
PingReply reply = pinger.Send(host);
|
2023-08-11 06:39:51 +00:00
|
|
|
|
return reply.Status == IPStatus.Success;
|
|
|
|
|
}
|
|
|
|
|
catch (PingException)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-12 14:06:04 +00:00
|
|
|
|
}
|