Merge remote-tracking branch 'origin/master' into feature/multus-4.0

This commit is contained in:
Tomofumi Hayashi 2022-04-26 16:46:16 +09:00
commit 59415ad0d6
7 changed files with 111 additions and 12 deletions

View File

@ -72,6 +72,10 @@ jobs:
working-directory: ./e2e
run: ./test-simple-macvlan1.sh
- name: Test static pod
working-directory: ./e2e
run: ./test-static-pod.sh
- name: Test default route1
working-directory: ./e2e
run: ./test-default-route1.sh

View File

@ -39,6 +39,12 @@ containerdConfigPatches:
nodes:
- role: control-plane
- role: worker
kubeadmConfigPatches:
- |
kind: InitConfiguration
nodeRegistration:
kubeletExtraArgs:
pod-manifest-path: "/etc/kubernetes/manifests/"
- role: worker
EOF

11
e2e/simple-static-pod.yml Normal file
View File

@ -0,0 +1,11 @@
apiVersion: v1
kind: Pod
metadata:
name: static-web
annotations:
k8s.v1.cni.cncf.io/networks: "bridge-nad"
spec:
containers:
- name: web
image: centos:8
command: ["/bin/bash", "-c", "trap : TERM INT; sleep infinity & wait"]

15
e2e/static-pod-nad.yml Normal file
View File

@ -0,0 +1,15 @@
apiVersion: "k8s.cni.cncf.io/v1"
kind: NetworkAttachmentDefinition
metadata:
name: bridge-nad
spec:
config: '{
"cniVersion": "0.3.1",
"name": "testnet",
"type": "bridge",
"bridge": "testnet0",
"ipam": {
"type": "host-local",
"subnet": "10.10.0.0/16"
}
}'

22
e2e/test-static-pod.sh Executable file
View File

@ -0,0 +1,22 @@
#!/usr/bin/env bash
set -o errexit
echo "Creating network attachment definition"
kubectl create -f static-pod-nad.yml
echo "Creating static pod config file"
docker cp simple-static-pod.yml kind-worker:/etc/kubernetes/manifests/static-web.yaml
echo "Waiting for static pod to start"
kubectl wait --for=condition=Ready --namespace=default pod/static-web-kind-worker
echo "Checking the pod annotation for net1 interface"
kubectl exec static-web-kind-worker --namespace=default -- ip a show dev net1
echo "Deleting static pod"
docker exec kind-worker /bin/bash -c "rm /etc/kubernetes/manifests/static-web.yaml"
echo "Deleting network attachment definition"
kubectl delete -f static-pod-nad.yml
echo "Test complete"

View File

@ -210,30 +210,32 @@ func NewCNIRuntimeConf(containerID, sandboxID, podName, podNamespace, podUID, ne
var cniDeviceInfoFile string
// get CNI_ARGS and set it if it does not exist in rt.Args
// Populate rt.Args with CNI_ARGS if the rt.Args value is not set
cniArgs := os.Getenv("CNI_ARGS")
if cniArgs != "" {
logging.Debugf("ARGS: %s", cniArgs)
for _, arg := range strings.Split(cniArgs, ";") {
logging.Debugf("arg: /%v/", arg)
keyval := strings.Split(arg, "=")
logging.Debugf("arg: /%q/, keyval: /%q/", arg, keyval)
// SplitN to handle = within values, like BLAH=foo=bar
keyval := strings.SplitN(arg, "=", 2)
if len(keyval) != 2 {
logging.Errorf("CreateCNIRuntimeConf: CNI_ARGS %v %v %d is not recognized as CNI arg, skipped", arg, keyval, len(keyval))
logging.Errorf("CreateCNIRuntimeConf: CNI_ARGS %s %s %d is not recognized as CNI arg, skipped", arg, keyval, len(keyval))
continue
}
envKey := string(keyval[0])
envVal := string(keyval[1])
isExists := false
for _, rtArg := range rt.Args {
if rtArg[0] == envKey {
isExists = true
found := false
for i := range rt.Args {
// Update existing key if its value is empty
if rt.Args[i][0] == envKey && rt.Args[i][1] == "" && envVal != "" {
logging.Debugf("CreateCNIRuntimeConf: add new val: %s", arg)
rt.Args[i][1] = envVal
found = true
break
}
}
if isExists != false {
logging.Debugf("CreateCNIRuntimeConf: add new val: %s", arg)
if !found {
// Add the new key if it didn't exist yet
rt.Args = append(rt.Args, [2]string{envKey, envVal})
}
}

View File

@ -56,6 +56,7 @@ var _ = Describe("config operations", func() {
AfterEach(func() {
Expect(testNS.Close()).To(Succeed())
os.Unsetenv("CNI_PATH")
os.Unsetenv("CNI_ARGS")
err := os.RemoveAll(tmpDir)
Expect(err).NotTo(HaveOccurred())
})
@ -639,6 +640,44 @@ var _ = Describe("config operations", func() {
Expect(rt.CapabilityArgs["portMappings"]).To(Equal(rc.PortMaps))
})
It("creates a valid CNI runtime config with K8s args passed via CNI_ARGS environment variable", 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"
}]
}`),
}
os.Setenv("CNI_ARGS", "K8S_POD_NAME=dummy;K8S_POD_NAMESPACE=namespacedummy;K8S_POD_INFRA_CONTAINER_ID=123456789;K8S_POD_UID=aaaaa;BLAHBLAH=foo=bar")
k8sArgs := &K8sArgs{}
rt, _ := CreateCNIRuntimeConf(args, k8sArgs, "", &RuntimeConfig{}, nil)
fmt.Println("rt.ContainerID: ", rt.ContainerID)
Expect(rt.ContainerID).To(Equal("123456789"))
Expect(rt.NetNS).To(Equal(args.Netns))
Expect(rt.IfName).To(Equal(""))
fmt.Println("rt.ContainerID: ", rt.ContainerID)
Expect(rt.Args[0]).To(Equal([2]string{"IgnoreUnknown", "true"}))
Expect(rt.Args[1]).To(Equal([2]string{"K8S_POD_NAMESPACE", "namespacedummy"}))
Expect(rt.Args[2]).To(Equal([2]string{"K8S_POD_NAME", "dummy"}))
Expect(rt.Args[3]).To(Equal([2]string{"K8S_POD_INFRA_CONTAINER_ID", "123456789"}))
Expect(rt.Args[4]).To(Equal([2]string{"K8S_POD_UID", "aaaaa"}))
Expect(rt.Args[5]).To(Equal([2]string{"BLAHBLAH", "foo=bar"}))
})
It("can loadnetworkstatus", func() {
result := &types020.Result{
CNIVersion: "0.2.0",