// doLoadBalance implement the loadbalance function
doLoadBalance implement the loadbalance function
// doLoadBalance implement the loadbalance function
func doLoadBalance(svcName string, lport int64) (net.Addr, error) {
	svc := strings.Split(svcName, ".")
	namespace, name := svc[0], svc[1]
	pods, err := metaClient.Services(namespace).GetPods(name)
	if err != nil {
		klog.Errorf("[L4 Proxy] get svc error : %s", err)
	}
	runPods := make([]v1.Pod, 0, len(pods))
	for i := 0; i < len(pods); i++ {
		if pods[i].Status.Phase == v1.PodRunning {
			runPods = append(runPods, pods[i])
		}
	}
	rand.Seed(time.Now().UnixNano())
	idx := rand.Uint32() % uint32(len(runPods))
	hostIP := runPods[idx].Status.HostIP
	st := addrByService.getAddrTable(svcName)
	index := 0
	for i, p := range st.ports {
		if p == int32(lport) {
			index = i
			break
		}
	}
	tp := st.targetPort[index]
	targetPort := int32(0)
	for _, value := range runPods[idx].Spec.Containers {
		for _, v := range value.Ports {
			if v.ContainerPort == tp {
				targetPort = v.HostPort
				break
			}
		}
	}
	return &net.TCPAddr{
		IP:   net.ParseIP(hostIP),
		Port: int(targetPort),
	}, nil
}
func doLoadBalance ( svcName string , lport int64 ) ( net . Addr , error ) { svc := strings . Split ( svcName , " " ) namespace , name := svc [ 0 ] , svc [ 1 ] pods , err := metaClient . Services ( namespace ) . GetPods ( name ) if err != nil { klog . Errorf ( " " , err ) } runPods := make ( [ ] v1 . Pod , 0 , len ( pods ) ) for i := 0 ; i < len ( pods ) ; i ++ { if pods [ i ] . Status . Phase == v1 . PodRunning { runPods = append ( runPods , pods [ i ] ) } } rand . Seed ( time . Now ( ) . UnixNano ( ) ) idx := rand . Uint32 ( ) % uint32 ( len ( runPods ) ) hostIP := runPods [ idx ] . Status . HostIP st := addrByService . getAddrTable ( svcName ) index := 0 for i , p := range st . ports { if p == int32 ( lport ) { index = i break } } tp := st . targetPort [ index ] targetPort := int32 ( 0 ) for _ , value := range runPods [ idx ] . Spec . Containers { for _ , v := range value . Ports { if v . ContainerPort == tp { targetPort = v . HostPort break } } } return & net . TCPAddr { IP : net . ParseIP ( hostIP ) , Port : int ( targetPort ) , } , nil }
doLoadBalance implement the loadbalance function
doLoadBalance implement the loadbalance function
// checkout the status of pods // support random LB for the early version // kubeedge edgemesh support bridge net ,so, use hostport to access