mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 15:25:57 +00:00
Merge pull request #50447 from m1093782566/e2e-session-affinity
Automatic merge from submit-queue (batch tested with PRs 50447, 53308). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. [e2e] add service session affinity test case **What this PR does / why we need it**: **Which issue this PR fixes**: Add service session affinity test case for e2e fixes #31712 **Special notes for your reviewer**: **Release note**: ```release-note NONE ```
This commit is contained in:
commit
8849878bab
@ -61,6 +61,8 @@ const (
|
||||
testTries = 30
|
||||
// Maximum number of pods in a test, to make test work in large clusters.
|
||||
maxNetProxyPodsCount = 10
|
||||
// Number of checks to hit a given set of endpoints when enable session affinity.
|
||||
SessionAffinityChecks = 10
|
||||
)
|
||||
|
||||
var NetexecImageName = imageutils.GetE2EImage(imageutils.Netexec)
|
||||
@ -218,6 +220,47 @@ func (config *NetworkingTestConfig) DialFromContainer(protocol, containerIP, tar
|
||||
Failf("Failed to find expected endpoints:\nTries %d\nCommand %v\nretrieved %v\nexpected %v\n", maxTries, cmd, eps, expectedEps)
|
||||
}
|
||||
|
||||
func (config *NetworkingTestConfig) GetEndpointsFromTestContainer(protocol, targetIP string, targetPort, maxTries, minTries int) (sets.String, error) {
|
||||
return config.GetEndpointsFromContainer(protocol, config.TestContainerPod.Status.PodIP, targetIP, TestContainerHttpPort, targetPort, maxTries, minTries)
|
||||
}
|
||||
|
||||
func (config *NetworkingTestConfig) GetEndpointsFromContainer(protocol, containerIP, targetIP string, containerHttpPort, targetPort, maxTries, minTries int) (sets.String, error) {
|
||||
cmd := fmt.Sprintf("curl -q -s 'http://%s:%d/dial?request=hostName&protocol=%s&host=%s&port=%d&tries=1'",
|
||||
containerIP,
|
||||
containerHttpPort,
|
||||
protocol,
|
||||
targetIP,
|
||||
targetPort)
|
||||
|
||||
eps := sets.NewString()
|
||||
|
||||
for i := 0; i < maxTries; i++ {
|
||||
stdout, stderr, err := config.f.ExecShellInPodWithFullOutput(config.HostTestContainerPod.Name, cmd)
|
||||
if err != nil {
|
||||
// A failure to kubectl exec counts as a try, not a hard fail.
|
||||
// Also note that we will keep failing for maxTries in tests where
|
||||
// we confirm unreachability.
|
||||
Logf("Failed to execute %q: %v, stdout: %q, stderr: %q", cmd, err, stdout, stderr)
|
||||
} else {
|
||||
var output map[string][]string
|
||||
if err := json.Unmarshal([]byte(stdout), &output); err != nil {
|
||||
Logf("WARNING: Failed to unmarshal curl response. Cmd %v run in %v, output: %s, err: %v",
|
||||
cmd, config.HostTestContainerPod.Name, stdout, err)
|
||||
continue
|
||||
}
|
||||
|
||||
for _, hostName := range output["responses"] {
|
||||
trimmed := strings.TrimSpace(hostName)
|
||||
if trimmed != "" {
|
||||
eps.Insert(trimmed)
|
||||
}
|
||||
}
|
||||
return eps, nil
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("Failed to get endpoints:\nTries %d\nCommand %v\n", minTries, cmd)
|
||||
}
|
||||
|
||||
// DialFromNode executes a tcp or udp request based on protocol via kubectl exec
|
||||
// in a test container running with host networking.
|
||||
// - minTries is the minimum number of curl attempts required before declaring
|
||||
|
@ -19,7 +19,9 @@ package network
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/util/sets"
|
||||
"k8s.io/kubernetes/pkg/master/ports"
|
||||
"k8s.io/kubernetes/test/e2e/framework"
|
||||
@ -199,6 +201,51 @@ var _ = SIGDescribe("Networking", func() {
|
||||
By(fmt.Sprintf("dialing(udp) %v (node) --> %v:%v (nodeIP)", config.NodeIP, config.NodeIP, config.NodeUdpPort))
|
||||
config.DialFromNode("udp", config.NodeIP, config.NodeUdpPort, config.MaxTries, config.MaxTries, sets.NewString())
|
||||
})
|
||||
// TODO: Test sessionAffinity #31712
|
||||
|
||||
It("should function for client IP based session affinity: http", func() {
|
||||
config := framework.NewNetworkingTestConfig(f)
|
||||
By(fmt.Sprintf("dialing(http) %v --> %v:%v (config.clusterIP)", config.TestContainerPod.Name, config.ClusterIP, framework.ClusterHttpPort))
|
||||
updateSessionAffinity := func(svc *v1.Service) {
|
||||
svc.Spec.SessionAffinity = v1.ServiceAffinityClientIP
|
||||
}
|
||||
framework.UpdateService(f.ClientSet, config.NodePortService.Namespace, config.NodePortService.Name, updateSessionAffinity)
|
||||
firstEndpoints, err := config.GetEndpointsFromTestContainer("http", config.ClusterIP, framework.ClusterHttpPort, config.MaxTries, 0)
|
||||
if err != nil {
|
||||
framework.Failf("Unable to get endpoints from test containers: %v", err)
|
||||
}
|
||||
for i := 0; i < framework.SessionAffinityChecks; i++ {
|
||||
eps, err := config.GetEndpointsFromTestContainer("http", config.ClusterIP, framework.ClusterHttpPort, config.MaxTries, 0)
|
||||
if err != nil {
|
||||
framework.Failf("Unable to get endpoints from test containers: %v", err)
|
||||
}
|
||||
if !eps.Equal(firstEndpoints) {
|
||||
framework.Failf("Expect endpoints: %v, got: %v", firstEndpoints, eps)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
It("should function for client IP based session affinity: udp", func() {
|
||||
startTime := time.Now()
|
||||
config := framework.NewNetworkingTestConfig(f)
|
||||
By(fmt.Sprintf("dialing(udp) %v --> %v:%v (config.clusterIP)", config.TestContainerPod.Name, config.ClusterIP, framework.ClusterHttpPort))
|
||||
updateSessionAffinity := func(svc *v1.Service) {
|
||||
svc.Spec.SessionAffinity = v1.ServiceAffinityClientIP
|
||||
}
|
||||
framework.UpdateService(f.ClientSet, config.NodePortService.Namespace, config.NodePortService.Name, updateSessionAffinity)
|
||||
firstEndpoints, err := config.GetEndpointsFromTestContainer("udp", config.ClusterIP, framework.ClusterUdpPort, config.MaxTries, 0)
|
||||
if err != nil {
|
||||
framework.Failf("Unable to get endpoints from test containers: %v", err)
|
||||
}
|
||||
for i := 0; i < framework.SessionAffinityChecks; i++ {
|
||||
eps, err := config.GetEndpointsFromTestContainer("http", config.ClusterIP, framework.ClusterUdpPort, config.MaxTries, 0)
|
||||
if err != nil {
|
||||
framework.Failf("Unable to get endpoints from test containers: %v", err)
|
||||
}
|
||||
if !eps.Equal(firstEndpoints) {
|
||||
framework.Failf("Expect endpoints: %v, got: %v", firstEndpoints, eps)
|
||||
}
|
||||
}
|
||||
framework.Failf("test session affinity, cost time: %v", time.Now().Sub(startTime))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user