|
| 1 | +package network |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/Azure/azure-container-networking/cni" |
| 10 | + "github.com/Azure/azure-container-networking/cns" |
| 11 | + "github.com/Azure/azure-container-networking/cns/cnsclient" |
| 12 | + "github.com/Azure/azure-container-networking/common" |
| 13 | + "github.com/Azure/azure-container-networking/log" |
| 14 | + "github.com/Azure/azure-container-networking/network" |
| 15 | + cniTypes "github.com/containernetworking/cni/pkg/types" |
| 16 | + cniTypesCurr "github.com/containernetworking/cni/pkg/types/current" |
| 17 | +) |
| 18 | + |
| 19 | +func SetupRoutingForMultitenancy(nwCfg *cni.NetworkConfig, cnsNetworkConfig *cns.GetNetworkContainerResponse, epInfo *network.EndpointInfo, result *cniTypesCurr.Result) { |
| 20 | + // Adding default gateway |
| 21 | + if nwCfg.MultiTenancy { |
| 22 | + // if snat enabled, add 169.254.0.1 as default gateway |
| 23 | + if nwCfg.EnableSnatOnHost { |
| 24 | + log.Printf("add default route for multitenancy.snat on host enabled") |
| 25 | + addDefaultRoute(cnsNetworkConfig.LocalIPConfiguration.GatewayIPAddress, epInfo, result) |
| 26 | + } else { |
| 27 | + _, defaultIPNet, _ := net.ParseCIDR("0.0.0.0/0") |
| 28 | + dstIP := net.IPNet{IP: net.ParseIP("0.0.0.0"), Mask: defaultIPNet.Mask} |
| 29 | + gwIP := net.ParseIP(cnsNetworkConfig.IPConfiguration.GatewayIPAddress) |
| 30 | + epInfo.Routes = append(epInfo.Routes, network.RouteInfo{Dst: dstIP, Gw: gwIP}) |
| 31 | + result.Routes = append(result.Routes, &cniTypes.Route{Dst: dstIP, GW: gwIP}) |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func GetContainerNetworkConfiguration(multiTenancy bool, address string, podName string, podNamespace string) (*cniTypesCurr.Result, *cns.GetNetworkContainerResponse, net.IPNet, error) { |
| 37 | + if multiTenancy { |
| 38 | + podNameWithoutSuffix := getPodNameWithoutSuffix(podName) |
| 39 | + log.Printf("Podname without suffix %v", podNameWithoutSuffix) |
| 40 | + return getContainerNetworkConfiguration(address, podNamespace, podNameWithoutSuffix) |
| 41 | + } |
| 42 | + |
| 43 | + return nil, nil, net.IPNet{}, nil |
| 44 | +} |
| 45 | + |
| 46 | +func getContainerNetworkConfiguration(address string, namespace string, podName string) (*cniTypesCurr.Result, *cns.GetNetworkContainerResponse, net.IPNet, error) { |
| 47 | + cnsClient, err := cnsclient.NewCnsClient(address) |
| 48 | + if err != nil { |
| 49 | + log.Printf("Initializing CNS client error %v", err) |
| 50 | + return nil, nil, net.IPNet{}, err |
| 51 | + } |
| 52 | + |
| 53 | + podInfo := cns.KubernetesPodInfo{PodName: podName, PodNamespace: namespace} |
| 54 | + orchestratorContext, err := json.Marshal(podInfo) |
| 55 | + if err != nil { |
| 56 | + log.Printf("Marshalling KubernetesPodInfo failed with %v", err) |
| 57 | + return nil, nil, net.IPNet{}, err |
| 58 | + } |
| 59 | + |
| 60 | + networkConfig, err := cnsClient.GetNetworkConfiguration(orchestratorContext) |
| 61 | + if err != nil { |
| 62 | + log.Printf("GetNetworkConfiguration failed with %v", err) |
| 63 | + return nil, nil, net.IPNet{}, err |
| 64 | + } |
| 65 | + |
| 66 | + log.Printf("Network config received from cns %+v", networkConfig) |
| 67 | + |
| 68 | + subnetPrefix := common.GetInterfaceSubnetWithSpecificIp(networkConfig.PrimaryInterfaceIdentifier) |
| 69 | + if subnetPrefix == nil { |
| 70 | + errBuf := fmt.Sprintf("Interface not found for this ip %v", networkConfig.PrimaryInterfaceIdentifier) |
| 71 | + log.Printf(errBuf) |
| 72 | + return nil, nil, net.IPNet{}, fmt.Errorf(errBuf) |
| 73 | + } |
| 74 | + |
| 75 | + return convertToCniResult(networkConfig), networkConfig, *subnetPrefix, nil |
| 76 | +} |
| 77 | + |
| 78 | +func convertToCniResult(networkConfig *cns.GetNetworkContainerResponse) *cniTypesCurr.Result { |
| 79 | + result := &cniTypesCurr.Result{} |
| 80 | + resultIpconfig := &cniTypesCurr.IPConfig{} |
| 81 | + |
| 82 | + ipconfig := networkConfig.IPConfiguration |
| 83 | + ipAddr := net.ParseIP(ipconfig.IPSubnet.IPAddress) |
| 84 | + |
| 85 | + if ipAddr.To4() != nil { |
| 86 | + resultIpconfig.Version = "4" |
| 87 | + resultIpconfig.Address = net.IPNet{IP: ipAddr, Mask: net.CIDRMask(int(ipconfig.IPSubnet.PrefixLength), 32)} |
| 88 | + } else { |
| 89 | + resultIpconfig.Version = "6" |
| 90 | + resultIpconfig.Address = net.IPNet{IP: ipAddr, Mask: net.CIDRMask(int(ipconfig.IPSubnet.PrefixLength), 128)} |
| 91 | + } |
| 92 | + |
| 93 | + resultIpconfig.Gateway = net.ParseIP(ipconfig.GatewayIPAddress) |
| 94 | + result.IPs = append(result.IPs, resultIpconfig) |
| 95 | + result.DNS.Nameservers = ipconfig.DNSServers |
| 96 | + |
| 97 | + if networkConfig.Routes != nil && len(networkConfig.Routes) > 0 { |
| 98 | + for _, route := range networkConfig.Routes { |
| 99 | + _, routeIPnet, _ := net.ParseCIDR(route.IPAddress) |
| 100 | + gwIP := net.ParseIP(route.GatewayIPAddress) |
| 101 | + result.Routes = append(result.Routes, &cniTypes.Route{Dst: *routeIPnet, GW: gwIP}) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + for _, ipRouteSubnet := range networkConfig.CnetAddressSpace { |
| 106 | + log.Printf("Adding cnetAddressspace routes %v %v", ipRouteSubnet.IPAddress, ipRouteSubnet.PrefixLength) |
| 107 | + routeIPnet := net.IPNet{IP: net.ParseIP(ipRouteSubnet.IPAddress), Mask: net.CIDRMask(int(ipRouteSubnet.PrefixLength), 32)} |
| 108 | + gwIP := net.ParseIP(ipconfig.GatewayIPAddress) |
| 109 | + result.Routes = append(result.Routes, &cniTypes.Route{Dst: routeIPnet, GW: gwIP}) |
| 110 | + } |
| 111 | + |
| 112 | + return result |
| 113 | +} |
| 114 | + |
| 115 | +func getPodNameWithoutSuffix(podName string) string { |
| 116 | + nameSplit := strings.Split(podName, "-") |
| 117 | + log.Printf("namesplit %v", nameSplit) |
| 118 | + if len(nameSplit) > 2 { |
| 119 | + nameSplit = nameSplit[:len(nameSplit)-2] |
| 120 | + } else { |
| 121 | + return podName |
| 122 | + } |
| 123 | + |
| 124 | + log.Printf("Pod name after splitting based on - : %v", nameSplit) |
| 125 | + return strings.Join(nameSplit, "-") |
| 126 | +} |
0 commit comments