支持指定ip/You can request a specific IP address through Kubernetes annotations
代码拷贝自已有的 cni ipam 实现——static
copy from CNI static ipam plugin
const kubeconfig = `
set your own kubeconfig
`
func NewClient() (*kubernetes.Clientset, error) {
//kubeconfig, err := ioutil.ReadFile("./kube-config")
//if err != nil {
// return nil, err
//}
restConf, err := clientcmd.RESTConfigFromKubeConfig([]byte(kubeconfig))
if err != nil {
return nil, err
}
return kubernetes.NewForConfig(restConf)
}
{
"cniVersion": "0.3.1",
"name": "macnet",
"type": "macvlan",
"master": "eno3.96",
"ipam": {
"type": "static-ipam",
"addresses": [
{
"address": "172.31.204.19/16", // return default ip if there is error
"gateway": "172.31.0.1"
}
],
"routes": [
{ "dst": "0.0.0.0/0" }
]
}
}
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: hello-world
spec:
replicas: 1
selector:
matchLabels:
appName: hello-world
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
appName: hello-world
type: web
name: hello-world
annotations:
static-ipam.ipv4: "172.31.204.12"
...