Skip to content

Commit

Permalink
feat(trafficManager): dynamic cluster monitoring and selection weight…
Browse files Browse the repository at this point in the history
… modification
  • Loading branch information
ESWZY committed Sep 18, 2023
1 parent c8ddc0e commit ffc5267
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 241 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package automatic

import (
"fmt"
"strconv"
"testing"
"time"

"lmp/eTrafficManager/bpf"
"lmp/eTrafficManager/pkg/k8s"
"lmp/eTrafficManager/pkg/metrics"
)

func TestAutomatic(t *testing.T) {
fmt.Println("Starting...")
namespace := "default"
serviceName := "sisyphe-sfs"
interval := time.Second * 3
promHost, err := metrics.GetPromHost()
if err != nil || promHost == "" {
fmt.Println("[WARNING]", err)
fmt.Println("[WARNING] Skipping test...")
return
}
cs := metrics.NodeExporterClusterMetrics{Address: promHost}
err = cs.Update()
if err != nil {
fmt.Println("[WARNING]", err)
fmt.Println("[WARNING] Skipping test...")
return
}

programs, err := bpf.LoadProgram()
defer programs.Close()
if err != nil {
fmt.Println("[ERROR] Loading program failed:", err)
return
}
err = programs.Attach()
if err != nil {
fmt.Println("[ERROR] Attaching failed:", err)
}
service, pods, err := k8s.GetPodByService(serviceName, namespace, "")
if err != nil {
panic(err.Error())
}

for {
programs.InsertServiceItem(service.Spec.ClusterIP, strconv.Itoa(int(service.Spec.Ports[0].Port)), len(pods.Items), bpf.RandomAction)
fmt.Printf("Service: %s, Service IP: %s, Ports: %v\n", service.Name, service.Spec.ClusterIPs, service.Spec.Ports)
fmt.Printf("Pods:\n")

var totalAvailableRate float64

for _, pod := range pods.Items {
p := pod.Spec.Containers[0].Ports[0]
fmt.Printf("- %s, IP: %s, Ports: %v\n", pod.Name, pod.Status.PodIP, strconv.Itoa(int(p.ContainerPort))+"|"+strconv.Itoa(int(p.HostPort))+"|"+string(p.Protocol))
hostIP := pod.Status.HostIP
nodeMetric := cs.Query(hostIP).(metrics.NodeExporterNodeMetric)
fmt.Println(nodeMetric.AvailableRate())
totalAvailableRate += nodeMetric.AvailableRate()
}

totalPercentage := 0.0
for i := 0; i < len(pods.Items); i++ {
hostIP := pods.Items[i].Status.HostIP
nodeMetric := cs.Query(hostIP).(metrics.NodeExporterNodeMetric)
possibility := nodeMetric.AvailableRate() / totalAvailableRate
totalPercentage += possibility
programs.AutoInsertBackend(service.Spec.ClusterIP, strconv.Itoa(int(service.Spec.Ports[0].Port)), pods.Items[i].Status.PodIP, strconv.Itoa(int(pods.Items[i].Spec.Containers[0].Ports[0].ContainerPort)), i+1, possibility) // TODO: +totalPercentage
}

time.Sleep(interval)
service, pods, err = k8s.GetPodByService(serviceName, namespace, "")
if err != nil {
panic(err.Error())
}
cs.Update()
s := bpf.Service{
IP: service.Spec.ClusterIPs[0],
Port: strconv.Itoa(int(service.Spec.Ports[0].Port)),
}
programs.AutoDeleteService(s, nil)
}

}
6 changes: 3 additions & 3 deletions eBPF_Supermarket/TrafficManager/bpf/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,8 @@ func (p *Programs) AutoDeleteService(service Service, affiliatedServiceList []Se
return true
}

// DeclareBackendID gets an increasing backendID
func (p *Programs) DeclareBackendID() int {
// declareBackendID gets an increasing backendID
func (p *Programs) declareBackendID() int {
// TODO: concurrency protection
backendID := p.currentIndex
p.currentIndex++
Expand All @@ -287,7 +287,7 @@ func (p *Programs) DeclareBackendID() int {

// AutoInsertBackend inserts an organized backend item into map
func (p *Programs) AutoInsertBackend(serviceIP string, servicePortStr string, backendIP string, backendPortStr string, slotIndex int, possibility float64, possibilityUpperBound float64) (bool, int) {
backendID := p.DeclareBackendID()
backendID := p.declareBackendID()
servicePort, _ := strconv.Atoi(servicePortStr)
backendPort, _ := strconv.Atoi(backendPortStr)
ok := p.InsertBackendItem(serviceIP, servicePort, backendIP, backendPort, backendID, slotIndex, possibility, possibilityUpperBound)
Expand Down
2 changes: 2 additions & 0 deletions eBPF_Supermarket/TrafficManager/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ go 1.20
require (
github.com/cilium/cilium v1.14.0-snapshot.6
github.com/cilium/ebpf v0.11.0
github.com/prometheus/client_golang v1.16.0
github.com/prometheus/common v0.42.0
k8s.io/api v0.27.2
k8s.io/apimachinery v0.27.2
k8s.io/client-go v0.27.2
Expand Down
Loading

0 comments on commit ffc5267

Please sign in to comment.