1
0
Fork 0

feat: add highest random weight in kubernetes CRD

This commit is contained in:
Landry Benguigui 2025-09-19 10:18:04 +02:00 committed by GitHub
parent c09d3fb03c
commit 634f892370
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
17 changed files with 1328 additions and 24 deletions

View file

@ -1217,6 +1217,74 @@ func (s *SimpleSuite) TestMirrorCanceled() {
assert.Equal(s.T(), int32(0), val2)
}
func (s *SimpleSuite) TestHighestRandomWeight() {
var count1, count2, count3 int32
service1 := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
atomic.AddInt32(&count1, 1)
rw.WriteHeader(http.StatusOK)
}))
service2 := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
atomic.AddInt32(&count2, 1)
rw.WriteHeader(http.StatusOK)
}))
service3 := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
atomic.AddInt32(&count3, 1)
rw.WriteHeader(http.StatusOK)
}))
service1Server := service1.URL
service2Server := service2.URL
service3Server := service3.URL
file := s.adaptFile("fixtures/highest_random_weight.toml", struct {
Service1Server string
Service2Server string
Service3Server string
}{Service1Server: service1Server, Service2Server: service2Server, Service3Server: service3Server})
s.traefikCmd(withConfigFile(file))
err := try.GetRequest("http://127.0.0.1:8080/api/http/services", 3*time.Second, try.BodyContains("service1", "service2", "service3", "hrw"))
require.NoError(s.T(), err)
// Make 10 requests from the same client (127.0.0.1) - should all go to the same service
client := &http.Client{}
for range 10 {
req, err := http.NewRequest(http.MethodGet, "http://127.0.0.1:8000/whoami", nil)
require.NoError(s.T(), err)
response, err := client.Do(req)
require.NoError(s.T(), err)
assert.Equal(s.T(), http.StatusOK, response.StatusCode)
response.Body.Close()
}
// Check if all requests went to the same service
val1 := atomic.LoadInt32(&count1)
val2 := atomic.LoadInt32(&count2)
val3 := atomic.LoadInt32(&count3)
// All requests should have been handled (total should be 10)
assert.Equal(s.T(), int32(10), val1+val2+val3)
// All requests from same remoteAddr (127.0.0.1) should go to exactly one service
servicesUsed := 0
if val1 > 0 {
servicesUsed++
}
if val2 > 0 {
servicesUsed++
}
if val3 > 0 {
servicesUsed++
}
assert.Equal(s.T(), 1, servicesUsed, "All requests from same remoteAddr should go to exactly one service")
}
func (s *SimpleSuite) TestSecureAPI() {
s.createComposeProject("base")