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: generate random names for NFS pv and PVS used for migration #757

Merged
merged 3 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
97 changes: 44 additions & 53 deletions pkg/controller/plan/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -1604,12 +1604,14 @@

switch r.Source.Provider.Type() {
case api.Ova:
err = r.CreatePvForNfs()
var pvName string
pvName, err = r.CreatePvForNfs()

Check warning on line 1608 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L1607-L1608

Added lines #L1607 - L1608 were not covered by tests
if err != nil {
return
}
pvcName := getEntityName("pvc", r.Source.Provider.Name, r.Plan.Name)
err = r.CreatePvcForNfs(pvcName)
pvcNamePrefix := getEntityPrefixName("pvc", r.Source.Provider.Name, r.Plan.Name)
var pvcName string
pvcName, err = r.CreatePvcForNfs(pvcNamePrefix, pvName)

Check warning on line 1614 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L1612-L1614

Added lines #L1612 - L1614 were not covered by tests
if err != nil {
return
}
Expand Down Expand Up @@ -2102,16 +2104,20 @@
return
}

func GetOvaPvNfs(client client.Client, planName string, providerName string) (pv *core.PersistentVolume, found bool, err error) {
pv = &core.PersistentVolume{}
err = client.Get(
func GetOvaPvNfs(dClient client.Client, planID string) (pvs *core.PersistentVolumeList, found bool, err error) {
bkhizgiy marked this conversation as resolved.
Show resolved Hide resolved
pvs = &core.PersistentVolumeList{}
pvLabels := map[string]string{
"plan": planID,
"ova": "nfs-pv",
}

err = dClient.List(

Check warning on line 2114 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2107-L2114

Added lines #L2107 - L2114 were not covered by tests
context.TODO(),
types.NamespacedName{
Name: getEntityName("pv", providerName, planName),
pvs,
&client.ListOptions{
LabelSelector: labels.SelectorFromSet(pvLabels),

Check warning on line 2118 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2116-L2118

Added lines #L2116 - L2118 were not covered by tests
},
pv,
)

if err != nil {
if k8serr.IsNotFound(err) {
return nil, false, nil
Expand All @@ -2122,17 +2128,21 @@
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(
func GetOvaPvcNfs(dClient client.Client, planID string, planNamespace string) (pvcs *core.PersistentVolumeClaimList, found bool, err error) {
pvcs = &core.PersistentVolumeClaimList{}
pvcLabels := map[string]string{
"plan": planID,
"ova": "nfs-pvc",
}

err = dClient.List(

Check warning on line 2138 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2131-L2138

Added lines #L2131 - L2138 were not covered by tests
context.TODO(),
types.NamespacedName{
Name: getEntityName("pvc", providerName, planName),
Namespace: planNamespace,
pvcs,
&client.ListOptions{
LabelSelector: labels.SelectorFromSet(pvcLabels),
Namespace: planNamespace,

Check warning on line 2143 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2140-L2143

Added lines #L2140 - L2143 were not covered by tests
},
pvc,
)

if err != nil {
if k8serr.IsNotFound(err) {
return nil, false, nil
Expand All @@ -2143,28 +2153,18 @@
return
}

func (r *KubeVirt) CreatePvForNfs() (err error) {
func (r *KubeVirt) CreatePvForNfs() (pvName string, err error) {

Check warning on line 2156 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2156

Added line #L2156 was not covered by tests
sourceProvider := r.Source.Provider
splitted := strings.Split(sourceProvider.Spec.URL, ":")
nfsServer := splitted[0]
nfsPath := splitted[1]
pvcNamePrefix := getEntityPrefixName("pv", r.Source.Provider.Name, r.Plan.Name)

Check warning on line 2161 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2161

Added line #L2161 was not covered by tests

_, 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}
labels := map[string]string{"provider": r.Plan.Provider.Source.Name, "app": "forklift", "migration": r.Migration.Name, "plan": string(r.Plan.UID), "ova": "nfs-pv"}

Check warning on line 2163 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2163

Added line #L2163 was not covered by tests
pv := &core.PersistentVolume{
ObjectMeta: meta.ObjectMeta{
Name: pvName,
Labels: labels,
GenerateName: pvcNamePrefix,
Labels: labels,

Check warning on line 2167 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2166-L2167

Added lines #L2166 - L2167 were not covered by tests
},
Spec: core.PersistentVolumeSpec{
Capacity: core.ResourceList{
Expand All @@ -2186,28 +2186,18 @@
r.Log.Error(err, "Failed to create OVA plan PV")
return
}
pvName = pv.Name

Check warning on line 2189 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2189

Added line #L2189 was not covered by tests
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
}

func (r *KubeVirt) CreatePvcForNfs(pvcNamePrefix string, pvName string) (pvcName string, err error) {

Check warning on line 2193 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2193

Added line #L2193 was not covered by tests
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}
labels := map[string]string{"provider": r.Plan.Provider.Source.Name, "app": "forklift", "migration": r.Migration.Name, "plan": string(r.Plan.UID), "ova": "nfs-pvc"}

Check warning on line 2195 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2195

Added line #L2195 was not covered by tests
pvc := &core.PersistentVolumeClaim{
ObjectMeta: meta.ObjectMeta{
Name: pvcName,
Namespace: r.Plan.Spec.TargetNamespace,
Labels: labels,
GenerateName: pvcNamePrefix,
Copy link
Member

Choose a reason for hiding this comment

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

you can call getEntityPrefixName here directly I think

Namespace: r.Plan.Spec.TargetNamespace,
Labels: labels,

Check warning on line 2200 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2198-L2200

Added lines #L2198 - L2200 were not covered by tests
},
Spec: core.PersistentVolumeClaimSpec{
Resources: core.ResourceRequirements{
Expand All @@ -2230,7 +2220,7 @@

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

Check warning on line 2223 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2223

Added line #L2223 was not covered by tests
}

if err = wait.PollUntilContextTimeout(context.TODO(), 5*time.Second, 45*time.Second, true, func(ctx context.Context) (done bool, err error) {
Expand All @@ -2246,11 +2236,12 @@
return

}
return nil
pvcName = pvc.Name
return

Check warning on line 2240 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2239-L2240

Added lines #L2239 - L2240 were not covered by tests
}

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

Check warning on line 2244 in pkg/controller/plan/kubevirt.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/kubevirt.go#L2243-L2244

Added lines #L2243 - L2244 were not covered by tests
}

// Ensure the PV exist on the destination.
Expand Down
36 changes: 20 additions & 16 deletions pkg/controller/plan/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,36 +518,40 @@
}

func (r *Migration) deletePvcPvForOva() (err error) {
pvc, _, err := GetOvaPvcNfs(r.Destination.Client, r.Plan.Name, r.Plan.Spec.TargetNamespace, r.Plan.Provider.Source.Name)
pvcs, _, err := GetOvaPvcNfs(r.Destination.Client, r.Plan.Name, r.Plan.Spec.TargetNamespace)

Check warning on line 521 in pkg/controller/plan/migration.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/migration.go#L521

Added line #L521 was not covered by tests
if err != nil {
r.Log.Error(err, "Failed to get the plan PVC")
r.Log.Error(err, "Failed to get the plan PVCs")

Check warning on line 523 in pkg/controller/plan/migration.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/migration.go#L523

Added line #L523 was not covered by tests
return
}
// The PVC was already deleted
if pvc == nil {
// The PVCs was already deleted
if len(pvcs.Items) == 0 {

Check warning on line 527 in pkg/controller/plan/migration.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/migration.go#L527

Added line #L527 was not covered by tests
return
}

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

Check warning on line 536 in pkg/controller/plan/migration.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/migration.go#L531-L536

Added lines #L531 - L536 were not covered by tests
}

pv, _, err := GetOvaPvNfs(r.Destination.Client, r.Plan.Name, r.Plan.Provider.Source.Name)
pvs, _, err := GetOvaPvNfs(r.Destination.Client, string(r.Plan.UID))

Check warning on line 539 in pkg/controller/plan/migration.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/migration.go#L539

Added line #L539 was not covered by tests
if err != nil {
r.Log.Error(err, "Failed to get the plan PV")
r.Log.Error(err, "Failed to get the plan PVs")

Check warning on line 541 in pkg/controller/plan/migration.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/migration.go#L541

Added line #L541 was not covered by tests
return
}
// The PV was already deleted
if pv == nil {
// The PVs was already deleted
if len(pvs.Items) == 0 {

Check warning on line 545 in pkg/controller/plan/migration.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/migration.go#L545

Added line #L545 was not covered by tests
return
}

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

Check warning on line 554 in pkg/controller/plan/migration.go

View check run for this annotation

Codecov / codecov/patch

pkg/controller/plan/migration.go#L549-L554

Added lines #L549 - L554 were not covered by tests
}
return
}
Expand Down
Loading