From 44d776fed32ed539f4140b54753d30fa25e0e02c Mon Sep 17 00:00:00 2001 From: yaacov Date: Sun, 7 Jul 2024 10:08:01 +0300 Subject: [PATCH] Parse defaultTransferNetwork anotation whith namespace Signed-off-by: yaacov --- .../mutating-webhook/mutators/plan-mutator.go | 19 ++++- tests/suit/forklift_test.go | 69 +++++++++++++++++++ 2 files changed, 85 insertions(+), 3 deletions(-) diff --git a/pkg/forklift-api/webhooks/mutating-webhook/mutators/plan-mutator.go b/pkg/forklift-api/webhooks/mutating-webhook/mutators/plan-mutator.go index c8efd47e9..1d939c3cd 100644 --- a/pkg/forklift-api/webhooks/mutating-webhook/mutators/plan-mutator.go +++ b/pkg/forklift-api/webhooks/mutating-webhook/mutators/plan-mutator.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "net/http" + "strings" 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" @@ -78,9 +79,21 @@ func (mutator *PlanMutator) setTransferNetworkIfNotSet() (bool, error) { } if network := targetProvider.Annotations["forklift.konveyor.io/defaultTransferNetwork"]; network != "" { - key := client.ObjectKey{ - Namespace: mutator.plan.Spec.TargetNamespace, - Name: network, + var key client.ObjectKey + + // Netwoek key format / or , in case the network key does not + // include the namespace, use the plan target namespace. + if strings.Contains(network, "/") { + parts := strings.SplitN(network, "/", 2) + key = client.ObjectKey{ + Namespace: parts[0], + Name: parts[1], + } + } else { + key = client.ObjectKey{ + Namespace: mutator.plan.Spec.TargetNamespace, + Name: network, + } } var tcl client.Client // target client, i.e., client to a possibly remote cluster diff --git a/tests/suit/forklift_test.go b/tests/suit/forklift_test.go index 2f1c4efce..c685d0afb 100644 --- a/tests/suit/forklift_test.go +++ b/tests/suit/forklift_test.go @@ -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)) + }) + }) })