Dynamically allocates Locations-enum to available k8s nodes in cluster.
This commit is contained in:
parent
ad71cff465
commit
cda63ba245
|
@ -11,7 +11,7 @@ namespace KubernetesWorkflow
|
|||
|
||||
public Configuration Configuration { get; }
|
||||
public string HostAddress { get; private set; } = string.Empty;
|
||||
public string[] AvailableK8sNodes { get; set; } = new string[0];
|
||||
public K8sNodeLabel[] AvailableK8sNodes { get; set; } = new K8sNodeLabel[0];
|
||||
|
||||
public KubernetesClientConfiguration GetK8sClientConfig()
|
||||
{
|
||||
|
@ -20,7 +20,7 @@ namespace KubernetesWorkflow
|
|||
return config;
|
||||
}
|
||||
|
||||
public string GetNodeLabelForLocation(Location location)
|
||||
public K8sNodeLabel? GetNodeLabelForLocation(Location location)
|
||||
{
|
||||
switch (location)
|
||||
{
|
||||
|
@ -31,7 +31,7 @@ namespace KubernetesWorkflow
|
|||
case Location.Three:
|
||||
return K8sNodeIfAvailable(2);
|
||||
}
|
||||
return string.Empty;
|
||||
return null;
|
||||
}
|
||||
|
||||
public TimeSpan K8sOperationTimeout()
|
||||
|
@ -69,10 +69,22 @@ namespace KubernetesWorkflow
|
|||
}
|
||||
}
|
||||
|
||||
private string K8sNodeIfAvailable(int index)
|
||||
private K8sNodeLabel? K8sNodeIfAvailable(int index)
|
||||
{
|
||||
if (AvailableK8sNodes.Length <= index) return string.Empty;
|
||||
if (AvailableK8sNodes.Length <= index) return null;
|
||||
return AvailableK8sNodes[index];
|
||||
}
|
||||
}
|
||||
|
||||
public class K8sNodeLabel
|
||||
{
|
||||
public K8sNodeLabel(string key, string value)
|
||||
{
|
||||
Key = key;
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public string Key { get; }
|
||||
public string Value { get; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -116,15 +116,29 @@ namespace KubernetesWorkflow
|
|||
cluster.AvailableK8sNodes = GetAvailableK8sNodes();
|
||||
if (cluster.AvailableK8sNodes.Length < 3)
|
||||
{
|
||||
log.Debug($"Warning: For full location support, at least 3 Kubernetes Nodes are required in the cluster. Nodes found: '{string.Join(",", cluster.AvailableK8sNodes)}'.");
|
||||
log.Debug($"Warning: For full location support, at least 3 Kubernetes Nodes are required in the cluster. Nodes found: '{string.Join(",", cluster.AvailableK8sNodes.Select(p => $"{p.Key}={p.Value}"))}'.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private string[] GetAvailableK8sNodes()
|
||||
private K8sNodeLabel[] GetAvailableK8sNodes()
|
||||
{
|
||||
var nodes = client.Run(c => c.ListNode());
|
||||
return nodes.Items.Select(i => i.Metadata.Name).ToArray();
|
||||
|
||||
var optionals = nodes.Items.Select(i => CreateNodeLabel(i));
|
||||
return optionals.Where(n => n != null).Select(n => n!).ToArray();
|
||||
}
|
||||
|
||||
private K8sNodeLabel? CreateNodeLabel(V1Node i)
|
||||
{
|
||||
var keys = i.Metadata.Labels.Keys;
|
||||
var hostnameKey = keys.SingleOrDefault(k => k.ToLowerInvariant().Contains("hostname"));
|
||||
if (hostnameKey != null)
|
||||
{
|
||||
var hostnameValue = i.Metadata.Labels[hostnameKey];
|
||||
return new K8sNodeLabel(hostnameKey, hostnameValue);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
@ -337,11 +351,12 @@ namespace KubernetesWorkflow
|
|||
|
||||
private IDictionary<string, string> CreateNodeSelector(Location location)
|
||||
{
|
||||
if (location == Location.Unspecified) return new Dictionary<string, string>();
|
||||
var nodeLabel = cluster.GetNodeLabelForLocation(location);
|
||||
if (nodeLabel == null) return new Dictionary<string, string>();
|
||||
|
||||
return new Dictionary<string, string>
|
||||
{
|
||||
{ "codex-test-location", cluster.GetNodeLabelForLocation(location) }
|
||||
{ nodeLabel.Key, nodeLabel.Value }
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue