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 all commits
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
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.20.x
go-version: 1.21.x

- name: Set env
shell: bash
Expand Down Expand Up @@ -44,7 +44,7 @@ jobs:

strategy:
matrix:
go-version: [1.19.x, 1.20.x]
go-version: ["1.21.8", "1.22.1"]
os: [ubuntu-22.04]

steps:
Expand All @@ -64,7 +64,7 @@ jobs:

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

tests:
Expand All @@ -74,7 +74,7 @@ jobs:

strategy:
matrix:
go-version: [1.19.x, 1.20.x]
go-version: ["1.21.8", "1.22.1"]

steps:
- uses: actions/checkout@v3
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
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
module github.com/containerd/go-cni

go 1.19
go 1.21

require (
github.com/containernetworking/cni v1.1.2
github.com/stretchr/testify v1.8.0
github.com/stretchr/testify v1.9.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.4.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 4 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,11 @@ github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAl
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down Expand Up @@ -103,6 +102,5 @@ gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6 changes: 4 additions & 2 deletions integration/go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
module github.com/containerd/go-cni/integration

go 1.17
go 1.21

toolchain go1.21.5

require (
github.com/containerd/continuity v0.2.2
github.com/containerd/go-cni v0.0.0-00010101000000-000000000000
github.com/stretchr/testify v1.8.0
github.com/stretchr/testify v1.9.0
)

require (
Expand Down
10 changes: 4 additions & 6 deletions integration/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,12 @@ github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tv42/httpunix v0.0.0-20191220191345-2ba4b9c3382c/go.mod h1:hzIxponao9Kjc7aWznkXaL4U4TWaDSs8zcsY4Ka08nM=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
Expand Down Expand Up @@ -238,7 +237,6 @@ gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
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, ", "), ErrInvalidConfig)
}

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