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 committed Sep 28, 2023
1 parent 79fc21a commit 5f1cc2e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
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"

api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1"
liberr "github.com/konveyor/forklift-controller/pkg/lib/error"
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 5f1cc2e

Please sign in to comment.