From b5e5570145d4a85f8373d33f5e2c80c4d67306f3 Mon Sep 17 00:00:00 2001 From: benbierens Date: Tue, 31 Oct 2023 11:38:54 +0100 Subject: [PATCH] Creates volume mount for kubeconfig file --- Framework/KubernetesWorkflow/ContainerRecipe.cs | 6 ++++-- .../KubernetesWorkflow/ContainerRecipeFactory.cs | 7 ++++++- Framework/KubernetesWorkflow/K8sController.cs | 14 +++++++++----- .../DeployAndRunContainerRecipe.cs | 2 ++ 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/Framework/KubernetesWorkflow/ContainerRecipe.cs b/Framework/KubernetesWorkflow/ContainerRecipe.cs index 5123b8d..a4a7f8d 100644 --- a/Framework/KubernetesWorkflow/ContainerRecipe.cs +++ b/Framework/KubernetesWorkflow/ContainerRecipe.cs @@ -112,15 +112,17 @@ public class VolumeMount { - public VolumeMount(string volumeName, string mountPath, string resourceQuantity) + public VolumeMount(string volumeName, string mountPath, string? subPath = null, string? resourceQuantity = null) { VolumeName = volumeName; MountPath = mountPath; + SubPath = subPath; ResourceQuantity = resourceQuantity; } public string VolumeName { get; } public string MountPath { get; } - public string ResourceQuantity { get; } + public string? SubPath { get; } + public string? ResourceQuantity { get; } } } diff --git a/Framework/KubernetesWorkflow/ContainerRecipeFactory.cs b/Framework/KubernetesWorkflow/ContainerRecipeFactory.cs index 5fa9980..290f85f 100644 --- a/Framework/KubernetesWorkflow/ContainerRecipeFactory.cs +++ b/Framework/KubernetesWorkflow/ContainerRecipeFactory.cs @@ -97,12 +97,17 @@ namespace KubernetesWorkflow podAnnotations.Add(name, value); } + protected void AddVolume(string name, string mountPath, string subPath) + { + volumeMounts.Add(new VolumeMount(name, mountPath, subPath)); + } + protected void AddVolume(string mountPath, ByteSize volumeSize) { volumeMounts.Add(new VolumeMount( $"autovolume-{Guid.NewGuid().ToString().ToLowerInvariant()}", mountPath, - volumeSize.ToSuffixNotation())); + resourceQuantity: volumeSize.ToSuffixNotation())); } protected void Additional(object userData) diff --git a/Framework/KubernetesWorkflow/K8sController.cs b/Framework/KubernetesWorkflow/K8sController.cs index 3557a12..75d3802 100644 --- a/Framework/KubernetesWorkflow/K8sController.cs +++ b/Framework/KubernetesWorkflow/K8sController.cs @@ -441,7 +441,8 @@ namespace KubernetesWorkflow return new V1VolumeMount { Name = v.VolumeName, - MountPath = v.MountPath + MountPath = v.MountPath, + SubPath = v.SubPath, }; } @@ -457,6 +458,12 @@ namespace KubernetesWorkflow private V1Volume CreateVolume(VolumeMount v) { + var resourcesRequests = new Dictionary(); + if (v.ResourceQuantity != null) + { + resourcesRequests.Add("storage", new ResourceQuantity(v.ResourceQuantity)); + } + client.Run(c => c.CreateNamespacedPersistentVolumeClaim(new V1PersistentVolumeClaim { ApiVersion = "v1", @@ -472,10 +479,7 @@ namespace KubernetesWorkflow }, Resources = new V1ResourceRequirements { - Requests = new Dictionary - { - {"storage", new ResourceQuantity(v.ResourceQuantity) } - } + Requests = resourcesRequests } } }, K8sNamespace)); diff --git a/ProjectPlugins/DeployAndRunPlugin/DeployAndRunContainerRecipe.cs b/ProjectPlugins/DeployAndRunPlugin/DeployAndRunContainerRecipe.cs index 12435a8..55605a3 100644 --- a/ProjectPlugins/DeployAndRunPlugin/DeployAndRunContainerRecipe.cs +++ b/ProjectPlugins/DeployAndRunPlugin/DeployAndRunContainerRecipe.cs @@ -20,6 +20,8 @@ namespace DeployAndRunPlugin AddEnvVar("DNR_NAME", setup.Name); AddEnvVar("DNR_FILTER", setup.Filter); AddEnvVar("DNR_DURATION", setup.Duration.TotalSeconds.ToString()); + + AddVolume(name: "kubeconfig", mountPath: "/opt/kubeconfig.yaml", subPath: "kubeconfig.yaml"); } }