Skip to content

Commit

Permalink
ocp: use GetNetworkNameAndNamespace in builder.go
Browse files Browse the repository at this point in the history
Signed-off-by: Benny Zlotnik <[email protected]>
  • Loading branch information
bennyz authored and ahadas committed Sep 28, 2023
1 parent abe1a4c commit 0236256
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
3 changes: 2 additions & 1 deletion pkg/controller/plan/adapter/ocp/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"],
)
4 changes: 1 addition & 3 deletions pkg/controller/plan/adapter/ocp/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions pkg/controller/plan/adapter/ocp/util.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
15 changes: 1 addition & 14 deletions pkg/controller/plan/adapter/ocp/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(
Expand All @@ -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
}

0 comments on commit 0236256

Please sign in to comment.