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 4a2f843
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 <namespace>/<name> or <name>, 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
Expand Down
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 := "my-network"
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 4a2f843

Please sign in to comment.