Skip to content

Commit

Permalink
Parse defaultTransferNetwork anotation whith namespace
Browse files Browse the repository at this point in the history
Signed-off-by: yaacov <[email protected]>
  • Loading branch information
yaacov committed Jul 7, 2024
1 parent a8aeb93 commit d74883b
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 4 deletions.
3 changes: 2 additions & 1 deletion pkg/controller/plan/adapter/ocp/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1/ref"
planbase "github.com/konveyor/forklift-controller/pkg/controller/plan/adapter/base"
plancontext "github.com/konveyor/forklift-controller/pkg/controller/plan/context"
ocpclient "github.com/konveyor/forklift-controller/pkg/lib/client/openshift"
core "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -477,7 +478,7 @@ func (r *Builder) mapNetworks(sourceVm *cnv.VirtualMachine, targetVmSpec *cnv.Vi

switch {
case network.Multus != nil:
name, namespace := GetNetworkNameAndNamespace(network.Multus.NetworkName, &ref.Ref{Name: sourceVm.Name, Namespace: sourceVm.Namespace})
name, namespace := ocpclient.GetNetworkNameAndNamespace(network.Multus.NetworkName, &ref.Ref{Name: sourceVm.Name, Namespace: sourceVm.Namespace})
pair, found := r.Map.Network.FindNetworkByNameAndNamespace(namespace, name)
if !found {
r.Log.Info("Network not found", "namespace", namespace, "name", name)
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/plan/adapter/ocp/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1/ref"
"github.com/konveyor/forklift-controller/pkg/controller/provider/web"
ocpclient "github.com/konveyor/forklift-controller/pkg/lib/client/openshift"
core "k8s.io/api/core/v1"
cnv "kubevirt.io/api/core/v1"
k8sclient "sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -164,7 +165,7 @@ func (r *Validator) NetworksMapped(vmRef ref.Ref) (ok bool, err error) {
return false, err
}
} else if net.Multus != nil {
name, namespace := GetNetworkNameAndNamespace(net.Multus.NetworkName, &vmRef)
name, namespace := ocpclient.GetNetworkNameAndNamespace(net.Multus.NetworkName, &vmRef)
_, found := r.plan.Referenced.Map.Network.FindNetworkByNameAndNamespace(namespace, name)
if !found {
err = liberr.Wrap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (

net "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1"
api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1"
"github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1/ref"
"github.com/konveyor/forklift-controller/pkg/forklift-api/webhooks/util"
ocp "github.com/konveyor/forklift-controller/pkg/lib/client/openshift"
ocpclient "github.com/konveyor/forklift-controller/pkg/lib/client/openshift"
admissionv1 "k8s.io/api/admission/v1beta1"
core "k8s.io/api/core/v1"
k8serr "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -78,9 +80,10 @@ func (mutator *PlanMutator) setTransferNetworkIfNotSet() (bool, error) {
}

if network := targetProvider.Annotations["forklift.konveyor.io/defaultTransferNetwork"]; network != "" {
name, namespace := ocpclient.GetNetworkNameAndNamespace(network, &ref.Ref{Name: network, Namespace: mutator.plan.Spec.TargetNamespace})
key := client.ObjectKey{
Namespace: mutator.plan.Spec.TargetNamespace,
Name: network,
Namespace: namespace,
Name: name,
}

var tcl client.Client // target client, i.e., client to a possibly remote cluster
Expand Down
File renamed without changes.
File renamed without changes.
69 changes: 69 additions & 0 deletions tests/suit/forklift_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,73 @@ var _ = Describe("Forklift", func() {
Expect(plan.Annotations[AnnPopulatorLabels]).To(Equal("True"))
})
})

Context("Plan with provider that is set with transfer network that include namespace", func() {
providerNetworkNamespace := "default"
providerNetworkName := "my-network"
providerNetwork := providerNetworkNamespace + "/" + providerNetworkName

var provider *forkliftv1.Provider
var targetNS *v1.Namespace
var err error

BeforeEach(func() {
targetNS, err = f.CreateNamespace("target", map[string]string{})
Expect(err).ToNot(HaveOccurred())
By("Create target Openshift provider")
annotations := map[string]string{"forklift.konveyor.io/defaultTransferNetwork": providerNetwork}
target := utils.NewProvider(utils.TargetProviderName, forkliftv1.OpenShift, namespace, annotations, map[string]string{}, "", nil)
err = utils.CreateProviderFromDefinition(f.CrClient, target)
Expect(err).ToNot(HaveOccurred())
_, err = utils.WaitForProviderReadyWithTimeout(f.CrClient, namespace, utils.TargetProviderName, 30*time.Second)
Expect(err).ToNot(HaveOccurred())
By("Create oVirt provider")
pr := utils.NewProvider(ovirtProviderName, forkliftv1.OVirt, namespace, map[string]string{}, map[string]string{}, f.OvirtClient.OvirtURL, secret)
err = utils.CreateProviderFromDefinition(f.CrClient, pr)
Expect(err).ToNot(HaveOccurred())
provider, err = utils.WaitForProviderReadyWithTimeout(f.CrClient, namespace, ovirtProviderName, 30*time.Second)
Expect(err).ToNot(HaveOccurred())
By("Create Network Map")
networkMapDef := utils.NewNetworkMap(namespace, *provider, networkMapName, vmData.GetVMNics()[0])
err = utils.CreateNetworkMapFromDefinition(f.CrClient, networkMapDef)
Expect(err).ToNot(HaveOccurred())
err = utils.WaitForNetworkMapReadyWithTimeout(f.CrClient, namespace, networkMapName, 30*time.Second)
Expect(err).ToNot(HaveOccurred())
By("Create Storage Map")
storageMapDef := utils.NewStorageMap(namespace, *provider, test_storage_map_name, vmData.GetVMSDs(), ovirtStorageClass)
err = utils.CreateStorageMapFromDefinition(f.CrClient, storageMapDef)
Expect(err).ToNot(HaveOccurred())
err = utils.WaitForStorageMapReadyWithTimeout(f.CrClient, namespace, test_storage_map_name, 30*time.Second)
Expect(err).ToNot(HaveOccurred())
})

It("Transfer network should not be set to that of the Provider when it doesn't exist", func() {
By("Create plan")
planDef := utils.NewPlanWithVmId(*provider, namespace, test_plan_name, test_storage_map_name, networkMapName, targetNS.Name, []string{vmData.GetTestVMId()})
err = utils.CreatePlanFromDefinition(f.CrClient, planDef)
Expect(err).ToNot(HaveOccurred())
err, plan := utils.WaitForPlanReadyWithTimeout(f.CrClient, namespace, planDef.Name, 15*time.Second)
Expect(err).ToNot(HaveOccurred())
By("Verify created plan")
Expect(plan).ToNot(BeNil())
Expect(plan.Spec.TransferNetwork).To(BeNil())
})

It("Transfer network should be set to that of the Provider when it exists", func() {
By("Create Network Attachment Definition (namespaced)")
err, _ = utils.CreateNetworkAttachmentDefinition(f.CrClient, providerNetworkName, providerNetworkNamespace)
Expect(err).ToNot(HaveOccurred())
By("Create plan")
planDef := utils.NewPlanWithVmId(*provider, namespace, test_plan_name, test_storage_map_name, networkMapName, targetNS.Name, []string{vmData.GetTestVMId()})
err = utils.CreatePlanFromDefinition(f.CrClient, planDef)
Expect(err).ToNot(HaveOccurred())
err, plan := utils.WaitForPlanReadyWithTimeout(f.CrClient, namespace, planDef.Name, 15*time.Second)
Expect(err).ToNot(HaveOccurred())
By("Verify created plan")
Expect(plan).ToNot(BeNil())
Expect(plan.Spec.TransferNetwork).ToNot(BeNil())
Expect(plan.Spec.TransferNetwork.Name).To(Equal(providerNetworkName))
Expect(plan.Spec.TransferNetwork.Namespace).To(Equal(providerNetworkNamespace))
})
})
})

0 comments on commit d74883b

Please sign in to comment.