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 authored and ahadas committed Jul 7, 2024
1 parent a8aeb93 commit e0e7020
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 17 deletions.
10 changes: 1 addition & 9 deletions pkg/controller/plan/adapter/ocp/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "ocp",
Expand All @@ -7,7 +7,6 @@ go_library(
"builder.go",
"client.go",
"destinationclient.go",
"util.go",
"validator.go",
],
importpath = "github.com/konveyor/forklift-controller/pkg/controller/plan/adapter/ocp",
Expand Down Expand Up @@ -38,10 +37,3 @@ go_library(
"//vendor/sigs.k8s.io/controller-runtime/pkg/client/config",
],
)

go_test(
name = "ocp_test",
srcs = ["util_test.go"],
embed = [":ocp"],
deps = ["//pkg/apis/forklift/v1beta1/ref"],
)
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 @@ -11,6 +11,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//pkg/apis/forklift/v1beta1",
"//pkg/apis/forklift/v1beta1/ref",
"//pkg/forklift-api/webhooks/util",
"//pkg/lib/client/openshift",
"//pkg/lib/error",
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 Expand Up @@ -111,8 +114,8 @@ func (mutator *PlanMutator) setTransferNetworkIfNotSet() (bool, error) {
if err = tcl.Get(context.TODO(), key, netAttachDef); err == nil {
log.Info("Patching the plan's transfer network")
mutator.plan.Spec.TransferNetwork = &core.ObjectReference{
Name: network,
Namespace: mutator.plan.Spec.TargetNamespace,
Namespace: key.Namespace,
Name: key.Name,
}
planChanged = true
} else if !k8serr.IsNotFound(err) { // TODO: else if !NotFound ...
Expand Down
12 changes: 10 additions & 2 deletions pkg/lib/client/openshift/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "openshift",
srcs = ["client.go"],
srcs = [
"client.go",
"util.go",
],
importpath = "github.com/konveyor/forklift-controller/pkg/lib/client/openshift",
visibility = ["//visibility:public"],
deps = [
"//pkg/apis/forklift/v1beta1",
"//pkg/apis/forklift/v1beta1/ref",
"//pkg/lib/error",
"//vendor/k8s.io/api/core/v1:core",
"//vendor/k8s.io/client-go/kubernetes/scheme",
Expand All @@ -19,10 +23,14 @@ go_library(

go_test(
name = "openshift_test",
srcs = ["client_test.go"],
srcs = [
"client_test.go",
"util_test.go",
],
embed = [":openshift"],
deps = [
"//pkg/apis/forklift/v1beta1",
"//pkg/apis/forklift/v1beta1/ref",
"//vendor/k8s.io/api/core/v1:core",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:meta",
],
Expand Down
File renamed without changes.
File renamed without changes.
71 changes: 71 additions & 0 deletions tests/suit/forklift_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,75 @@ var _ = Describe("Forklift", func() {
Expect(plan.Annotations[AnnPopulatorLabels]).To(Equal("True"))
})
})

Context("Plan with provider that is set with transfer network that includes 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())
targetNS, err = f.CreateNamespace(providerNetworkNamespace, 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("Namespaced transfer network should not be set to that of the Provider when it doesn't exist", func() {
By("Create plan for provider with defaultTransferNetwork")
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("Namespaced transfer network should be set to that of the Provider when it exists", func() {
By("Create namespaced Network Attachment Definition")
err, _ = utils.CreateNetworkAttachmentDefinition(f.CrClient, providerNetworkName, providerNetworkNamespace)
Expect(err).ToNot(HaveOccurred())
By("Create plan for provider with defaultTransferNetwork")
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 for provider with defaultTransferNetwork")
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 e0e7020

Please sign in to comment.