generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 528
/
Copy pathpod.go
179 lines (154 loc) · 5.02 KB
/
pod.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
Copyright 2020 The Kubernetes 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
http://www.apache.org/licenses/LICENSE-2.0
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.
*/
package echo
import (
"bytes"
"context"
"fmt"
"strings"
"testing"
"time"
v1 "k8s.io/api/core/v1"
klabels "k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/remotecommand"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/gateway-api/conformance/utils/config"
"sigs.k8s.io/gateway-api/conformance/utils/http"
"sigs.k8s.io/gateway-api/conformance/utils/suite"
"sigs.k8s.io/gateway-api/conformance/utils/tlog"
)
// MeshPod represents a connection to a specific pod running in the mesh.
// This can be used to trigger requests *from* that pod.
type MeshPod struct {
Name string
Namespace string
Address string
rc rest.Interface
rcfg *rest.Config
}
type MeshApplication string
const (
MeshAppEchoV1 MeshApplication = "app=echo,version=v1"
MeshAppEchoV2 MeshApplication = "app=echo,version=v2"
)
func (m *MeshPod) MakeRequestAndExpectEventuallyConsistentResponse(t *testing.T, exp http.ExpectedResponse, timeoutConfig config.TimeoutConfig) {
t.Helper()
http.AwaitConvergence(t, timeoutConfig.RequiredConsecutiveSuccesses, timeoutConfig.MaxTimeToConsistency, func(elapsed time.Duration) bool {
req := makeRequest(t, exp.Request)
resp, err := m.request(req)
if err != nil {
tlog.Logf(t, "Request %v failed, not ready yet: %v (after %v)", req, err.Error(), elapsed)
return false
}
tlog.Logf(t, "Got resp %v", resp)
if err := compareRequest(exp, resp); err != nil {
tlog.Logf(t, "Response expectation failed for request: %v not ready yet: %v (after %v)", req, err, elapsed)
return false
}
return true
})
tlog.Logf(t, "Request passed")
}
func makeRequest(t *testing.T, r http.Request) []string {
protocol := strings.ToLower(r.Protocol)
if protocol == "" {
protocol = "http"
}
host := http.CalculateHost(t, r.Host, protocol)
args := []string{"client", fmt.Sprintf("%s://%s%s", protocol, host, r.Path)}
if r.Method != "" {
args = append(args, "--method="+r.Method)
}
for k, v := range r.Headers {
args = append(args, "-H", fmt.Sprintf("%v: %v", k, v))
}
return args
}
func compareRequest(exp http.ExpectedResponse, resp Response) error {
want := exp.Response
if fmt.Sprint(want.StatusCode) != resp.Code {
return fmt.Errorf("wanted status code %v, got %v", want.StatusCode, resp.Code)
}
for _, name := range want.AbsentHeaders {
if v := resp.ResponseHeaders.Values(name); len(v) != 0 {
return fmt.Errorf("expected no header %q, got %v", name, v)
}
}
for k, v := range want.Headers {
if got := resp.ResponseHeaders.Get(k); got != v {
return fmt.Errorf("expected header %v=%v, got %v", k, v, got)
}
}
if !strings.HasPrefix(resp.Hostname, exp.Backend) {
return fmt.Errorf("expected pod name to start with %s, got %s", exp.Backend, resp.Hostname)
}
return nil
}
func (m *MeshPod) request(args []string) (Response, error) {
container := "echo"
req := m.rc.Post().
Resource("pods").
Name(m.Name).
Namespace(m.Namespace).
SubResource("exec").
Param("container", container).
VersionedParams(&v1.PodExecOptions{
Container: container,
Command: args,
Stdin: false,
Stdout: true,
Stderr: true,
TTY: false,
}, scheme.ParameterCodec)
exec, err := remotecommand.NewSPDYExecutor(m.rcfg, "POST", req.URL())
if err != nil {
return Response{}, err
}
var stdoutBuf, stderrBuf bytes.Buffer
err = exec.StreamWithContext(context.Background(), remotecommand.StreamOptions{
Stdin: nil,
Stdout: &stdoutBuf,
Stderr: &stderrBuf,
Tty: false,
})
if err != nil {
return Response{}, err
}
return ParseResponse(stdoutBuf.String()), nil
}
func ConnectToApp(t *testing.T, s *suite.ConformanceTestSuite, app MeshApplication) MeshPod {
return ConnectToAppInNamespace(t, s, app, "gateway-conformance-mesh")
}
func ConnectToAppInNamespace(t *testing.T, s *suite.ConformanceTestSuite, app MeshApplication, ns string) MeshPod {
lbls, _ := klabels.Parse(string(app))
podsList := v1.PodList{}
err := s.Client.List(context.Background(), &podsList, client.InNamespace(ns), client.MatchingLabelsSelector{Selector: lbls})
if err != nil {
tlog.Fatalf(t, "failed to query pods in app %v", app)
}
if len(podsList.Items) == 0 {
tlog.Fatalf(t, "no pods found in app %v", app)
}
pod := podsList.Items[0]
podName := pod.Name
podNamespace := pod.Namespace
return MeshPod{
Name: podName,
Namespace: podNamespace,
Address: pod.Status.PodIP,
rc: s.Clientset.CoreV1().RESTClient(),
rcfg: s.RestConfig,
}
}