diff --git a/KubernetesWorkflow/Configuration.cs b/KubernetesWorkflow/Configuration.cs new file mode 100644 index 0000000..b5a4779 --- /dev/null +++ b/KubernetesWorkflow/Configuration.cs @@ -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; } + } +} diff --git a/KubernetesWorkflow/K8sCluster.cs b/KubernetesWorkflow/K8sCluster.cs index 7d6e8eb..c823a09 100644 --- a/KubernetesWorkflow/K8sCluster.cs +++ b/KubernetesWorkflow/K8sCluster.cs @@ -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 K8sNodeLocationMap = new Dictionary - { - { 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; } } } diff --git a/KubernetesWorkflow/K8sController.cs b/KubernetesWorkflow/K8sController.cs index 4049607..df68f18 100644 --- a/KubernetesWorkflow/K8sController.cs +++ b/KubernetesWorkflow/K8sController.cs @@ -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); diff --git a/KubernetesWorkflow/WorkflowCreator.cs b/KubernetesWorkflow/WorkflowCreator.cs index 450bdd6..88af0e8 100644 --- a/KubernetesWorkflow/WorkflowCreator.cs +++ b/KubernetesWorkflow/WorkflowCreator.cs @@ -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() {