2023-04-12 13:11:36 +00:00
|
|
|
|
using k8s;
|
|
|
|
|
using k8s.Models;
|
2023-11-12 09:07:23 +00:00
|
|
|
|
using KubernetesWorkflow.Recipe;
|
|
|
|
|
using KubernetesWorkflow.Types;
|
2023-04-25 09:31:15 +00:00
|
|
|
|
using Logging;
|
2023-04-17 14:28:07 +00:00
|
|
|
|
using Utils;
|
2023-04-12 13:11:36 +00:00
|
|
|
|
|
|
|
|
|
namespace KubernetesWorkflow
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
|
|
|
|
public class K8sController
|
|
|
|
|
{
|
2023-09-12 08:31:55 +00:00
|
|
|
|
private readonly ILog log;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
private readonly K8sCluster cluster;
|
2023-04-12 13:11:36 +00:00
|
|
|
|
private readonly WorkflowNumberSource workflowNumberSource;
|
2023-05-04 06:25:48 +00:00
|
|
|
|
private readonly K8sClient client;
|
2023-11-06 13:33:47 +00:00
|
|
|
|
public const string PodLabelKey = "pod-uuid";
|
2023-04-12 11:53:55 +00:00
|
|
|
|
|
2023-10-31 13:48:16 +00:00
|
|
|
|
public K8sController(ILog log, K8sCluster cluster, WorkflowNumberSource workflowNumberSource, string k8sNamespace)
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-04-25 09:31:15 +00:00
|
|
|
|
this.log = log;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
this.cluster = cluster;
|
2023-04-12 13:11:36 +00:00
|
|
|
|
this.workflowNumberSource = workflowNumberSource;
|
2023-05-04 06:25:48 +00:00
|
|
|
|
client = new K8sClient(cluster.GetK8sClientConfig());
|
|
|
|
|
|
2023-09-12 08:31:55 +00:00
|
|
|
|
K8sNamespace = k8sNamespace;
|
2023-04-12 13:11:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
client.Dispose();
|
|
|
|
|
}
|
2023-09-25 06:47:19 +00:00
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
public StartResult BringOnline(ContainerRecipe[] containerRecipes, ILocation location)
|
2023-04-12 13:11:36 +00:00
|
|
|
|
{
|
2023-04-25 09:31:15 +00:00
|
|
|
|
log.Debug();
|
2023-10-04 07:26:11 +00:00
|
|
|
|
EnsureNamespace();
|
2023-04-12 13:11:36 +00:00
|
|
|
|
|
2023-10-31 13:48:16 +00:00
|
|
|
|
var podLabel = K8sNameUtils.Format(Guid.NewGuid().ToString());
|
2023-11-06 13:33:47 +00:00
|
|
|
|
var deployment = CreateDeployment(containerRecipes, location, podLabel);
|
|
|
|
|
var internalService = CreateInternalService(containerRecipes);
|
|
|
|
|
var externalService = CreateExternalService(containerRecipes);
|
2023-10-31 13:48:16 +00:00
|
|
|
|
|
2023-11-07 12:06:49 +00:00
|
|
|
|
return new StartResult(cluster, containerRecipes, deployment, internalService, externalService);
|
2023-04-13 09:07:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-09 07:30:45 +00:00
|
|
|
|
public void WaitUntilOnline(RunningContainer container)
|
|
|
|
|
{
|
|
|
|
|
WaitUntilDeploymentOnline(container.Recipe.Name);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
public PodInfo GetPodInfo(RunningDeployment deployment)
|
2023-10-31 14:04:59 +00:00
|
|
|
|
{
|
2023-11-06 13:33:47 +00:00
|
|
|
|
var pod = GetPodForDeployment(deployment);
|
|
|
|
|
return CreatePodInfo(pod);
|
2023-10-31 14:04:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-13 09:57:26 +00:00
|
|
|
|
public void Stop(StartResult startResult, bool waitTillStopped)
|
2023-04-13 09:07:36 +00:00
|
|
|
|
{
|
2023-04-25 09:31:15 +00:00
|
|
|
|
log.Debug();
|
2023-11-06 13:33:47 +00:00
|
|
|
|
if (startResult.InternalService != null) DeleteService(startResult.InternalService);
|
|
|
|
|
if (startResult.ExternalService != null) DeleteService(startResult.ExternalService);
|
|
|
|
|
DeleteDeployment(startResult.Deployment);
|
2024-03-13 09:57:26 +00:00
|
|
|
|
|
|
|
|
|
if (waitTillStopped) WaitUntilPodsForDeploymentAreOffline(startResult.Deployment);
|
2023-04-12 13:11:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
public void DownloadPodLog(RunningContainer container, ILogHandler logHandler, int? tailLines)
|
2023-04-13 09:30:19 +00:00
|
|
|
|
{
|
2023-04-25 09:31:15 +00:00
|
|
|
|
log.Debug();
|
2023-11-06 13:33:47 +00:00
|
|
|
|
|
|
|
|
|
var podName = GetPodName(container);
|
|
|
|
|
var recipeName = container.Recipe.Name;
|
|
|
|
|
|
|
|
|
|
using var stream = client.Run(c => c.ReadNamespacedPodLog(podName, K8sNamespace, recipeName, tailLines: tailLines));
|
2023-04-13 09:30:19 +00:00
|
|
|
|
logHandler.Log(stream);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
public string ExecuteCommand(RunningContainer container, string command, params string[] args)
|
2023-04-14 07:54:07 +00:00
|
|
|
|
{
|
2023-11-06 15:10:19 +00:00
|
|
|
|
var containerName = container.Recipe.Name;
|
2023-06-01 13:56:26 +00:00
|
|
|
|
var cmdAndArgs = $"{containerName}: {command} ({string.Join(",", args)})";
|
|
|
|
|
log.Debug(cmdAndArgs);
|
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
var podName = GetPodName(container);
|
|
|
|
|
var runner = new CommandRunner(client, K8sNamespace, podName, containerName, command, args);
|
2023-04-14 07:54:07 +00:00
|
|
|
|
runner.Run();
|
2023-06-01 13:56:26 +00:00
|
|
|
|
var result = runner.GetStdOut();
|
|
|
|
|
|
|
|
|
|
log.Debug($"{cmdAndArgs} = '{result}'");
|
|
|
|
|
return result;
|
2023-04-14 07:54:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-14 09:49:14 +00:00
|
|
|
|
public int[] GetUsedExternalPorts()
|
|
|
|
|
{
|
|
|
|
|
return client.Run(c =>
|
|
|
|
|
{
|
|
|
|
|
var result = new List<int>();
|
|
|
|
|
|
|
|
|
|
var services = c.ListServiceForAllNamespaces();
|
|
|
|
|
var nodePorts = services.Items.Where(s => s.Spec.Type == "NodePort").ToArray();
|
|
|
|
|
if (!nodePorts.Any()) return result.ToArray();
|
|
|
|
|
|
|
|
|
|
foreach (var service in nodePorts)
|
|
|
|
|
{
|
|
|
|
|
foreach (var port in service.Spec.Ports)
|
|
|
|
|
{
|
|
|
|
|
if (port.NodePort.HasValue)
|
|
|
|
|
{
|
|
|
|
|
result.Add(port.NodePort.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result.ToArray();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-12 08:31:55 +00:00
|
|
|
|
public void DeleteAllNamespacesStartingWith(string prefix)
|
2023-04-12 13:11:36 +00:00
|
|
|
|
{
|
2023-04-25 09:31:15 +00:00
|
|
|
|
log.Debug();
|
2023-04-12 13:11:36 +00:00
|
|
|
|
|
2023-05-04 06:25:48 +00:00
|
|
|
|
var all = client.Run(c => c.ListNamespace().Items);
|
2023-09-12 08:31:55 +00:00
|
|
|
|
var namespaces = all.Select(n => n.Name()).Where(n => n.StartsWith(prefix));
|
2023-05-03 12:18:37 +00:00
|
|
|
|
|
|
|
|
|
foreach (var ns in namespaces)
|
|
|
|
|
{
|
|
|
|
|
DeleteNamespace(ns);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-12 08:31:55 +00:00
|
|
|
|
public void DeleteNamespace()
|
2023-05-03 12:18:37 +00:00
|
|
|
|
{
|
|
|
|
|
log.Debug();
|
2023-10-04 07:26:11 +00:00
|
|
|
|
if (IsNamespaceOnline(K8sNamespace))
|
2023-05-03 12:18:37 +00:00
|
|
|
|
{
|
2023-09-12 08:31:55 +00:00
|
|
|
|
client.Run(c => c.DeleteNamespace(K8sNamespace, null, null, gracePeriodSeconds: 0));
|
2023-05-03 12:18:37 +00:00
|
|
|
|
}
|
2023-04-12 13:11:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-03 12:18:37 +00:00
|
|
|
|
public void DeleteNamespace(string ns)
|
|
|
|
|
{
|
|
|
|
|
log.Debug();
|
|
|
|
|
if (IsNamespaceOnline(ns))
|
|
|
|
|
{
|
2023-05-04 06:25:48 +00:00
|
|
|
|
client.Run(c => c.DeleteNamespace(ns, null, null, gracePeriodSeconds: 0));
|
2023-05-03 12:18:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-02 08:04:07 +00:00
|
|
|
|
#region Discover K8s Nodes
|
|
|
|
|
|
2023-09-25 06:47:19 +00:00
|
|
|
|
public K8sNodeLabel[] GetAvailableK8sNodes()
|
2023-06-02 08:04:07 +00:00
|
|
|
|
{
|
|
|
|
|
var nodes = client.Run(c => c.ListNode());
|
2023-06-02 08:27:57 +00:00
|
|
|
|
|
|
|
|
|
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;
|
2023-06-02 08:04:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2023-04-12 13:11:36 +00:00
|
|
|
|
#region Namespace management
|
|
|
|
|
|
2023-09-12 08:31:55 +00:00
|
|
|
|
private string K8sNamespace { get; }
|
2023-05-04 06:25:48 +00:00
|
|
|
|
|
2023-10-04 07:26:11 +00:00
|
|
|
|
private void EnsureNamespace()
|
2023-04-12 13:11:36 +00:00
|
|
|
|
{
|
2023-10-04 07:26:11 +00:00
|
|
|
|
if (IsNamespaceOnline(K8sNamespace)) return;
|
2023-04-12 13:11:36 +00:00
|
|
|
|
|
|
|
|
|
var namespaceSpec = new V1Namespace
|
|
|
|
|
{
|
|
|
|
|
ApiVersion = "v1",
|
|
|
|
|
Metadata = new V1ObjectMeta
|
|
|
|
|
{
|
2023-09-12 08:31:55 +00:00
|
|
|
|
Name = K8sNamespace,
|
|
|
|
|
Labels = new Dictionary<string, string> { { "name", K8sNamespace } }
|
2023-04-12 13:11:36 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
2023-05-04 06:25:48 +00:00
|
|
|
|
client.Run(c => c.CreateNamespace(namespaceSpec));
|
2023-04-12 13:11:36 +00:00
|
|
|
|
WaitUntilNamespaceCreated();
|
2023-05-04 12:55:39 +00:00
|
|
|
|
|
|
|
|
|
CreatePolicy();
|
2023-04-12 13:11:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-03 12:18:37 +00:00
|
|
|
|
private bool IsNamespaceOnline(string name)
|
2023-04-12 13:11:36 +00:00
|
|
|
|
{
|
2023-05-04 06:25:48 +00:00
|
|
|
|
return client.Run(c => c.ListNamespace().Items.Any(n => n.Metadata.Name == name));
|
2023-04-12 13:11:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-04 12:55:39 +00:00
|
|
|
|
private void CreatePolicy()
|
|
|
|
|
{
|
|
|
|
|
client.Run(c =>
|
|
|
|
|
{
|
|
|
|
|
var body = new V1NetworkPolicy
|
|
|
|
|
{
|
|
|
|
|
Metadata = new V1ObjectMeta
|
|
|
|
|
{
|
|
|
|
|
Name = "isolate-policy",
|
2023-09-12 08:31:55 +00:00
|
|
|
|
NamespaceProperty = K8sNamespace
|
2023-05-04 12:55:39 +00:00
|
|
|
|
},
|
|
|
|
|
Spec = new V1NetworkPolicySpec
|
|
|
|
|
{
|
2023-05-30 19:41:34 +00:00
|
|
|
|
PodSelector = new V1LabelSelector {},
|
2023-05-04 12:55:39 +00:00
|
|
|
|
PolicyTypes = new[]
|
|
|
|
|
{
|
|
|
|
|
"Ingress",
|
|
|
|
|
"Egress"
|
|
|
|
|
},
|
|
|
|
|
Ingress = new List<V1NetworkPolicyIngressRule>
|
|
|
|
|
{
|
|
|
|
|
new V1NetworkPolicyIngressRule
|
|
|
|
|
{
|
|
|
|
|
FromProperty = new List<V1NetworkPolicyPeer>
|
|
|
|
|
{
|
|
|
|
|
new V1NetworkPolicyPeer
|
|
|
|
|
{
|
2023-05-30 19:41:34 +00:00
|
|
|
|
PodSelector = new V1LabelSelector {}
|
2023-05-04 12:55:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-31 15:36:40 +00:00
|
|
|
|
},
|
|
|
|
|
new V1NetworkPolicyIngressRule
|
|
|
|
|
{
|
|
|
|
|
FromProperty = new List<V1NetworkPolicyPeer>
|
|
|
|
|
{
|
|
|
|
|
new V1NetworkPolicyPeer
|
|
|
|
|
{
|
|
|
|
|
NamespaceSelector = new V1LabelSelector
|
|
|
|
|
{
|
|
|
|
|
MatchLabels = GetRunnerNamespaceSelector()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-04 12:58:43 +00:00
|
|
|
|
},
|
|
|
|
|
new V1NetworkPolicyIngressRule
|
|
|
|
|
{
|
|
|
|
|
FromProperty = new List<V1NetworkPolicyPeer>
|
|
|
|
|
{
|
|
|
|
|
new V1NetworkPolicyPeer
|
|
|
|
|
{
|
|
|
|
|
NamespaceSelector = new V1LabelSelector
|
|
|
|
|
{
|
|
|
|
|
MatchLabels = GetPrometheusNamespaceSelector()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-04 12:55:39 +00:00
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Egress = new List<V1NetworkPolicyEgressRule>
|
|
|
|
|
{
|
2023-05-30 19:41:34 +00:00
|
|
|
|
new V1NetworkPolicyEgressRule
|
|
|
|
|
{
|
|
|
|
|
To = new List<V1NetworkPolicyPeer>
|
|
|
|
|
{
|
|
|
|
|
new V1NetworkPolicyPeer
|
|
|
|
|
{
|
|
|
|
|
PodSelector = new V1LabelSelector {}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
2023-05-04 12:55:39 +00:00
|
|
|
|
new V1NetworkPolicyEgressRule
|
|
|
|
|
{
|
|
|
|
|
To = new List<V1NetworkPolicyPeer>
|
|
|
|
|
{
|
|
|
|
|
new V1NetworkPolicyPeer
|
|
|
|
|
{
|
|
|
|
|
NamespaceSelector = new V1LabelSelector
|
|
|
|
|
{
|
2023-05-30 19:41:34 +00:00
|
|
|
|
MatchLabels = new Dictionary<string, string> { { "kubernetes.io/metadata.name", "kube-system" } }
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
new V1NetworkPolicyPeer
|
|
|
|
|
{
|
|
|
|
|
PodSelector = new V1LabelSelector
|
|
|
|
|
{
|
|
|
|
|
MatchLabels = new Dictionary<string, string> { { "k8s-app", "kube-dns" } }
|
2023-05-04 12:55:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-30 19:41:34 +00:00
|
|
|
|
},
|
|
|
|
|
Ports = new List<V1NetworkPolicyPort>
|
|
|
|
|
{
|
|
|
|
|
new V1NetworkPolicyPort
|
|
|
|
|
{
|
|
|
|
|
Port = new IntstrIntOrString
|
|
|
|
|
{
|
|
|
|
|
Value = "53"
|
|
|
|
|
},
|
|
|
|
|
Protocol = "UDP"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
new V1NetworkPolicyEgressRule
|
|
|
|
|
{
|
|
|
|
|
To = new List<V1NetworkPolicyPeer>
|
|
|
|
|
{
|
|
|
|
|
new V1NetworkPolicyPeer
|
|
|
|
|
{
|
|
|
|
|
IpBlock = new V1IPBlock
|
|
|
|
|
{
|
2023-09-04 07:08:34 +00:00
|
|
|
|
Cidr = "0.0.0.0/0"
|
2023-05-30 19:41:34 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
Ports = new List<V1NetworkPolicyPort>
|
|
|
|
|
{
|
|
|
|
|
new V1NetworkPolicyPort
|
|
|
|
|
{
|
|
|
|
|
Port = new IntstrIntOrString
|
|
|
|
|
{
|
|
|
|
|
Value = "80"
|
|
|
|
|
},
|
|
|
|
|
Protocol = "TCP"
|
|
|
|
|
},
|
|
|
|
|
new V1NetworkPolicyPort
|
|
|
|
|
{
|
|
|
|
|
Port = new IntstrIntOrString
|
|
|
|
|
{
|
|
|
|
|
Value = "443"
|
|
|
|
|
},
|
|
|
|
|
Protocol = "TCP"
|
|
|
|
|
}
|
2023-05-04 12:55:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-30 19:41:34 +00:00
|
|
|
|
|
2023-05-04 12:55:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-09-12 08:31:55 +00:00
|
|
|
|
c.CreateNamespacedNetworkPolicy(body, K8sNamespace);
|
2023-05-04 12:55:39 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 13:11:36 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Deployment management
|
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
private RunningDeployment CreateDeployment(ContainerRecipe[] containerRecipes, ILocation location, string podLabel)
|
2023-04-12 13:11:36 +00:00
|
|
|
|
{
|
|
|
|
|
var deploymentSpec = new V1Deployment
|
|
|
|
|
{
|
|
|
|
|
ApiVersion = "apps/v1",
|
2023-09-04 07:08:34 +00:00
|
|
|
|
Metadata = CreateDeploymentMetadata(containerRecipes),
|
2023-04-12 13:11:36 +00:00
|
|
|
|
Spec = new V1DeploymentSpec
|
|
|
|
|
{
|
|
|
|
|
Replicas = 1,
|
|
|
|
|
Selector = new V1LabelSelector
|
|
|
|
|
{
|
2023-09-04 07:08:34 +00:00
|
|
|
|
MatchLabels = GetSelector(containerRecipes)
|
2023-04-12 13:11:36 +00:00
|
|
|
|
},
|
|
|
|
|
Template = new V1PodTemplateSpec
|
|
|
|
|
{
|
|
|
|
|
Metadata = new V1ObjectMeta
|
|
|
|
|
{
|
2023-10-31 13:48:16 +00:00
|
|
|
|
Labels = GetSelector(containerRecipes, podLabel),
|
2023-09-04 07:08:34 +00:00
|
|
|
|
Annotations = GetAnnotations(containerRecipes)
|
2023-04-12 13:11:36 +00:00
|
|
|
|
},
|
|
|
|
|
Spec = new V1PodSpec
|
|
|
|
|
{
|
2023-11-23 13:50:54 +00:00
|
|
|
|
PriorityClassName = GetPriorityClassName(containerRecipes),
|
2023-11-14 09:16:00 +00:00
|
|
|
|
Affinity = CreatePodAffinity(containerRecipes),
|
2023-04-12 13:11:36 +00:00
|
|
|
|
NodeSelector = CreateNodeSelector(location),
|
2023-09-07 08:37:52 +00:00
|
|
|
|
Containers = CreateDeploymentContainers(containerRecipes),
|
|
|
|
|
Volumes = CreateVolumes(containerRecipes)
|
2023-04-12 13:11:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-09-12 08:31:55 +00:00
|
|
|
|
client.Run(c => c.CreateNamespacedDeployment(deploymentSpec, K8sNamespace));
|
2023-04-13 09:07:36 +00:00
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
var name = deploymentSpec.Metadata.Name;
|
|
|
|
|
return new RunningDeployment(name, podLabel);
|
2023-04-13 09:07:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
private void DeleteDeployment(RunningDeployment deployment)
|
2023-04-13 09:07:36 +00:00
|
|
|
|
{
|
2023-11-06 13:33:47 +00:00
|
|
|
|
client.Run(c => c.DeleteNamespacedDeployment(deployment.Name, K8sNamespace));
|
|
|
|
|
WaitUntilDeploymentOffline(deployment.Name);
|
2023-04-12 13:11:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-25 06:47:19 +00:00
|
|
|
|
private IDictionary<string, string> CreateNodeSelector(ILocation location)
|
2023-04-12 13:11:36 +00:00
|
|
|
|
{
|
2023-09-25 06:47:19 +00:00
|
|
|
|
var nodeLabel = GetNodeLabelForLocation(location);
|
2023-06-02 08:27:57 +00:00
|
|
|
|
if (nodeLabel == null) return new Dictionary<string, string>();
|
2023-04-12 13:11:36 +00:00
|
|
|
|
|
|
|
|
|
return new Dictionary<string, string>
|
|
|
|
|
{
|
2023-06-02 08:27:57 +00:00
|
|
|
|
{ nodeLabel.Key, nodeLabel.Value }
|
2023-04-12 13:11:36 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-14 09:16:00 +00:00
|
|
|
|
private V1Affinity? CreatePodAffinity(ContainerRecipe[] recipes)
|
|
|
|
|
{
|
|
|
|
|
var notIns = recipes
|
|
|
|
|
.Select(r => r.SchedulingAffinity.NotIn)
|
|
|
|
|
.Where(n => !string.IsNullOrEmpty(n))
|
|
|
|
|
.Distinct()
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
if (!notIns.Any()) return null;
|
|
|
|
|
|
|
|
|
|
return new V1Affinity
|
|
|
|
|
{
|
|
|
|
|
NodeAffinity = new V1NodeAffinity
|
|
|
|
|
{
|
|
|
|
|
RequiredDuringSchedulingIgnoredDuringExecution = new V1NodeSelector
|
|
|
|
|
{
|
|
|
|
|
NodeSelectorTerms = new List<V1NodeSelectorTerm>
|
|
|
|
|
{
|
|
|
|
|
new V1NodeSelectorTerm
|
|
|
|
|
{
|
|
|
|
|
MatchExpressions = new List<V1NodeSelectorRequirement>
|
|
|
|
|
{
|
|
|
|
|
new V1NodeSelectorRequirement
|
|
|
|
|
{
|
2023-11-27 17:06:04 +00:00
|
|
|
|
Key = "allow-tests-pods",
|
2023-11-14 09:16:00 +00:00
|
|
|
|
OperatorProperty = "NotIn",
|
|
|
|
|
Values = notIns
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-25 06:47:19 +00:00
|
|
|
|
private K8sNodeLabel? GetNodeLabelForLocation(ILocation location)
|
|
|
|
|
{
|
|
|
|
|
var l = (Location)location;
|
|
|
|
|
return l.NodeLabel;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-23 13:50:54 +00:00
|
|
|
|
private string GetPriorityClassName(ContainerRecipe[] containerRecipes)
|
|
|
|
|
{
|
|
|
|
|
if (containerRecipes.Any(c => c.SetCriticalPriority))
|
|
|
|
|
{
|
|
|
|
|
return "system-node-critical";
|
|
|
|
|
}
|
|
|
|
|
return null!;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-04 07:08:34 +00:00
|
|
|
|
private IDictionary<string, string> GetSelector(ContainerRecipe[] containerRecipes)
|
2023-04-12 13:11:36 +00:00
|
|
|
|
{
|
2023-09-04 07:08:34 +00:00
|
|
|
|
return containerRecipes.First().PodLabels.GetLabels();
|
2023-04-12 13:11:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-31 13:48:16 +00:00
|
|
|
|
private IDictionary<string, string> GetSelector(ContainerRecipe[] containerRecipes, string podLabel)
|
|
|
|
|
{
|
|
|
|
|
var labels = containerRecipes.First().PodLabels.Clone();
|
2023-11-06 13:33:47 +00:00
|
|
|
|
labels.Add(PodLabelKey, podLabel);
|
2023-10-31 13:48:16 +00:00
|
|
|
|
return labels.GetLabels();
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-31 15:36:40 +00:00
|
|
|
|
private IDictionary<string, string> GetRunnerNamespaceSelector()
|
|
|
|
|
{
|
|
|
|
|
return new Dictionary<string, string> { { "kubernetes.io/metadata.name", "default" } };
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-04 12:58:43 +00:00
|
|
|
|
private IDictionary<string, string> GetPrometheusNamespaceSelector()
|
|
|
|
|
{
|
|
|
|
|
return new Dictionary<string, string> { { "kubernetes.io/metadata.name", "monitoring" } };
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-04 07:08:34 +00:00
|
|
|
|
private IDictionary<string, string> GetAnnotations(ContainerRecipe[] containerRecipes)
|
|
|
|
|
{
|
|
|
|
|
return containerRecipes.First().PodAnnotations.GetAnnotations();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private V1ObjectMeta CreateDeploymentMetadata(ContainerRecipe[] containerRecipes)
|
2023-04-12 13:11:36 +00:00
|
|
|
|
{
|
|
|
|
|
return new V1ObjectMeta
|
|
|
|
|
{
|
2023-09-15 10:25:10 +00:00
|
|
|
|
Name = string.Join('-',containerRecipes.Select(r => r.Name)),
|
2023-09-12 08:31:55 +00:00
|
|
|
|
NamespaceProperty = K8sNamespace,
|
2023-09-04 07:08:34 +00:00
|
|
|
|
Labels = GetSelector(containerRecipes),
|
|
|
|
|
Annotations = GetAnnotations(containerRecipes)
|
2023-04-12 13:11:36 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<V1Container> CreateDeploymentContainers(ContainerRecipe[] containerRecipes)
|
|
|
|
|
{
|
2023-09-07 08:37:52 +00:00
|
|
|
|
return containerRecipes.Select(CreateDeploymentContainer).ToList();
|
2023-04-12 13:11:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private V1Container CreateDeploymentContainer(ContainerRecipe recipe)
|
|
|
|
|
{
|
|
|
|
|
return new V1Container
|
|
|
|
|
{
|
|
|
|
|
Name = recipe.Name,
|
|
|
|
|
Image = recipe.Image,
|
2023-06-06 14:10:30 +00:00
|
|
|
|
ImagePullPolicy = "Always",
|
2023-04-12 13:11:36 +00:00
|
|
|
|
Ports = CreateContainerPorts(recipe),
|
2023-09-07 08:37:52 +00:00
|
|
|
|
Env = CreateEnv(recipe),
|
2023-09-08 08:14:52 +00:00
|
|
|
|
VolumeMounts = CreateContainerVolumeMounts(recipe),
|
2024-03-13 09:01:14 +00:00
|
|
|
|
Resources = CreateResourceLimits(recipe),
|
|
|
|
|
Command = CreateCommandList(recipe)
|
2023-09-07 08:37:52 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-13 09:01:14 +00:00
|
|
|
|
private IList<string> CreateCommandList(ContainerRecipe recipe)
|
|
|
|
|
{
|
|
|
|
|
if (recipe.CommandOverride == null || !recipe.CommandOverride.Command.Any()) return null!;
|
|
|
|
|
return recipe.CommandOverride.Command.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-08 08:14:52 +00:00
|
|
|
|
private V1ResourceRequirements CreateResourceLimits(ContainerRecipe recipe)
|
|
|
|
|
{
|
|
|
|
|
return new V1ResourceRequirements
|
|
|
|
|
{
|
2023-09-08 11:47:49 +00:00
|
|
|
|
Requests = CreateResourceQuantities(recipe.Resources.Requests),
|
|
|
|
|
Limits = CreateResourceQuantities(recipe.Resources.Limits)
|
2023-09-08 08:14:52 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-08 11:47:49 +00:00
|
|
|
|
private Dictionary<string, ResourceQuantity> CreateResourceQuantities(ContainerResourceSet set)
|
2023-09-08 08:14:52 +00:00
|
|
|
|
{
|
|
|
|
|
var result = new Dictionary<string, ResourceQuantity>();
|
2023-09-08 11:47:49 +00:00
|
|
|
|
if (set.MilliCPUs != 0)
|
2023-09-08 08:14:52 +00:00
|
|
|
|
{
|
2023-09-08 11:47:49 +00:00
|
|
|
|
result.Add("cpu", new ResourceQuantity($"{set.MilliCPUs}m"));
|
2023-09-08 08:14:52 +00:00
|
|
|
|
}
|
2023-09-08 11:47:49 +00:00
|
|
|
|
if (set.Memory.SizeInBytes != 0)
|
2023-09-08 08:14:52 +00:00
|
|
|
|
{
|
2023-09-08 11:47:49 +00:00
|
|
|
|
result.Add("memory", new ResourceQuantity(set.Memory.ToSuffixNotation()));
|
2023-09-08 08:14:52 +00:00
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-07 08:37:52 +00:00
|
|
|
|
private List<V1VolumeMount> CreateContainerVolumeMounts(ContainerRecipe recipe)
|
|
|
|
|
{
|
|
|
|
|
return recipe.Volumes.Select(CreateContainerVolumeMount).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private V1VolumeMount CreateContainerVolumeMount(VolumeMount v)
|
|
|
|
|
{
|
|
|
|
|
return new V1VolumeMount
|
|
|
|
|
{
|
|
|
|
|
Name = v.VolumeName,
|
2023-10-31 10:38:54 +00:00
|
|
|
|
MountPath = v.MountPath,
|
|
|
|
|
SubPath = v.SubPath,
|
2023-09-07 08:37:52 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<V1Volume> CreateVolumes(ContainerRecipe[] containerRecipes)
|
|
|
|
|
{
|
|
|
|
|
return containerRecipes.Where(c => c.Volumes.Any()).SelectMany(CreateVolumes).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<V1Volume> CreateVolumes(ContainerRecipe recipe)
|
|
|
|
|
{
|
|
|
|
|
return recipe.Volumes.Select(CreateVolume).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private V1Volume CreateVolume(VolumeMount v)
|
|
|
|
|
{
|
2023-10-31 14:04:59 +00:00
|
|
|
|
CreatePersistentVolumeClaimIfNeeded(v);
|
2023-10-31 13:20:50 +00:00
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(v.HostPath))
|
|
|
|
|
{
|
|
|
|
|
return new V1Volume
|
|
|
|
|
{
|
|
|
|
|
Name = v.VolumeName,
|
|
|
|
|
HostPath = new V1HostPathVolumeSource
|
2023-09-07 08:37:52 +00:00
|
|
|
|
{
|
2023-10-31 13:20:50 +00:00
|
|
|
|
Path = v.HostPath
|
2023-09-07 08:37:52 +00:00
|
|
|
|
}
|
2023-10-31 13:20:50 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(v.Secret))
|
|
|
|
|
{
|
|
|
|
|
return new V1Volume
|
|
|
|
|
{
|
|
|
|
|
Name = v.VolumeName,
|
|
|
|
|
Secret = CreateVolumeSecret(v)
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-09-07 08:37:52 +00:00
|
|
|
|
|
|
|
|
|
return new V1Volume
|
|
|
|
|
{
|
|
|
|
|
Name = v.VolumeName,
|
|
|
|
|
PersistentVolumeClaim = new V1PersistentVolumeClaimVolumeSource
|
|
|
|
|
{
|
|
|
|
|
ClaimName = v.VolumeName
|
|
|
|
|
}
|
2023-04-12 13:11:36 +00:00
|
|
|
|
};
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-31 14:04:59 +00:00
|
|
|
|
private void CreatePersistentVolumeClaimIfNeeded(VolumeMount v)
|
|
|
|
|
{
|
|
|
|
|
var pvcs = client.Run(c => c.ListNamespacedPersistentVolumeClaim(K8sNamespace));
|
2023-11-02 10:32:24 +00:00
|
|
|
|
if (pvcs != null && pvcs.Items.Any(i => i.Name() == v.VolumeName)) return;
|
2023-10-31 14:04:59 +00:00
|
|
|
|
|
2023-09-07 08:37:52 +00:00
|
|
|
|
client.Run(c => c.CreateNamespacedPersistentVolumeClaim(new V1PersistentVolumeClaim
|
|
|
|
|
{
|
|
|
|
|
ApiVersion = "v1",
|
|
|
|
|
Metadata = new V1ObjectMeta
|
|
|
|
|
{
|
2023-10-31 14:04:59 +00:00
|
|
|
|
Name = v.VolumeName,
|
2023-09-07 08:37:52 +00:00
|
|
|
|
},
|
|
|
|
|
Spec = new V1PersistentVolumeClaimSpec
|
|
|
|
|
{
|
|
|
|
|
AccessModes = new List<string>
|
|
|
|
|
{
|
2023-09-07 08:55:35 +00:00
|
|
|
|
"ReadWriteOnce"
|
2023-09-07 08:37:52 +00:00
|
|
|
|
},
|
2023-10-31 14:04:59 +00:00
|
|
|
|
Resources = CreateVolumeResourceRequirements(v),
|
|
|
|
|
},
|
2023-09-12 08:31:55 +00:00
|
|
|
|
}, K8sNamespace));
|
2023-10-31 14:04:59 +00:00
|
|
|
|
}
|
2023-09-07 08:37:52 +00:00
|
|
|
|
|
2023-10-31 13:20:50 +00:00
|
|
|
|
private V1SecretVolumeSource CreateVolumeSecret(VolumeMount v)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(v.Secret)) return null!;
|
|
|
|
|
return new V1SecretVolumeSource
|
2023-09-07 08:37:52 +00:00
|
|
|
|
{
|
2023-10-31 13:20:50 +00:00
|
|
|
|
SecretName = v.Secret
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private V1ResourceRequirements CreateVolumeResourceRequirements(VolumeMount v)
|
|
|
|
|
{
|
|
|
|
|
if (v.ResourceQuantity == null) return null!;
|
|
|
|
|
return new V1ResourceRequirements
|
|
|
|
|
{
|
|
|
|
|
Requests = new Dictionary<string, ResourceQuantity>()
|
2023-09-07 08:37:52 +00:00
|
|
|
|
{
|
2023-10-31 13:20:50 +00:00
|
|
|
|
{"storage", new ResourceQuantity(v.ResourceQuantity) }
|
2023-09-07 08:37:52 +00:00
|
|
|
|
}
|
2023-04-12 13:11:36 +00:00
|
|
|
|
};
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 13:11:36 +00:00
|
|
|
|
private List<V1EnvVar> CreateEnv(ContainerRecipe recipe)
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-04-12 13:11:36 +00:00
|
|
|
|
return recipe.EnvVars.Select(CreateEnvVar).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private V1EnvVar CreateEnvVar(EnvVar envVar)
|
|
|
|
|
{
|
|
|
|
|
return new V1EnvVar
|
|
|
|
|
{
|
|
|
|
|
Name = envVar.Name,
|
|
|
|
|
Value = envVar.Value,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<V1ContainerPort> CreateContainerPorts(ContainerRecipe recipe)
|
|
|
|
|
{
|
2023-10-25 12:23:07 +00:00
|
|
|
|
var exposedPorts = recipe.ExposedPorts.SelectMany(p => CreateContainerPort(recipe, p));
|
|
|
|
|
var internalPorts = recipe.InternalPorts.SelectMany(p => CreateContainerPort(recipe, p));
|
2023-04-12 13:11:36 +00:00
|
|
|
|
return exposedPorts.Concat(internalPorts).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-25 12:23:07 +00:00
|
|
|
|
private List<V1ContainerPort> CreateContainerPort(ContainerRecipe recipe, Port port)
|
|
|
|
|
{
|
|
|
|
|
var result = new List<V1ContainerPort>();
|
|
|
|
|
if (port.IsTcp()) CreateTcpContainerPort(result, recipe, port);
|
|
|
|
|
if (port.IsUdp()) CreateUdpContainerPort(result, recipe, port);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CreateUdpContainerPort(List<V1ContainerPort> result, ContainerRecipe recipe, Port port)
|
|
|
|
|
{
|
|
|
|
|
result.Add(CreateContainerPort(recipe, port, "UDP"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CreateTcpContainerPort(List<V1ContainerPort> result, ContainerRecipe recipe, Port port)
|
|
|
|
|
{
|
|
|
|
|
result.Add(CreateContainerPort(recipe, port, "TCP"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private V1ContainerPort CreateContainerPort(ContainerRecipe recipe, Port port, string protocol)
|
2023-04-12 13:11:36 +00:00
|
|
|
|
{
|
|
|
|
|
return new V1ContainerPort
|
|
|
|
|
{
|
|
|
|
|
Name = GetNameForPort(recipe, port),
|
2023-10-25 12:23:07 +00:00
|
|
|
|
ContainerPort = port.Number,
|
|
|
|
|
Protocol = protocol
|
2023-04-12 13:11:36 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetNameForPort(ContainerRecipe recipe, Port port)
|
|
|
|
|
{
|
2023-11-06 13:33:47 +00:00
|
|
|
|
var inputs = new[]
|
|
|
|
|
{
|
|
|
|
|
$"p{workflowNumberSource.WorkflowNumber}",
|
|
|
|
|
recipe.Number.ToString(),
|
|
|
|
|
port.Number.ToString(),
|
|
|
|
|
port.Protocol.ToString().ToLowerInvariant()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return K8sNameUtils.FormatPortName(string.Join(",", inputs));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetPodName(RunningContainer container)
|
|
|
|
|
{
|
2024-04-13 14:09:17 +00:00
|
|
|
|
return GetPodForDeployment(container.RunningPod.StartResult.Deployment).Metadata.Name;
|
2023-11-06 13:33:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private V1Pod GetPodForDeployment(RunningDeployment deployment)
|
2023-11-10 14:23:16 +00:00
|
|
|
|
{
|
|
|
|
|
return Time.Retry(() => GetPodForDeplomentInternal(deployment),
|
2023-11-23 13:15:37 +00:00
|
|
|
|
// We will wait up to 1 minute, k8s might be moving pods around.
|
2024-05-02 06:41:20 +00:00
|
|
|
|
maxTimeout: TimeSpan.FromMinutes(1),
|
2023-11-10 14:23:16 +00:00
|
|
|
|
retryTime: TimeSpan.FromSeconds(10),
|
|
|
|
|
description: "Find pod by label for deployment.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private V1Pod GetPodForDeplomentInternal(RunningDeployment deployment)
|
2023-11-06 13:33:47 +00:00
|
|
|
|
{
|
|
|
|
|
var allPods = client.Run(c => c.ListNamespacedPod(K8sNamespace));
|
|
|
|
|
var pods = allPods.Items.Where(p => p.GetLabel(PodLabelKey) == deployment.PodLabel).ToArray();
|
|
|
|
|
|
2023-11-10 14:23:16 +00:00
|
|
|
|
if (pods.Length != 1)
|
|
|
|
|
{
|
|
|
|
|
var allLabels = allPods.Items.Select(p =>
|
|
|
|
|
{
|
|
|
|
|
var labels = string.Join(",", p.Labels().Select(l => $"{l.Key}={l.Value}"));
|
|
|
|
|
return $"pod:'{p.Name()}' has labels: [{labels}]";
|
|
|
|
|
});
|
|
|
|
|
throw new Exception($"Expected to find 1 pod by podLabel '{deployment.PodLabel}'. Found: {pods.Length}. " +
|
|
|
|
|
$"Total number of pods: {allPods.Items.Count}. Their labels: {string.Join(Environment.NewLine, allLabels)}");
|
|
|
|
|
}
|
2023-11-06 13:33:47 +00:00
|
|
|
|
return pods[0];
|
2023-04-12 13:11:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Service management
|
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
private RunningService? CreateInternalService(ContainerRecipe[] recipes)
|
2023-04-12 13:11:36 +00:00
|
|
|
|
{
|
2023-11-13 12:58:34 +00:00
|
|
|
|
return CreateService(recipes, r => r.InternalPorts.Concat(r.ExposedPorts).ToArray(), "ClusterIP", "int", false);
|
2023-11-06 13:33:47 +00:00
|
|
|
|
}
|
2023-04-12 13:11:36 +00:00
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
private RunningService? CreateExternalService(ContainerRecipe[] recipes)
|
|
|
|
|
{
|
2023-11-13 12:58:34 +00:00
|
|
|
|
return CreateService(recipes, r => r.ExposedPorts, "NodePort", "ext", true);
|
2023-11-06 13:33:47 +00:00
|
|
|
|
}
|
2023-04-12 13:11:36 +00:00
|
|
|
|
|
2023-11-13 12:58:34 +00:00
|
|
|
|
private RunningService? CreateService(ContainerRecipe[] recipes, Func<ContainerRecipe, Port[]> portSelector, string serviceType, string namePostfix, bool isNodePort)
|
2023-11-06 13:33:47 +00:00
|
|
|
|
{
|
2023-11-13 12:58:34 +00:00
|
|
|
|
var ports = CreateServicePorts(recipes, portSelector, isNodePort);
|
2023-11-06 13:33:47 +00:00
|
|
|
|
if (!ports.Any()) return null;
|
2023-04-12 13:11:36 +00:00
|
|
|
|
|
|
|
|
|
var serviceSpec = new V1Service
|
|
|
|
|
{
|
|
|
|
|
ApiVersion = "v1",
|
2023-11-06 13:33:47 +00:00
|
|
|
|
Metadata = CreateServiceMetadata(recipes, namePostfix),
|
2023-04-12 13:11:36 +00:00
|
|
|
|
Spec = new V1ServiceSpec
|
|
|
|
|
{
|
2023-11-06 13:33:47 +00:00
|
|
|
|
Type = serviceType,
|
|
|
|
|
Selector = GetSelector(recipes),
|
|
|
|
|
Ports = ports,
|
2023-04-12 13:11:36 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-09-12 08:31:55 +00:00
|
|
|
|
client.Run(c => c.CreateNamespacedService(serviceSpec, K8sNamespace));
|
2023-04-13 09:07:36 +00:00
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
var result = ReadBackServiceAndMapPorts(serviceSpec, recipes);
|
|
|
|
|
var name = serviceSpec.Metadata.Name;
|
2023-06-02 09:07:36 +00:00
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
return new RunningService(name, result);
|
2023-04-13 09:07:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
private List<ContainerRecipePortMapEntry> ReadBackServiceAndMapPorts(V1Service serviceSpec, ContainerRecipe[] containerRecipes)
|
2023-06-02 09:07:36 +00:00
|
|
|
|
{
|
2023-11-06 13:33:47 +00:00
|
|
|
|
var result = new List<ContainerRecipePortMapEntry>();
|
|
|
|
|
|
|
|
|
|
// For each container-recipe-port, we need to figure out which service-ports it was assigned by K8s.
|
2023-09-12 08:31:55 +00:00
|
|
|
|
var readback = client.Run(c => c.ReadNamespacedService(serviceSpec.Metadata.Name, K8sNamespace));
|
2023-06-02 09:07:36 +00:00
|
|
|
|
foreach (var r in containerRecipes)
|
|
|
|
|
{
|
2023-11-06 13:33:47 +00:00
|
|
|
|
var recipePorts = r.ExposedPorts.Concat(r.InternalPorts).ToArray();
|
|
|
|
|
foreach (var port in recipePorts)
|
2023-06-02 09:07:36 +00:00
|
|
|
|
{
|
2023-10-19 12:03:36 +00:00
|
|
|
|
var portName = GetNameForPort(r, port);
|
2023-06-02 09:07:36 +00:00
|
|
|
|
|
|
|
|
|
var matchingServicePorts = readback.Spec.Ports.Where(p => p.Name == portName);
|
2023-11-06 13:33:47 +00:00
|
|
|
|
var ports = matchingServicePorts.Select(p => MapPortIfAble(p, port.Tag, port.Protocol)).ToArray();
|
2023-06-27 13:28:00 +00:00
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
if (ports.Any())
|
|
|
|
|
{
|
2023-06-27 13:28:00 +00:00
|
|
|
|
result.Add(new ContainerRecipePortMapEntry(r.Number, ports));
|
2023-11-06 13:33:47 +00:00
|
|
|
|
log.Debug($"Service Readback: {portName} found: {string.Join(",", ports.Select(p => p.ToString()))}");
|
2023-06-02 09:07:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-06 13:33:47 +00:00
|
|
|
|
|
|
|
|
|
return result;
|
2023-06-02 09:07:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
private Port MapPortIfAble(V1ServicePort p, string tag, PortProtocol protocol)
|
2023-06-02 09:07:36 +00:00
|
|
|
|
{
|
2023-11-06 13:33:47 +00:00
|
|
|
|
if (p.NodePort != null) return new Port(p.NodePort.Value, tag, protocol);
|
|
|
|
|
if (p.Port > 0) return new Port(p.Port, tag, protocol);
|
|
|
|
|
throw new Exception("Unable to map port.");
|
2023-06-02 09:07:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
private void DeleteService(RunningService service)
|
2023-04-13 09:07:36 +00:00
|
|
|
|
{
|
2023-11-06 13:33:47 +00:00
|
|
|
|
client.Run(c => c.DeleteNamespacedService(service.Name, K8sNamespace));
|
2023-04-12 13:11:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
private V1ObjectMeta CreateServiceMetadata(ContainerRecipe[] containerRecipes, string namePostfix)
|
2023-04-12 13:11:36 +00:00
|
|
|
|
{
|
2023-11-06 13:33:47 +00:00
|
|
|
|
var recipeName = containerRecipes.First().Name;
|
|
|
|
|
var name = K8sNameUtils.Format($"{recipeName}-{workflowNumberSource.WorkflowNumber}-{namePostfix}");
|
|
|
|
|
log.Debug("Creating service: " + name);
|
2023-04-12 13:11:36 +00:00
|
|
|
|
return new V1ObjectMeta
|
|
|
|
|
{
|
2023-11-03 13:53:27 +00:00
|
|
|
|
Name = name,
|
2023-11-06 13:33:47 +00:00
|
|
|
|
NamespaceProperty = K8sNamespace,
|
2023-04-12 13:11:36 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-13 12:58:34 +00:00
|
|
|
|
private List<V1ServicePort> CreateServicePorts(ContainerRecipe[] recipes, Func<ContainerRecipe, Port[]> portSelector, bool isNodePort)
|
2023-04-12 13:11:36 +00:00
|
|
|
|
{
|
|
|
|
|
var result = new List<V1ServicePort>();
|
|
|
|
|
foreach (var recipe in recipes)
|
|
|
|
|
{
|
2023-11-06 13:33:47 +00:00
|
|
|
|
var ports = portSelector(recipe);
|
|
|
|
|
foreach (var port in ports)
|
|
|
|
|
{
|
2023-11-13 12:58:34 +00:00
|
|
|
|
result.AddRange(CreateServicePorts(recipe, port, isNodePort));
|
2023-11-06 13:33:47 +00:00
|
|
|
|
}
|
2023-04-12 13:11:36 +00:00
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-13 12:58:34 +00:00
|
|
|
|
private List<V1ServicePort> CreateServicePorts(ContainerRecipe recipe, Port recipePort, bool isNodePort)
|
2023-04-12 13:11:36 +00:00
|
|
|
|
{
|
|
|
|
|
var result = new List<V1ServicePort>();
|
2023-11-13 12:58:34 +00:00
|
|
|
|
if (recipePort.IsTcp()) CreateServicePort(result, recipe, recipePort, "TCP", isNodePort);
|
|
|
|
|
if (recipePort.IsUdp()) CreateServicePort(result, recipe, recipePort, "UDP", isNodePort);
|
2023-04-12 13:11:36 +00:00
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-13 12:58:34 +00:00
|
|
|
|
private void CreateServicePort(List<V1ServicePort> result, ContainerRecipe recipe, Port port, string protocol, bool isNodePort)
|
2023-10-25 12:23:07 +00:00
|
|
|
|
{
|
2023-11-13 12:58:34 +00:00
|
|
|
|
var p = new V1ServicePort
|
2023-10-25 12:23:07 +00:00
|
|
|
|
{
|
|
|
|
|
Name = GetNameForPort(recipe, port),
|
2023-10-25 12:42:53 +00:00
|
|
|
|
Protocol = protocol,
|
2023-10-25 12:23:07 +00:00
|
|
|
|
Port = port.Number,
|
2023-11-06 13:33:47 +00:00
|
|
|
|
TargetPort = GetNameForPort(recipe, port)
|
2023-11-13 12:58:34 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (isNodePort) p.NodePort = port.Number;
|
|
|
|
|
|
|
|
|
|
result.Add(p);
|
2023-10-25 12:23:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 13:11:36 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Waiting
|
|
|
|
|
|
|
|
|
|
private void WaitUntilNamespaceCreated()
|
|
|
|
|
{
|
2024-04-14 06:56:22 +00:00
|
|
|
|
WaitUntil(() => IsNamespaceOnline(K8sNamespace), nameof(WaitUntilNamespaceCreated));
|
2023-04-12 13:11:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void WaitUntilDeploymentOnline(string deploymentName)
|
|
|
|
|
{
|
|
|
|
|
WaitUntil(() =>
|
|
|
|
|
{
|
2023-09-12 08:31:55 +00:00
|
|
|
|
var deployment = client.Run(c => c.ReadNamespacedDeployment(deploymentName, K8sNamespace));
|
2023-04-12 13:11:36 +00:00
|
|
|
|
return deployment?.Status.AvailableReplicas != null && deployment.Status.AvailableReplicas > 0;
|
2024-04-14 06:56:22 +00:00
|
|
|
|
}, nameof(WaitUntilDeploymentOnline));
|
2023-04-12 13:11:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-13 09:07:36 +00:00
|
|
|
|
private void WaitUntilDeploymentOffline(string deploymentName)
|
|
|
|
|
{
|
|
|
|
|
WaitUntil(() =>
|
|
|
|
|
{
|
2023-09-12 08:31:55 +00:00
|
|
|
|
var deployments = client.Run(c => c.ListNamespacedDeployment(K8sNamespace));
|
2023-04-13 09:07:36 +00:00
|
|
|
|
var deployment = deployments.Items.SingleOrDefault(d => d.Metadata.Name == deploymentName);
|
|
|
|
|
return deployment == null || deployment.Status.AvailableReplicas == 0;
|
2024-04-14 06:56:22 +00:00
|
|
|
|
}, nameof(WaitUntilDeploymentOffline));
|
2023-04-13 09:07:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-06 13:33:47 +00:00
|
|
|
|
private void WaitUntilPodsForDeploymentAreOffline(RunningDeployment deployment)
|
2023-04-13 09:07:36 +00:00
|
|
|
|
{
|
|
|
|
|
WaitUntil(() =>
|
|
|
|
|
{
|
2023-11-06 13:33:47 +00:00
|
|
|
|
var pods = FindPodsByLabel(deployment.PodLabel);
|
|
|
|
|
return !pods.Any();
|
2024-04-14 06:56:22 +00:00
|
|
|
|
}, nameof(WaitUntilPodsForDeploymentAreOffline));
|
2023-04-13 09:07:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-14 06:56:22 +00:00
|
|
|
|
private void WaitUntil(Func<bool> predicate, string msg)
|
2023-04-12 13:11:36 +00:00
|
|
|
|
{
|
2023-04-25 09:31:15 +00:00
|
|
|
|
var sw = Stopwatch.Begin(log, true);
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-04-14 07:17:25 +00:00
|
|
|
|
Time.WaitUntil(predicate, cluster.K8sOperationTimeout(), cluster.K8sOperationRetryDelay(), msg);
|
2023-04-25 09:31:15 +00:00
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
2024-04-14 06:56:22 +00:00
|
|
|
|
sw.End(msg, 1);
|
2023-04-25 09:31:15 +00:00
|
|
|
|
}
|
2023-04-12 13:11:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2023-08-15 09:01:18 +00:00
|
|
|
|
public CrashWatcher CreateCrashWatcher(RunningContainer container)
|
2023-08-14 13:10:36 +00:00
|
|
|
|
{
|
2023-11-06 13:33:47 +00:00
|
|
|
|
var containerName = container.Name;
|
|
|
|
|
var podName = GetPodName(container);
|
|
|
|
|
var recipeName = container.Recipe.Name;
|
|
|
|
|
|
|
|
|
|
return new CrashWatcher(log, cluster.GetK8sClientConfig(), containerName, podName, recipeName, K8sNamespace);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private V1Pod[] FindPodsByLabel(string podLabel)
|
|
|
|
|
{
|
|
|
|
|
var pods = client.Run(c => c.ListNamespacedPod(K8sNamespace));
|
|
|
|
|
return pods.Items.Where(p => p.GetLabel(PodLabelKey) == podLabel).ToArray();
|
2023-08-14 13:10:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-31 13:48:16 +00:00
|
|
|
|
private PodInfo CreatePodInfo(V1Pod pod)
|
2023-04-12 13:11:36 +00:00
|
|
|
|
{
|
2023-10-31 13:48:16 +00:00
|
|
|
|
var name = pod.Name();
|
|
|
|
|
var ip = pod.Status.PodIP;
|
|
|
|
|
var k8sNodeName = pod.Spec.NodeName;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
|
2023-04-12 13:11:36 +00:00
|
|
|
|
if (string.IsNullOrEmpty(name)) throw new InvalidOperationException("Invalid pod name received. Test infra failure.");
|
|
|
|
|
if (string.IsNullOrEmpty(ip)) throw new InvalidOperationException("Invalid pod IP received. Test infra failure.");
|
2023-04-12 11:53:55 +00:00
|
|
|
|
|
2023-06-02 08:04:07 +00:00
|
|
|
|
return new PodInfo(name, ip, k8sNodeName);
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|