2018-06-14 10:20:58 -05:00
// Copyright (c) 2017 Intel Corporation
//
// 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
//
// 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.
//
2020-05-28 16:43:59 -04:00
package multus
2018-06-14 10:20:58 -05:00
import (
2019-05-15 17:06:26 +09:00
"context"
2018-06-14 10:20:58 -05:00
"fmt"
"io/ioutil"
"os"
"reflect"
"github.com/containernetworking/cni/pkg/skel"
2019-07-11 15:03:53 -04:00
types020 "github.com/containernetworking/cni/pkg/types/020"
2019-08-16 18:55:12 +09:00
current "github.com/containernetworking/cni/pkg/types/current"
2018-06-14 10:20:58 -05:00
"github.com/containernetworking/plugins/pkg/ns"
"github.com/containernetworking/plugins/pkg/testutils"
2021-03-16 16:08:53 +09:00
"gopkg.in/k8snetworkplumbingwg/multus-cni.v3/pkg/k8sclient"
"gopkg.in/k8snetworkplumbingwg/multus-cni.v3/pkg/logging"
testhelpers "gopkg.in/k8snetworkplumbingwg/multus-cni.v3/pkg/testing"
"gopkg.in/k8snetworkplumbingwg/multus-cni.v3/pkg/types"
2020-06-11 13:04:20 +09:00
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2020-03-04 16:42:29 +09:00
"k8s.io/client-go/tools/record"
2018-06-14 10:20:58 -05:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
2020-01-13 09:57:22 -05:00
var _ = Describe ( "multus operations cniVersion 0.2.0 config" , func ( ) {
2018-06-14 10:20:58 -05:00
var testNS ns . NetNS
var tmpDir string
2019-08-16 18:55:12 +09:00
resultCNIVersion := "0.4.0"
2022-02-21 23:55:33 +09:00
configPath := "/tmp/foo.multus.conf"
2018-06-14 10:20:58 -05:00
BeforeEach ( func ( ) {
// Create a new NetNS so we don't modify the host
var err error
testNS , err = testutils . NewNS ( )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
2022-02-21 23:55:33 +09:00
os . Setenv ( "CNI_NETNS" , testNS . Path ( ) )
2018-06-14 10:20:58 -05:00
os . Setenv ( "CNI_PATH" , "/some/path" )
tmpDir , err = ioutil . TempDir ( "" , "multus_tmp" )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
2019-07-11 15:03:53 -04:00
2018-10-10 14:09:38 -04:00
// Touch the default network file.
os . OpenFile ( configPath , os . O_RDONLY | os . O_CREATE , 0755 )
2019-07-11 15:03:53 -04:00
} )
2022-02-21 23:55:33 +09:00
AfterEach ( func ( ) {
2019-08-20 13:54:08 -04:00
// Cleanup default network file.
if _ , errStat := os . Stat ( configPath ) ; errStat == nil {
errRemove := os . Remove ( configPath )
Expect ( errRemove ) . NotTo ( HaveOccurred ( ) )
}
2022-02-21 23:55:33 +09:00
Expect ( testNS . Close ( ) ) . To ( Succeed ( ) )
os . Unsetenv ( "CNI_PATH" )
os . Unsetenv ( "CNI_ARGS" )
err := os . RemoveAll ( tmpDir )
2019-08-20 13:54:08 -04:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
} )
2022-02-21 23:55:33 +09:00
It ( "executes delegates" , func ( ) {
2019-08-20 13:54:08 -04:00
args := & skel . CmdArgs {
ContainerID : "123456789" ,
Netns : testNS . Path ( ) ,
IfName : "eth0" ,
StdinData : [ ] byte ( ` {
"name" : "node-cni-network" ,
"type" : "multus" ,
"defaultnetworkfile" : "/tmp/foo.multus.conf" ,
"defaultnetworkwaitseconds" : 3 ,
"delegates" : [ {
"name" : "weave1" ,
"cniVersion" : "0.2.0" ,
"type" : "weave-net"
} , {
"name" : "other1" ,
"cniVersion" : "0.2.0" ,
"type" : "other-plugin"
} ]
} ` ) ,
}
logging . SetLogLevel ( "verbose" )
2022-02-21 23:55:33 +09:00
fExec := newFakeExec ( )
2019-08-20 13:54:08 -04:00
expectedResult1 := & types020 . Result {
CNIVersion : "0.2.0" ,
IP4 : & types020 . IPConfig {
IP : * testhelpers . EnsureCIDR ( "1.1.1.2/24" ) ,
} ,
}
expectedConf1 := ` {
"name" : "weave1" ,
"cniVersion" : "0.2.0" ,
"type" : "weave-net"
} `
2019-08-16 18:55:12 +09:00
fExec . addPlugin020 ( nil , "eth0" , expectedConf1 , expectedResult1 , nil )
2019-08-20 13:54:08 -04:00
expectedResult2 := & types020 . Result {
CNIVersion : "0.2.0" ,
IP4 : & types020 . IPConfig {
IP : * testhelpers . EnsureCIDR ( "1.1.1.5/24" ) ,
} ,
}
expectedConf2 := ` {
"name" : "other1" ,
"cniVersion" : "0.2.0" ,
"type" : "other-plugin"
} `
2019-08-16 18:55:12 +09:00
fExec . addPlugin020 ( nil , "net1" , expectedConf2 , expectedResult2 , nil )
2019-08-20 13:54:08 -04:00
2020-05-28 16:43:59 -04:00
result , err := CmdAdd ( args , fExec , nil )
2019-08-20 13:54:08 -04:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
Expect ( fExec . addIndex ) . To ( Equal ( len ( fExec . plugins ) ) )
r := result . ( * types020 . Result )
// plugin 1 is the masterplugin
Expect ( reflect . DeepEqual ( r , expectedResult1 ) ) . To ( BeTrue ( ) )
2020-05-28 16:43:59 -04:00
err = CmdDel ( args , fExec , nil )
2019-08-20 13:54:08 -04:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
Expect ( fExec . delIndex ) . To ( Equal ( len ( fExec . plugins ) ) )
} )
2022-02-21 23:55:33 +09:00
It ( "executes delegates given faulty namespace" , func ( ) {
2019-08-05 15:04:35 -04:00
args := & skel . CmdArgs {
ContainerID : "123456789" ,
2022-02-21 23:55:33 +09:00
Netns : "fsdadfad" ,
2019-08-05 15:04:35 -04:00
IfName : "eth0" ,
StdinData : [ ] byte ( ` {
"name" : "node-cni-network" ,
"type" : "multus" ,
"defaultnetworkfile" : "/tmp/foo.multus.conf" ,
"defaultnetworkwaitseconds" : 3 ,
"delegates" : [ {
"name" : "weave1" ,
"cniVersion" : "0.2.0" ,
"type" : "weave-net"
} , {
"name" : "other1" ,
"cniVersion" : "0.2.0" ,
"type" : "other-plugin"
} ]
} ` ) ,
}
2022-02-21 23:55:33 +09:00
// Netns is given garbage value
2019-08-05 15:04:35 -04:00
2022-02-21 23:55:33 +09:00
fExec := newFakeExec ( )
2019-08-05 15:04:35 -04:00
expectedResult1 := & types020 . Result {
CNIVersion : "0.2.0" ,
IP4 : & types020 . IPConfig {
IP : * testhelpers . EnsureCIDR ( "1.1.1.2/24" ) ,
} ,
}
expectedConf1 := ` {
"name" : "weave1" ,
"cniVersion" : "0.2.0" ,
"type" : "weave-net"
} `
2019-08-16 18:55:12 +09:00
fExec . addPlugin020 ( nil , "eth0" , expectedConf1 , expectedResult1 , nil )
2019-08-05 15:04:35 -04:00
expectedResult2 := & types020 . Result {
CNIVersion : "0.2.0" ,
IP4 : & types020 . IPConfig {
IP : * testhelpers . EnsureCIDR ( "1.1.1.5/24" ) ,
} ,
}
expectedConf2 := ` {
"name" : "other1" ,
"cniVersion" : "0.2.0" ,
"type" : "other-plugin"
} `
2019-08-16 18:55:12 +09:00
fExec . addPlugin020 ( nil , "net1" , expectedConf2 , expectedResult2 , nil )
2019-08-05 15:04:35 -04:00
2022-02-21 23:55:33 +09:00
_ , err := CmdAdd ( args , fExec , nil )
Expect ( err ) . To ( MatchError ( "[//:weave1]: error adding container to network \"weave1\": delegateAdd: cannot set \"weave-net\" interface name to \"eth0\": validateIfName: no net namespace fsdadfad found: failed to Statfs \"fsdadfad\": no such file or directory" ) )
2019-08-05 15:04:35 -04:00
} )
2020-05-28 16:43:59 -04:00
It ( "returns the previous result using CmdCheck" , func ( ) {
2019-07-11 15:03:53 -04:00
args := & skel . CmdArgs {
ContainerID : "123456789" ,
Netns : testNS . Path ( ) ,
IfName : "eth0" ,
StdinData : [ ] byte ( ` {
"name" : "node-cni-network" ,
"type" : "multus" ,
"defaultnetworkfile" : "/tmp/foo.multus.conf" ,
"defaultnetworkwaitseconds" : 3 ,
"delegates" : [ {
"name" : "weave1" ,
"cniVersion" : "0.2.0" ,
"type" : "weave-net"
} , {
"name" : "other1" ,
"cniVersion" : "0.2.0" ,
"type" : "other-plugin"
} ]
} ` ) ,
}
2022-02-21 23:55:33 +09:00
logging . SetLogLevel ( "verbose" )
2019-07-11 15:03:53 -04:00
2022-02-21 23:55:33 +09:00
fExec := newFakeExec ( )
2019-07-11 15:03:53 -04:00
expectedResult1 := & types020 . Result {
CNIVersion : "0.2.0" ,
IP4 : & types020 . IPConfig {
IP : * testhelpers . EnsureCIDR ( "1.1.1.2/24" ) ,
} ,
}
expectedConf1 := ` {
"name" : "weave1" ,
"cniVersion" : "0.2.0" ,
"type" : "weave-net"
} `
2019-08-16 18:55:12 +09:00
fExec . addPlugin020 ( nil , "eth0" , expectedConf1 , expectedResult1 , nil )
2019-07-11 15:03:53 -04:00
expectedResult2 := & types020 . Result {
CNIVersion : "0.2.0" ,
IP4 : & types020 . IPConfig {
IP : * testhelpers . EnsureCIDR ( "1.1.1.5/24" ) ,
} ,
}
expectedConf2 := ` {
"name" : "other1" ,
"cniVersion" : "0.2.0" ,
"type" : "other-plugin"
} `
2019-08-16 18:55:12 +09:00
fExec . addPlugin020 ( nil , "net1" , expectedConf2 , expectedResult2 , nil )
2019-07-11 15:03:53 -04:00
2020-05-28 16:43:59 -04:00
result , err := CmdAdd ( args , fExec , nil )
2019-07-11 15:03:53 -04:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
Expect ( fExec . addIndex ) . To ( Equal ( len ( fExec . plugins ) ) )
r := result . ( * types020 . Result )
// plugin 1 is the masterplugin
Expect ( reflect . DeepEqual ( r , expectedResult1 ) ) . To ( BeTrue ( ) )
2020-01-13 09:57:22 -05:00
// Check is not supported until v 0.4.0
2020-05-28 16:43:59 -04:00
err = CmdCheck ( args , fExec , nil )
2020-01-13 09:57:22 -05:00
Expect ( err ) . To ( HaveOccurred ( ) )
2019-07-11 15:03:53 -04:00
2020-05-28 16:43:59 -04:00
err = CmdDel ( args , fExec , nil )
2019-07-11 15:03:53 -04:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
Expect ( fExec . delIndex ) . To ( Equal ( len ( fExec . plugins ) ) )
} )
2020-05-28 16:43:59 -04:00
It ( "fails to load NetConf with bad json in CmdAdd/Del" , func ( ) {
2019-07-11 15:03:53 -04:00
args := & skel . CmdArgs {
ContainerID : "123456789" ,
Netns : testNS . Path ( ) ,
IfName : "eth0" ,
StdinData : [ ] byte ( ` {
"name" : "node-cni-network" ,
"type" : "multus" ,
"defaultnetworkfile" : "/tmp/foo.multus.conf" ,
"defaultnetworkwaitseconds" : 3 ,
"delegates" : [ {
"name" : "weave1" ,
"cniVersion" : "0.2.0" ,
"type" : "weave-net"
} , {
"name" : "other1" ,
"cniVersion" : "0.2.0" ,
"type" : "other-plugin"
} ]
` ) ,
}
// Missing close bracket in StdinData
2022-02-21 23:55:33 +09:00
fExec := newFakeExec ( )
2019-07-11 15:03:53 -04:00
expectedResult1 := & types020 . Result {
CNIVersion : "0.2.0" ,
IP4 : & types020 . IPConfig {
IP : * testhelpers . EnsureCIDR ( "1.1.1.2/24" ) ,
} ,
}
expectedConf1 := ` {
"name" : "weave1" ,
"cniVersion" : "0.2.0" ,
"type" : "weave-net"
} `
2019-08-16 18:55:12 +09:00
fExec . addPlugin020 ( nil , "eth0" , expectedConf1 , expectedResult1 , nil )
2019-07-11 15:03:53 -04:00
expectedResult2 := & types020 . Result {
CNIVersion : "0.2.0" ,
IP4 : & types020 . IPConfig {
IP : * testhelpers . EnsureCIDR ( "1.1.1.5/24" ) ,
} ,
}
expectedConf2 := ` {
"name" : "other1" ,
"cniVersion" : "0.2.0" ,
"type" : "other-plugin"
} `
2019-08-16 18:55:12 +09:00
fExec . addPlugin020 ( nil , "net1" , expectedConf2 , expectedResult2 , nil )
2018-10-10 14:09:38 -04:00
2020-05-28 16:43:59 -04:00
_ , err := CmdAdd ( args , fExec , nil )
2019-07-11 15:03:53 -04:00
Expect ( err ) . To ( HaveOccurred ( ) )
2020-05-28 16:43:59 -04:00
err = CmdDel ( args , fExec , nil )
2019-07-11 15:03:53 -04:00
Expect ( err ) . To ( HaveOccurred ( ) )
} )
It ( "fails to save NetConf with bad filepath" , func ( ) {
meme := [ ] byte ( ` meme ` )
err := saveScratchNetConf ( "123456789" , "" , meme )
Expect ( err ) . To ( HaveOccurred ( ) )
} )
It ( "fails to delete delegates with bad filepath" , func ( ) {
err := deleteDelegates ( "123456789" , "bad!file!~?Path$^" )
Expect ( err ) . To ( HaveOccurred ( ) )
} )
It ( "delete delegates given good filepath" , func ( ) {
os . MkdirAll ( "/opt/cni/bin" , 0755 )
d1 := [ ] byte ( "blah" )
ioutil . WriteFile ( "/opt/cni/bin/123456789" , d1 , 0644 )
err := deleteDelegates ( "123456789" , "/opt/cni/bin" )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
2018-06-14 10:20:58 -05:00
} )
2019-02-04 09:33:47 -06:00
It ( "executes delegates and cleans up on failure" , func ( ) {
expectedConf1 := ` {
2019-07-11 15:03:53 -04:00
"name" : "weave1" ,
"cniVersion" : "0.2.0" ,
"type" : "weave-net"
} `
2019-02-04 09:33:47 -06:00
expectedConf2 := ` {
2019-07-11 15:03:53 -04:00
"name" : "other1" ,
"cniVersion" : "0.2.0" ,
"type" : "other-plugin"
} `
2019-02-04 09:33:47 -06:00
args := & skel . CmdArgs {
ContainerID : "123456789" ,
Netns : testNS . Path ( ) ,
IfName : "eth0" ,
StdinData : [ ] byte ( fmt . Sprintf ( ` {
2019-07-11 15:03:53 -04:00
"name" : "node-cni-network" ,
"type" : "multus" ,
"defaultnetworkfile" : "/tmp/foo.multus.conf" ,
"defaultnetworkwaitseconds" : 3 ,
"delegates" : [ % s , % s ]
} ` , expectedConf1 , expectedConf2 ) ) ,
2019-02-04 09:33:47 -06:00
}
2022-02-21 23:55:33 +09:00
fExec := newFakeExec ( )
2019-02-04 09:33:47 -06:00
expectedResult1 := & types020 . Result {
CNIVersion : "0.2.0" ,
IP4 : & types020 . IPConfig {
IP : * testhelpers . EnsureCIDR ( "1.1.1.2/24" ) ,
} ,
}
2019-08-16 18:55:12 +09:00
fExec . addPlugin020 ( nil , "eth0" , expectedConf1 , expectedResult1 , nil )
2019-02-04 09:33:47 -06:00
// This plugin invocation should fail
err := fmt . Errorf ( "expected plugin failure" )
2019-08-16 18:55:12 +09:00
fExec . addPlugin020 ( nil , "net1" , expectedConf2 , nil , err )
2019-02-04 09:33:47 -06:00
2020-05-28 16:43:59 -04:00
_ , err = CmdAdd ( args , fExec , nil )
2019-02-04 09:33:47 -06:00
Expect ( fExec . addIndex ) . To ( Equal ( 2 ) )
Expect ( fExec . delIndex ) . To ( Equal ( 2 ) )
2021-10-21 10:44:29 -05:00
Expect ( err ) . To ( MatchError ( "[//:other1]: error adding container to network \"other1\": expected plugin failure" ) )
2019-02-04 09:33:47 -06:00
} )
2019-07-11 15:03:53 -04:00
It ( "executes delegates and cleans up on failure with missing name field" , func ( ) {
expectedConf1 := ` {
"name" : "weave1" ,
"cniVersion" : "0.2.0" ,
"type" : "weave-net"
} `
expectedConf2 := ` {
"name" : "" ,
"cniVersion" : "0.2.0" ,
"type" : "other-plugin"
} `
2020-05-28 16:43:59 -04:00
// took out the name in expectedConf2, expecting a new value to be filled in by CmdAdd
2019-07-11 15:03:53 -04:00
args := & skel . CmdArgs {
ContainerID : "123456789" ,
Netns : testNS . Path ( ) ,
IfName : "eth0" ,
StdinData : [ ] byte ( fmt . Sprintf ( ` {
"name" : "node-cni-network" ,
"type" : "multus" ,
"defaultnetworkfile" : "/tmp/foo.multus.conf" ,
"defaultnetworkwaitseconds" : 3 ,
"delegates" : [ % s , % s ]
} ` , expectedConf1 , expectedConf2 ) ) ,
}
2022-02-21 23:55:33 +09:00
fExec := newFakeExec ( )
2019-07-11 15:03:53 -04:00
expectedResult1 := & types020 . Result {
CNIVersion : "0.2.0" ,
IP4 : & types020 . IPConfig {
IP : * testhelpers . EnsureCIDR ( "1.1.1.2/24" ) ,
} ,
}
2019-08-16 18:55:12 +09:00
fExec . addPlugin020 ( nil , "eth0" , expectedConf1 , expectedResult1 , nil )
2019-07-11 15:03:53 -04:00
// This plugin invocation should fail
err := fmt . Errorf ( "expected plugin failure" )
2019-08-16 18:55:12 +09:00
fExec . addPlugin020 ( nil , "net1" , expectedConf2 , nil , err )
2019-07-11 15:03:53 -04:00
2020-05-28 16:43:59 -04:00
_ , err = CmdAdd ( args , fExec , nil )
2021-03-11 04:14:57 +09:00
Expect ( fExec . addIndex ) . To ( Equal ( 1 ) )
2019-07-11 15:03:53 -04:00
Expect ( fExec . delIndex ) . To ( Equal ( 2 ) )
Expect ( err ) . To ( HaveOccurred ( ) )
} )
2019-08-16 18:55:12 +09:00
It ( "executes delegates with runtimeConfigs" , func ( ) {
2018-08-28 19:21:50 +09:00
podNet := ` [ { "name" : "net1" ,
2019-08-16 18:55:12 +09:00
"mac" : "c2:11:22:33:44:66" ,
"ips" : [ "10.0.0.1" ] ,
"bandwidth" : {
"ingressRate" : 2048 ,
"ingressBurst" : 1600 ,
"egressRate" : 4096 ,
"egressBurst" : 1600
} ,
"portMappings" : [
{
"hostPort" : 8080 , "containerPort" : 80 , "protocol" : "tcp"
} ,
{
"hostPort" : 8000 , "containerPort" : 8001 , "protocol" : "udp"
} ]
}
2019-07-11 15:03:53 -04:00
] `
2018-09-28 15:02:05 +08:00
fakePod := testhelpers . NewFakePod ( "testpod" , podNet , "" )
2018-08-28 19:21:50 +09:00
net1 := ` {
2019-07-11 15:03:53 -04:00
"name" : "net1" ,
"type" : "mynet" ,
2019-08-16 18:55:12 +09:00
"capabilities" : { "mac" : true , "ips" : true , "bandwidth" : true , "portMappings" : true } ,
"cniVersion" : "0.3.1"
2019-07-11 15:03:53 -04:00
} `
2018-08-28 19:21:50 +09:00
args := & skel . CmdArgs {
ContainerID : "123456789" ,
Netns : testNS . Path ( ) ,
IfName : "eth0" ,
Args : fmt . Sprintf ( "K8S_POD_NAME=%s;K8S_POD_NAMESPACE=%s" , fakePod . ObjectMeta . Name , fakePod . ObjectMeta . Namespace ) ,
StdinData : [ ] byte ( ` {
2019-07-11 15:03:53 -04:00
"name" : "node-cni-network" ,
"type" : "multus" ,
"kubeconfig" : "/etc/kubernetes/node-kubeconfig.yaml" ,
"delegates" : [ {
"name" : "weave1" ,
2019-08-16 18:55:12 +09:00
"cniVersion" : "0.3.1" ,
2019-07-11 15:03:53 -04:00
"type" : "weave-net"
} ]
} ` ) ,
2018-08-28 19:21:50 +09:00
}
2022-02-21 23:55:33 +09:00
fExec := newFakeExec ( )
2019-08-16 18:55:12 +09:00
expectedResult1 := & current . Result {
CNIVersion : resultCNIVersion ,
2019-10-28 14:59:39 -04:00
IPs : [ ] * current . IPConfig { {
2019-08-16 18:55:12 +09:00
Address : * testhelpers . EnsureCIDR ( "1.1.1.2/24" ) ,
} ,
2018-08-28 19:21:50 +09:00
} ,
}
expectedConf1 := ` {
2019-07-11 15:03:53 -04:00
"name" : "weave1" ,
2019-08-16 18:55:12 +09:00
"cniVersion" : "0.3.1" ,
2019-07-11 15:03:53 -04:00
"type" : "weave-net"
2019-08-16 18:55:12 +09:00
} `
expectedNet1 := ` {
"name" : "net1" ,
"type" : "mynet" ,
"capabilities" : {
"mac" : true ,
"ips" : true ,
"bandwidth" : true ,
"portMappings" : true
} ,
"runtimeConfig" : {
"ips" : [ "10.0.0.1" ] ,
"mac" : "c2:11:22:33:44:66" ,
"bandwidth" : {
"ingressRate" : 2048 ,
"ingressBurst" : 1600 ,
"egressRate" : 4096 ,
"egressBurst" : 1600
} ,
"portMappings" : [
{
"hostPort" : 8080 ,
"containerPort" : 80 ,
"protocol" : "tcp"
} ,
{
"hostPort" : 8000 ,
"containerPort" : 8001 ,
"protocol" : "udp"
} ]
} ,
"cniVersion" : "0.3.1"
2019-07-11 15:03:53 -04:00
} `
2018-11-28 13:27:49 +02:00
fExec . addPlugin ( nil , "eth0" , expectedConf1 , expectedResult1 , nil )
2019-08-16 18:55:12 +09:00
fExec . addPlugin ( nil , "net1" , expectedNet1 , & current . Result {
CNIVersion : "0.3.1" ,
2019-10-28 14:59:39 -04:00
IPs : [ ] * current . IPConfig { {
2019-08-16 18:55:12 +09:00
Address : * testhelpers . EnsureCIDR ( "1.1.1.3/24" ) ,
2018-08-28 19:21:50 +09:00
} ,
} ,
} , nil )
2019-12-05 15:43:27 +09:00
clientInfo := NewFakeClientInfo ( )
2020-10-23 10:54:20 -04:00
_ , err := clientInfo . Client . CoreV1 ( ) . Pods ( fakePod . ObjectMeta . Namespace ) . Create (
context . TODO ( ) , fakePod , metav1 . CreateOptions { } )
2019-12-04 18:21:06 +09:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
_ , err = clientInfo . AddNetAttachDef (
testhelpers . NewFakeNetAttachDef ( fakePod . ObjectMeta . Namespace , "net1" , net1 ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
2018-08-28 19:21:50 +09:00
2020-05-28 16:43:59 -04:00
result , err := CmdAdd ( args , fExec , clientInfo )
2018-08-28 19:21:50 +09:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
Expect ( fExec . addIndex ) . To ( Equal ( len ( fExec . plugins ) ) )
2019-08-16 18:55:12 +09:00
r := result . ( * current . Result )
2018-08-28 19:21:50 +09:00
// plugin 1 is the masterplugin
Expect ( reflect . DeepEqual ( r , expectedResult1 ) ) . To ( BeTrue ( ) )
} )
2021-10-21 10:44:29 -05:00
It ( "fails when pod UID is provided and does not match Kube API pod UID" , func ( ) {
fakePod := testhelpers . NewFakePod ( "testpod" , "net1" , "" )
net1 := ` {
"name" : "net1" ,
"type" : "mynet" ,
"capabilities" : { "mac" : true , "ips" : true , "bandwidth" : true , "portMappings" : true } ,
"cniVersion" : "0.3.1"
} `
args := & skel . CmdArgs {
ContainerID : "123456789" ,
Netns : testNS . Path ( ) ,
IfName : "eth0" ,
Args : fmt . Sprintf ( "K8S_POD_NAME=%s;K8S_POD_NAMESPACE=%s;K8S_POD_UID=foobar" , fakePod . Name , fakePod . Namespace ) ,
StdinData : [ ] byte ( ` {
"name" : "node-cni-network" ,
"type" : "multus" ,
"kubeconfig" : "/etc/kubernetes/node-kubeconfig.yaml" ,
"delegates" : [ {
"name" : "weave1" ,
"cniVersion" : "0.3.1" ,
"type" : "weave-net"
} ]
} ` ) ,
}
clientInfo := NewFakeClientInfo ( )
_ , err := clientInfo . Client . CoreV1 ( ) . Pods ( fakePod . Namespace ) . Create (
context . TODO ( ) , fakePod , metav1 . CreateOptions { } )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
_ , err = clientInfo . AddNetAttachDef (
testhelpers . NewFakeNetAttachDef ( fakePod . Namespace , "net1" , net1 ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
2022-02-21 23:55:33 +09:00
_ , err = CmdAdd ( args , newFakeExec ( ) , clientInfo )
2021-10-21 10:44:29 -05:00
Expect ( err . Error ( ) ) . To ( ContainSubstring ( "expected pod UID \"foobar\" but got %q from Kube API" , fakePod . UID ) )
} )
It ( "executes delegates when runtime provides a matching pod UID" , func ( ) {
fakePod := testhelpers . NewFakePod ( "testpod" , "net1" , "" )
net1 := ` {
"name" : "net1" ,
"type" : "mynet" ,
"cniVersion" : "0.3.1"
} `
args := & skel . CmdArgs {
ContainerID : "123456789" ,
Netns : testNS . Path ( ) ,
IfName : "eth0" ,
Args : fmt . Sprintf ( "K8S_POD_NAME=%s;K8S_POD_NAMESPACE=%s;K8S_POD_UID=%s" , fakePod . Name , fakePod . Namespace , fakePod . UID ) ,
StdinData : [ ] byte ( ` {
"name" : "node-cni-network" ,
"type" : "multus" ,
"kubeconfig" : "/etc/kubernetes/node-kubeconfig.yaml" ,
"delegates" : [ {
"name" : "weave1" ,
"cniVersion" : "0.3.1" ,
"type" : "weave-net"
} ]
} ` ) ,
}
2022-02-21 23:55:33 +09:00
fExec := newFakeExec ( )
2021-10-21 10:44:29 -05:00
expectedResult1 := & current . Result {
CNIVersion : resultCNIVersion ,
IPs : [ ] * current . IPConfig { {
Address : * testhelpers . EnsureCIDR ( "1.1.1.2/24" ) ,
} ,
} ,
}
expectedConf1 := ` {
"name" : "weave1" ,
"cniVersion" : "0.3.1" ,
"type" : "weave-net"
} `
expectedNet1 := ` {
"name" : "net1" ,
"type" : "mynet" ,
"cniVersion" : "0.3.1"
} `
fExec . addPlugin ( nil , "eth0" , expectedConf1 , expectedResult1 , nil )
fExec . addPlugin ( nil , "net1" , expectedNet1 , & current . Result {
CNIVersion : "0.3.1" ,
IPs : [ ] * current . IPConfig { {
Address : * testhelpers . EnsureCIDR ( "1.1.1.3/24" ) ,
} ,
} ,
} , nil )
clientInfo := NewFakeClientInfo ( )
_ , err := clientInfo . Client . CoreV1 ( ) . Pods ( fakePod . ObjectMeta . Namespace ) . Create (
context . TODO ( ) , fakePod , metav1 . CreateOptions { } )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
_ , err = clientInfo . AddNetAttachDef (
testhelpers . NewFakeNetAttachDef ( fakePod . ObjectMeta . Namespace , "net1" , net1 ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
result , err := CmdAdd ( args , fExec , clientInfo )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
Expect ( fExec . addIndex ) . To ( Equal ( len ( fExec . plugins ) ) )
r := result . ( * current . Result )
// plugin 1 is the masterplugin
Expect ( reflect . DeepEqual ( r , expectedResult1 ) ) . To ( BeTrue ( ) )
} )
2021-10-22 09:56:43 -05:00
It ( "executes delegate with pod UID when runtime provides a pod UID" , func ( ) {
fakePod := testhelpers . NewFakePod ( "testpod" , "" , "" )
args := & skel . CmdArgs {
ContainerID : "123456789" ,
Netns : testNS . Path ( ) ,
IfName : "eth0" ,
Args : fmt . Sprintf ( "K8S_POD_NAME=%s;K8S_POD_NAMESPACE=%s;K8S_POD_UID=%s" , fakePod . Name , fakePod . Namespace , fakePod . UID ) ,
StdinData : [ ] byte ( ` {
"name" : "node-cni-network" ,
"type" : "multus" ,
"kubeconfig" : "/etc/kubernetes/node-kubeconfig.yaml" ,
"delegates" : [ {
"name" : "weave1" ,
"cniVersion" : "0.3.1" ,
"type" : "weave-net"
} ]
} ` ) ,
}
2022-02-21 23:55:33 +09:00
fExec := newFakeExec ( )
2021-10-22 09:56:43 -05:00
expectedResult1 := & current . Result {
CNIVersion : resultCNIVersion ,
IPs : [ ] * current . IPConfig { {
Address : * testhelpers . EnsureCIDR ( "1.1.1.2/24" ) ,
} ,
} ,
}
expectedConf1 := ` {
"name" : "weave1" ,
"cniVersion" : "0.3.1" ,
"type" : "weave-net"
} `
expectedEnv := [ ] string {
fmt . Sprintf ( "CNI_ARGS=IgnoreUnknown=true;K8S_POD_NAMESPACE=%s;K8S_POD_NAME=%s;K8S_POD_INFRA_CONTAINER_ID=;K8S_POD_UID=%s" , fakePod . Namespace , fakePod . Name , fakePod . UID ) ,
"CNI_COMMAND=ADD" ,
"CNI_IFNAME=eth0" ,
}
fExec . addPlugin ( expectedEnv , "eth0" , expectedConf1 , expectedResult1 , nil )
clientInfo := NewFakeClientInfo ( )
_ , err := clientInfo . Client . CoreV1 ( ) . Pods ( fakePod . ObjectMeta . Namespace ) . Create (
context . TODO ( ) , fakePod , metav1 . CreateOptions { } )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
result , err := CmdAdd ( args , fExec , clientInfo )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
Expect ( fExec . addIndex ) . To ( Equal ( len ( fExec . plugins ) ) )
r := result . ( * current . Result )
// plugin 1 is the masterplugin
Expect ( reflect . DeepEqual ( r , expectedResult1 ) ) . To ( BeTrue ( ) )
} )
It ( "executes delegate with an empty pod UID when runtime does not provide a pod UID" , func ( ) {
fakePod := testhelpers . NewFakePod ( "testpod" , "" , "" )
args := & skel . CmdArgs {
ContainerID : "123456789" ,
Netns : testNS . Path ( ) ,
IfName : "eth0" ,
Args : fmt . Sprintf ( "K8S_POD_NAME=%s;K8S_POD_NAMESPACE=%s" , fakePod . Name , fakePod . Namespace ) ,
StdinData : [ ] byte ( ` {
"name" : "node-cni-network" ,
"type" : "multus" ,
"kubeconfig" : "/etc/kubernetes/node-kubeconfig.yaml" ,
"delegates" : [ {
"name" : "weave1" ,
"cniVersion" : "0.3.1" ,
"type" : "weave-net"
} ]
} ` ) ,
}
2022-02-21 23:55:33 +09:00
fExec := newFakeExec ( )
2021-10-22 09:56:43 -05:00
expectedResult1 := & current . Result {
CNIVersion : resultCNIVersion ,
IPs : [ ] * current . IPConfig { {
Address : * testhelpers . EnsureCIDR ( "1.1.1.2/24" ) ,
} ,
} ,
}
expectedConf1 := ` {
"name" : "weave1" ,
"cniVersion" : "0.3.1" ,
"type" : "weave-net"
} `
expectedEnv := [ ] string {
fmt . Sprintf ( "CNI_ARGS=IgnoreUnknown=true;K8S_POD_NAMESPACE=%s;K8S_POD_NAME=%s;K8S_POD_INFRA_CONTAINER_ID=;K8S_POD_UID=" , fakePod . Namespace , fakePod . Name ) ,
"CNI_COMMAND=ADD" ,
"CNI_IFNAME=eth0" ,
}
fExec . addPlugin ( expectedEnv , "eth0" , expectedConf1 , expectedResult1 , nil )
clientInfo := NewFakeClientInfo ( )
_ , err := clientInfo . Client . CoreV1 ( ) . Pods ( fakePod . ObjectMeta . Namespace ) . Create (
context . TODO ( ) , fakePod , metav1 . CreateOptions { } )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
result , err := CmdAdd ( args , fExec , clientInfo )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
Expect ( fExec . addIndex ) . To ( Equal ( len ( fExec . plugins ) ) )
r := result . ( * current . Result )
// plugin 1 is the masterplugin
Expect ( reflect . DeepEqual ( r , expectedResult1 ) ) . To ( BeTrue ( ) )
} )
2020-03-04 16:42:29 +09:00
It ( "executes delegates and kubernetes networks with events check" , func ( ) {
2018-09-28 15:02:05 +08:00
fakePod := testhelpers . NewFakePod ( "testpod" , "net1,net2" , "" )
2018-06-14 10:20:58 -05:00
net1 := ` {
2019-07-11 15:03:53 -04:00
"name" : "net1" ,
"type" : "mynet" ,
"cniVersion" : "0.2.0"
} `
2018-06-14 10:20:58 -05:00
net2 := ` {
2019-07-11 15:03:53 -04:00
"name" : "net2" ,
"type" : "mynet2" ,
"cniVersion" : "0.2.0"
} `
2018-06-14 10:20:58 -05:00
net3 := ` {
2019-07-11 15:03:53 -04:00
"name" : "net3" ,
"type" : "mynet3" ,
"cniVersion" : "0.2.0"
} `
2018-06-14 10:20:58 -05:00
args := & skel . CmdArgs {
ContainerID : "123456789" ,
Netns : testNS . Path ( ) ,
IfName : "eth0" ,
Args : fmt . Sprintf ( "K8S_POD_NAME=%s;K8S_POD_NAMESPACE=%s" , fakePod . ObjectMeta . Name , fakePod . ObjectMeta . Namespace ) ,
StdinData : [ ] byte ( ` {
2019-07-11 15:03:53 -04:00
"name" : "node-cni-network" ,
"type" : "multus" ,
"kubeconfig" : "/etc/kubernetes/node-kubeconfig.yaml" ,
"delegates" : [ {
"name" : "weave1" ,
"cniVersion" : "0.2.0" ,
"type" : "weave-net"
} ]
} ` ) ,
2018-06-14 10:20:58 -05:00
}
2022-02-21 23:55:33 +09:00
fExec := newFakeExec ( )
2018-06-14 10:20:58 -05:00
expectedResult1 := & types020 . Result {
CNIVersion : "0.2.0" ,
IP4 : & types020 . IPConfig {
IP : * testhelpers . EnsureCIDR ( "1.1.1.2/24" ) ,
} ,
}
expectedConf1 := ` {
2019-07-11 15:03:53 -04:00
"name" : "weave1" ,
"cniVersion" : "0.2.0" ,
"type" : "weave-net"
} `
2019-08-16 18:55:12 +09:00
fExec . addPlugin020 ( nil , "eth0" , expectedConf1 , expectedResult1 , nil )
fExec . addPlugin020 ( nil , "net1" , net1 , & types020 . Result {
2018-06-14 10:20:58 -05:00
CNIVersion : "0.2.0" ,
IP4 : & types020 . IPConfig {
IP : * testhelpers . EnsureCIDR ( "1.1.1.3/24" ) ,
} ,
} , nil )
2019-08-16 18:55:12 +09:00
fExec . addPlugin020 ( nil , "net2" , net2 , & types020 . Result {
2018-06-14 10:20:58 -05:00
CNIVersion : "0.2.0" ,
IP4 : & types020 . IPConfig {
IP : * testhelpers . EnsureCIDR ( "1.1.1.4/24" ) ,
} ,
} , nil )
2019-12-05 15:43:27 +09:00
clientInfo := NewFakeClientInfo ( )
2020-10-23 10:54:20 -04:00
_ , err := clientInfo . Client . CoreV1 ( ) . Pods ( fakePod . ObjectMeta . Namespace ) . Create (
context . TODO ( ) , fakePod , metav1 . CreateOptions { } )
2019-12-04 18:21:06 +09:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
_ , err = clientInfo . AddNetAttachDef (
testhelpers . NewFakeNetAttachDef ( fakePod . ObjectMeta . Namespace , "net1" , net1 ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
_ , err = clientInfo . AddNetAttachDef (
testhelpers . NewFakeNetAttachDef ( fakePod . ObjectMeta . Namespace , "net2" , net2 ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
2018-06-14 10:20:58 -05:00
// net3 is not used; make sure it's not accessed
2019-12-04 18:21:06 +09:00
_ , err = clientInfo . AddNetAttachDef (
testhelpers . NewFakeNetAttachDef ( fakePod . ObjectMeta . Namespace , "net3" , net3 ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
2018-06-14 10:20:58 -05:00
2020-05-28 16:43:59 -04:00
result , err := CmdAdd ( args , fExec , clientInfo )
2018-06-14 10:20:58 -05:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
Expect ( fExec . addIndex ) . To ( Equal ( len ( fExec . plugins ) ) )
r := result . ( * types020 . Result )
// plugin 1 is the masterplugin
Expect ( reflect . DeepEqual ( r , expectedResult1 ) ) . To ( BeTrue ( ) )
2020-03-04 16:42:29 +09:00
recorder := clientInfo . EventRecorder . ( * record . FakeRecorder )
events := collectEvents ( recorder . Events )
Expect ( len ( events ) ) . To ( Equal ( 3 ) )
2021-04-19 23:20:07 +09:00
Expect ( events [ 0 ] ) . To ( Equal ( "Normal AddedInterface Add eth0 [1.1.1.2/24] from weave1" ) )
2020-06-09 12:43:49 +09:00
Expect ( events [ 1 ] ) . To ( Equal ( "Normal AddedInterface Add net1 [1.1.1.3/24] from test/net1" ) )
Expect ( events [ 2 ] ) . To ( Equal ( "Normal AddedInterface Add net2 [1.1.1.4/24] from test/net2" ) )
2018-06-14 10:20:58 -05:00
} )
2018-09-26 09:52:37 +02:00
2019-01-25 17:49:16 +09:00
It ( "executes kubernetes networks and delete it after pod removal" , func ( ) {
fakePod := testhelpers . NewFakePod ( "testpod" , "net1" , "" )
net1 := ` {
2019-07-11 15:03:53 -04:00
"name" : "net1" ,
"type" : "mynet" ,
"cniVersion" : "0.2.0"
} `
args := & skel . CmdArgs {
ContainerID : "123456789" ,
Netns : testNS . Path ( ) ,
IfName : "eth0" ,
Args : fmt . Sprintf ( "K8S_POD_NAME=%s;K8S_POD_NAMESPACE=%s" , fakePod . ObjectMeta . Name , fakePod . ObjectMeta . Namespace ) ,
StdinData : [ ] byte ( ` {
"name" : "node-cni-network" ,
"type" : "multus" ,
"kubeconfig" : "/etc/kubernetes/node-kubeconfig.yaml" ,
"delegates" : [ {
"name" : "weave1" ,
"cniVersion" : "0.2.0" ,
"type" : "weave-net"
} ]
} ` ) ,
}
2022-02-21 23:55:33 +09:00
fExec := newFakeExec ( )
2019-07-11 15:03:53 -04:00
expectedResult1 := & types020 . Result {
CNIVersion : "0.2.0" ,
IP4 : & types020 . IPConfig {
IP : * testhelpers . EnsureCIDR ( "1.1.1.2/24" ) ,
} ,
}
expectedConf1 := ` {
"name" : "weave1" ,
"cniVersion" : "0.2.0" ,
"type" : "weave-net"
} `
2019-08-16 18:55:12 +09:00
fExec . addPlugin020 ( nil , "eth0" , expectedConf1 , expectedResult1 , nil )
fExec . addPlugin020 ( nil , "net1" , net1 , & types020 . Result {
2019-07-11 15:03:53 -04:00
CNIVersion : "0.2.0" ,
IP4 : & types020 . IPConfig {
IP : * testhelpers . EnsureCIDR ( "1.1.1.3/24" ) ,
} ,
} , nil )
2019-12-05 15:43:27 +09:00
clientInfo := NewFakeClientInfo ( )
2020-10-23 10:54:20 -04:00
_ , err := clientInfo . Client . CoreV1 ( ) . Pods ( fakePod . ObjectMeta . Namespace ) . Create (
context . TODO ( ) , fakePod , metav1 . CreateOptions { } )
2019-12-04 18:21:06 +09:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
_ , err = clientInfo . AddNetAttachDef (
testhelpers . NewFakeNetAttachDef ( fakePod . ObjectMeta . Namespace , "net1" , net1 ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
2019-07-11 15:03:53 -04:00
2020-05-28 16:43:59 -04:00
result , err := CmdAdd ( args , fExec , clientInfo )
2019-07-11 15:03:53 -04:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
Expect ( fExec . addIndex ) . To ( Equal ( len ( fExec . plugins ) ) )
r := result . ( * types020 . Result )
// plugin 1 is the masterplugin
Expect ( reflect . DeepEqual ( r , expectedResult1 ) ) . To ( BeTrue ( ) )
2020-06-11 13:04:20 +09:00
// delete pod to emulate no pod info
2019-12-04 18:21:06 +09:00
clientInfo . DeletePod ( fakePod . ObjectMeta . Namespace , fakePod . ObjectMeta . Name )
2020-10-23 10:54:20 -04:00
nilPod , err := clientInfo . Client . CoreV1 ( ) . Pods ( fakePod . ObjectMeta . Namespace ) . Get (
context . TODO ( ) , fakePod . ObjectMeta . Name , metav1 . GetOptions { } )
2020-06-11 13:04:20 +09:00
Expect ( nilPod ) . To ( BeNil ( ) )
Expect ( errors . IsNotFound ( err ) ) . To ( BeTrue ( ) )
2020-05-28 16:43:59 -04:00
err = CmdDel ( args , fExec , clientInfo )
2019-07-11 15:03:53 -04:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
Expect ( fExec . delIndex ) . To ( Equal ( len ( fExec . plugins ) ) )
} )
It ( "executes kubernetes networks and delete it after pod removal" , func ( ) {
fakePod := testhelpers . NewFakePod ( "testpod" , "net1" , "" )
net1 := ` {
"name" : "net1" ,
"type" : "mynet" ,
"cniVersion" : "0.2.0"
} `
2019-01-25 17:49:16 +09:00
args := & skel . CmdArgs {
ContainerID : "123456789" ,
Netns : testNS . Path ( ) ,
IfName : "eth0" ,
Args : fmt . Sprintf ( "K8S_POD_NAME=%s;K8S_POD_NAMESPACE=%s" , fakePod . ObjectMeta . Name , fakePod . ObjectMeta . Namespace ) ,
StdinData : [ ] byte ( ` {
2019-07-11 15:03:53 -04:00
"name" : "node-cni-network" ,
"type" : "multus" ,
"kubeconfig" : "/etc/kubernetes/node-kubeconfig.yaml" ,
"delegates" : [ {
"name" : "weave1" ,
"cniVersion" : "0.2.0" ,
"type" : "weave-net"
} ]
} ` ) ,
2019-01-25 17:49:16 +09:00
}
2022-02-21 23:55:33 +09:00
fExec := newFakeExec ( )
2019-01-25 17:49:16 +09:00
expectedResult1 := & types020 . Result {
CNIVersion : "0.2.0" ,
IP4 : & types020 . IPConfig {
IP : * testhelpers . EnsureCIDR ( "1.1.1.2/24" ) ,
} ,
}
expectedConf1 := ` {
2019-07-11 15:03:53 -04:00
"name" : "weave1" ,
"cniVersion" : "0.2.0" ,
"type" : "weave-net"
} `
2019-08-16 18:55:12 +09:00
fExec . addPlugin020 ( nil , "eth0" , expectedConf1 , expectedResult1 , nil )
fExec . addPlugin020 ( nil , "net1" , net1 , & types020 . Result {
2019-01-25 17:49:16 +09:00
CNIVersion : "0.2.0" ,
IP4 : & types020 . IPConfig {
IP : * testhelpers . EnsureCIDR ( "1.1.1.3/24" ) ,
} ,
} , nil )
2019-12-05 15:43:27 +09:00
fKubeClient := NewFakeClientInfo ( )
2019-01-25 17:49:16 +09:00
fKubeClient . AddPod ( fakePod )
2019-12-04 18:21:06 +09:00
_ , err := fKubeClient . AddNetAttachDef (
testhelpers . NewFakeNetAttachDef ( fakePod . ObjectMeta . Namespace , "net1" , net1 ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
2019-01-25 17:49:16 +09:00
2020-05-28 16:43:59 -04:00
result , err := CmdAdd ( args , fExec , fKubeClient )
2019-01-25 17:49:16 +09:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
Expect ( fExec . addIndex ) . To ( Equal ( len ( fExec . plugins ) ) )
r := result . ( * types020 . Result )
// plugin 1 is the masterplugin
Expect ( reflect . DeepEqual ( r , expectedResult1 ) ) . To ( BeTrue ( ) )
// set fKubeClient to nil to emulate no pod info
2019-12-04 18:21:06 +09:00
fKubeClient . DeletePod ( fakePod . ObjectMeta . Namespace , fakePod . ObjectMeta . Name )
2020-05-28 16:43:59 -04:00
err = CmdDel ( args , fExec , fKubeClient )
2019-01-25 17:49:16 +09:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
Expect ( fExec . delIndex ) . To ( Equal ( len ( fExec . plugins ) ) )
} )
2019-08-05 15:04:35 -04:00
It ( "executes kubernetes networks and delete it after pod removal" , func ( ) {
fakePod := testhelpers . NewFakePod ( "testpod" , "net1" , "" )
net1 := ` {
"name" : "net1" ,
"type" : "mynet" ,
"cniVersion" : "0.2.0"
} `
args := & skel . CmdArgs {
ContainerID : "123456789" ,
Netns : testNS . Path ( ) ,
IfName : "eth0" ,
Args : fmt . Sprintf ( "K8S_POD_NAME=%s;K8S_POD_NAMESPACE=%s" , fakePod . ObjectMeta . Name , fakePod . ObjectMeta . Namespace ) ,
StdinData : [ ] byte ( ` {
"name" : "node-cni-network" ,
"type" : "multus" ,
"kubeconfig" : "/etc/kubernetes/node-kubeconfig.yaml" ,
"delegates" : [ {
"name" : "weave1" ,
"cniVersion" : "0.2.0" ,
"type" : "weave-net"
} ]
} ` ) ,
}
2022-02-21 23:55:33 +09:00
fExec := newFakeExec ( )
2019-08-05 15:04:35 -04:00
expectedResult1 := & types020 . Result {
CNIVersion : "0.2.0" ,
IP4 : & types020 . IPConfig {
IP : * testhelpers . EnsureCIDR ( "1.1.1.2/24" ) ,
} ,
}
expectedConf1 := ` {
"name" : "weave1" ,
"cniVersion" : "0.2.0" ,
"type" : "weave-net"
} `
2019-08-16 18:55:12 +09:00
fExec . addPlugin020 ( nil , "eth0" , expectedConf1 , expectedResult1 , nil )
fExec . addPlugin020 ( nil , "net1" , net1 , & types020 . Result {
2019-08-05 15:04:35 -04:00
CNIVersion : "0.2.0" ,
IP4 : & types020 . IPConfig {
IP : * testhelpers . EnsureCIDR ( "1.1.1.3/24" ) ,
} ,
} , nil )
2019-12-05 15:43:27 +09:00
fKubeClient := NewFakeClientInfo ( )
2019-08-05 15:04:35 -04:00
fKubeClient . AddPod ( fakePod )
2019-12-04 18:21:06 +09:00
_ , err := fKubeClient . AddNetAttachDef (
testhelpers . NewFakeNetAttachDef ( fakePod . ObjectMeta . Namespace , "net1" , net1 ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
2019-08-05 15:04:35 -04:00
2020-05-28 16:43:59 -04:00
result , err := CmdAdd ( args , fExec , fKubeClient )
2019-08-05 15:04:35 -04:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
Expect ( fExec . addIndex ) . To ( Equal ( len ( fExec . plugins ) ) )
r := result . ( * types020 . Result )
// plugin 1 is the masterplugin
Expect ( reflect . DeepEqual ( r , expectedResult1 ) ) . To ( BeTrue ( ) )
// set fKubeClient to nil to emulate no pod info
2019-12-04 18:21:06 +09:00
fKubeClient . DeletePod ( fakePod . ObjectMeta . Namespace , fakePod . ObjectMeta . Name )
2020-05-28 16:43:59 -04:00
err = CmdDel ( args , fExec , fKubeClient )
2019-08-05 15:04:35 -04:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
Expect ( fExec . delIndex ) . To ( Equal ( len ( fExec . plugins ) ) )
} )
2018-09-26 09:52:37 +02:00
It ( "ensure delegates get portmap runtime config" , func ( ) {
args := & skel . CmdArgs {
ContainerID : "123456789" ,
Netns : testNS . Path ( ) ,
IfName : "eth0" ,
StdinData : [ ] byte ( ` {
2019-07-11 15:03:53 -04:00
"name" : "node-cni-network" ,
"type" : "multus" ,
"delegates" : [ {
"cniVersion" : "0.3.1" ,
"name" : "mynet-confList" ,
"plugins" : [
{
"type" : "firstPlugin" ,
"capabilities" : { "portMappings" : true }
}
]
} ] ,
"runtimeConfig" : {
"portMappings" : [
{ "hostPort" : 8080 , "containerPort" : 80 , "protocol" : "tcp" }
]
}
} ` ) ,
2018-09-26 09:52:37 +02:00
}
2022-02-21 23:55:33 +09:00
fExec := newFakeExec ( )
2018-09-26 09:52:37 +02:00
expectedConf1 := ` {
2019-07-11 15:03:53 -04:00
"capabilities" : { "portMappings" : true } ,
"name" : "mynet-confList" ,
"cniVersion" : "0.3.1" ,
"type" : "firstPlugin" ,
"runtimeConfig" : {
"portMappings" : [
{ "hostPort" : 8080 , "containerPort" : 80 , "protocol" : "tcp" }
]
}
} `
2019-08-16 18:55:12 +09:00
fExec . addPlugin020 ( nil , "eth0" , expectedConf1 , nil , nil )
2020-05-28 16:43:59 -04:00
_ , err := CmdAdd ( args , fExec , nil )
2018-09-26 09:52:37 +02:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
} )
2019-01-11 17:40:16 +08:00
It ( "executes clusterNetwork delegate" , func ( ) {
fakePod := testhelpers . NewFakePod ( "testpod" , "" , "kube-system/net1" )
net1 := ` {
2019-07-11 15:03:53 -04:00
"name" : "net1" ,
"type" : "mynet" ,
"cniVersion" : "0.2.0"
} `
2019-01-11 17:40:16 +08:00
expectedResult1 := & types020 . Result {
CNIVersion : "0.2.0" ,
IP4 : & types020 . IPConfig {
IP : * testhelpers . EnsureCIDR ( "1.1.1.2/24" ) ,
} ,
}
args := & skel . CmdArgs {
ContainerID : "123456789" ,
Netns : testNS . Path ( ) ,
IfName : "eth0" ,
Args : fmt . Sprintf ( "K8S_POD_NAME=%s;K8S_POD_NAMESPACE=%s" , fakePod . ObjectMeta . Name , fakePod . ObjectMeta . Namespace ) ,
StdinData : [ ] byte ( ` {
2019-07-11 15:03:53 -04:00
"name" : "node-cni-network" ,
"type" : "multus" ,
"kubeconfig" : "/etc/kubernetes/node-kubeconfig.yaml" ,
"defaultNetworks" : [ ] ,
"clusterNetwork" : "net1" ,
"delegates" : [ ]
} ` ) ,
2019-01-11 17:40:16 +08:00
}
2022-02-21 23:55:33 +09:00
fExec := newFakeExec ( )
2019-08-16 18:55:12 +09:00
fExec . addPlugin020 ( nil , "eth0" , net1 , expectedResult1 , nil )
2019-01-11 17:40:16 +08:00
2019-12-05 15:43:27 +09:00
fKubeClient := NewFakeClientInfo ( )
2019-01-11 17:40:16 +08:00
fKubeClient . AddPod ( fakePod )
2019-12-04 18:21:06 +09:00
_ , err := fKubeClient . AddNetAttachDef ( testhelpers . NewFakeNetAttachDef ( "kube-system" , "net1" , net1 ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
2019-01-11 17:40:16 +08:00
2020-05-28 16:43:59 -04:00
result , err := CmdAdd ( args , fExec , fKubeClient )
2019-01-11 17:40:16 +08:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
Expect ( fExec . addIndex ) . To ( Equal ( len ( fExec . plugins ) ) )
r := result . ( * types020 . Result )
Expect ( reflect . DeepEqual ( r , expectedResult1 ) ) . To ( BeTrue ( ) )
2020-05-28 16:43:59 -04:00
err = CmdDel ( args , fExec , fKubeClient )
2019-01-11 17:40:16 +08:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
Expect ( fExec . delIndex ) . To ( Equal ( len ( fExec . plugins ) ) )
} )
2019-01-25 17:49:16 +09:00
It ( "Verify the cache is created in dataDir" , func ( ) {
tmpCNIDir := tmpDir + "/cniData"
err := os . Mkdir ( tmpCNIDir , 0777 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
fakePod := testhelpers . NewFakePod ( "testpod" , "net1" , "" )
net1 := ` {
2019-07-11 15:03:53 -04:00
"name" : "net1" ,
"type" : "mynet" ,
"cniVersion" : "0.2.0"
} `
2019-01-25 17:49:16 +09:00
args := & skel . CmdArgs {
ContainerID : "123456789" ,
Netns : testNS . Path ( ) ,
IfName : "eth0" ,
Args : fmt . Sprintf ( "K8S_POD_NAME=%s;K8S_POD_NAMESPACE=%s" , fakePod . ObjectMeta . Name , fakePod . ObjectMeta . Namespace ) ,
StdinData : [ ] byte ( fmt . Sprintf ( ` {
2019-07-11 15:03:53 -04:00
"name" : "node-cni-network" ,
"type" : "multus" ,
"kubeconfig" : "/etc/kubernetes/node-kubeconfig.yaml" ,
"cniDir" : "%s" ,
"delegates" : [ {
"name" : "weave1" ,
"cniVersion" : "0.2.0" ,
"type" : "weave-net"
} ]
} ` , tmpCNIDir ) ) ,
2019-01-25 17:49:16 +09:00
}
2022-02-21 23:55:33 +09:00
fExec := newFakeExec ( )
2019-01-25 17:49:16 +09:00
expectedResult1 := & types020 . Result {
CNIVersion : "0.2.0" ,
IP4 : & types020 . IPConfig {
IP : * testhelpers . EnsureCIDR ( "1.1.1.2/24" ) ,
} ,
}
expectedConf1 := ` {
2019-07-11 15:03:53 -04:00
"name" : "weave1" ,
"cniVersion" : "0.2.0" ,
"type" : "weave-net"
} `
2019-08-16 18:55:12 +09:00
fExec . addPlugin020 ( nil , "eth0" , expectedConf1 , expectedResult1 , nil )
fExec . addPlugin020 ( nil , "net1" , net1 , & types020 . Result {
2019-01-25 17:49:16 +09:00
CNIVersion : "0.2.0" ,
IP4 : & types020 . IPConfig {
IP : * testhelpers . EnsureCIDR ( "1.1.1.3/24" ) ,
} ,
} , nil )
2019-12-05 15:43:27 +09:00
fKubeClient := NewFakeClientInfo ( )
2019-01-25 17:49:16 +09:00
fKubeClient . AddPod ( fakePod )
2019-12-04 18:21:06 +09:00
_ , err = fKubeClient . AddNetAttachDef (
testhelpers . NewFakeNetAttachDef ( fakePod . ObjectMeta . Namespace , "net1" , net1 ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
2020-05-28 16:43:59 -04:00
result , err := CmdAdd ( args , fExec , fKubeClient )
2019-01-25 17:49:16 +09:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
Expect ( fExec . addIndex ) . To ( Equal ( len ( fExec . plugins ) ) )
r := result . ( * types020 . Result )
// plugin 1 is the masterplugin
Expect ( reflect . DeepEqual ( r , expectedResult1 ) ) . To ( BeTrue ( ) )
By ( "Verify cache file existence" )
cacheFilePath := fmt . Sprintf ( "%s/%s" , tmpCNIDir , "123456789" )
_ , err = os . Stat ( cacheFilePath )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Delete and check net count is not incremented" )
2020-05-28 16:43:59 -04:00
err = CmdDel ( args , fExec , fKubeClient )
2019-01-25 17:49:16 +09:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
Expect ( fExec . delIndex ) . To ( Equal ( len ( fExec . plugins ) ) )
} )
It ( "Delete pod without cache" , func ( ) {
tmpCNIDir := tmpDir + "/cniData"
err := os . Mkdir ( tmpCNIDir , 0777 )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
fakePod := testhelpers . NewFakePod ( "testpod" , "net1" , "" )
net1 := ` {
2019-07-11 15:03:53 -04:00
"name" : "net1" ,
"type" : "mynet" ,
"cniVersion" : "0.2.0"
} `
2019-01-25 17:49:16 +09:00
args := & skel . CmdArgs {
ContainerID : "123456789" ,
Netns : testNS . Path ( ) ,
IfName : "eth0" ,
Args : fmt . Sprintf ( "K8S_POD_NAME=%s;K8S_POD_NAMESPACE=%s" , fakePod . ObjectMeta . Name , fakePod . ObjectMeta . Namespace ) ,
StdinData : [ ] byte ( fmt . Sprintf ( ` {
2019-07-11 15:03:53 -04:00
"name" : "node-cni-network" ,
"type" : "multus" ,
"kubeconfig" : "/etc/kubernetes/node-kubeconfig.yaml" ,
"cniDir" : "%s" ,
"delegates" : [ {
"name" : "weave1" ,
"cniVersion" : "0.2.0" ,
"type" : "weave-net"
} ]
} ` , tmpCNIDir ) ) ,
2019-01-25 17:49:16 +09:00
}
2022-02-21 23:55:33 +09:00
fExec := newFakeExec ( )
2019-01-25 17:49:16 +09:00
expectedResult1 := & types020 . Result {
CNIVersion : "0.2.0" ,
IP4 : & types020 . IPConfig {
IP : * testhelpers . EnsureCIDR ( "1.1.1.2/24" ) ,
} ,
}
expectedConf1 := ` {
2019-07-11 15:03:53 -04:00
"name" : "weave1" ,
"cniVersion" : "0.2.0" ,
"type" : "weave-net"
} `
2019-08-16 18:55:12 +09:00
fExec . addPlugin020 ( nil , "eth0" , expectedConf1 , expectedResult1 , nil )
fExec . addPlugin020 ( nil , "net1" , net1 , & types020 . Result {
2019-01-25 17:49:16 +09:00
CNIVersion : "0.2.0" ,
IP4 : & types020 . IPConfig {
IP : * testhelpers . EnsureCIDR ( "1.1.1.3/24" ) ,
} ,
} , nil )
2019-12-05 15:43:27 +09:00
fKubeClient := NewFakeClientInfo ( )
2019-01-25 17:49:16 +09:00
fKubeClient . AddPod ( fakePod )
2019-12-04 18:21:06 +09:00
_ , err = fKubeClient . AddNetAttachDef (
testhelpers . NewFakeNetAttachDef ( fakePod . ObjectMeta . Namespace , "net1" , net1 ) )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
2020-05-28 16:43:59 -04:00
result , err := CmdAdd ( args , fExec , fKubeClient )
2019-01-25 17:49:16 +09:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
Expect ( fExec . addIndex ) . To ( Equal ( len ( fExec . plugins ) ) )
r := result . ( * types020 . Result )
// plugin 1 is the masterplugin
Expect ( reflect . DeepEqual ( r , expectedResult1 ) ) . To ( BeTrue ( ) )
By ( "Verify cache file existence" )
cacheFilePath := fmt . Sprintf ( "%s/%s" , tmpCNIDir , "123456789" )
_ , err = os . Stat ( cacheFilePath )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
err = os . Remove ( cacheFilePath )
Expect ( err ) . NotTo ( HaveOccurred ( ) )
By ( "Delete and check pod/net count is incremented" )
2020-05-28 16:43:59 -04:00
err = CmdDel ( args , fExec , fKubeClient )
2019-01-25 17:49:16 +09:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
Expect ( fExec . delIndex ) . To ( Equal ( len ( fExec . plugins ) ) )
} )
2019-08-05 15:04:35 -04:00
It ( "fails to execute confListDel given no 'plugins' key" , func ( ) {
args := & skel . CmdArgs {
ContainerID : "123456789" ,
Netns : testNS . Path ( ) ,
IfName : "eth0" ,
StdinData : [ ] byte ( ` {
"name" : "node-cni-network" ,
"type" : "multus" ,
"defaultnetworkfile" : "/tmp/foo.multus.conf" ,
"defaultnetworkwaitseconds" : 3 ,
"delegates" : [ {
"name" : "weave1" ,
"cniVersion" : "0.2.0" ,
"type" : "weave-net"
} , {
"name" : "other1" ,
"cniVersion" : "0.2.0" ,
"type" : "other-plugin"
} ]
} ` ) ,
}
2022-02-21 23:55:33 +09:00
fExec := newFakeExec ( )
2019-08-05 15:04:35 -04:00
expectedResult1 := & types020 . Result {
CNIVersion : "0.2.0" ,
IP4 : & types020 . IPConfig {
IP : * testhelpers . EnsureCIDR ( "1.1.1.2/24" ) ,
} ,
}
expectedConf1 := ` {
"name" : "weave1" ,
"cniVersion" : "0.2.0" ,
"type" : "weave-net"
} `
2019-08-16 18:55:12 +09:00
fExec . addPlugin020 ( nil , "eth0" , expectedConf1 , expectedResult1 , nil )
2019-08-05 15:04:35 -04:00
expectedResult2 := & types020 . Result {
CNIVersion : "0.2.0" ,
IP4 : & types020 . IPConfig {
IP : * testhelpers . EnsureCIDR ( "1.1.1.5/24" ) ,
} ,
}
expectedConf2 := ` {
"name" : "other1" ,
"cniVersion" : "0.2.0" ,
"type" : "other-plugin"
} `
2019-08-16 18:55:12 +09:00
fExec . addPlugin020 ( nil , "net1" , expectedConf2 , expectedResult2 , nil )
2019-08-05 15:04:35 -04:00
2021-03-27 04:12:10 +09:00
fakeMultusNetConf := types . NetConf {
BinDir : "/opt/cni/bin" ,
}
2019-08-05 15:04:35 -04:00
// use fExec for the exec param
rawnetconflist := [ ] byte ( ` { "cniVersion":"0.2.0","name":"weave1","type":"weave-net"} ` )
k8sargs , err := k8sclient . GetK8sArgs ( args )
n , err := types . LoadNetConf ( args . StdinData )
2020-11-03 09:42:30 -05:00
rt , _ := types . CreateCNIRuntimeConf ( args , k8sargs , args . IfName , n . RuntimeConfig , nil )
2019-08-05 15:04:35 -04:00
2021-03-27 04:12:10 +09:00
err = conflistDel ( rt , rawnetconflist , & fakeMultusNetConf , fExec )
2019-08-05 15:04:35 -04:00
Expect ( err ) . To ( HaveOccurred ( ) )
} )
It ( "executes confListDel without error" , func ( ) {
args := & skel . CmdArgs {
ContainerID : "123456789" ,
Netns : testNS . Path ( ) ,
IfName : "eth0" ,
StdinData : [ ] byte ( ` {
"name" : "node-cni-network" ,
"type" : "multus" ,
"delegates" : [ {
"cniVersion" : "0.3.1" ,
"name" : "mynet-confList" ,
"plugins" : [
{
"type" : "firstPlugin" ,
"capabilities" : { "portMappings" : true }
}
]
} ] ,
"runtimeConfig" : {
"portMappings" : [
{ "hostPort" : 8080 , "containerPort" : 80 , "protocol" : "tcp" }
]
}
} ` ) ,
}
2022-02-21 23:55:33 +09:00
fExec := newFakeExec ( )
2019-08-05 15:04:35 -04:00
expectedConf1 := ` {
"capabilities" : { "portMappings" : true } ,
"name" : "mynet-confList" ,
"cniVersion" : "0.3.1" ,
"type" : "firstPlugin" ,
"runtimeConfig" : {
"portMappings" : [
{ "hostPort" : 8080 , "containerPort" : 80 , "protocol" : "tcp" }
]
}
} `
2019-08-16 18:55:12 +09:00
fExec . addPlugin020 ( nil , "eth0" , expectedConf1 , nil , nil )
2020-05-28 16:43:59 -04:00
_ , err := CmdAdd ( args , fExec , nil )
2020-06-11 13:04:20 +09:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
2020-05-28 16:43:59 -04:00
err = CmdDel ( args , fExec , nil )
2019-08-05 15:04:35 -04:00
Expect ( err ) . NotTo ( HaveOccurred ( ) )
} )
2018-06-14 10:20:58 -05:00
} )
2020-01-13 09:57:22 -05:00