Skip to content

Commit

Permalink
ocp: consider missing namespace in network name
Browse files Browse the repository at this point in the history
The namespace may be missing from the network name, in this case we need
to use the VM's namespace

Also contains:

ocp: extract to function and add unit test

Signed-off-by: Benny Zlotnik <bzlotnik@redhat.com>
  • Loading branch information
bennyz committed Sep 19, 2023
1 parent b1c4297 commit f6872d5
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
9 changes: 8 additions & 1 deletion 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")
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "ocp",
Expand Down Expand Up @@ -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"],
)
16 changes: 14 additions & 2 deletions pkg/controller/plan/adapter/ocp/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
}
41 changes: 41 additions & 0 deletions pkg/controller/plan/adapter/ocp/validator_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit f6872d5

Please sign in to comment.