cni-ipam-etcd demo

链接:https://github.com/jeremyxu2010/cni-ipam-etcd

测试demo:

package main

import (
        "fmt"
        "github.com/containernetworking/cni/pkg/types"
        "github.com/jeremyxu2010/cni-ipam-etcd/backend/allocator"
        "github.com/jeremyxu2010/cni-ipam-etcd/backend/etcd"
        "net"
)

func main() {
        fmt.Println("Hello, World!")
        n := allocator.Net{}
        fmt.Println("%+v", n)

        mask := net.IPv4Mask(byte(255), byte(255), byte(255), byte(0))
        ip := net.ParseIP("192.168.1.0").Mask(mask)
        subnet := types.IPNet{ip, mask}

        ipamRange := allocator.Range{
                RangeStart: net.ParseIP("192.168.1.10"),
                RangeEnd:   net.ParseIP("192.168.1.250"),
                Subnet:     subnet,
                Gateway:    net.ParseIP("192.168.1.1"),
        }

        etcdConfig := allocator.EtcdConfig{
                EtcdURL: "http://0.0.0.0:2379",
        }

        ipamConf := allocator.IPAMConfig{
                Name:       "network-id",
                Type:       "etcd",
                Ranges:     []allocator.RangeSet{[]allocator.Range{ipamRange}},
                IPArgs:     []net.IP{net.ParseIP("192.168.1.100"), net.ParseIP("192.168.1.200")},
                EtcdConfig: &etcdConfig,
        }
        n.IPAM = &ipamConf

        fmt.Println("%+v", ipamConf.Ranges)

        store, err := etcd.New(ipamConf.Name, &ipamConf)
        if err != nil {
                return
        }
        defer store.Close()

        portId := "port-id"
        allocator := allocator.NewIPAllocator(&ipamConf.Ranges[0], store, 0)

        // allocate ip
        if false {
                var requestedIP net.IP
                ipConf, err := allocator.Get(portId, requestedIP)
                if err != nil {
                        fmt.Errorf("failed to allocate for range: %v", err)
                        return
                }
                fmt.Println(allocator, ipConf)
        }

        // release ip
        {
                err := allocator.Release(portId)
                if err != nil {
                        fmt.Errorf("failed to relase for range: %v", err)
                        return
                }
                fmt.Println(allocator)
        }
}

运行结果:

Hello, World!
%+v {  <nil> {[]} <nil>}
%+v [[{192.168.1.10 192.168.1.250 {192.168.1.0 ffffff00} 192.168.1.1}]]
&{0xc0000a6b00 0xc00000e180 0} {Version:4 Interface:<nil> Address:{IP:192.168.1.20 Mask:ffffff00} Gateway:192.168.1.1}

 etcd 数据查看:

[root@wangjunqiang cni-ipam-etcd]# export ETCDCTL_API=3
[root@wangjunqiang cni-ipam-etcd]# etcdctl get --prefix /
/etcd-cni/networks/network-id
[[{"rangeStart":"192.168.1.10","rangeEnd":"192.168.1.250","subnet":"192.168.1.0/24","gateway":"192.168.1.1"}]]
/etcd-cni/networks/network-id/lastReserved/0
192.168.1.11
[root@wangjunqiang cni-ipam-etcd]# etcdctl get --prefix /
/etcd-cni/networks/network-id
[[{"rangeStart":"192.168.1.10","rangeEnd":"192.168.1.250","subnet":"192.168.1.0/24","gateway":"192.168.1.1"}]]
/etcd-cni/networks/network-id/lastReserved/0
192.168.1.12
/etcd-cni/networks/network-id/used/192.168.1.12
port-id
[root@wangjunqiang cni-ipam-etcd]# etcdctl get --prefix /
/etcd-cni/networks/network-id
[[{"rangeStart":"192.168.1.10","rangeEnd":"192.168.1.250","subnet":"192.168.1.0/24","gateway":"192.168.1.1"}]]
/etcd-cni/networks/network-id/lastReserved/0
192.168.1.12
原文地址:https://www.cnblogs.com/wangjq19920210/p/13454689.html