Skip to content

Commit

Permalink
Merge pull request #165 from sunya-ch/v1.2.0
Browse files Browse the repository at this point in the history
fix CNI log and host-device delete
  • Loading branch information
sunya-ch authored Dec 13, 2023
2 parents b5301ea + 0b90432 commit bb72c11
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
5 changes: 4 additions & 1 deletion cni/plugins/main/multi-nic/host-device.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/containernetworking/cni/pkg/types"
current "github.com/containernetworking/cni/pkg/types/100"
"github.com/containernetworking/plugins/pkg/utils"
)

const (
Expand Down Expand Up @@ -49,6 +50,7 @@ func loadHostDeviceConf(bytes []byte, ifName string, n *NetConf, ipConfigs []*cu
// interfaces are orderly assigned from interface set
for index, deviceID := range n.DeviceIDs {
if deviceID == "" {
utils.Logger.Debug(fmt.Sprintf("skip %d: no device ID", index))
continue
}
// add config
Expand All @@ -70,7 +72,8 @@ func loadHostDeviceConf(bytes []byte, ifName string, n *NetConf, ipConfigs []*cu
if n.IPAM.Type == HostDeviceIPAMType {
ipConfig := getHostIPConfig(index, n.Masters[index])
if ipConfig == nil {
continue
utils.Logger.Debug(fmt.Sprintf("skip %d: no host IP", index))
confBytes = replaceEmptyIPAM(confBytes)
}
confBytes = replaceMultiNicIPAM(confBytes, ipConfig)
} else if n.IsMultiNICIPAM {
Expand Down
6 changes: 4 additions & 2 deletions cni/plugins/main/multi-nic/multi-nic.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,10 @@ func cmdDel(args *skel.CmdArgs) error {
if n.IPAM.Type != "" {
injectedStdIn := injectMaster(args.StdinData, n.MasterNetAddrs, n.Masters, n.DeviceIDs)
if n.IPAM.Type != "multi-nic-ipam" {
err = ipam.ExecDel(n.IPAM.Type, injectedStdIn)
utils.Logger.Debug(fmt.Sprintf("Failed ipam.ExecDel %s: %v", err, string(injectedStdIn)))
if n.IPAM.Type != HostDeviceIPAMType {
err = ipam.ExecDel(n.IPAM.Type, injectedStdIn)
utils.Logger.Debug(fmt.Sprintf("Failed ipam.ExecDel %s: %v", err, string(injectedStdIn)))
}
} else {
r, err := ipam.ExecDelWithResult(n.IPAM.Type, injectedStdIn)
if err != nil {
Expand Down
12 changes: 9 additions & 3 deletions cni/plugins/main/multi-nic/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ package main

import (
"encoding/json"
"log"
"strings"

"fmt"

current "github.com/containernetworking/cni/pkg/types/100"
"github.com/containernetworking/plugins/pkg/utils"
"github.com/vishvananda/netlink"
)

Expand Down Expand Up @@ -68,6 +68,12 @@ func replaceMultiNicIPAM(singleNicConfBytes []byte, ipConfig *current.IPConfig)
return []byte(injectedStr)
}

func replaceEmptyIPAM(singleNicConfBytes []byte) []byte {
confStr := string(singleNicConfBytes)
injectedStr := strings.ReplaceAll(confStr, "\"ipam\":{}", "\"ipam\":{\"type\":\"static\",\"addresses\":[]}")
return []byte(injectedStr)
}

// injectMaster replaces original pool with selected master network addresses
func injectMaster(inData []byte, selectedNetAddrs []string, selectedMasters []string, selectedDeviceIDs []string) []byte {
var obj map[string]interface{}
Expand All @@ -83,12 +89,12 @@ func injectMaster(inData []byte, selectedNetAddrs []string, selectedMasters []st
func getHostIPConfig(index int, devName string) *current.IPConfig {
devLink, err := netlink.LinkByName(devName)
if err != nil {
log.Printf("cannot find link %s: %v", devName, err)
utils.Logger.Debug(fmt.Sprintf("cannot find link %s: %v", devName, err))
return nil
}
addrs, err := netlink.AddrList(devLink, netlink.FAMILY_V4)
if err != nil || len(addrs) == 0 {
log.Printf("cannot list address on %s: %v", devName, err)
utils.Logger.Debug(fmt.Sprintf("cannot list address on %s: %v", devName, err))
return nil
}
addr := addrs[0].IPNet
Expand Down

0 comments on commit bb72c11

Please sign in to comment.