Skip to content

Commit

Permalink
Fix panics in agent when setting up VLAN with netplan (#430)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaitanyaKulkarni28 authored Sep 21, 2024
1 parent 7c5874f commit 90761e1
Show file tree
Hide file tree
Showing 5 changed files with 482 additions and 25 deletions.
37 changes: 37 additions & 0 deletions google_guest_agent/network/manager/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,33 @@ func interfaceNames(nics []metadata.NetworkInterfaces) ([]string, error) {
return ifaces, nil
}

// vlanInterfaceParentMap gets a map of VLAN IDs and its parent NIC.
func vlanInterfaceParentMap(nics map[int]metadata.VlanInterface, allEthernetInterfaces []string) (map[int]string, error) {
vlans := make(map[int]string)

for _, ni := range nics {
parentInterface, err := vlanParentInterface(allEthernetInterfaces, ni)
if err != nil {
return nil, fmt.Errorf("failed to determine vlan's parent interface: %+v", err)
}
vlans[ni.Vlan] = parentInterface
}

return vlans, nil
}

// vlanInterfaceListsIpv6 gets a list of VLAN IDs that support IPv6.
func vlanInterfaceListsIpv6(nics map[int]metadata.VlanInterface) []int {
var googleIpv6Interfaces []int

for _, ni := range nics {
if ni.DHCPv6Refresh != "" {
googleIpv6Interfaces = append(googleIpv6Interfaces, ni.Vlan)
}
}
return googleIpv6Interfaces
}

// interfaceListsIpv4Ipv6 gets a list of interface names. The first list is a list of all
// interfaces, and the second list consists of only interfaces that support IPv6.
func interfaceListsIpv4Ipv6(nics []metadata.NetworkInterfaces) ([]string, []string) {
Expand Down Expand Up @@ -186,3 +213,13 @@ func writeYamlFile(filePath string, ptr any) error {
}
return nil
}

// readYamlFile reads and parses the content of filePath and loads it into ptr.
func readYamlFile(filepath string, ptr any) error {
bytes, err := os.ReadFile(filepath)
if err != nil {
return fmt.Errorf("unable to read %q: %w", filepath, err)
}

return yaml.Unmarshal(bytes, ptr)
}
61 changes: 61 additions & 0 deletions google_guest_agent/network/manager/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ package manager

import (
"fmt"
"sort"
"testing"

"github.com/GoogleCloudPlatform/guest-agent/metadata"
"github.com/google/go-cmp/cmp"
)

func TestVlanParentInterfaceSuccess(t *testing.T) {
Expand Down Expand Up @@ -75,3 +77,62 @@ func TestVlanParentInterfaceFailure(t *testing.T) {
})
}
}

func TestVlanInterfaceListsIpv6(t *testing.T) {
nics := map[int]metadata.VlanInterface{
0: {Vlan: 4, DHCPv6Refresh: "123456"},
1: {Vlan: 5},
2: {Vlan: 6, MTU: 1234},
3: {Vlan: 7, Mac: "acd", ParentInterface: "/parent/0", DHCPv6Refresh: "7890"},
}
want := []int{4, 7}
got := vlanInterfaceListsIpv6(nics)
sort.Ints(got)

if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("vlanInterfaceListsIpv6(%+v) returned unexpected diff (-want,+got)\n%s", nics, diff)
}
}

func TestVlanInterfaceParentMap(t *testing.T) {
tests := []struct {
name string
nics map[int]metadata.VlanInterface
allEthernetInterfaces []string
wantErr bool
wantMap map[int]string
}{
{
name: "all_valid_nics",
allEthernetInterfaces: []string{"ens3", "ens4"},
nics: map[int]metadata.VlanInterface{
4: {Vlan: 4, ParentInterface: "/computeMetadata/v1/instance/network-interfaces/0/"},
5: {Vlan: 5, ParentInterface: "/computeMetadata/v1/instance/network-interfaces/1/"},
},
wantMap: map[int]string{
4: "ens3",
5: "ens4",
},
},
{
name: "invalid_parent",
allEthernetInterfaces: []string{"ens3"},
nics: map[int]metadata.VlanInterface{
5: {Vlan: 5, ParentInterface: "/computeMetadata/v1/instance/network-interfaces/1/"},
},
wantErr: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := vlanInterfaceParentMap(test.nics, test.allEthernetInterfaces)
if (err != nil) != test.wantErr {
t.Fatalf("vlanInterfaceParentMap(%+v, %v) = error [%v], want error: %t", test.nics, test.allEthernetInterfaces, err, test.wantErr)
}
if diff := cmp.Diff(test.wantMap, got); diff != "" {
t.Errorf("vlanInterfaceParentMap(%+v, %v) returned unexpected diff (-want,+got)\n%s", test.nics, test.allEthernetInterfaces, diff)
}
})
}
}
Loading

0 comments on commit 90761e1

Please sign in to comment.