containerized-data-importer/tests/utils/upload.go
Michael Henriksen 75f4fd6f2f
update k8s deps to 18.6 and controller runtime to 0.6.2 (#1330)
* update k8s deps to 1.18.6 and controller runtime to 0.6.2

Signed-off-by: Michael Henriksen <mhenriks@redhat.com>

* remove building code generators from docker image.  This way the k8s ligray version only has to be updated in go.mod

Do more stuff in the bazel container.  Faster and better interop

Fix unit tests

Signed-off-by: Michael Henriksen <mhenriks@redhat.com>

* make format

Signed-off-by: Michael Henriksen <mhenriks@redhat.com>

* remove unnecessary rsync

Signed-off-by: Michael Henriksen <mhenriks@redhat.com>

* redo code generator dep management

Signed-off-by: Michael Henriksen <mhenriks@redhat.com>

* builder uses go modules

Signed-off-by: Michael Henriksen <mhenriks@redhat.com>
2020-08-07 14:09:52 +02:00

76 lines
2.7 KiB
Go

package utils
import (
"context"
k8sv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
cdiuploadv1 "kubevirt.io/containerized-data-importer/pkg/apis/upload/v1beta1"
cdiClientset "kubevirt.io/containerized-data-importer/pkg/client/clientset/versioned"
"kubevirt.io/containerized-data-importer/pkg/util/naming"
)
const (
// UploadFile is the file to upload
UploadFile = "./images/tinyCore.iso"
// UploadFileLargeVirtualDisk is the file to upload
UploadFileLargeVirtualDisk = "./images/cirros-large-vdisk.qcow2"
// UploadFileSize is the size of UploadFile
UploadFileSize = 18874368
// UploadFileMD5 is the expected MD5 of the uploaded file
UploadFileMD5 = "2a7a52285c846314d1dbd79e9818270d"
// UploadFileMD5100kbytes is the size of the image after being extended
UploadFileMD5100kbytes = "3710416a680523c7d07538cb1026c60c"
uploadTargetAnnotation = "cdi.kubevirt.io/storage.upload.target"
uploadStatusAnnotation = "cdi.kubevirt.io/storage.pod.phase"
uploadReadyAnnotation = "cdi.kubevirt.io/storage.pod.ready"
)
// UploadPodName returns the name of the upload server pod associated with a PVC
func UploadPodName(pvc *k8sv1.PersistentVolumeClaim) string {
return naming.GetResourceName("cdi-upload", pvc.Name)
}
// UploadPVCDefinition creates a PVC with the upload target annotation
func UploadPVCDefinition() *k8sv1.PersistentVolumeClaim {
annotations := map[string]string{uploadTargetAnnotation: ""}
return NewPVCDefinition("upload-test", "1Gi", annotations, nil)
}
// UploadBlockPVCDefinition creates a PVC with the upload target annotation for block PV
func UploadBlockPVCDefinition(storageClass string) *k8sv1.PersistentVolumeClaim {
annotations := map[string]string{uploadTargetAnnotation: ""}
return NewBlockPVCDefinition("upload-test", "500Mi", annotations, nil, storageClass)
}
// WaitPVCUploadPodStatusRunning waits for the upload server pod status annotation to be Running
func WaitPVCUploadPodStatusRunning(clientSet *kubernetes.Clientset, pvc *k8sv1.PersistentVolumeClaim) (bool, error) {
return WaitForPVCAnnotationWithValue(clientSet, pvc.Namespace, pvc, uploadStatusAnnotation, string(k8sv1.PodRunning))
}
// RequestUploadToken sends an upload token request to the server
func RequestUploadToken(clientSet *cdiClientset.Clientset, pvc *k8sv1.PersistentVolumeClaim) (string, error) {
request := &cdiuploadv1.UploadTokenRequest{
ObjectMeta: metav1.ObjectMeta{
Name: "test-token",
Namespace: pvc.Namespace,
},
Spec: cdiuploadv1.UploadTokenRequestSpec{
PvcName: pvc.Name,
},
}
response, err := clientSet.UploadV1beta1().UploadTokenRequests(pvc.Namespace).Create(context.TODO(), request, metav1.CreateOptions{})
if err != nil {
return "", err
}
return response.Status.Token, nil
}