From 462f400ca0f7ae961c70663bdec48241e9dca36c Mon Sep 17 00:00:00 2001 From: Alex Kalenyuk Date: Sun, 15 Oct 2023 23:46:42 +0300 Subject: [PATCH] Refactor content type funcs to not return strings Signed-off-by: Alex Kalenyuk --- pkg/controller/clone-controller.go | 2 +- pkg/controller/common/util.go | 18 +++++++++--------- pkg/controller/datavolume/controller-base.go | 2 +- pkg/controller/import-controller.go | 4 ++-- pkg/controller/import-controller_test.go | 2 +- pkg/controller/populators/import-populator.go | 2 +- pkg/controller/populators/upload-populator.go | 2 +- 7 files changed, 16 insertions(+), 16 deletions(-) diff --git a/pkg/controller/clone-controller.go b/pkg/controller/clone-controller.go index 0770202e6b..b76af53b25 100644 --- a/pkg/controller/clone-controller.go +++ b/pkg/controller/clone-controller.go @@ -753,7 +753,7 @@ func ValidateCanCloneSourceAndTargetContentType(sourcePvc, targetPvc *corev1.Per if sourceContentType != targetContentType { return "", fmt.Errorf("source contentType (%s) and target contentType (%s) do not match", sourceContentType, targetContentType) } - return cdiv1.DataVolumeContentType(sourceContentType), nil + return sourceContentType, nil } // ValidateCanCloneSourceAndTargetSpec validates the specs passed in are compatible for cloning. diff --git a/pkg/controller/common/util.go b/pkg/controller/common/util.go index 9ef85cac3d..d621d85929 100644 --- a/pkg/controller/common/util.go +++ b/pkg/controller/common/util.go @@ -483,7 +483,7 @@ func getFallbackStorageClass(ctx context.Context, client client.Client, contentT return nil, errors.New("unable to retrieve storage classes") } - if GetContentType(string(contentType)) == string(cdiv1.DataVolumeKubeVirt) { + if GetContentType(contentType) == cdiv1.DataVolumeKubeVirt { virtSc := GetPlatformDefaultStorageClass(ctx, storageClasses, AnnDefaultVirtStorageClass) if virtSc != nil { return virtSc, nil @@ -1285,7 +1285,7 @@ func CreateImporterTestPod(pvc *corev1.PersistentVolumeClaim, dvname string, scr }, { Name: common.ImporterContentType, - Value: contentType, + Value: string(contentType), }, { Name: common.ImporterImageSize, @@ -1346,27 +1346,27 @@ func ErrQuotaExceeded(err error) bool { } // GetContentType returns the content type. If invalid or not set, default to kubevirt -func GetContentType(contentType string) string { +func GetContentType(contentType cdiv1.DataVolumeContentType) cdiv1.DataVolumeContentType { switch contentType { case - string(cdiv1.DataVolumeKubeVirt), - string(cdiv1.DataVolumeArchive): + cdiv1.DataVolumeKubeVirt, + cdiv1.DataVolumeArchive: default: // TODO - shouldn't archive be the default? - contentType = string(cdiv1.DataVolumeKubeVirt) + contentType = cdiv1.DataVolumeKubeVirt } return contentType } // GetPVCContentType returns the content type of the source image. If invalid or not set, default to kubevirt -func GetPVCContentType(pvc *corev1.PersistentVolumeClaim) string { +func GetPVCContentType(pvc *corev1.PersistentVolumeClaim) cdiv1.DataVolumeContentType { contentType, found := pvc.Annotations[AnnContentType] if !found { // TODO - shouldn't archive be the default? - return string(cdiv1.DataVolumeKubeVirt) + return cdiv1.DataVolumeKubeVirt } - return GetContentType(contentType) + return GetContentType(cdiv1.DataVolumeContentType(contentType)) } // GetNamespace returns the given namespace if not empty, otherwise the default namespace diff --git a/pkg/controller/datavolume/controller-base.go b/pkg/controller/datavolume/controller-base.go index 3c2f64cab8..f1062b90bb 100644 --- a/pkg/controller/datavolume/controller-base.go +++ b/pkg/controller/datavolume/controller-base.go @@ -1034,7 +1034,7 @@ func (r *ReconcilerBase) newPersistentVolumeClaim(dataVolume *cdiv1.DataVolume, annotations[k] = v } annotations[cc.AnnPodRestarts] = "0" - annotations[cc.AnnContentType] = cc.GetContentType(string(dataVolume.Spec.ContentType)) + annotations[cc.AnnContentType] = string(cc.GetContentType(dataVolume.Spec.ContentType)) if dataVolume.Spec.PriorityClassName != "" { annotations[cc.AnnPriorityClassName] = dataVolume.Spec.PriorityClassName } diff --git a/pkg/controller/import-controller.go b/pkg/controller/import-controller.go index ae2b96d084..011be11f6c 100644 --- a/pkg/controller/import-controller.go +++ b/pkg/controller/import-controller.go @@ -541,7 +541,7 @@ func createScratchNameFromPvc(pvc *v1.PersistentVolumeClaim) string { func (r *ImportReconciler) createImportEnvVar(pvc *corev1.PersistentVolumeClaim) (*importPodEnvVar, error) { podEnvVar := &importPodEnvVar{} podEnvVar.source = cc.GetSource(pvc) - podEnvVar.contentType = cc.GetPVCContentType(pvc) + podEnvVar.contentType = string(cc.GetPVCContentType(pvc)) var err error if podEnvVar.source != cc.SourceNone { @@ -691,7 +691,7 @@ func (r *ImportReconciler) requiresScratchSpace(pvc *corev1.PersistentVolumeClai scratchRequired := false contentType := cc.GetPVCContentType(pvc) // All archive requires scratch space. - if contentType == "archive" { + if contentType == cdiv1.DataVolumeArchive { scratchRequired = true } else { switch cc.GetSource(pvc) { diff --git a/pkg/controller/import-controller_test.go b/pkg/controller/import-controller_test.go index 4cca099778..2bc9e831d2 100644 --- a/pkg/controller/import-controller_test.go +++ b/pkg/controller/import-controller_test.go @@ -935,7 +935,7 @@ var _ = Describe("GetContentType", func() { DescribeTable("should", func(pvc *corev1.PersistentVolumeClaim, expectedResult cdiv1.DataVolumeContentType) { result := cc.GetPVCContentType(pvc) - Expect(result).To(BeEquivalentTo(expectedResult)) + Expect(result).To(Equal(expectedResult)) }, Entry("return kubevirt contenttype if no annotation provided", pvcNoAnno, cdiv1.DataVolumeKubeVirt), Entry("return archive contenttype if archive annotation present", pvcArchiveAnno, cdiv1.DataVolumeArchive), diff --git a/pkg/controller/populators/import-populator.go b/pkg/controller/populators/import-populator.go index 7c1b34b282..ca1540969d 100644 --- a/pkg/controller/populators/import-populator.go +++ b/pkg/controller/populators/import-populator.go @@ -178,7 +178,7 @@ func (r *ImportPopulatorReconciler) updatePVCForPopulation(pvc *corev1.Persisten volumeImportSource := source.(*cdiv1.VolumeImportSource) annotations := pvc.Annotations annotations[cc.AnnPopulatorKind] = cdiv1.VolumeImportSourceRef - annotations[cc.AnnContentType] = cc.GetContentType(string(volumeImportSource.Spec.ContentType)) + annotations[cc.AnnContentType] = string(cc.GetContentType(volumeImportSource.Spec.ContentType)) annotations[cc.AnnPreallocationRequested] = strconv.FormatBool(cc.GetPreallocation(context.TODO(), r.client, volumeImportSource.Spec.Preallocation)) if checkpoint := cc.GetNextCheckpoint(pvc, r.getCheckpointArgs(source)); checkpoint != nil { diff --git a/pkg/controller/populators/upload-populator.go b/pkg/controller/populators/upload-populator.go index efd0ca7b3b..8cc29179de 100644 --- a/pkg/controller/populators/upload-populator.go +++ b/pkg/controller/populators/upload-populator.go @@ -113,7 +113,7 @@ func (r *UploadPopulatorReconciler) getPopulationSource(pvc *corev1.PersistentVo func (r *UploadPopulatorReconciler) updatePVCForPopulation(pvc *corev1.PersistentVolumeClaim, source client.Object) { pvc.Annotations[cc.AnnUploadRequest] = "" uploadSource := source.(*cdiv1.VolumeUploadSource) - pvc.Annotations[cc.AnnContentType] = cc.GetContentType(string(uploadSource.Spec.ContentType)) + pvc.Annotations[cc.AnnContentType] = string(cc.GetContentType(uploadSource.Spec.ContentType)) pvc.Annotations[cc.AnnPopulatorKind] = cdiv1.VolumeUploadSourceRef pvc.Annotations[cc.AnnPreallocationRequested] = strconv.FormatBool(cc.GetPreallocation(context.TODO(), r.client, uploadSource.Spec.Preallocation)) }