func InsertPodCidrInCniSpec(cniConfFilePath string, cidr string) error {
	file, err := ioutil.ReadFile(cniConfFilePath)
	if err != nil {
		return fmt.Errorf("failed to load CNI conf file: %s", err.Error())
	}
	var config interface{}
	if strings.HasSuffix(cniConfFilePath, ".conflist") {
		err = json.Unmarshal(file, &config)
		if err != nil {
			return fmt.Errorf("failed to parse JSON from CNI conf file: %s", err.Error())
		}
		updatedCidr := false
		configMap := config.(map[string]interface{})
		for key := range configMap {
			if key != "plugins" {
				continue
			}
			pluginConfigs := configMap["plugins"].([]interface{})
			for _, pluginConfig := range pluginConfigs {
				pluginConfigMap := pluginConfig.(map[string]interface{})
				if val, ok := pluginConfigMap["ipam"]; ok {
					valObj := val.(map[string]interface{})
					valObj["subnet"] = cidr
					updatedCidr = true
					break
				}
			}
		}
		if !updatedCidr {
			return fmt.Errorf("failed to insert subnet cidr into CNI conf file: %s as CNI file is invalid", cniConfFilePath)
		}
	} else {
		err = json.Unmarshal(file, &config)
		if err != nil {
			return fmt.Errorf("failed to parse JSON from CNI conf file: %s", err.Error())
		}
		pluginConfig := config.(map[string]interface{})
		pluginConfig["ipam"].(map[string]interface{})["subnet"] = cidr
	}
	configJSON, _ := json.Marshal(config)
	err = ioutil.WriteFile(cniConfFilePath, configJSON, 0644)
	if err != nil {
		return fmt.Errorf("failed to insert subnet cidr into CNI conf file: %s", err.Error())
	}
	return nil
}
InsertPodCidrInCniSpec inserts the pod CIDR allocated to the node by kubernetes controller manager
and stored it in the CNI specification
package utils
import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net"
	"strings"
	"github.com/containernetworking/cni/libcni"
	"github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator"
	"k8s.io/client-go/kubernetes"
)
const (
	podCIDRAnnotation = "kube-router.io/pod-cidr"
)
func GetPodCidrFromCniSpec(cniConfFilePath string) (net.IPNet, error) {
	var podCidr = net.IPNet{}
	var err error
	var ipamConfig *allocator.IPAMConfig
	if strings.HasSuffix(cniConfFilePath, ".conflist") {
		var confList *libcni.NetworkConfigList
		confList, err = libcni.ConfListFromFile(cniConfFilePath)
		if err != nil {
			return net.IPNet{}, fmt.Errorf("failed to load CNI config list file: %s", err.Error())
		}
		for _, conf := range confList.Plugins {
			if conf.Network.IPAM.Type != "" {
				ipamConfig, _, err = allocator.LoadIPAMConfig(conf.Bytes, "")
				if err != nil {
					if err.Error() != "no IP ranges specified" {
						return net.IPNet{}, fmt.Errorf("failed to get IPAM details from the CNI conf file: %s", err.Error())
					}
				}
				break
			}
		}
	} else {
		netconfig, err := libcni.ConfFromFile(cniConfFilePath)
		if err != nil {
			return net.IPNet{}, fmt.Errorf("failed to load CNI conf file: %s", err.Error())
		}
		ipamConfig, _, err = allocator.LoadIPAMConfig(netconfig.Bytes, "")
		if err != nil {
			if err.Error() != "no IP ranges specified" {
				return net.IPNet{}, fmt.Errorf("failed to get IPAM details from the CNI conf file: %s", err.Error())
			}
			return net.IPNet{}, nil
		}
	}
	if ipamConfig != nil && len(ipamConfig.Ranges) > 0 {
		for _, rangeset := range ipamConfig.Ranges {
			for _, item := range rangeset {
				if item.Subnet.IP != nil {
					podCidr = net.IPNet(item.Subnet)
					break
				}
			}
		}
	}
	return podCidr, nil
}