-
Notifications
You must be signed in to change notification settings - Fork 208
/
Copy pathpod.go
157 lines (133 loc) · 4.41 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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package tests
import (
"context"
"fmt"
"github.com/cilium/cilium-cli/connectivity/check"
)
// PodToPod generates one HTTP request from each client pod
// to each echo (server) pod in the test context. The remote Pod is contacted
// directly, no DNS is involved.
func PodToPod(opts ...Option) check.Scenario {
options := &labelsOption{}
for _, opt := range opts {
opt(options)
}
return &podToPod{
sourceLabels: options.sourceLabels,
destinationLabels: options.destinationLabels,
method: options.method,
}
}
// podToPod implements a Scenario.
type podToPod struct {
sourceLabels map[string]string
destinationLabels map[string]string
method string
}
func (s *podToPod) Name() string {
return "pod-to-pod"
}
func (s *podToPod) Run(ctx context.Context, t *check.Test) {
var i int
ct := t.Context()
for _, client := range ct.ClientPods() {
client := client // copy to avoid memory aliasing when using reference
if !hasAllLabels(client, s.sourceLabels) {
continue
}
for _, echo := range ct.EchoPods() {
if !hasAllLabels(echo, s.destinationLabels) {
continue
}
t.ForEachIPFamily(func(ipFam check.IPFamily) {
actionDesc := fmt.Sprintf("curl-%s-%d", ipFam.String(), i)
t.NewAction(s, actionDesc, &client, echo, ipFam).Run(func(a *check.Action) {
if s.method == "" {
a.ExecInPod(ctx, ct.CurlCommand(echo, ipFam))
} else {
a.ExecInPod(ctx, ct.CurlCommand(echo, ipFam, "-X", s.method))
}
a.ValidateFlows(ctx, client, a.GetEgressRequirements(check.FlowParameters{}))
a.ValidateFlows(ctx, echo, a.GetIngressRequirements(check.FlowParameters{}))
})
})
i++
}
}
}
func PodToPodWithEndpoints(opts ...Option) check.Scenario {
options := &labelsOption{}
for _, opt := range opts {
opt(options)
}
return &podToPodWithEndpoints{
sourceLabels: options.sourceLabels,
destinationLabels: options.destinationLabels,
method: options.method,
}
}
// podToPodWithEndpoints implements a Scenario.
type podToPodWithEndpoints struct {
sourceLabels map[string]string
destinationLabels map[string]string
method string
}
func (s *podToPodWithEndpoints) Name() string {
return "pod-to-pod-with-endpoints"
}
func (s *podToPodWithEndpoints) Run(ctx context.Context, t *check.Test) {
var i int
ct := t.Context()
for _, client := range ct.ClientPods() {
client := client // copy to avoid memory aliasing when using reference
if !hasAllLabels(client, s.sourceLabels) {
continue
}
for _, echo := range ct.EchoPods() {
if !hasAllLabels(echo, s.destinationLabels) {
continue
}
t.ForEachIPFamily(func(ipFam check.IPFamily) {
s.curlEndpoints(ctx, t, fmt.Sprintf("curl-%d", i), &client, echo, ipFam)
})
i++
}
}
}
func (s *podToPodWithEndpoints) curlEndpoints(ctx context.Context, t *check.Test, name string,
client *check.Pod, echo check.TestPeer, ipFam check.IPFamily) {
ct := t.Context()
baseURL := fmt.Sprintf("%s://%s:%d", echo.Scheme(), echo.Address(ipFam), echo.Port())
var curlOpts []string
if s.method != "" {
curlOpts = append(curlOpts, "-X", s.method)
}
// Manually construct an HTTP endpoint for each API endpoint.
for _, path := range []string{"public", "private"} {
epName := fmt.Sprintf("%s-%s", name, path)
url := fmt.Sprintf("%s/%s", baseURL, path)
ep := check.HTTPEndpointWithLabels(epName, url, echo.Labels())
t.NewAction(s, epName, client, ep, ipFam).Run(func(a *check.Action) {
a.ExecInPod(ctx, ct.CurlCommand(ep, ipFam, curlOpts...))
a.ValidateFlows(ctx, client, a.GetEgressRequirements(check.FlowParameters{}))
a.ValidateFlows(ctx, ep, a.GetIngressRequirements(check.FlowParameters{}))
})
// Additionally test private endpoint access with HTTP header expected by policy.
if path == "private" {
epName += "with-header"
labels := echo.Labels()
labels["X-Very-Secret-Token"] = "42"
ep = check.HTTPEndpointWithLabels(epName, url, labels)
t.NewAction(s, epName, client, ep, ipFam).Run(func(a *check.Action) {
opts := make([]string, 0, len(curlOpts)+2)
opts = append(opts, curlOpts...)
opts = append(opts, "-H", "X-Very-Secret-Token: 42")
a.ExecInPod(ctx, ct.CurlCommand(ep, ipFam, opts...))
a.ValidateFlows(ctx, client, a.GetEgressRequirements(check.FlowParameters{}))
a.ValidateFlows(ctx, ep, a.GetIngressRequirements(check.FlowParameters{}))
})
}
}
}