Skip to content

Commit

Permalink
Refactor content type funcs to not return strings
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Kalenyuk <akalenyu@redhat.com>
  • Loading branch information
akalenyu committed Oct 19, 2023
1 parent 24de9f1 commit f0c7ab7
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion pkg/controller/clone-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 9 additions & 9 deletions pkg/controller/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,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
Expand Down Expand Up @@ -1292,7 +1292,7 @@ func CreateImporterTestPod(pvc *corev1.PersistentVolumeClaim, dvname string, scr
},
{
Name: common.ImporterContentType,
Value: contentType,
Value: string(contentType),
},
{
Name: common.ImporterImageSize,
Expand Down Expand Up @@ -1353,27 +1353,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
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/datavolume/controller-base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/import-controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/import-controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/populators/import-populator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/populators/upload-populator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down

0 comments on commit f0c7ab7

Please sign in to comment.