/
framework.go
292 lines (260 loc) · 10.3 KB
/
framework.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright 2022 Antrea 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 e2e
import (
"fmt"
"math/rand"
"os"
"strings"
"time"
log "github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
crdv1alpha1 "antrea.io/antrea/pkg/apis/crd/v1alpha1"
antreae2e "antrea.io/antrea/test/e2e"
"antrea.io/antrea/test/e2e/providers"
)
var (
homedir, _ = os.UserHomeDir()
)
const (
defaultTimeout = 90 * time.Second
importServiceDelay = 2 * time.Second
multiClusterTestNamespace string = "antrea-multicluster-test"
eastClusterTestService string = "east-nginx"
westClusterTestService string = "west-nginx"
eastCluster string = "east-cluster"
westCluster string = "west-cluster"
leaderCluster string = "leader-cluster"
serviceExportYML string = "serviceexport.yml"
nameSuffixLength int = 8
nginxImage = "projects.registry.vmware.com/antrea/nginx:1.21.6-alpine"
agnhostImage = "agnhost:2.26"
)
var provider providers.ProviderInterface
type TestOptions struct {
leaderClusterKubeConfigPath string
westClusterKubeConfigPath string
eastClusterKubeConfigPath string
enableGateway bool
logsExportDir string
}
var testOptions TestOptions
type MCTestData struct {
clusters []string
clusterTestDataMap map[string]antreae2e.TestData
logsDirForTestCase string
clusterGateways map[string]string
clusterRegularNodes map[string]string
}
var testData *MCTestData
func (data *MCTestData) createClients() error {
kubeConfigPaths := []string{
testOptions.leaderClusterKubeConfigPath,
testOptions.eastClusterKubeConfigPath,
testOptions.westClusterKubeConfigPath,
}
data.clusters = []string{
leaderCluster, eastCluster, westCluster,
}
data.clusterTestDataMap = map[string]antreae2e.TestData{}
for i, cluster := range data.clusters {
testData := antreae2e.TestData{}
if err := testData.CreateClient(kubeConfigPaths[i]); err != nil {
return fmt.Errorf("error initializing clients for cluster %s: %v", cluster, err)
}
data.clusterTestDataMap[cluster] = testData
}
return nil
}
func (data *MCTestData) initProviders() error {
for cluster, d := range data.clusterTestDataMap {
if err := d.InitProvider("remote", "multicluster"); err != nil {
log.Errorf("Failed to initialize provider for cluster %s", cluster)
return err
}
}
provider, _ = providers.NewRemoteProvider("multicluster")
return nil
}
func (data *MCTestData) createTestNamespaces() error {
for cluster, d := range data.clusterTestDataMap {
if err := d.CreateNamespace(multiClusterTestNamespace, nil); err != nil {
log.Errorf("Failed to create Namespace %s in cluster %s", multiClusterTestNamespace, cluster)
return err
}
}
return nil
}
func (data *MCTestData) deletePod(clusterName, namespace, name string) error {
if d, ok := data.clusterTestDataMap[clusterName]; ok {
if err := d.DeletePod(namespace, name); err != nil {
return err
}
}
return nil
}
func (data *MCTestData) deleteService(clusterName, namespace, name string) error {
if d, ok := data.clusterTestDataMap[clusterName]; ok {
if err := d.DeleteService(namespace, name); err != nil {
return err
}
}
return nil
}
func (data *MCTestData) getService(clusterName, namespace, name string) (*corev1.Service, error) {
if d, ok := data.clusterTestDataMap[clusterName]; ok {
return d.GetService(namespace, name)
}
return nil, fmt.Errorf("clusterName %s not found", clusterName)
}
func (data *MCTestData) deleteTestNamespaces(timeout time.Duration) error {
var failedClusters []string
for cluster, d := range data.clusterTestDataMap {
if err := d.DeleteNamespace(multiClusterTestNamespace, timeout); err != nil {
failedClusters = append(failedClusters, cluster)
}
}
if len(failedClusters) > 0 {
return fmt.Errorf("failed to delete Namespace %s in clusters %v", multiClusterTestNamespace, failedClusters)
}
return nil
}
func (data *MCTestData) createPod(clusterName, name, nodeName, namespace, ctrName, image string, command []string,
args []string, env []corev1.EnvVar, ports []corev1.ContainerPort, hostNetwork bool, mutateFunc func(pod *corev1.Pod)) error {
if d, ok := data.clusterTestDataMap[clusterName]; ok {
return antreae2e.NewPodBuilder(name, namespace, image).
OnNode(nodeName).WithContainerName(ctrName).
WithCommand(command).WithArgs(args).
WithEnv(env).WithPorts(ports).WithHostNetwork(hostNetwork).
WithMutateFunc(mutateFunc).
Create(&d)
}
return fmt.Errorf("clusterName %s not found", clusterName)
}
func (data *MCTestData) createService(clusterName, serviceName, namespace string, port int32, targetPort int32,
protocol corev1.Protocol, selector map[string]string, affinity bool, nodeLocalExternal bool, serviceType corev1.ServiceType,
ipFamily *corev1.IPFamily, annotation map[string]string) (*corev1.Service, error) {
if d, ok := data.clusterTestDataMap[clusterName]; ok {
svc, err := d.CreateServiceWithAnnotations(serviceName, namespace, port, targetPort, protocol, selector, affinity, nodeLocalExternal, serviceType, ipFamily, annotation)
if err != nil {
return nil, err
}
return svc, nil
}
return nil, fmt.Errorf("clusterName %s not found", clusterName)
}
func (data *MCTestData) createOrUpdateANP(clusterName string, anp *crdv1alpha1.NetworkPolicy) (*crdv1alpha1.NetworkPolicy, error) {
if d, ok := data.clusterTestDataMap[clusterName]; ok {
return d.CreateOrUpdateANP(anp)
}
return nil, fmt.Errorf("clusterName %s not found", clusterName)
}
// deleteANP is a convenience function for deleting ANP by name and Namespace.
func (data *MCTestData) deleteANP(clusterName, namespace, name string) error {
if d, ok := data.clusterTestDataMap[clusterName]; ok {
return d.DeleteANP(namespace, name)
}
return fmt.Errorf("clusterName %s not found", clusterName)
}
// podWaitFor polls the K8s apiserver until the specified Pod is found (in the test Namespace) and
// the condition predicate is met (or until the provided timeout expires).
func (data *MCTestData) podWaitFor(timeout time.Duration, clusterName, name, namespace string, condition antreae2e.PodCondition) (*corev1.Pod, error) {
if d, ok := data.clusterTestDataMap[clusterName]; ok {
return d.PodWaitFor(timeout, name, namespace, condition)
}
return nil, fmt.Errorf("clusterName %s not found", clusterName)
}
// A DNS-1123 subdomain must consist of lower case alphanumeric characters
var lettersAndDigits = []rune("abcdefghijklmnopqrstuvwxyz0123456789")
func randSeq(n int) string {
b := make([]rune, n)
for i := range b {
// #nosec G404: random number generator not used for security purposes
randIdx := rand.Intn(len(lettersAndDigits))
b[i] = lettersAndDigits[randIdx]
}
return string(b)
}
func randName(prefix string) string {
return prefix + randSeq(nameSuffixLength)
}
func (data *MCTestData) probeServiceFromPodInCluster(
cluster string,
podName string,
containerName string,
podNamespace string,
serviceIP string,
) error {
cmd := []string{
"/bin/sh",
"-c",
fmt.Sprintf("curl --connect-timeout 5 -s %s", serviceIP),
}
log.Tracef("Running: kubectl exec %s -c %s -n %s -- %s", podName, containerName, podNamespace, strings.Join(cmd, " "))
stdout, stderr, err := data.runCommandFromPod(cluster, podNamespace, podName, containerName, cmd)
if err != nil || stderr != "" {
return fmt.Errorf("%s -> %s: error when running command: err - %v /// stdout - %s /// stderr - %s", podName, serviceIP, err, stdout, stderr)
}
return nil
}
func (data *MCTestData) probeFromPodInCluster(
cluster string,
podNamespace string,
podName string,
containerName string,
dstAddr string,
dstName string,
port int32,
protocol corev1.Protocol,
) antreae2e.PodConnectivityMark {
protocolStr := map[corev1.Protocol]string{
corev1.ProtocolTCP: "tcp",
corev1.ProtocolUDP: "udp",
corev1.ProtocolSCTP: "sctp",
}
// There seems to be an issue when running Antrea in Kind where tunnel traffic is dropped at
// first. This leads to the first test being run consistently failing. To avoid this issue
// until it is resolved, we try to connect 3 times.
// See https://github.com/antrea-io/antrea/issues/467.
cmd := []string{
"/bin/sh",
"-c",
fmt.Sprintf("for i in $(seq 1 3); do /agnhost connect %s:%d --timeout=1s --protocol=%s; done;", dstAddr, port, protocolStr[protocol]),
}
log.Tracef("Running: kubectl exec %s -c %s -n %s -- %s", podName, containerName, podNamespace, strings.Join(cmd, " "))
stdout, stderr, err := data.runCommandFromPod(cluster, podNamespace, podName, containerName, cmd)
// It needs to check both err and stderr because:
// 1. The probe tried 3 times. If it checks err only, failure+failure+success would be considered connected.
// 2. There might be an issue in Pod exec API that it sometimes doesn't return error when the probe fails. See #2394.
if err != nil || stderr != "" {
// log this error as trace since may be an expected failure
log.Tracef("%s -> %s: error when running command: err - %v /// stdout - %s /// stderr - %s", podName, dstName, err, stdout, stderr)
// If err != nil and stderr == "", then it means this probe failed because of
// the command instead of connectivity. For example, container name doesn't exist.
if stderr == "" {
return antreae2e.Error
}
return antreae2e.DecideProbeResult(stderr, 3)
}
return antreae2e.Connected
}
// Run the provided command in the specified Container for the given Pod and returns the contents of
// stdout and stderr as strings. An error either indicates that the command couldn't be run or that
// the command returned a non-zero error code.
func (data *MCTestData) runCommandFromPod(clusterName, podNamespace, podName, containerName string, cmd []string) (stdout string, stderr string, err error) {
if d, ok := data.clusterTestDataMap[clusterName]; ok {
return d.RunCommandFromPod(podNamespace, podName, containerName, cmd)
}
return "", "", fmt.Errorf("clusterName %s not found", clusterName)
}