Cleanup and make kubernetesworkflow configurable

This commit is contained in:
benbierens 2023-04-12 15:22:09 +02:00
parent 2bcf512737
commit 7c8a278cd9
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
4 changed files with 61 additions and 96 deletions

View File

@ -0,0 +1,32 @@
namespace KubernetesWorkflow
{
public class Configuration
{
public Configuration(string k8sNamespace, string? kubeConfigFile, TimeSpan operationTimeout, TimeSpan retryDelay, ConfigurationLocationEntry[] locationMap)
{
K8sNamespace = k8sNamespace;
KubeConfigFile = kubeConfigFile;
OperationTimeout = operationTimeout;
RetryDelay = retryDelay;
LocationMap = locationMap;
}
public string K8sNamespace { get; }
public string? KubeConfigFile { get; }
public TimeSpan OperationTimeout { get; }
public TimeSpan RetryDelay { get; }
public ConfigurationLocationEntry[] LocationMap { get; }
}
public class ConfigurationLocationEntry
{
public ConfigurationLocationEntry(Location location, string workerName)
{
Location = location;
WorkerName = workerName;
}
public Location Location { get; }
public string WorkerName { get; }
}
}

View File

@ -4,21 +4,28 @@ namespace KubernetesWorkflow
{
public class K8sCluster
{
public const string K8sNamespace = "codex-test-namespace";
private const string KubeConfigFile = "C:\\kube\\config";
private readonly Dictionary<Location, string> K8sNodeLocationMap = new Dictionary<Location, string>
{
{ Location.BensLaptop, "worker01" },
{ Location.BensOldGamingMachine, "worker02" },
};
private KubernetesClientConfiguration? config;
public K8sCluster(Configuration configuration)
{
Configuration = configuration;
}
public Configuration Configuration { get; }
public KubernetesClientConfiguration GetK8sClientConfig()
{
if (config != null) return config;
//config = KubernetesClientConfiguration.BuildConfigFromConfigFile(KubeConfigFile);
config = KubernetesClientConfiguration.BuildDefaultConfig();
if (Configuration.KubeConfigFile != null)
{
config = KubernetesClientConfiguration.BuildConfigFromConfigFile(Configuration.KubeConfigFile);
}
else
{
config = KubernetesClientConfiguration.BuildDefaultConfig();
}
return config;
}
@ -34,18 +41,17 @@ namespace KubernetesWorkflow
public string GetNodeLabelForLocation(Location location)
{
if (location == Location.Unspecified) return string.Empty;
return K8sNodeLocationMap[location];
return Configuration.LocationMap.Single(l => l.Location == location).WorkerName;
}
// make configurable from test env!
public TimeSpan K8sOperationTimeout()
{
return TimeSpan.FromMinutes(5);
return Configuration.OperationTimeout;
}
public TimeSpan WaitForK8sServiceDelay()
{
return TimeSpan.FromSeconds(5);
return Configuration.RetryDelay;
}
}
}

View File

@ -71,7 +71,7 @@ namespace KubernetesWorkflow
private string K8sNamespace
{
get { return K8sCluster.K8sNamespace; }
get { return cluster.Configuration.K8sNamespace; }
}
private bool IsTestNamespaceOnline()
@ -135,7 +135,7 @@ namespace KubernetesWorkflow
return new V1ObjectMeta
{
Name = "deploy-" + workflowNumberSource.WorkflowNumber,
NamespaceProperty = K8sCluster.K8sNamespace
NamespaceProperty = K8sNamespace
};
}
@ -190,29 +190,6 @@ namespace KubernetesWorkflow
return $"P{workflowNumberSource.WorkflowNumber}-{recipe.Number}-{port.Number}";
}
//private void DeleteDeployment(CodexNodeGroup group)
//{
// if (group.Deployment == null) return;
// client.DeleteNamespacedDeployment(group.Deployment.Name(), K8sNamespace);
// group.Deployment = null;
//}
//private void CreatePrometheusDeployment(K8sPrometheusSpecs spec)
//{
// client.CreateNamespacedDeployment(spec.CreatePrometheusDeployment(), K8sNamespace);
//}
//private void CreateGethBootstrapDeployment(K8sGethBoostrapSpecs spec)
//{
// client.CreateNamespacedDeployment(spec.CreateGethBootstrapDeployment(), K8sNamespace);
//}
//private void CreateGethCompanionDeployment(GethBootstrapInfo info, GethCompanionGroup group)
//{
// client.CreateNamespacedDeployment(info.Spec.CreateGethCompanionDeployment(group, info), K8sNamespace);
//}
#endregion
#region Service management
@ -251,7 +228,7 @@ namespace KubernetesWorkflow
return new V1ObjectMeta
{
Name = "deploy-" + workflowNumberSource.WorkflowNumber,
NamespaceProperty = K8sCluster.K8sNamespace
NamespaceProperty = K8sNamespace
};
}
@ -288,50 +265,10 @@ namespace KubernetesWorkflow
return result;
}
//private void DeleteService(CodexNodeGroup online)
//{
// if (online.Service == null) return;
// client.DeleteNamespacedService(online.Service.Name(), K8sNamespace);
// online.Service = null;
//}
//private void CreatePrometheusService(K8sPrometheusSpecs spec)
//{
// client.CreateNamespacedService(spec.CreatePrometheusService(), K8sNamespace);
//}
//private void CreateGethBootstrapService(K8sGethBoostrapSpecs spec)
//{
// client.CreateNamespacedService(spec.CreateGethBootstrapService(), K8sNamespace);
//}
#endregion
#region Waiting
//private void WaitUntilOnline(CodexNodeGroup online)
//{
// WaitUntil(() =>
// {
// online.Deployment = client.ReadNamespacedDeployment(online.Deployment.Name(), K8sNamespace);
// return online.Deployment?.Status.AvailableReplicas != null && online.Deployment.Status.AvailableReplicas > 0;
// });
//}
//private void WaitUntilOffline(string deploymentName)
//{
// WaitUntil(() =>
// {
// var deployment = client.ReadNamespacedDeployment(deploymentName, K8sNamespace);
// return deployment == null || deployment.Status.AvailableReplicas == 0;
// });
//}
//private void WaitUntilZeroPods()
//{
// WaitUntil(() => !client.ListNamespacedPod(K8sNamespace).Items.Any());
//}
private void WaitUntilNamespaceCreated()
{
WaitUntil(() => IsTestNamespaceOnline());
@ -342,21 +279,6 @@ namespace KubernetesWorkflow
WaitUntil(() => !IsTestNamespaceOnline());
}
//private void WaitUntilPrometheusOnline(K8sPrometheusSpecs spec)
//{
// WaitUntilDeploymentOnline(spec.GetDeploymentName());
//}
//private void WaitUntilGethBootstrapOnline(K8sGethBoostrapSpecs spec)
//{
// WaitUntilDeploymentOnline(spec.GetBootstrapDeploymentName());
//}
//private void WaitUntilGethCompanionGroupOnline(K8sGethBoostrapSpecs spec, GethCompanionGroup group)
//{
// WaitUntilDeploymentOnline(spec.GetCompanionDeploymentName(group));
//}
private void WaitUntilDeploymentCreated(V1Deployment deploymentSpec)
{
WaitUntilDeploymentOnline(deploymentSpec.Metadata.Name);

View File

@ -6,8 +6,13 @@ namespace KubernetesWorkflow
{
private readonly NumberSource numberSource = new NumberSource(0);
private readonly NumberSource servicePortNumberSource = new NumberSource(30001);
private readonly K8sCluster cluster = new K8sCluster();
private readonly KnownK8sPods knownPods = new KnownK8sPods();
private readonly K8sCluster cluster;
public WorkflowCreator(Configuration configuration)
{
cluster = new K8sCluster(configuration);
}
public StartupWorkflow CreateWorkflow()
{