Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Virtctl - Force Bind the PVC on WFFC storage #6513

Merged
merged 3 commits into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 22 additions & 8 deletions pkg/virtctl/imageupload/imageupload.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ const (
// PodReadyAnnotation tells whether the uploadserver pod is ready
PodReadyAnnotation = "cdi.kubevirt.io/storage.pod.ready"

uploadRequestAnnotation = "cdi.kubevirt.io/storage.upload.target"
uploadRequestAnnotation = "cdi.kubevirt.io/storage.upload.target"
forceImmediateBindingAnnotation = "cdi.kubevirt.io/storage.bind.immediate.requested"
brybacki marked this conversation as resolved.
Show resolved Hide resolved

uploadReadyWaitInterval = 2 * time.Second

Expand Down Expand Up @@ -88,6 +89,7 @@ var (
blockVolume bool
noCreate bool
createPVC bool
forceBind bool
)

// HTTPClientCreator is a function that creates http clients
Expand Down Expand Up @@ -139,6 +141,7 @@ func NewImageUploadCommand(clientConfig clientcmd.ClientConfig) *cobra.Command {
cmd.MarkFlagRequired("image-path")
cmd.Flags().BoolVar(&noCreate, "no-create", false, "Don't attempt to create a new DataVolume/PVC.")
cmd.Flags().UintVar(&uploadPodWaitSecs, "wait-secs", 300, "Seconds to wait for upload pod to start.")
cmd.Flags().BoolVar(&forceBind, "force-bind", false, "Force bind the PVC, ignoring the WaitForFirstConsumer logic.")
cmd.SetUsageTemplate(templates.UsageTemplate())
return cmd
}
Expand Down Expand Up @@ -522,10 +525,16 @@ func createUploadDataVolume(client kubecli.KubevirtClient, namespace, name, size
return nil, err
}

annotations := map[string]string{}
if forceBind {
annotations[forceImmediateBindingAnnotation] = ""
}

dv := &cdiv1.DataVolume{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Name: name,
Namespace: namespace,
Annotations: annotations,
},
Spec: cdiv1.DataVolumeSpec{
Source: &cdiv1.DataVolumeSource{
Expand Down Expand Up @@ -582,13 +591,18 @@ func createUploadPVC(client kubernetes.Interface, namespace, name, size, storage
return nil, err
}

annotations := map[string]string{
uploadRequestAnnotation: "",
}
if forceBind {
annotations[forceImmediateBindingAnnotation] = ""
brybacki marked this conversation as resolved.
Show resolved Hide resolved
}

pvc := &v1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Annotations: map[string]string{
uploadRequestAnnotation: "",
},
Name: name,
Namespace: namespace,
Annotations: annotations,
},
Spec: *pvcSpec,
}
Expand Down
43 changes: 33 additions & 10 deletions pkg/virtctl/imageupload/imageupload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ import (
)

const (
commandName = "image-upload"
uploadRequestAnnotation = "cdi.kubevirt.io/storage.upload.target"
podPhaseAnnotation = "cdi.kubevirt.io/storage.pod.phase"
podReadyAnnotation = "cdi.kubevirt.io/storage.pod.ready"
commandName = "image-upload"
uploadRequestAnnotation = "cdi.kubevirt.io/storage.upload.target"
forceImmediateBindingAnnotation = "cdi.kubevirt.io/storage.bind.immediate.requested"
podPhaseAnnotation = "cdi.kubevirt.io/storage.pod.phase"
podReadyAnnotation = "cdi.kubevirt.io/storage.pod.ready"
)

const (
Expand Down Expand Up @@ -299,19 +300,31 @@ var _ = Describe("ImageUpload", func() {
validatePVCArgs(v1.PersistentVolumeBlock)
}

validateDataVolumeArgs := func(mode v1.PersistentVolumeMode) {
dv, err := cdiClient.CdiV1beta1().DataVolumes(targetNamespace).Get(context.Background(), targetName, metav1.GetOptions{})
Expect(err).To(BeNil())

validateDataVolumeArgs := func(dv *cdiv1.DataVolume, mode v1.PersistentVolumeMode) {
validateDvStorageSpec(dv.Spec, mode)
}

validateDataVolume := func() {
validateDataVolumeArgs(v1.PersistentVolumeFilesystem)
dv, err := cdiClient.CdiV1beta1().DataVolumes(targetNamespace).Get(context.Background(), targetName, metav1.GetOptions{})
Expect(err).To(BeNil())

validateDataVolumeArgs(dv, v1.PersistentVolumeFilesystem)
}

validateBlockDataVolume := func() {
validateDataVolumeArgs(v1.PersistentVolumeBlock)
dv, err := cdiClient.CdiV1beta1().DataVolumes(targetNamespace).Get(context.Background(), targetName, metav1.GetOptions{})
Expect(err).To(BeNil())

validateDataVolumeArgs(dv, v1.PersistentVolumeBlock)
}

validateDataVolumeWithForceBind := func() {
dv, err := cdiClient.CdiV1beta1().DataVolumes(targetNamespace).Get(context.Background(), targetName, metav1.GetOptions{})
Expect(err).To(BeNil())
_, ok := dv.Annotations[forceImmediateBindingAnnotation]
Expect(ok).To(BeTrue(), "storage.bind.immediate.requested annotation")

validateDataVolumeArgs(dv, v1.PersistentVolumeFilesystem)
}

expectedStorageClassMatchesActual := func(storageClass string) {
Expand Down Expand Up @@ -441,6 +454,16 @@ var _ = Describe("ImageUpload", func() {
validateDataVolume()
})

It("DV does not exist --force-bind", func() {
testInit(http.StatusOK)
cmd := tests.NewRepeatableVirtctlCommand(commandName, "dv", targetName, "--pvc-size", pvcSize,
"--uploadproxy-url", server.URL, "--insecure", "--image-path", imagePath, "--force-bind")
Expect(cmd()).To(BeNil())
Expect(dvCreateCalled.IsTrue()).To(BeTrue())
validatePVC()
validateDataVolumeWithForceBind()
})

It("DV does not exist and --no-create", func() {
testInit(http.StatusOK)
cmd := tests.NewRepeatableVirtctlCommand(commandName, "dv", targetName, "--pvc-size", pvcSize,
Expand Down
43 changes: 43 additions & 0 deletions tests/storage/imageupload.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,49 @@ var _ = SIGDescribe("[Serial]ImageUpload", func() {
)
})

validateDataVolumeForceBind := func(targetName string) {
By("Get DataVolume")
dv, err := virtClient.CdiClient().CdiV1beta1().DataVolumes(util.NamespaceTestDefault).Get(context.Background(), targetName, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())

_, found := dv.Annotations["cdi.kubevirt.io/storage.bind.immediate.requested"]
Expect(found).To(BeTrue())
}

validatePVCForceBind := func(targetName string) {
By("Don't DataVolume")
_, err := virtClient.CdiClient().CdiV1beta1().DataVolumes(util.NamespaceTestDefault).Get(context.Background(), targetName, metav1.GetOptions{})
Expect(errors.IsNotFound(err)).To(BeTrue())

By("Get PVC")
pvc, err := virtClient.CoreV1().PersistentVolumeClaims(util.NamespaceTestDefault).Get(context.Background(), targetName, metav1.GetOptions{})
Expect(err).ToNot(HaveOccurred())
_, found := pvc.Annotations["cdi.kubevirt.io/storage.bind.immediate.requested"]
Expect(found).To(BeTrue())
}

Context("Create upload volume with force-bind flag", func() {
DescribeTable("Should succeed", func(resource, targetName string, validateFunc func(string), deleteFunc func(string)) {
defer deleteFunc(targetName)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this defer is needed, but it does not hurt either.


By("Upload image")
virtctlCmd := tests.NewRepeatableVirtctlCommand("image-upload",
resource, targetName,
"--namespace", util.NamespaceTestDefault,
"--image-path", imagePath,
"--size", pvcSize,
"--force-bind",
"--block-volume",
"--insecure")

Expect(virtctlCmd()).To(Succeed())
validateFunc(targetName)
},
Entry("DataVolume", "dv", "alpine-dv-"+rand.String(12), validateDataVolumeForceBind, deleteDataVolume),
Entry("PVC", "pvc", "alpine-pvc-"+rand.String(12), validatePVCForceBind, deletePVC),
)
})

AfterEach(func() {
if kubectlCmd != nil {
kubectlCmd.Process.Kill()
Expand Down