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

[DRAFT] Status should ensure CNI Network Configuration and CNI binaries in the NetConf are present to set Network Ready #116

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:

- uses: golangci/golangci-lint-action@v3
with:
version: v1.51.1
version: v1.57.2
working-directory: src/github.com/containerd/go-cni

tests:
Expand Down
19 changes: 10 additions & 9 deletions cni_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func TestLibCNIType020(t *testing.T) {
// Create a fake cni config directory and file
cniDir, confDir := makeFakeCNIConfig(t)
defer tearDownCNIConfig(t, cniDir)
l.pluginDirs = []string{cniDir}
l.pluginConfDir = confDir
// Set the minimum network count as 2 for this test
l.networkCount = 2
Expand Down Expand Up @@ -99,7 +100,7 @@ func TestLibCNIType020(t *testing.T) {
assert.NotNil(t, c.Prefix)
assert.Equal(t, "eth", c.Prefix)
assert.NotNil(t, c.PluginDirs)
assert.Equal(t, DefaultCNIDir, c.PluginDirs[0])
assert.Equal(t, cniDir, c.PluginDirs[0])
assert.NotNil(t, c.PluginConfDir)
assert.Equal(t, confDir, c.PluginConfDir)
assert.NotNil(t, c.Networks)
Expand Down Expand Up @@ -289,32 +290,32 @@ type MockCNI struct {
mock.Mock
}

func (m *MockCNI) AddNetwork(ctx context.Context, net *cnilibrary.NetworkConfig, rt *cnilibrary.RuntimeConf) (types.Result, error) {
func (m *MockCNI) AddNetwork(_ context.Context, net *cnilibrary.NetworkConfig, rt *cnilibrary.RuntimeConf) (types.Result, error) {
args := m.Called(net, rt)
return args.Get(0).(types.Result), args.Error(1)
}

func (m *MockCNI) DelNetwork(ctx context.Context, net *cnilibrary.NetworkConfig, rt *cnilibrary.RuntimeConf) error {
func (m *MockCNI) DelNetwork(_ context.Context, net *cnilibrary.NetworkConfig, rt *cnilibrary.RuntimeConf) error {
args := m.Called(net, rt)
return args.Error(0)
}

func (m *MockCNI) DelNetworkList(ctx context.Context, net *cnilibrary.NetworkConfigList, rt *cnilibrary.RuntimeConf) error {
func (m *MockCNI) DelNetworkList(_ context.Context, net *cnilibrary.NetworkConfigList, rt *cnilibrary.RuntimeConf) error {
args := m.Called(net, rt)
return args.Error(0)
}

func (m *MockCNI) AddNetworkList(ctx context.Context, net *cnilibrary.NetworkConfigList, rt *cnilibrary.RuntimeConf) (types.Result, error) {
func (m *MockCNI) AddNetworkList(_ context.Context, net *cnilibrary.NetworkConfigList, rt *cnilibrary.RuntimeConf) (types.Result, error) {
args := m.Called(net, rt)
return args.Get(0).(types.Result), args.Error(1)
}

func (m *MockCNI) CheckNetworkList(ctx context.Context, net *cnilibrary.NetworkConfigList, rt *cnilibrary.RuntimeConf) error {
func (m *MockCNI) CheckNetworkList(_ context.Context, net *cnilibrary.NetworkConfigList, rt *cnilibrary.RuntimeConf) error {
args := m.Called(net, rt)
return args.Error(0)
}

func (m *MockCNI) CheckNetwork(ctx context.Context, net *cnilibrary.NetworkConfig, rt *cnilibrary.RuntimeConf) error {
func (m *MockCNI) CheckNetwork(_ context.Context, net *cnilibrary.NetworkConfig, rt *cnilibrary.RuntimeConf) error {
args := m.Called(net, rt)
return args.Error(0)
}
Expand All @@ -329,12 +330,12 @@ func (m *MockCNI) GetNetworkCachedResult(net *cnilibrary.NetworkConfig, rt *cnil
return args.Get(0).(types.Result), args.Error(1)
}

func (m *MockCNI) ValidateNetworkList(ctx context.Context, net *cnilibrary.NetworkConfigList) ([]string, error) {
func (m *MockCNI) ValidateNetworkList(_ context.Context, net *cnilibrary.NetworkConfigList) ([]string, error) {
args := m.Called(net)
return args.Get(0).([]string), args.Error(1)
}

func (m *MockCNI) ValidateNetwork(ctx context.Context, net *cnilibrary.NetworkConfig) ([]string, error) {
func (m *MockCNI) ValidateNetwork(_ context.Context, net *cnilibrary.NetworkConfig) ([]string, error) {
args := m.Called(net)
return args.Get(0).([]string), args.Error(1)
}
Expand Down
14 changes: 8 additions & 6 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ import (
)

var (
ErrCNINotInitialized = errors.New("cni plugin not initialized")
ErrInvalidConfig = errors.New("invalid cni config")
ErrNotFound = errors.New("not found")
ErrRead = errors.New("failed to read config file")
ErrInvalidResult = errors.New("invalid result")
ErrLoad = errors.New("failed to load cni config")
ErrCNINotInitialized = errors.New("cni plugin not initialized")
ErrInvalidConfig = errors.New("invalid cni config")
ErrNotFound = errors.New("not found")
ErrRead = errors.New("failed to read config file")
ErrInvalidResult = errors.New("invalid result")
ErrLoad = errors.New("failed to load cni config")
ErrCNIPluginNotFound = errors.New("cni plugin not found")
ErrCNIPluginDirNotFound = errors.New("cni plugin directory not found")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not used..

)

// IsCNINotInitialized returns true if the error is due to cni config not being initialized
Expand Down
76 changes: 76 additions & 0 deletions opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cni
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"

Expand Down Expand Up @@ -94,6 +95,12 @@ func WithLoNetwork(c *libcni) error {
}]
}`))

err := checkPluginExists(c, loConfig)

if err != nil {
return err
}

c.networks = append(c.networks, &Network{
cni: c.cniConfig,
config: loConfig,
Expand All @@ -120,6 +127,13 @@ func WithConfIndex(bytes []byte, index int) Opt {
if err != nil {
return err
}

err = checkPluginExists(c, confList)

if err != nil {
return err
}

c.networks = append(c.networks, &Network{
cni: c.cniConfig,
config: confList,
Expand All @@ -143,6 +157,13 @@ func WithConfFile(fileName string) Opt {
if err != nil {
return err
}

err = checkPluginExists(c, confList)

if err != nil {
return err
}

c.networks = append(c.networks, &Network{
cni: c.cniConfig,
config: confList,
Expand All @@ -160,6 +181,13 @@ func WithConfListBytes(bytes []byte) Opt {
if err != nil {
return err
}

err = checkPluginExists(c, confList)

if err != nil {
return err
}

i := len(c.networks)
c.networks = append(c.networks, &Network{
cni: c.cniConfig,
Expand All @@ -179,6 +207,13 @@ func WithConfListFile(fileName string) Opt {
if err != nil {
return err
}

err = checkPluginExists(c, confList)

if err != nil {
return err
}

i := len(c.networks)
c.networks = append(c.networks, &Network{
cni: c.cniConfig,
Expand Down Expand Up @@ -253,13 +288,20 @@ func loadFromConfDir(c *libcni, max int) error {
}
if len(confList.Plugins) == 0 {
return fmt.Errorf("CNI config list in config file %s has no networks, skipping: %w", confFile, ErrInvalidConfig)
}

err := checkPluginExists(c, confList)

if err != nil {
return err
}

networks = append(networks, &Network{
cni: c.cniConfig,
config: confList,
ifName: getIfName(c.prefix, i),
})

i++
if i == max {
break
Expand All @@ -271,3 +313,37 @@ func loadFromConfDir(c *libcni, max int) error {
c.networks = append(c.networks, networks...)
return nil
}

func checkPluginExists(c *libcni, confList *cnilibrary.NetworkConfigList) error {
missing := make(map[string]interface{})
for _, plug := range confList.Plugins {
plugin := plug.Network.Type
for _, dir := range c.pluginDirs {
if !fileExistsInDir(dir, plugin) {
missing[plugin] = plugin
} else {
delete(missing, plugin)
break
}
}
}

if len(missing) > 0 {
var plugins []string
for k := range missing {
plugins = append(plugins, k)
}

return fmt.Errorf("unable to find cni plugins %s in directories %s: %v",
strings.Join(plugins, ", "), strings.Join(c.pluginDirs, ", "), ErrCNIPluginNotFound)
}

return nil
}

// FileExistsInDir checks if a file exists in a specific directory
func fileExistsInDir(directory, filename string) bool {
filePath := filepath.Join(directory, filename)
_, err := os.Stat(filePath)
return !os.IsExist(err)
}
14 changes: 13 additions & 1 deletion testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ func makeFakeCNIConfig(t *testing.T) (string, string) {
t.Fatalf("Failed to create network config dir: %v", err)
}

cniPluginDir := path.Join(cniDir, "bin")
err = os.MkdirAll(cniPluginDir, 0777)
if err != nil {
t.Fatalf("Failed to create plugin dir: %v", err)
}

networkConfig1 := path.Join(cniConfDir, "mocknetwork1.conf")
f1, err := os.Create(networkConfig1)
if err != nil {
Expand All @@ -54,6 +60,12 @@ func makeFakeCNIConfig(t *testing.T) (string, string) {
t.Fatalf("Failed to create network config %v: %v", f2, err)
}

file, err := os.Create(path.Join(cniPluginDir, "fakecni"))
if err != nil {
t.Fatalf("Error creating file: %v", err)
}
defer file.Close()

cfg1 := fmt.Sprintf(`{ "name": "%s", "type": "%s", "capabilities": {"portMappings": true} }`, "plugin1", "fakecni")
_, err = f1.WriteString(cfg1)
if err != nil {
Expand All @@ -66,7 +78,7 @@ func makeFakeCNIConfig(t *testing.T) (string, string) {
t.Fatalf("Failed to write network config file %v: %v", f2, err)
}
f2.Close()
return cniDir, cniConfDir
return cniPluginDir, cniConfDir
}

func tearDownCNIConfig(t *testing.T, confDir string) {
Expand Down
Loading