首页 > 其他分享 >手写实现cni插件

手写实现cni插件

时间:2024-02-08 22:11:40浏览次数:33  
标签:插件 github exec ip 手写 klog com cni

k8s v1.19.0

mycni配置文件

cat >> /etc/cni/net.d/mycni.json << EOF
{
  "cniVersion": "0.2.0",
  "name": "mycni",
  "type": "mycni"
}
EOF

type对应/opt/cni/bin目录下二进制文件。

mycni代码并编译

mkdir /run/netns
# go.mod
module mycni

go 1.19

require (
	github.com/containernetworking/cni v1.0.1
	github.com/containernetworking/plugins v1.1.1
	k8s.io/klog/v2 v2.100.1
)

require (
	github.com/fsnotify/fsnotify v1.5.1 // indirect
	github.com/go-logr/logr v1.2.0 // indirect
	github.com/onsi/ginkgo v1.16.5 // indirect
	github.com/onsi/gomega v1.17.0 // indirect
	golang.org/x/net v0.0.0-20210825183410-e898025ed96a // indirect
	golang.org/x/sys v0.0.0-20211029165221-6e7872819dc8 // indirect
	golang.org/x/text v0.3.7 // indirect
)

# main.go
package main

import (
	"flag"
	"fmt"
	"os/exec"

	"github.com/containernetworking/cni/pkg/skel"
	"github.com/containernetworking/cni/pkg/version"
	"github.com/containernetworking/plugins/pkg/utils/buildversion"
	"k8s.io/klog/v2"
)

func cmdAdd(args *skel.CmdArgs) error {
	klog.InitFlags(nil)
	flag.Set("logtostderr", "false")
	flag.Set("log_file", "/var/log/mycni.log")
	flag.Parse()
	defer klog.Flush()

	klog.Infof("cmd add args is %v", *args)

	cmds := []string{
		"ip link add veth00 type veth peer name veth01",
		"ip link set dev veth01 up",
		fmt.Sprintf("ln -s %s /run/netns/ns1", args.Netns),
		"ip link set veth00 netns ns1",
		"ip netns exec ns1 ip link set veth00 name eth0",
		"ip netns exec ns1 ip addr add 10.16.0.10/24 dev eth0",
		"ip netns exec ns1 ip link set dev eth0 up",
		"ip netns exec ns1 ip route add default dev eth0",
	}
	for i := 0; i < 8; i++ {
		if out, err := exec.Command("bash", "-c", cmds[i]).CombinedOutput(); err != nil {
			klog.Errorf("exec %s failed, out is %s", cmds[i], string(out))
			return err
		}
	}

	fmt.Println("{}")
	klog.Infof("cmd add success")
	return nil
}

func cmdDel(args *skel.CmdArgs) error {
	klog.InitFlags(nil)
	flag.Set("logtostderr", "false")
	flag.Set("log_file", "/var/log/mycni.log")
	flag.Parse()
	defer klog.Flush()

	cmd := "ip link delete veth01 type veth"
	exec.Command("bash", "-c", cmd).CombinedOutput()
	cmd = "rm -f /run/netns/ns1"
	exec.Command("bash", "-c", cmd).CombinedOutput()

	klog.Infof("cmd del end")
	return nil
}

// 确认cni是否配置完成
func cmdCheck(args *skel.CmdArgs) error {
	return nil
}

func main() {
	skel.PluginMain(cmdAdd, cmdCheck, cmdDel, version.All, buildversion.BuildString("mycni"))
}
ip route add 10.16.0.10/32 dev veth01

问题:unexpected end of JSON input

CNI规定ADD操作结果以JSON格式打印到标准输出Stdout,否则ADD失败。

如果cni二进制执行后没有输出结果到Stdout,那么json.Unmarshal长度是0的[]byte时报错unexpected end of JSON input。

解决方法是fmt.Println("{}")。

标签:插件,github,exec,ip,手写,klog,com,cni
From: https://www.cnblogs.com/WJQ2017/p/18012180

相关文章

  • pytorch MNIST数据集手写数字识别
    MNIST包括6万张28x28的训练样本,1万张测试样本,很多教程都会对它”下手”几乎成为一个“典范”,可以说它就是计算机视觉里面的HelloWorld。所以我们这里也会使用MNIST来进行实战。importtorchimporttorch.nnasnnimporttorch.nn.functionalasFimporttorch.optimasopt......
  • 解析Sermant热插拔能力:服务运行时动态挂载JavaAgent和插件
    本文分享自华为云社区《服务运行时动态挂载JavaAgent和插件——Sermant热插拔能力解析》,作者:华为云高级软件工程师栾文飞一、概述Sermant是基于Java字节码增强技术的无代理服务网格,其利用Java字节码增强技术,为宿主应用程序提供服务治理功能,以解决大规模微服务场景中的服务治理......
  • 图片延迟加载插件lazyload使用
    图片懒加载,又称图片延时加载、惰性加载,即在用户需要使用图片的时候才加载。这种技术的主要好处包括:优化页面加载速度:在页面被请求时,只加载可视区域的图片,其它部分的图片则不加载,只有当这些图片出现在可视区域时才会动态加载。这样可以减少首次加载时需要加载的内容量,显著提高页面加......
  • x64dbg 防止丢失数据库的插件 DbGit
    我跟了一晚上,说丢就丢了,真的无语~找到个使用git的方式备份数据库的插件DbGit 试了下,使用很简单!在手动保存或退出x64dbg时会自动执行 git add -A git commit -m命令。数据库似乎必须得在软件目录下的db目录才行,在被调试程序目录,似乎不行! 从此不会丢数据了,也算......
  • SpringBoot的maven插件生成可以直接启动的jar
    简单使用<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration&g......
  • Android Studio 中使用uiautomatorviewer插件
    在Android设备中,各种的组件如何分布以及控件的具体信息如何查看呢。在AndroidStudio中有一个可以查看的插件,本篇文章就介绍这个插件的使用- uiautomatorviewer.bat1.综述  AndroidStudio是Google官方提供的一款用于开发Android应用程序的集成开发环境(IDE),它基于IntelliJIDEA......
  • MyBatis使用分页插件
    MyBatis使用分页插件PageHelper是国内非常优秀的开源mybatis分页插件,它支持基本主流与常用的数据库,例如:mysql、Oracle、DB2等。PageHelper在GitHub的项目地址:https://github.com/pagehelper/Mybatis-PageHelper分页插件的原理(1)在MyBatis执行SQL语句之前,拦截器可以获取SQL......
  • 利用Jquery Lazyload JS插件实现网页图片延迟加载
    JqueryLazyload是一款网页图片延迟加载JS插件,本文介绍该JS的使用方法。最新的jquerylazyload可以单独使用(即不依赖jquery),本文介绍的是依赖jquery的使用及配置方法。Github项目地址:https://github.com/tuupola/lazyload本文使用的是1.9.7版本(实际与1.9.6版本一致)首先需要引用......
  • 第22天:安全开发-PHP应用&留言板功能&超全局变量&数据库操作&第三方插件引用
    #数据库操作-mysqli函数&增删改查PHP函数:连接,选择,执行,结果,关闭等参考:https://www.runoob.com/php/php-ref-mysqli.html常用:mysqli_connect()打开一个到MySQL的新的连接。mysqli_select_db()更改连接的默认数据库。mysqli_query()执行某个针对数据库的查询。mysqli_fetch......
  • pinterest插件集中移动图片到指定文件夹
    importosimportshutildefis_image_file(filename):"""检查文件是否为图片文件。"""image_extensions=['.jpg','.jpeg','.png','.gif','.bmp','.tiff']returnany......