Skip to content

Commit

Permalink
Ensure namespace when validating VDDK
Browse files Browse the repository at this point in the history
In order to validate the VDDK init image, we create a job on the
destination provider. However, that would fail when the target namespace
doesn't exist - it would be created only when the plan is triggered. So
now we ensure the target namespace exists also during the validation of
the VDDK init image.

Signed-off-by: Arik Hadas <[email protected]>
  • Loading branch information
ahadas committed Feb 18, 2024
1 parent 219bbdb commit bee581d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 21 deletions.
1 change: 1 addition & 0 deletions pkg/controller/plan/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ go_library(
"metrics.go",
"migration.go",
"predicate.go",
"util.go",
"validation.go",
"vm_name_handler.go",
],
Expand Down
25 changes: 8 additions & 17 deletions pkg/controller/plan/kubevirt.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,24 +174,15 @@ func (r *KubeVirt) ListVMs() ([]VirtualMachine, error) {
}

// Ensure the namespace exists on the destination.
func (r *KubeVirt) EnsureNamespace() (err error) {
ns := &core.Namespace{
ObjectMeta: meta.ObjectMeta{
Name: r.Plan.Spec.TargetNamespace,
},
}
err = r.Destination.Client.Create(context.TODO(), ns)
if err != nil {
if k8serr.IsAlreadyExists(err) {
err = nil
}
func (r *KubeVirt) EnsureNamespace() error {
err := ensureNamespace(r.Plan, r.Destination.Client)
if err == nil {
r.Log.Info(
"Created namespace.",
"import",
r.Plan.Spec.TargetNamespace)
}
r.Log.Info(
"Created namespace.",
"import",
ns.Name)

return
return err
}

// Get the importer pod for a PersistentVolumeClaim.
Expand Down
25 changes: 25 additions & 0 deletions pkg/controller/plan/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package plan

import (
"context"

api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1"
core "k8s.io/api/core/v1"
k8serr "k8s.io/apimachinery/pkg/api/errors"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// Ensure the namespace exists on the destination.
func ensureNamespace(plan *api.Plan, client client.Client) error {
ns := &core.Namespace{
ObjectMeta: meta.ObjectMeta{
Name: plan.Spec.TargetNamespace,
},
}
err := client.Create(context.TODO(), ns)
if err != nil && k8serr.IsAlreadyExists(err) {
err = nil
}
return err
}
27 changes: 23 additions & 4 deletions pkg/controller/plan/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -773,10 +773,18 @@ func (r *Reconciler) validateVddkImage(plan *api.Plan) (err error) {
}

func (r *Reconciler) ensureVddkImageValidationJob(plan *api.Plan, el9 bool, vddkImage string) (*batchv1.Job, error) {
jobLabels := getVddkImageValidationJobLabels(plan)
ctx, err := plancontext.New(r, plan, r.Log)
if err != nil {
return nil, err
}

if err = r.ensureNamespace(ctx); err != nil {
return nil, liberr.Wrap(err)
}

jobLabels := getVddkImageValidationJobLabels(ctx.Plan)
jobs := &batchv1.JobList{}
ctx, _ := plancontext.New(r, plan, r.Log)
err := ctx.Destination.Client.List(
err = ctx.Destination.Client.List(
context.TODO(),
jobs,
&client.ListOptions{
Expand All @@ -788,7 +796,7 @@ func (r *Reconciler) ensureVddkImageValidationJob(plan *api.Plan, el9 bool, vddk
case err != nil:
return nil, err
case len(jobs.Items) == 0:
job := createVddkCheckJob(plan, jobLabels, el9, vddkImage)
job := createVddkCheckJob(ctx.Plan, jobLabels, el9, vddkImage)
err = ctx.Destination.Client.Create(context.Background(), job, &client.CreateOptions{})
if err != nil {
return nil, err
Expand All @@ -799,6 +807,17 @@ func (r *Reconciler) ensureVddkImageValidationJob(plan *api.Plan, el9 bool, vddk
}
}

func (r *Reconciler) ensureNamespace(ctx *plancontext.Context) error {
err := ensureNamespace(ctx.Plan, ctx.Destination.Client)
if err == nil {
r.Log.Info(
"Created namespace.",
"import",
ctx.Plan.Spec.TargetNamespace)
}
return err
}

func getVddkImageValidationJobLabels(plan *api.Plan) map[string]string {
image := plan.Referenced.Provider.Source.Spec.Settings[api.VDDK]
sum := md5.Sum([]byte(image))
Expand Down

0 comments on commit bee581d

Please sign in to comment.