mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 10:51:29 +00:00
Merge pull request #95503 from ii/pod-service-proxy-with-path
Write PodProxyWithPath & ServiceProxyWithPath test - + 12 endpoint coverage
This commit is contained in:
commit
3dbd7498f2
@ -65,6 +65,7 @@ go_library(
|
||||
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/tools/cache:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/tools/watch:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/transport:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/util/flowcontrol:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/util/retry:go_default_library",
|
||||
"//staging/src/k8s.io/client-go/util/workqueue:go_default_library",
|
||||
|
@ -19,7 +19,9 @@ limitations under the License.
|
||||
package network
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"net/http"
|
||||
@ -32,7 +34,9 @@ import (
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/util/intstr"
|
||||
"k8s.io/apimachinery/pkg/util/net"
|
||||
"k8s.io/apimachinery/pkg/util/wait"
|
||||
clientset "k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/transport"
|
||||
"k8s.io/kubernetes/test/e2e/framework"
|
||||
e2enode "k8s.io/kubernetes/test/e2e/framework/node"
|
||||
e2erc "k8s.io/kubernetes/test/e2e/framework/rc"
|
||||
@ -52,8 +56,15 @@ const (
|
||||
|
||||
// We have seen one of these calls take just over 15 seconds, so putting this at 30.
|
||||
proxyHTTPCallTimeout = 30 * time.Second
|
||||
podRetryPeriod = 1 * time.Second
|
||||
podRetryTimeout = 1 * time.Minute
|
||||
)
|
||||
|
||||
type jsonResponse struct {
|
||||
Method string
|
||||
Body string
|
||||
}
|
||||
|
||||
var _ = SIGDescribe("Proxy", func() {
|
||||
version := "v1"
|
||||
ginkgo.Context("version "+version, func() {
|
||||
@ -258,9 +269,161 @@ var _ = SIGDescribe("Proxy", func() {
|
||||
framework.Failf(strings.Join(errs, "\n"))
|
||||
}
|
||||
})
|
||||
|
||||
ginkgo.It("A set of valid responses are returned for both pod and service ProxyWithPath", func() {
|
||||
|
||||
ns := f.Namespace.Name
|
||||
msg := "foo"
|
||||
|
||||
framework.Logf("Creating pod...")
|
||||
_, err := f.ClientSet.CoreV1().Pods(ns).Create(context.TODO(), &v1.Pod{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "agnhost",
|
||||
Labels: map[string]string{
|
||||
"test": "response"},
|
||||
},
|
||||
Spec: v1.PodSpec{
|
||||
Containers: []v1.Container{{
|
||||
Image: imageutils.GetE2EImage(imageutils.Agnhost),
|
||||
Name: "agnhost",
|
||||
Command: []string{"/agnhost", "porter", "--json-response"},
|
||||
Env: []v1.EnvVar{{
|
||||
Name: "SERVE_PORT_80",
|
||||
Value: msg,
|
||||
}},
|
||||
}},
|
||||
RestartPolicy: v1.RestartPolicyNever,
|
||||
}}, metav1.CreateOptions{})
|
||||
framework.ExpectNoError(err, "failed to create pod")
|
||||
|
||||
err = wait.PollImmediate(podRetryPeriod, podRetryTimeout, checkPodStatus(f, "test=response"))
|
||||
framework.ExpectNoError(err, "Pod didn't start within time out period")
|
||||
|
||||
framework.Logf("Creating service...")
|
||||
_, err = f.ClientSet.CoreV1().Services(ns).Create(context.TODO(), &v1.Service{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "test-service",
|
||||
Namespace: ns,
|
||||
Labels: map[string]string{
|
||||
"test": "response",
|
||||
},
|
||||
},
|
||||
Spec: v1.ServiceSpec{
|
||||
Ports: []v1.ServicePort{{
|
||||
Port: 80,
|
||||
TargetPort: intstr.FromInt(80),
|
||||
Protocol: v1.ProtocolTCP,
|
||||
}},
|
||||
Selector: map[string]string{
|
||||
"test": "response",
|
||||
},
|
||||
}}, metav1.CreateOptions{})
|
||||
framework.ExpectNoError(err, "Failed to create the service")
|
||||
|
||||
transportCfg, err := f.ClientConfig().TransportConfig()
|
||||
framework.ExpectNoError(err, "Error creating transportCfg")
|
||||
restTransport, err := transport.New(transportCfg)
|
||||
framework.ExpectNoError(err, "Error creating restTransport")
|
||||
|
||||
client := &http.Client{
|
||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||
return http.ErrUseLastResponse
|
||||
},
|
||||
Transport: restTransport,
|
||||
}
|
||||
|
||||
// All methods for Pod ProxyWithPath return 200
|
||||
// For all methods other than HEAD the response body returns 'foo' with the received http method
|
||||
httpVerbs := []string{"DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"}
|
||||
for _, httpVerb := range httpVerbs {
|
||||
|
||||
urlString := f.ClientConfig().Host + "/api/v1/namespaces/" + ns + "/pods/agnhost/proxy/some/path/with/" + httpVerb
|
||||
framework.Logf("Starting http.Client for %s", urlString)
|
||||
request, err := http.NewRequest(httpVerb, urlString, nil)
|
||||
framework.ExpectNoError(err, "processing request")
|
||||
|
||||
resp, err := client.Do(request)
|
||||
framework.ExpectNoError(err, "processing response")
|
||||
defer resp.Body.Close()
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
buf.ReadFrom(resp.Body)
|
||||
response := buf.String()
|
||||
|
||||
switch httpVerb {
|
||||
case "HEAD":
|
||||
framework.Logf("http.Client request:%s | StatusCode:%d", httpVerb, resp.StatusCode)
|
||||
framework.ExpectEqual(resp.StatusCode, 200, "The resp.StatusCode returned: %d", resp.StatusCode)
|
||||
default:
|
||||
var jr *jsonResponse
|
||||
err = json.Unmarshal([]byte(response), &jr)
|
||||
framework.ExpectNoError(err, "Failed to process jsonResponse: %v | err: %v ", buf.String(), err)
|
||||
|
||||
framework.Logf("http.Client request:%s | StatusCode:%d | Response: %s | Method: %s", httpVerb, resp.StatusCode, jr.Body, jr.Method)
|
||||
framework.ExpectEqual(resp.StatusCode, 200, "The resp.StatusCode returned: %d", resp.StatusCode)
|
||||
|
||||
framework.ExpectEqual(msg, jr.Body, "The resp.Body returned: %v", jr.Body)
|
||||
framework.ExpectEqual(httpVerb, jr.Method, "The resp.Body returned: %v", jr.Body)
|
||||
}
|
||||
}
|
||||
|
||||
// All methods for Service ProxyWithPath return 200
|
||||
// For all methods other than HEAD the response body returns 'foo' with the received http method
|
||||
for _, httpVerb := range httpVerbs {
|
||||
|
||||
urlString := f.ClientConfig().Host + "/api/v1/namespaces/" + ns + "/services/test-service/proxy/some/path/with/" + httpVerb
|
||||
framework.Logf("Starting http.Client for %s", urlString)
|
||||
request, err := http.NewRequest(httpVerb, urlString, nil)
|
||||
framework.ExpectNoError(err, "processing request")
|
||||
|
||||
resp, err := client.Do(request)
|
||||
framework.ExpectNoError(err, "processing response")
|
||||
defer resp.Body.Close()
|
||||
|
||||
buf := new(bytes.Buffer)
|
||||
buf.ReadFrom(resp.Body)
|
||||
response := buf.String()
|
||||
|
||||
switch httpVerb {
|
||||
case "HEAD":
|
||||
framework.Logf("http.Client request:%s | StatusCode:%d", httpVerb, resp.StatusCode)
|
||||
framework.ExpectEqual(resp.StatusCode, 200, "The resp.StatusCode returned: %d", resp.StatusCode)
|
||||
default:
|
||||
var jr *jsonResponse
|
||||
err = json.Unmarshal([]byte(response), &jr)
|
||||
framework.ExpectNoError(err, "Failed to process jsonResponse: %v | err: %v ", buf.String(), err)
|
||||
|
||||
framework.Logf("http.Client request:%s | StatusCode:%d | Response: %s | Method: %s", httpVerb, resp.StatusCode, jr.Body, jr.Method)
|
||||
framework.ExpectEqual(resp.StatusCode, 200, "The resp.StatusCode returned: %d", resp.StatusCode)
|
||||
|
||||
framework.ExpectEqual(msg, jr.Body, "The resp.Body returned: %v", jr.Body)
|
||||
framework.ExpectEqual(httpVerb, jr.Method, "The resp.Body returned: %v", jr.Body)
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
func checkPodStatus(f *framework.Framework, label string) func() (bool, error) {
|
||||
return func() (bool, error) {
|
||||
var err error
|
||||
|
||||
list, err := f.ClientSet.CoreV1().Pods(f.Namespace.Name).List(context.TODO(), metav1.ListOptions{
|
||||
LabelSelector: label})
|
||||
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
if list.Items[0].Status.Phase != "Running" {
|
||||
framework.Logf("Pod Quantity: %d Status: %s", len(list.Items), list.Items[0].Status.Phase)
|
||||
return false, err
|
||||
}
|
||||
framework.Logf("Pod Status: %v", list.Items[0].Status.Phase)
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
func doProxy(f *framework.Framework, path string, i int) (body []byte, statusCode int, d time.Duration, err error) {
|
||||
// About all of the proxy accesses in this file:
|
||||
// * AbsPath is used because it preserves the trailing '/'.
|
||||
|
Loading…
Reference in New Issue
Block a user