Skip to content

Commit

Permalink
Fixing the meaning of "container_prefix"
Browse files Browse the repository at this point in the history
Currently this API attribute means "exact name of a container" interface, but it was never the original intention.
It was just a temporary change when we have moved to the asynchronous provisioning method.
Even the name suggest this is just a prefix of the NICs :)
Functionally speaking this makes it hard to attach multiple NICs to the same DanmNet.

After this change implicit and explicit naming schemes will be aligned: in both cases we will postfix the interface names with the provisioning sequence number.
  • Loading branch information
Levovar committed Apr 16, 2019
1 parent 57911a5 commit e3b3c2f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
1 change: 1 addition & 0 deletions crd/apis/danm/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ type Interface struct {
Proutes6 map[string]string `json:"proutes6"`
DefaultIfaceName string
Device string `json:"Device,omitempty"`
SequenceId int
}

type IpamConfig struct {
Expand Down
7 changes: 4 additions & 3 deletions pkg/cnidel/cnidel.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"log"
"os"
"strconv"
"strings"
"path/filepath"
"github.com/containernetworking/cni/pkg/invoke"
Expand Down Expand Up @@ -259,9 +260,9 @@ func GetEnv(key, fallback string) string {
// CalculateIfaceName decides what should be the name of a container's interface.
// If a name is explicitly set in the related DanmNet API object, the NIC will be named accordingly.
// If a name is not explicitly set, then DANM will name the interface ethX where X=sequence number of the interface
func CalculateIfaceName(chosenName, defaultName string) string {
func CalculateIfaceName(chosenName, defaultName string, sequenceId int) string {
if chosenName != "" {
return chosenName
return chosenName + strconv.Itoa(sequenceId)
}
return defaultName
return defaultName + strconv.Itoa(sequenceId)
}
18 changes: 11 additions & 7 deletions pkg/metacni/metacni.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net"
"os"
"runtime"
"strconv"
"strings"
"encoding/json"
"github.com/satori/go.uuid"
Expand All @@ -30,14 +29,18 @@ import (
checkpoint_utils "github.com/intel/multus-cni/checkpoint"
)

var (
apiHost = os.Getenv("API_SERVERS")
const (
danmApiPath = "danm.k8s.io"
danmIfDefinitionSyntax = danmApiPath + "/interfaces"
v1Endpoint = "/api/v1/"
cniVersion = "0.3.1"
kubeConf string
defaultNetworkName = "default"
defaultIfName = "eth"
)

var (
apiHost = os.Getenv("API_SERVERS")
kubeConf string
)

type NetConf struct {
Expand Down Expand Up @@ -260,7 +263,8 @@ func setupNetworking(args *cniArgs) (*current.Result, error) {
if err != nil {
return cniRes, errors.New("failed to get DanmNet due to:" + err.Error())
}
nicParams.DefaultIfaceName = "eth" + strconv.Itoa(nicID)
nicParams.SequenceId = nicID
nicParams.DefaultIfaceName = defaultIfName
if isDelegationRequired {
if cnidel.IsDeviceNeeded(netInfo.Spec.NetworkType) {
if _, ok := allocatedDevices[netInfo.Spec.Options.DevicePool]; !ok {
Expand All @@ -285,7 +289,7 @@ func setupNetworking(args *cniArgs) (*current.Result, error) {

func createDelegatedInterface(syncher *syncher.Syncher, danmClient danmclientset.Interface, iface danmtypes.Interface, netInfo *danmtypes.DanmNet, args *cniArgs) {
epIfaceSpec := danmtypes.DanmEpIface {
Name: cnidel.CalculateIfaceName(netInfo.Spec.Options.Prefix, iface.DefaultIfaceName),
Name: cnidel.CalculateIfaceName(netInfo.Spec.Options.Prefix, iface.DefaultIfaceName, iface.SequenceId),
Address: iface.Ip,
AddressIPv6: iface.Ip6,
Proutes: iface.Proutes,
Expand Down Expand Up @@ -322,7 +326,7 @@ func createDanmInterface(syncher *syncher.Syncher, danmClient danmclientset.Inte
syncher.PushResult(iface.Network, errors.New("IP address reservation failed for network:" + netId + " with error:" + err.Error()), nil)
}
epSpec := danmtypes.DanmEpIface {
Name: cnidel.CalculateIfaceName(netInfo.Spec.Options.Prefix, iface.DefaultIfaceName),
Name: cnidel.CalculateIfaceName(netInfo.Spec.Options.Prefix, iface.DefaultIfaceName, iface.SequenceId),
Address: ip4,
AddressIPv6: ip6,
MacAddress: macAddr,
Expand Down
12 changes: 8 additions & 4 deletions test/uts/cnidel_test/cnidel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cnidel_test

import (
"os"
"strconv"
"strings"
"testing"
"io/ioutil"
Expand Down Expand Up @@ -254,12 +255,15 @@ func TestGetEnv(t *testing.T) {
func TestCalculateIfaceName(t *testing.T) {
testChosenName := "thechosenone"
testDefaultName := "notthechosenone"
ifaceName := cnidel.CalculateIfaceName(testChosenName, testDefaultName)
if ifaceName != testChosenName {
testSequenceId := 4
expChosenName := testChosenName+strconv.Itoa(testSequenceId)
expDefName := testDefaultName+strconv.Itoa(testSequenceId)
ifaceName := cnidel.CalculateIfaceName(testChosenName, testDefaultName, testSequenceId)
if ifaceName != expChosenName {
t.Errorf("Received value for explicitly set interface name: %s does not match with expected: %s", ifaceName, testChosenName)
}
defIfaceName := cnidel.CalculateIfaceName("", testDefaultName)
if defIfaceName != testDefaultName {
defIfaceName := cnidel.CalculateIfaceName("", testDefaultName, testSequenceId)
if defIfaceName != expDefName {
t.Errorf("Received value for default interface name: %s does not match with expected: %s", defIfaceName, testChosenName)
}
}
Expand Down

0 comments on commit e3b3c2f

Please sign in to comment.