diff --git a/pkg/controller/plan/adapter/ocp/BUILD.bazel b/pkg/controller/plan/adapter/ocp/BUILD.bazel index 1e01c2479..87b5f8180 100644 --- a/pkg/controller/plan/adapter/ocp/BUILD.bazel +++ b/pkg/controller/plan/adapter/ocp/BUILD.bazel @@ -7,6 +7,7 @@ go_library( "builder.go", "client.go", "destinationclient.go", + "util.go", "validator.go", ], importpath = "github.com/konveyor/forklift-controller/pkg/controller/plan/adapter/ocp", @@ -39,7 +40,7 @@ go_library( go_test( name = "ocp_test", - srcs = ["validator_test.go"], + srcs = ["util_test.go"], embed = [":ocp"], deps = ["//pkg/apis/forklift/v1beta1/ref"], ) diff --git a/pkg/controller/plan/adapter/ocp/builder.go b/pkg/controller/plan/adapter/ocp/builder.go index 74363b6c4..1c32d52de 100644 --- a/pkg/controller/plan/adapter/ocp/builder.go +++ b/pkg/controller/plan/adapter/ocp/builder.go @@ -7,7 +7,6 @@ import ( "fmt" "io" "net/http" - "strings" liberr "github.com/konveyor/forklift-controller/pkg/lib/error" libitr "github.com/konveyor/forklift-controller/pkg/lib/itinerary" @@ -473,8 +472,7 @@ func (r *Builder) mapNetworks(sourceVm *cnv.VirtualMachine, targetVmSpec *cnv.Vi switch { case network.Multus != nil: - namespace := strings.Split(network.Multus.NetworkName, "/")[0] - name := strings.Split(network.Multus.NetworkName, "/")[1] + name, namespace := 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) diff --git a/pkg/controller/plan/adapter/ocp/util.go b/pkg/controller/plan/adapter/ocp/util.go new file mode 100644 index 000000000..fed08ce1f --- /dev/null +++ b/pkg/controller/plan/adapter/ocp/util.go @@ -0,0 +1,19 @@ +package ocp + +import ( + "strings" + + "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1/ref" +) + +func GetNetworkNameAndNamespace(networkName string, vmRef *ref.Ref) (name, namespace string) { + if !strings.Contains(networkName, "/") { + namespace = vmRef.Namespace + name = networkName + } else { + splitName := strings.Split(networkName, "/") + namespace, name = splitName[0], splitName[1] + } + + return +} diff --git a/pkg/controller/plan/adapter/ocp/validator_test.go b/pkg/controller/plan/adapter/ocp/util_test.go similarity index 93% rename from pkg/controller/plan/adapter/ocp/validator_test.go rename to pkg/controller/plan/adapter/ocp/util_test.go index 26c523f9b..1b6aa739a 100644 --- a/pkg/controller/plan/adapter/ocp/validator_test.go +++ b/pkg/controller/plan/adapter/ocp/util_test.go @@ -32,7 +32,7 @@ func TestGetNetworkNameAndNamespace(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - actualName, actualNS := getNetworkNameAndNamespace(tt.networkName, &ref.Ref{Namespace: tt.vmRef.Namespace}) + actualName, actualNS := GetNetworkNameAndNamespace(tt.networkName, &ref.Ref{Namespace: tt.vmRef.Namespace}) if actualName != tt.expectedName || actualNS != tt.expectedNS { t.Errorf("got (%s, %s), want (%s, %s)", actualName, actualNS, tt.expectedName, tt.expectedNS) } diff --git a/pkg/controller/plan/adapter/ocp/validator.go b/pkg/controller/plan/adapter/ocp/validator.go index a535ad8f7..35fa09a1f 100644 --- a/pkg/controller/plan/adapter/ocp/validator.go +++ b/pkg/controller/plan/adapter/ocp/validator.go @@ -3,7 +3,6 @@ package ocp import ( "context" "fmt" - "strings" "github.com/go-logr/logr" api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1" @@ -163,7 +162,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 := GetNetworkNameAndNamespace(net.Multus.NetworkName, &vmRef) _, found := r.plan.Referenced.Map.Network.FindNetworkByNameAndNamespace(namespace, name) if !found { err = liberr.Wrap( @@ -180,15 +179,3 @@ func (r *Validator) NetworksMapped(vmRef ref.Ref) (ok bool, err error) { return true, nil } - -func getNetworkNameAndNamespace(networkName string, vmRef *ref.Ref) (name, namespace string) { - if !strings.Contains(networkName, "/") { - namespace = vmRef.Namespace - name = networkName - } else { - splitName := strings.Split(networkName, "/") - namespace, name = splitName[0], splitName[1] - } - - return -}