2023-11-12 09:07:23 +00:00
|
|
|
|
namespace KubernetesWorkflow.Recipe
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
|
|
|
|
public class ContainerRecipe
|
|
|
|
|
{
|
2023-11-23 13:50:54 +00:00
|
|
|
|
public ContainerRecipe(int number, string? nameOverride, string image, ContainerResources resources, SchedulingAffinity schedulingAffinity, bool setCriticalPriority, Port[] exposedPorts, Port[] internalPorts, EnvVar[] envVars, PodLabels podLabels, PodAnnotations podAnnotations, VolumeMount[] volumes, ContainerAdditionals additionals)
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
2023-04-12 13:11:36 +00:00
|
|
|
|
Number = number;
|
2023-09-15 10:25:10 +00:00
|
|
|
|
NameOverride = nameOverride;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
Image = image;
|
2023-09-08 11:47:49 +00:00
|
|
|
|
Resources = resources;
|
2023-11-14 09:16:00 +00:00
|
|
|
|
SchedulingAffinity = schedulingAffinity;
|
2023-11-23 13:50:54 +00:00
|
|
|
|
SetCriticalPriority = setCriticalPriority;
|
2023-04-12 11:53:55 +00:00
|
|
|
|
ExposedPorts = exposedPorts;
|
|
|
|
|
InternalPorts = internalPorts;
|
|
|
|
|
EnvVars = envVars;
|
2023-09-04 07:08:34 +00:00
|
|
|
|
PodLabels = podLabels;
|
|
|
|
|
PodAnnotations = podAnnotations;
|
2023-09-07 06:19:19 +00:00
|
|
|
|
Volumes = volumes;
|
2023-04-14 10:37:05 +00:00
|
|
|
|
Additionals = additionals;
|
2023-09-15 10:25:10 +00:00
|
|
|
|
|
|
|
|
|
if (NameOverride != null)
|
|
|
|
|
{
|
|
|
|
|
Name = $"{K8sNameUtils.Format(NameOverride)}-{Number}";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Name = $"ctnr{Number}";
|
|
|
|
|
}
|
2023-10-19 09:12:08 +00:00
|
|
|
|
|
|
|
|
|
if (exposedPorts.Any(p => string.IsNullOrEmpty(p.Tag))) throw new Exception("Port tags are required for all exposed ports.");
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-15 10:25:10 +00:00
|
|
|
|
public string Name { get; }
|
2023-04-12 13:11:36 +00:00
|
|
|
|
public int Number { get; }
|
2023-09-15 10:25:10 +00:00
|
|
|
|
public string? NameOverride { get; }
|
2023-09-08 11:47:49 +00:00
|
|
|
|
public ContainerResources Resources { get; }
|
2023-11-14 09:16:00 +00:00
|
|
|
|
public SchedulingAffinity SchedulingAffinity { get; }
|
2023-11-23 13:50:54 +00:00
|
|
|
|
public bool SetCriticalPriority { get; }
|
2023-04-12 11:53:55 +00:00
|
|
|
|
public string Image { get; }
|
|
|
|
|
public Port[] ExposedPorts { get; }
|
|
|
|
|
public Port[] InternalPorts { get; }
|
|
|
|
|
public EnvVar[] EnvVars { get; }
|
2023-09-04 07:08:34 +00:00
|
|
|
|
public PodLabels PodLabels { get; }
|
|
|
|
|
public PodAnnotations PodAnnotations { get; }
|
2023-09-07 06:19:19 +00:00
|
|
|
|
public VolumeMount[] Volumes { get; }
|
2023-09-20 08:13:29 +00:00
|
|
|
|
public ContainerAdditionals Additionals { get; }
|
2023-04-13 12:36:17 +00:00
|
|
|
|
|
2023-09-13 09:59:21 +00:00
|
|
|
|
public Port? GetPortByTag(string tag)
|
2023-04-13 12:36:17 +00:00
|
|
|
|
{
|
2023-09-13 09:59:21 +00:00
|
|
|
|
return ExposedPorts.Concat(InternalPorts).SingleOrDefault(p => p.Tag == tag);
|
2023-04-13 12:36:17 +00:00
|
|
|
|
}
|
2023-05-04 09:34:43 +00:00
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return $"(container-recipe: {Name}, image: {Image}, " +
|
|
|
|
|
$"exposedPorts: {string.Join(",", ExposedPorts.Select(p => p.Number))}, " +
|
|
|
|
|
$"internalPorts: {string.Join(",", InternalPorts.Select(p => p.Number))}, " +
|
2023-09-27 07:09:42 +00:00
|
|
|
|
$"envVars: {string.Join(",", EnvVars.Select(v => v.ToString()))}, " +
|
2023-09-08 11:47:49 +00:00
|
|
|
|
$"limits: {Resources}, " +
|
2023-11-14 09:16:00 +00:00
|
|
|
|
$"affinity: {SchedulingAffinity}, " +
|
2023-09-07 06:19:19 +00:00
|
|
|
|
$"volumes: {string.Join(",", Volumes.Select(v => $"'{v.MountPath}'"))}";
|
2023-05-04 09:34:43 +00:00
|
|
|
|
}
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Port
|
|
|
|
|
{
|
2023-10-25 12:23:07 +00:00
|
|
|
|
public Port(int number, string tag, PortProtocol protocol)
|
2023-04-12 11:53:55 +00:00
|
|
|
|
{
|
|
|
|
|
Number = number;
|
2023-04-13 12:36:17 +00:00
|
|
|
|
Tag = tag;
|
2023-10-25 12:23:07 +00:00
|
|
|
|
Protocol = protocol;
|
2023-11-06 13:33:47 +00:00
|
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(Tag))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("A unique port tag is required");
|
|
|
|
|
}
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Number { get; }
|
2023-04-13 12:36:17 +00:00
|
|
|
|
public string Tag { get; }
|
2023-10-25 12:23:07 +00:00
|
|
|
|
public PortProtocol Protocol { get; }
|
|
|
|
|
|
|
|
|
|
public bool IsTcp()
|
|
|
|
|
{
|
|
|
|
|
return Protocol == PortProtocol.TCP;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool IsUdp()
|
|
|
|
|
{
|
|
|
|
|
return Protocol == PortProtocol.UDP;
|
|
|
|
|
}
|
2023-10-19 12:03:36 +00:00
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
2023-10-25 12:42:53 +00:00
|
|
|
|
if (string.IsNullOrEmpty(Tag)) return $"untagged-port={Number}/{Protocol}";
|
|
|
|
|
return $"{Tag}={Number}/{Protocol}";
|
2023-10-19 12:03:36 +00:00
|
|
|
|
}
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-25 12:23:07 +00:00
|
|
|
|
public enum PortProtocol
|
|
|
|
|
{
|
|
|
|
|
TCP,
|
|
|
|
|
UDP
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-12 11:53:55 +00:00
|
|
|
|
public class EnvVar
|
|
|
|
|
{
|
|
|
|
|
public EnvVar(string name, string value)
|
|
|
|
|
{
|
|
|
|
|
Name = name;
|
|
|
|
|
Value = value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Name { get; }
|
|
|
|
|
public string Value { get; }
|
2023-09-27 07:09:42 +00:00
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
return $"'{Name}' = '{Value}'";
|
|
|
|
|
}
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|
2023-09-07 06:19:19 +00:00
|
|
|
|
|
|
|
|
|
public class VolumeMount
|
|
|
|
|
{
|
2023-10-31 13:20:50 +00:00
|
|
|
|
public VolumeMount(string volumeName, string mountPath, string? subPath = null, string? resourceQuantity = null, string? secret = null, string? hostPath = null)
|
2023-09-07 06:19:19 +00:00
|
|
|
|
{
|
|
|
|
|
VolumeName = volumeName;
|
|
|
|
|
MountPath = mountPath;
|
2023-10-31 10:38:54 +00:00
|
|
|
|
SubPath = subPath;
|
2023-09-07 08:37:52 +00:00
|
|
|
|
ResourceQuantity = resourceQuantity;
|
2023-10-31 13:20:50 +00:00
|
|
|
|
Secret = secret;
|
|
|
|
|
HostPath = hostPath;
|
2023-09-07 06:19:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string VolumeName { get; }
|
|
|
|
|
public string MountPath { get; }
|
2023-10-31 10:38:54 +00:00
|
|
|
|
public string? SubPath { get; }
|
|
|
|
|
public string? ResourceQuantity { get; }
|
2023-10-31 13:20:50 +00:00
|
|
|
|
public string? Secret { get; }
|
|
|
|
|
public string? HostPath { get; }
|
2023-09-07 06:19:19 +00:00
|
|
|
|
}
|
2023-04-12 11:53:55 +00:00
|
|
|
|
}
|