Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing stacktrace on wrong brief link format #1691

Merged
merged 5 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions links/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,19 +307,29 @@ type Link interface {
GetMTU() int
}

func extractHostNodeInterfaceData(lb *LinkBriefRaw, specialEPIndex int) (host, hostIf, node, nodeIf string) {
func extractHostNodeInterfaceData(lb *LinkBriefRaw, specialEPIndex int) (host, hostIf, node, nodeIf string, err error) {
// the index of the node is the specialEndpointIndex +1 modulo 2
nodeindex := (specialEPIndex + 1) % 2

hostData := strings.SplitN(lb.Endpoints[specialEPIndex], ":", 2)
nodeData := strings.SplitN(lb.Endpoints[nodeindex], ":", 2)

if len(hostData) != 2 {
return "", "", "", "",
fmt.Errorf("invalid link endpoint format. expected <node>:<port>, got %s", lb.Endpoints[specialEPIndex])
}

if len(nodeData) != 2 {
return "", "", "", "",
fmt.Errorf("invalid link endpoint format. expected <node>:<port>, got %s", lb.Endpoints[nodeindex])
}

host = hostData[0]
hostIf = hostData[1]
node = nodeData[0]
nodeIf = nodeData[1]

return host, hostIf, node, nodeIf
return host, hostIf, node, nodeIf, nil
}

func genRandomIfName() string {
Expand Down
6 changes: 4 additions & 2 deletions links/link_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ func (r *LinkHostRaw) ToLinkBriefRaw() *LinkBriefRaw {
}

func hostLinkFromBrief(lb *LinkBriefRaw, specialEPIndex int) (*LinkHostRaw, error) {
_, hostIf, node, nodeIf := extractHostNodeInterfaceData(lb, specialEPIndex)

_, hostIf, node, nodeIf, err := extractHostNodeInterfaceData(lb, specialEPIndex)
if err != nil {
return nil, err
}
link := &LinkHostRaw{
LinkCommonParams: lb.LinkCommonParams,
HostInterface: hostIf,
Expand Down
6 changes: 4 additions & 2 deletions links/link_macvlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ func (*LinkMacVlanRaw) GetType() LinkType {
}

func macVlanLinkFromBrief(lb *LinkBriefRaw, specialEPIndex int) (*LinkMacVlanRaw, error) {
_, hostIf, node, nodeIf := extractHostNodeInterfaceData(lb, specialEPIndex)

_, hostIf, node, nodeIf, err := extractHostNodeInterfaceData(lb, specialEPIndex)
if err != nil {
return nil, err
}
link := &LinkMacVlanRaw{
LinkCommonParams: lb.LinkCommonParams,
HostInterface: hostIf,
Expand Down
5 changes: 4 additions & 1 deletion links/link_mgmt-net.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ func (*LinkMgmtNetRaw) GetType() LinkType {
}

func mgmtNetLinkFromBrief(lb *LinkBriefRaw, specialEPIndex int) (*LinkMgmtNetRaw, error) {
_, hostIf, node, nodeIf := extractHostNodeInterfaceData(lb, specialEPIndex)
_, hostIf, node, nodeIf, err := extractHostNodeInterfaceData(lb, specialEPIndex)
if err != nil {
return nil, err
}

link := &LinkMgmtNetRaw{
LinkCommonParams: lb.LinkCommonParams,
Expand Down
111 changes: 111 additions & 0 deletions links/link_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,114 @@ func TestUnmarshalRawLinksYaml(t *testing.T) {
})
}
}

func Test_extractHostNodeInterfaceData(t *testing.T) {
type args struct {
lb *LinkBriefRaw
specialEPIndex int
}
tests := []struct {
name string
args args
wantHost string
wantHostIf string
wantNode string
wantNodeIf string
wantErr bool
}{
{
name: "Valid input",
args: args{
lb: &LinkBriefRaw{
Endpoints: []string{
"node1:eth1",
"node2:eth2",
},
},
specialEPIndex: 0,
},
wantHost: "node1",
wantHostIf: "eth1",
wantNode: "node2",
wantNodeIf: "eth2",
wantErr: false,
},
{
name: "Valid input other specialindex",
args: args{
lb: &LinkBriefRaw{
Endpoints: []string{
"node1:eth1",
"node2:eth2",
},
},
specialEPIndex: 1,
},
wantHost: "node2",
wantHostIf: "eth2",
wantNode: "node1",
wantNodeIf: "eth1",
wantErr: false,
},
{
name: "Invalid specialNode",
args: args{
lb: &LinkBriefRaw{
Endpoints: []string{
"node1:eth1",
"node2eth2",
},
},
specialEPIndex: 1,
},
wantErr: true,
},
{
name: "Invalid Node",
args: args{
lb: &LinkBriefRaw{
Endpoints: []string{
"node1eth1",
"node2:eth2",
},
},
specialEPIndex: 1,
},
wantErr: true,
},
{
name: "Invalid Node too many colons",
args: args{
lb: &LinkBriefRaw{
Endpoints: []string{
"node1eth1",
"node2:et:h2",
},
},
specialEPIndex: 1,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotHost, gotHostIf, gotNode, gotNodeIf, err := extractHostNodeInterfaceData(tt.args.lb, tt.args.specialEPIndex)
if (err != nil) != tt.wantErr {
t.Errorf("extractHostNodeInterfaceData() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotHost != tt.wantHost {
t.Errorf("extractHostNodeInterfaceData() gotHost = %v, want %v", gotHost, tt.wantHost)
}
if gotHostIf != tt.wantHostIf {
t.Errorf("extractHostNodeInterfaceData() gotHostIf = %v, want %v", gotHostIf, tt.wantHostIf)
}
if gotNode != tt.wantNode {
t.Errorf("extractHostNodeInterfaceData() gotNode = %v, want %v", gotNode, tt.wantNode)
}
if gotNodeIf != tt.wantNodeIf {
t.Errorf("extractHostNodeInterfaceData() gotNodeIf = %v, want %v", gotNodeIf, tt.wantNodeIf)
}
})
}
}
5 changes: 4 additions & 1 deletion links/link_veth.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ func (r *LinkVEthRaw) Resolve(params *ResolveParams) (Link, error) {

// linkVEthRawFromLinkBriefRaw creates a raw veth link from a LinkBriefRaw.
func linkVEthRawFromLinkBriefRaw(lb *LinkBriefRaw) (*LinkVEthRaw, error) {
host, hostIf, node, nodeIf := extractHostNodeInterfaceData(lb, 0)
host, hostIf, node, nodeIf, err := extractHostNodeInterfaceData(lb, 0)
if err != nil {
return nil, err
}

link := &LinkVEthRaw{
LinkCommonParams: lb.LinkCommonParams,
Expand Down