• 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
  • 相关阅读:
    Android常用开发工具的用法
    搭建Android开发环境
    开篇 Android系统的体系结构
    学习安卓笔记
    C# DllImport用法和路径问题
    jq 实现无限级地区联动 样式为bootstrap
    YII2 日志
    centos6.5 lamp 环境 使用yum安装方法
    mysql 时间戳 按周、日、月 统计方法 附 date格式
    Yii2.0中文开发向导——控制器(Controller)
  • 原文地址:https://www.cnblogs.com/wangjq19920210/p/13454689.html
Copyright © 2020-2023  润新知