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

OVA: changing nfs mount to server and conversion pod to use shared PVC #592

Merged
merged 5 commits into from
Oct 4, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/controller/plan/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ go_library(
"//vendor/k8s.io/apimachinery/pkg/runtime",
"//vendor/k8s.io/apimachinery/pkg/types",
"//vendor/k8s.io/apimachinery/pkg/util/validation",
"//vendor/k8s.io/apimachinery/pkg/util/wait",
"//vendor/k8s.io/apiserver/pkg/storage/names",
"//vendor/k8s.io/client-go/kubernetes/scheme",
"//vendor/kubevirt.io/api/core/v1:core",
Expand Down
182 changes: 168 additions & 14 deletions pkg/controller/plan/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
k8svalidation "k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/wait"
cnv "kubevirt.io/api/core/v1"
libvirtxml "libvirt.org/libvirt-go-xml"

Expand Down Expand Up @@ -1184,7 +1185,10 @@ func (r *KubeVirt) findTemplate(vm *plan.VMStatus) (tmpl *template.Template, err
}

func (r *KubeVirt) guestConversionPod(vm *plan.VMStatus, vmVolumes []cnv.Volume, configMap *core.ConfigMap, pvcs *[]core.PersistentVolumeClaim, v2vSecret *core.Secret) (pod *core.Pod, err error) {
volumes, volumeMounts, volumeDevices := r.podVolumeMounts(vmVolumes, configMap, pvcs)
volumes, volumeMounts, volumeDevices, err := r.podVolumeMounts(vmVolumes, configMap, pvcs)
if err != nil {
return
}

// qemu group
fsGroup := qemuGroup
Expand Down Expand Up @@ -1290,7 +1294,7 @@ func (r *KubeVirt) guestConversionPod(vm *plan.VMStatus, vmVolumes []cnv.Volume,
return
}

func (r *KubeVirt) podVolumeMounts(vmVolumes []cnv.Volume, configMap *core.ConfigMap, pvcs *[]core.PersistentVolumeClaim) (volumes []core.Volume, mounts []core.VolumeMount, devices []core.VolumeDevice) {
func (r *KubeVirt) podVolumeMounts(vmVolumes []cnv.Volume, configMap *core.ConfigMap, pvcs *[]core.PersistentVolumeClaim) (volumes []core.Volume, mounts []core.VolumeMount, devices []core.VolumeDevice, err error) {
pvcsByName := make(map[string]core.PersistentVolumeClaim)
for _, pvc := range *pvcs {
pvcsByName[pvc.Name] = pvc
Expand Down Expand Up @@ -1336,23 +1340,22 @@ func (r *KubeVirt) podVolumeMounts(vmVolumes []cnv.Volume, configMap *core.Confi

switch r.Source.Provider.Type() {
case api.Ova:
server := r.Source.Provider.Spec.URL
splitted := strings.Split(server, ":")

if len(splitted) != 2 {
r.Log.Info("The NFS server path format is wrong")
err = r.CreatePvForNfs()
bkhizgiy marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return
}
pvcName := getEntityName("pvc", r.Source.Provider.Name, r.Plan.Name)
err = r.CreatePvcForNfs(pvcName)
if err != nil {
return
}
nfsServer := splitted[0]
nfsPath := splitted[1]

//path from disk
volumes = append(volumes, core.Volume{
Name: "nfs",
Name: "store-pv",
VolumeSource: core.VolumeSource{
NFS: &core.NFSVolumeSource{
Server: nfsServer,
Path: nfsPath,
PersistentVolumeClaim: &core.PersistentVolumeClaimVolumeSource{
ClaimName: pvcName,
},
},
})
Expand All @@ -1366,7 +1369,7 @@ func (r *KubeVirt) podVolumeMounts(vmVolumes []cnv.Volume, configMap *core.Confi
MountPath: "/opt",
},
core.VolumeMount{
Name: "nfs",
Name: "store-pv",
MountPath: "/ova",
},
)
Expand Down Expand Up @@ -1835,6 +1838,157 @@ func (r *KubeVirt) EnsurePersistentVolume(vmRef ref.Ref, persistentVolumes []cor
return
}

func GetOvaPvNfs(client client.Client, planName string, providerName string) (pv *core.PersistentVolume, found bool, err error) {
pv = &core.PersistentVolume{}
err = client.Get(
context.TODO(),
types.NamespacedName{
Name: getEntityName("pv", providerName, planName),
},
pv,
)

if err != nil {
if k8serr.IsNotFound(err) {
return nil, false, nil
}
err = liberr.Wrap(err)
return
}
return
}

func GetOvaPvcNfs(client client.Client, planName string, planNamespace string, providerName string) (pvc *core.PersistentVolumeClaim, found bool, err error) {
pvc = &core.PersistentVolumeClaim{}
err = client.Get(
context.TODO(),
types.NamespacedName{
Name: getEntityName("pvc", providerName, planName),
Namespace: planNamespace,
},
pvc,
)

if err != nil {
if k8serr.IsNotFound(err) {
return nil, false, nil
}
err = liberr.Wrap(err)
return
}
return
}

func (r *KubeVirt) CreatePvForNfs() (err error) {
sourceProvider := r.Source.Provider
splitted := strings.Split(sourceProvider.Spec.URL, ":")
nfsServer := splitted[0]
nfsPath := splitted[1]

_, found, err := GetOvaPvNfs(r.Destination.Client, r.Plan.Name, r.Plan.Provider.Source.Name)
if err != nil {
r.Log.Error(err, "Failed to get ova PV")
return
}
pvName := getEntityName("pv", r.Source.Provider.Name, r.Plan.Name)
if found {
r.Log.Info("The PV for OVA NFS exists", "PV", pvName)
return
}

labels := map[string]string{"provider": r.Plan.Provider.Source.Name, "app": "forklift", "migration": r.Migration.Name, "plan": r.Plan.Name}
pv := &core.PersistentVolume{
bkhizgiy marked this conversation as resolved.
Show resolved Hide resolved
ObjectMeta: meta.ObjectMeta{
Name: pvName,
Labels: labels,
},
Spec: core.PersistentVolumeSpec{
Capacity: core.ResourceList{
core.ResourceStorage: resource.MustParse("1Gi"),
},
AccessModes: []core.PersistentVolumeAccessMode{
core.ReadOnlyMany,
},
PersistentVolumeSource: core.PersistentVolumeSource{
NFS: &core.NFSVolumeSource{
Path: nfsPath,
Server: nfsServer,
},
},
},
}
err = r.Destination.Create(context.TODO(), pv)
if err != nil {
r.Log.Error(err, "Failed to create OVA plan PV")
return
}
return
}

func (r *KubeVirt) CreatePvcForNfs(pvcName string) (err error) {
_, found, err := GetOvaPvcNfs(r.Destination.Client, r.Plan.Name, r.Plan.Spec.TargetNamespace, r.Plan.Provider.Source.Name)
if err != nil {
r.Log.Error(err, "Failed to get ova PVC")
return
}
if found {
r.Log.Info("The PVC for OVA NFS exists", "PVC", pvcName)
return
}

sc := ""
pvName := getEntityName("pv", r.Source.Provider.Name, r.Plan.Name)
labels := map[string]string{"provider": r.Plan.Provider.Source.Name, "app": "forklift", "migration": r.Migration.Name, "plan": r.Plan.Name}
pvc := &core.PersistentVolumeClaim{
bkhizgiy marked this conversation as resolved.
Show resolved Hide resolved
ObjectMeta: meta.ObjectMeta{
Name: pvcName,
Namespace: r.Plan.Spec.TargetNamespace,
Labels: labels,
},
Spec: core.PersistentVolumeClaimSpec{
Resources: core.ResourceRequirements{
Requests: core.ResourceList{
core.ResourceStorage: resource.MustParse("1Gi"),
},
},
AccessModes: []core.PersistentVolumeAccessMode{
core.ReadOnlyMany,
},
VolumeName: pvName,
StorageClassName: &sc,
},
}
err = r.Destination.Create(context.TODO(), pvc)
if err != nil {
r.Log.Error(err, "Failed to create OVA plan PVC")
return
}

pvcNamespacedName := types.NamespacedName{
Namespace: r.Plan.Spec.TargetNamespace,
Name: pvcName,
}

if err = wait.PollUntilContextTimeout(context.TODO(), 5*time.Second, 45*time.Second, true, func(ctx context.Context) (done bool, err error) {
err = r.Get(context.TODO(), pvcNamespacedName, pvc)
if err != nil {
r.Log.Error(err, "Failed to get OVA plan PVC")
return false, err
}
return pvc.Status.Phase == "Bound", nil

}); err != nil {
r.Log.Error(err, "Failed to bind OVA PVC to PV ")
return

}
return nil
}

func getEntityName(resourceType, providerName, planName string) string {
return fmt.Sprintf("ova-store-%s-%s-%s", resourceType, providerName, planName)
}

// Ensure the PV exist on the destination.
func (r *KubeVirt) EnsurePersistentVolumeClaim(vmRef ref.Ref, persistentVolumeClaims []core.PersistentVolumeClaim) (err error) {
list, err := r.getPVCs(vmRef)
Expand Down
44 changes: 44 additions & 0 deletions pkg/controller/plan/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"
"time"

"github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1"
"github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1/plan"
"github.com/konveyor/forklift-controller/pkg/controller/plan/adapter"
plancontext "github.com/konveyor/forklift-controller/pkg/controller/plan/context"
Expand Down Expand Up @@ -336,6 +337,14 @@ func (r *Migration) Archive() {
return
}

if r.Plan.Provider.Source.Type() == v1beta1.Ova {
bkhizgiy marked this conversation as resolved.
Show resolved Hide resolved
err = r.deletePvcPvForOva()
if err != nil {
r.Log.Error(err, "Failed to clean up the PVC and PV for the OVA plan")
return
bkhizgiy marked this conversation as resolved.
Show resolved Hide resolved
}
}

for _, vm := range r.Plan.Status.Migration.VMs {
err = r.cleanup(vm)
if err != nil {
Expand Down Expand Up @@ -486,6 +495,41 @@ func (r *Migration) deleteImporterPods(vm *plan.VMStatus) (err error) {
return
}

func (r *Migration) deletePvcPvForOva() (err error) {
pvc, _, err := GetOvaPvcNfs(r.Destination.Client, r.Plan.Name, r.Plan.Spec.TargetNamespace, r.Plan.Provider.Source.Name)
if err != nil {
r.Log.Error(err, "Failed to get the plan PVC")
return
}
// The PVC was already deleted
if pvc == nil {
return
}

err = r.Destination.Client.Delete(context.TODO(), pvc)
if err != nil {
r.Log.Error(err, "Failed to delete the plan PVC")
return
}

pv, _, err := GetOvaPvNfs(r.Destination.Client, r.Plan.Name, r.Plan.Provider.Source.Name)
if err != nil {
r.Log.Error(err, "Failed to get the plan PV")
return
}
// The PV was already deleted
if pv == nil {
return
}

err = r.Destination.Client.Delete(context.TODO(), pv)
if err != nil {
r.Log.Error(err, "Failed to delete the plan PV")
return
}
return
}

// Best effort attempt to resolve canceled refs.
func (r *Migration) resolveCanceledRefs() {
for i := range r.Context.Migration.Spec.Cancel {
Expand Down
2 changes: 2 additions & 0 deletions pkg/controller/provider/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go_library(
name = "provider",
srcs = [
"controller.go",
"ova-setup.go",
"predicate.go",
"validation.go",
],
Expand All @@ -30,6 +31,7 @@ go_library(
"//vendor/k8s.io/api/apps/v1:apps",
"//vendor/k8s.io/api/core/v1:core",
"//vendor/k8s.io/apimachinery/pkg/api/errors",
"//vendor/k8s.io/apimachinery/pkg/api/resource",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:meta",
"//vendor/k8s.io/apimachinery/pkg/util/intstr",
"//vendor/k8s.io/apiserver/pkg/storage/names",
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/provider/container/ova/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (r *Client) Connect(provider *api.Provider) (err error) {
},
}

serverURL := fmt.Sprintf("http://ova-service-%s:8080", provider.Name)
serverURL := fmt.Sprintf("http://ova-service-%s.%s.svc.cluster.local:8080", provider.Name, provider.Namespace)
if serverURL == "" {
return
}
Expand Down
Loading
Loading