From bd3aad2819967cadc472e829615c793be948c371 Mon Sep 17 00:00:00 2001 From: Vladislav Sukhin Date: Fri, 13 Dec 2024 16:08:28 +0300 Subject: [PATCH] fix: convert pvc Signed-off-by: Vladislav Sukhin --- api/v1/testkube.yaml | 3 ++ go.mod | 2 +- go.sum | 2 + .../model_test_workflow_pvc_config.go | 6 ++- pkg/mapper/testworkflows/kube_openapi.go | 1 + pkg/mapper/testworkflows/openapi_kube.go | 1 + .../testworkflowresolver/merge.go | 51 ++++++++++++++++++- 7 files changed, 61 insertions(+), 5 deletions(-) diff --git a/api/v1/testkube.yaml b/api/v1/testkube.yaml index 3855d42115..60336c86c1 100644 --- a/api/v1/testkube.yaml +++ b/api/v1/testkube.yaml @@ -10618,6 +10618,9 @@ components: storageClassName: description: 'Storage class name specifies the name of a StorageClass. More info: https://kubernetes.io/docs/concepts/storage/storage-classes/' type: string + volumeName: + description: 'Volume name is used to identify the volume' + type: string selector: description: Only the volumes whose labels match the selector can be bound to the claim $ref: "#/components/schemas/LabelSelector" diff --git a/go.mod b/go.mod index a37d552c92..f3e1bd55f0 100644 --- a/go.mod +++ b/go.mod @@ -42,7 +42,7 @@ require ( github.com/keygen-sh/jsonapi-go v1.2.1 github.com/keygen-sh/keygen-go/v3 v3.2.0 github.com/kubepug/kubepug v1.7.1 - github.com/kubeshop/testkube-operator v1.17.55-0.20241212153943-9d18068064f1 + github.com/kubeshop/testkube-operator v1.17.55-0.20241213114930-df3327436207 github.com/minio/minio-go/v7 v7.0.66 github.com/montanaflynn/stats v0.7.1 github.com/moogar0880/problems v0.1.1 diff --git a/go.sum b/go.sum index a468a77b2c..d55ba5f3ef 100644 --- a/go.sum +++ b/go.sum @@ -338,6 +338,8 @@ github.com/kubepug/kubepug v1.7.1 h1:LKhfSxS8Y5mXs50v+3Lpyec+cogErDLcV7CMUuiaisw github.com/kubepug/kubepug v1.7.1/go.mod h1:lv+HxD0oTFL7ZWjj0u6HKhMbbTIId3eG7aWIW0gyF8g= github.com/kubeshop/testkube-operator v1.17.55-0.20241212153943-9d18068064f1 h1:TmmGhFII/sIdEy6lMRHkRovG7Xr1pW4NkF9sPiPgtNc= github.com/kubeshop/testkube-operator v1.17.55-0.20241212153943-9d18068064f1/go.mod h1:P47tw1nKQFufdsZndyq2HG2MSa0zK/lU0XpRfZtEmIk= +github.com/kubeshop/testkube-operator v1.17.55-0.20241213114930-df3327436207 h1:IQvoge5CpG0Z1uEjb/rcA4SN+ZiAmPrO0XSTLLlPDeg= +github.com/kubeshop/testkube-operator v1.17.55-0.20241213114930-df3327436207/go.mod h1:P47tw1nKQFufdsZndyq2HG2MSa0zK/lU0XpRfZtEmIk= github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= github.com/lithammer/fuzzysearch v1.1.8 h1:/HIuJnjHuXS8bKaiTMeeDlW2/AyIWk2brx1V8LFgLN4= diff --git a/pkg/api/v1/testkube/model_test_workflow_pvc_config.go b/pkg/api/v1/testkube/model_test_workflow_pvc_config.go index 289c61df36..77cacf8f69 100644 --- a/pkg/api/v1/testkube/model_test_workflow_pvc_config.go +++ b/pkg/api/v1/testkube/model_test_workflow_pvc_config.go @@ -16,6 +16,8 @@ type TestWorkflowPvcConfig struct { VolumeMode string `json:"volumeMode,omitempty"` Resources *TestWorkflowResources `json:"resources,omitempty"` // Storage class name specifies the name of a StorageClass. More info: https://kubernetes.io/docs/concepts/storage/storage-classes/ - StorageClassName string `json:"storageClassName,omitempty"` - Selector *LabelSelector `json:"selector,omitempty"` + StorageClassName string `json:"storageClassName,omitempty"` + // Volume name is used to identify the volume + VolumeName string `json:"volumeName,omitempty"` + Selector *LabelSelector `json:"selector,omitempty"` } diff --git a/pkg/mapper/testworkflows/kube_openapi.go b/pkg/mapper/testworkflows/kube_openapi.go index 79c69d3f8c..5509cc6736 100644 --- a/pkg/mapper/testworkflows/kube_openapi.go +++ b/pkg/mapper/testworkflows/kube_openapi.go @@ -1219,6 +1219,7 @@ func MapPvcConfigKubeToAPI(v testworkflowsv1.TestWorkflowPvcConfig) testkube.Tes VolumeMode: v.VolumeMode, Resources: common.MapPtr(v.Resources, MapResourcesKubeToAPI), StorageClassName: v.StorageClassName, + VolumeName: v.VolumeName, Selector: common.MapPtr(v.Selector, MapSelectorToAPI), } } diff --git a/pkg/mapper/testworkflows/openapi_kube.go b/pkg/mapper/testworkflows/openapi_kube.go index 0865309fbf..caf0f3860e 100644 --- a/pkg/mapper/testworkflows/openapi_kube.go +++ b/pkg/mapper/testworkflows/openapi_kube.go @@ -1498,6 +1498,7 @@ func MapPvcConfigAPIToKube(v testkube.TestWorkflowPvcConfig) testworkflowsv1.Tes VolumeMode: v.VolumeMode, Resources: common.MapPtr(v.Resources, MapResourcesAPIToKube), StorageClassName: v.StorageClassName, + VolumeName: v.VolumeName, Selector: common.MapPtr(v.Selector, MapLabelSelectorAPIToKube), } } diff --git a/pkg/testworkflows/testworkflowresolver/merge.go b/pkg/testworkflows/testworkflowresolver/merge.go index 9d932cee8d..9da13ade7a 100644 --- a/pkg/testworkflows/testworkflowresolver/merge.go +++ b/pkg/testworkflows/testworkflowresolver/merge.go @@ -12,6 +12,8 @@ import ( "maps" corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/resource" + "k8s.io/apimachinery/pkg/util/intstr" testworkflowsv1 "github.com/kubeshop/testkube-operator/api/testworkflows/v1" "github.com/kubeshop/testkube/internal/common" @@ -421,9 +423,54 @@ func ConvertTestWorkflowPvcConfigToPersistentVolumeClaimSpec(pvc testworkflowsv1 AccessModes: common.MapSlice(pvc.AccessModes, func(s string) corev1.PersistentVolumeAccessMode { return corev1.PersistentVolumeAccessMode(s) }), - Selector: pvc.Selector, - // Resources: pvc.Resources.Limits, + Selector: pvc.Selector, + Resources: ConvertResourcesToVolumeResourceRequirements(pvc.Resources), + VolumeName: pvc.VolumeName, StorageClassName: &pvc.StorageClassName, VolumeMode: (*corev1.PersistentVolumeMode)(&pvc.VolumeMode), } } + +func ConvertResourcesToVolumeResourceRequirements(r *testworkflowsv1.Resources) corev1.VolumeResourceRequirements { + var limits, requests corev1.ResourceList + if r != nil { + if len(r.Limits) != 0 { + limits = make(corev1.ResourceList) + } + + if len(r.Requests) != 0 { + requests = make(corev1.ResourceList) + } + + for key, value := range r.Limits { + var quantity resource.Quantity + if value.Type == intstr.Int { + quantity = *resource.NewQuantity(int64(value.IntVal), resource.BinarySI) + } + + if value.Type == intstr.String { + quantity, _ = resource.ParseQuantity(value.String()) + } + + limits[key] = quantity + } + + for key, value := range r.Requests { + var quantity resource.Quantity + if value.Type == intstr.Int { + quantity = *resource.NewQuantity(int64(value.IntVal), resource.BinarySI) + } + + if value.Type == intstr.String { + quantity, _ = resource.ParseQuantity(value.String()) + } + + requests[key] = quantity + } + } + + return corev1.VolumeResourceRequirements{ + Limits: limits, + Requests: requests, + } +}