Skip to content

Commit

Permalink
honor all interfaces in the result to create nw status
Browse files Browse the repository at this point in the history
This patch is to update nw status with all relevant information
when pod container has secondary interface with its vlan sub
interfaces.

Signed-off-by: Periyasamy Palanisamy <[email protected]>
  • Loading branch information
pperiyasamy committed May 19, 2021
1 parent 66a699a commit f9b9e17
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
47 changes: 25 additions & 22 deletions pkg/utils/net-attach-def.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,43 +123,46 @@ func GetNetworkStatus(pod *corev1.Pod) ([]v1.NetworkStatus, error) {
}

// CreateNetworkStatus create NetworkStatus from CNI result
func CreateNetworkStatus(r cnitypes.Result, networkName string, defaultNetwork bool, dev *v1.DeviceInfo) (*v1.NetworkStatus, error) {
netStatus := &v1.NetworkStatus{}
netStatus.Name = networkName
netStatus.Default = defaultNetwork
func CreateNetworkStatus(r cnitypes.Result, networkName string, defaultNetwork bool, dev *v1.DeviceInfo) ([]*v1.NetworkStatus, error) {
netStatuses := make([]*v1.NetworkStatus, 0)

// Convert whatever the IPAM result was into the current Result type
result, err := current.NewResultFromResult(r)
if err != nil {
return netStatus, fmt.Errorf("error convert the type.Result to current.Result: %v", err)
return netStatuses, fmt.Errorf("error convert the type.Result to current.Result: %v", err)
}

for _, ifs := range result.Interfaces {
v1dns := convertDNS(result.DNS)
for ifIdx, ifs := range result.Interfaces {
netStatus := &v1.NetworkStatus{}
netStatus.Name = networkName
netStatus.Default = defaultNetwork
// Only pod interfaces can have sandbox information
if ifs.Sandbox != "" {
if ifs.Sandbox == "" {
netStatus.Interface = ifs.Name
netStatus.Mac = ifs.Mac
}
}

for _, ipconfig := range result.IPs {
if ipconfig.Version == "4" && ipconfig.Address.IP.To4() != nil {
netStatus.IPs = append(netStatus.IPs, ipconfig.Address.IP.String())
}
for _, ipconfig := range result.IPs {
ipIfPtr := ipconfig.Interface
if ipIfPtr == nil || *ipIfPtr == ifIdx {
if ipconfig.Version == "4" && ipconfig.Address.IP.To4() != nil {
netStatus.IPs = append(netStatus.IPs, ipconfig.Address.IP.String())
}

if ipconfig.Version == "6" && ipconfig.Address.IP.To16() != nil {
netStatus.IPs = append(netStatus.IPs, ipconfig.Address.IP.String())
if ipconfig.Version == "6" && ipconfig.Address.IP.To16() != nil {
netStatus.IPs = append(netStatus.IPs, ipconfig.Address.IP.String())
}
}
}
netStatus.DNS = *v1dns
if dev != nil {
netStatus.DeviceInfo = dev
}
netStatuses = append(netStatuses, netStatus)
}

v1dns := convertDNS(result.DNS)
netStatus.DNS = *v1dns

if dev != nil {
netStatus.DeviceInfo = dev
}

return netStatus, nil
return netStatuses, nil
}

// ParsePodNetworkAnnotation parses Pod annotation for net-attach-def and get NetworkSelectionElement
Expand Down
16 changes: 8 additions & 8 deletions pkg/utils/net-attach-def_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,14 @@ var _ = Describe("Netwok Attachment Definition manipulations", func() {
}
status, err := CreateNetworkStatus(cniResult, "test-net-attach-def", false, &devInfo)
Expect(err).NotTo(HaveOccurred())
Expect(status.Name).To(Equal("test-net-attach-def"))
Expect(status.Interface).To(Equal("net1"))
Expect(status.Mac).To(Equal("92:79:27:01:7c:cf"))
Expect(status.IPs).To(Equal([]string{"1.1.1.3", "2001::1"}))
Expect(status.DeviceInfo.Type).To(Equal("pci"))
Expect(status.DeviceInfo.Version).To(Equal("v1.0.0"))
Expect(status.DeviceInfo.Pci.PciAddress).To(Equal("0000:01:02.2"))
Expect(status.DeviceInfo.Pci.PfPciAddress).To(Equal("0000:01:02.0"))
Expect(status[0].Name).To(Equal("test-net-attach-def"))
Expect(status[0].Interface).To(Equal("net1"))
Expect(status[0].Mac).To(Equal("92:79:27:01:7c:cf"))
Expect(status[0].IPs).To(Equal([]string{"1.1.1.3", "2001::1"}))
Expect(status[0].DeviceInfo.Type).To(Equal("pci"))
Expect(status[0].DeviceInfo.Version).To(Equal("v1.0.0"))
Expect(status[0].DeviceInfo.Pci.PciAddress).To(Equal("0000:01:02.2"))
Expect(status[0].DeviceInfo.Pci.PfPciAddress).To(Equal("0000:01:02.0"))
})

It("parse network selection element in pod", func() {
Expand Down

0 comments on commit f9b9e17

Please sign in to comment.