-
Notifications
You must be signed in to change notification settings - Fork 173
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(trafficManager): action redirecting to another service
- Loading branch information
Showing
13 changed files
with
385 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2023 The LMP Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://github.com/linuxkerneltravel/lmp/blob/develop/LICENSE | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// author: Woa <[email protected]> | ||
|
||
package acceptance | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
type Router struct { | ||
Route map[string]map[string]http.HandlerFunc | ||
} | ||
|
||
func (r *Router) ServeHTTP(writer http.ResponseWriter, request *http.Request) { | ||
if f, ok := r.Route[request.Method][request.URL.Path]; ok { | ||
f(writer, request) | ||
} | ||
} | ||
|
||
func (r *Router) HandleFunc(method, path string, f http.HandlerFunc) { | ||
method = strings.ToUpper(method) | ||
if r.Route == nil { | ||
r.Route = make(map[string]map[string]http.HandlerFunc) | ||
} | ||
if r.Route[method] == nil { | ||
r.Route[method] = make(map[string]http.HandlerFunc) | ||
} | ||
r.Route[method][path] = f | ||
} | ||
|
||
func startServer(portStr string) { | ||
route := Router{} | ||
route.HandleFunc("GET", "/", func(w http.ResponseWriter, r *http.Request) { | ||
fmt.Fprintf(w, portStr) | ||
}) | ||
|
||
log.Fatal(http.ListenAndServe("localhost:"+portStr, &route)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
eBPF_Supermarket/TrafficManager/acceptance/redirect_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
// Copyright 2023 The LMP Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://github.com/linuxkerneltravel/lmp/blob/develop/LICENSE | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// author: Woa <[email protected]> | ||
|
||
package acceptance | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"testing" | ||
|
||
"lmp/eTrafficManager/bpf" | ||
) | ||
|
||
func TestRedirect(t *testing.T) { | ||
var serverPortList = []string{ | ||
"8001", | ||
"8002", | ||
"8003", | ||
"8004", | ||
"8005", | ||
} | ||
|
||
var repeatNum int64 = 125000 | ||
for _, s := range serverPortList { | ||
go startServer(s) | ||
} | ||
|
||
progs, err := bpf.LoadProgram() | ||
if err != nil { | ||
fmt.Println("[ERROR] Loading program failed:", err) | ||
return | ||
} | ||
s := bpf.Service{ | ||
IP: "1.1.1.1", | ||
Port: "80", | ||
Possibility: 0.75, | ||
} | ||
b := []bpf.Backend{ | ||
{ | ||
IP: "127.0.0.1", | ||
Port: "8001", | ||
Possibility: 0.25, | ||
}, { | ||
IP: "127.0.0.1", | ||
Port: "8002", | ||
Possibility: 0.75, | ||
}, | ||
} | ||
progs.AutoInsertService(s, b, bpf.WeightedAction, nil) | ||
|
||
s2 := bpf.Service{ | ||
IP: "127.0.0.1", | ||
Port: "8003", | ||
} | ||
b2 := []bpf.Backend{ | ||
{ | ||
IP: "127.0.0.1", | ||
Port: "8004", | ||
Possibility: 0.125, | ||
}, | ||
{ | ||
IP: "127.0.0.1", | ||
Port: "8005", | ||
Possibility: 0.125, | ||
}, | ||
} | ||
|
||
progs.AutoInsertService(s2, b2, bpf.RedirectAction, []bpf.Service{s}) | ||
|
||
err = progs.Attach() | ||
if err != nil { | ||
fmt.Println("[ERROR] Attaching failed:", err) | ||
} | ||
|
||
countBucket := make(map[string]int) | ||
|
||
for i := 0; i < int(repeatNum); i++ { | ||
client := &http.Client{ | ||
Transport: &http.Transport{ | ||
// We must set DisableKeepAlives, otherwise all requests will be in one HTTP connection | ||
DisableKeepAlives: true, | ||
}, | ||
} | ||
|
||
url := "http://" + "127.0.0.1:8003" | ||
req, err := http.NewRequest("GET", url, nil) | ||
if err != nil { | ||
fmt.Println("Error creating request:", err) | ||
return | ||
} | ||
|
||
resp, err := client.Do(req) | ||
if err != nil { | ||
fmt.Println("Error sending request:", err) | ||
return | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
fmt.Println("Error reading response:", err) | ||
return | ||
} | ||
|
||
// fmt.Println("Response:", string(body)) | ||
countBucket[string(body)] += 1 | ||
} | ||
|
||
for i := 0; i < len(serverPortList); i++ { | ||
actualNumber := float64(countBucket[serverPortList[i]]) | ||
fmt.Printf("For port: %s, got actualNumber: %d.\n", serverPortList[i], int64(actualNumber)) | ||
} | ||
|
||
fmt.Println("[INFO] Test is done...") | ||
progs.AutoDeleteService(s, nil) | ||
progs.AutoDeleteService(s2, []bpf.Service{s}) | ||
progs.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Oops, something went wrong.