diff --git a/pkg/controller/plan/adapter/ocp/BUILD.bazel b/pkg/controller/plan/adapter/ocp/BUILD.bazel index 32a0260a7..1e01c2479 100644 --- a/pkg/controller/plan/adapter/ocp/BUILD.bazel +++ b/pkg/controller/plan/adapter/ocp/BUILD.bazel @@ -1,4 +1,4 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_library") +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") go_library( name = "ocp", @@ -36,3 +36,10 @@ go_library( "//vendor/sigs.k8s.io/controller-runtime/pkg/client/config", ], ) + +go_test( + name = "ocp_test", + srcs = ["validator_test.go"], + embed = [":ocp"], + deps = ["//pkg/apis/forklift/v1beta1/ref"], +) diff --git a/pkg/controller/plan/adapter/ocp/validator.go b/pkg/controller/plan/adapter/ocp/validator.go index 0caf6e17b..265bd28a0 100644 --- a/pkg/controller/plan/adapter/ocp/validator.go +++ b/pkg/controller/plan/adapter/ocp/validator.go @@ -161,8 +161,8 @@ func (r *Validator) NetworksMapped(vmRef ref.Ref) (ok bool, err error) { return false, err } } else if net.Multus != nil { - namespace := strings.Split(net.Multus.NetworkName, "/")[0] - name := strings.Split(net.Multus.NetworkName, "/")[1] + namespace, name := getNetworkNameAndNamespace(net.Multus.NetworkName, &vmRef) + _, found := r.plan.Referenced.Map.Network.FindNetworkByNameAndNamespace(namespace, name) if !found { err = liberr.Wrap( @@ -178,3 +178,15 @@ 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 +} diff --git a/pkg/controller/plan/adapter/ocp/validator_test.go b/pkg/controller/plan/adapter/ocp/validator_test.go new file mode 100644 index 000000000..26c523f9b --- /dev/null +++ b/pkg/controller/plan/adapter/ocp/validator_test.go @@ -0,0 +1,41 @@ +package ocp + +import ( + "testing" + + "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1/ref" +) + +func TestGetNetworkNameAndNamespace(t *testing.T) { + tests := []struct { + name string + networkName string + vmRef *ref.Ref + expectedName string + expectedNS string + }{ + { + name: "no slash in network name", + networkName: "network", + vmRef: &ref.Ref{Namespace: "vmNamespace"}, + expectedName: "network", + expectedNS: "vmNamespace", + }, + { + name: "slash in network name", + networkName: "namespace/network", + vmRef: &ref.Ref{Namespace: "vmNamespace"}, + expectedName: "network", + expectedNS: "namespace", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + 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) + } + }) + } +}