in code/KubernetesWrapper/KubernetesWrapper/Program.cs [177:279]
public static async Task<V1Job?> CreateJob(IKubernetes client, string jobName, string containerName, string imageName,
string namespaceName, int ttlSecondsAfterFinished, List<string> command, List<string> arguments, List<string> nodeList,
CancellationToken token)
{
V1Container? container = new()
{
Name = containerName,
Image = imageName,
};
if (command.Count != 0)
{
container.Command = command;
}
if (arguments.Count != 0)
{
container.Args = arguments;
}
var job = new V1Job
{
ApiVersion = "batch/v1",
Kind = "Job",
Metadata = new V1ObjectMeta
{
Name = jobName,
Labels = new System.Collections.Generic.Dictionary<string, string>
{
{ "app", containerName }
}
},
Spec = new V1JobSpec
{
Completions = nodeList.Count,
Parallelism = nodeList.Count,
TtlSecondsAfterFinished = ttlSecondsAfterFinished,
Template = new V1PodTemplateSpec
{
Metadata = new V1ObjectMeta
{
Labels = new System.Collections.Generic.Dictionary<string, string>
{
{ "app", containerName }
}
},
Spec = new V1PodSpec
{
Affinity = new V1Affinity
{
NodeAffinity = new V1NodeAffinity
{
RequiredDuringSchedulingIgnoredDuringExecution = new V1NodeSelector
{
NodeSelectorTerms =
[
new V1NodeSelectorTerm
{
MatchExpressions =
[
new()
{
Key = "kubernetes.io/hostname",
OperatorProperty = "In",
Values = nodeList
}
]
}
]
}
}
},
Containers =
[
container
],
RestartPolicy = "Never"
}
}
}
};
V1Job? result = null;
try
{
result = await client.BatchV1.CreateNamespacedJobAsync(job, namespaceName, cancellationToken: token).ConfigureAwait(false);
Console.WriteLine($"Job '{jobName}' created successfully.");
}
catch (TaskCanceledException ex)
{
Console.WriteLine($"Job will not be created. Task was canceled: {ex.Message}");
}
catch (k8s.Autorest.HttpOperationException ex) when (ex.Response.StatusCode == HttpStatusCode.Conflict)
{
Console.WriteLine($"Job '{jobName}' already exists. Error: {ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Error creating deployment: {ex.Message}");
}
return result;
}