Skip to content

Commit

Permalink
status should ensure plugins and conf are present
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Zappa <[email protected]>
  • Loading branch information
MikeZappa87 committed Apr 5, 2024
1 parent 6603d5b commit 4712895
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 28 deletions.
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
21 changes: 21 additions & 0 deletions cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"sync"

Expand Down Expand Up @@ -140,6 +141,19 @@ func (c *libcni) Status() error {
if len(c.networks) < c.networkCount {
return ErrCNINotInitialized
}

for _, dir := range c.pluginDirs {
for _, nets := range c.networks {
for _, plug := range nets.config.Plugins {
plugin := plug.Network.Type

if !FileExistsInDir(dir, plugin) {
return ErrCNIPluginNotFound
}
}
}
}

return nil
}

Expand Down Expand Up @@ -310,3 +324,10 @@ func (c *libcni) GetConfig() *ConfigResult {
func (c *libcni) reset() {
c.networks = 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)
}
40 changes: 20 additions & 20 deletions cni_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
/*
Copyright The containerd Authors.
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cni

import (
Expand All @@ -39,6 +38,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 +99,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 +289,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 +329,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")
)

// IsCNINotInitialized returns true if the error is due to cni config not being initialized
Expand Down
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

0 comments on commit 4712895

Please sign in to comment.