-
Notifications
You must be signed in to change notification settings - Fork 483
Expand file tree
/
Copy pathnetworking.go
More file actions
310 lines (237 loc) · 10.7 KB
/
networking.go
File metadata and controls
310 lines (237 loc) · 10.7 KB
Edit and raw actions
OlderNewer
1
/*
2
Copyright 2017 The Kubernetes Authors.
3
4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7
8
http://www.apache.org/licenses/LICENSE-2.0
9
10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16
17
package validate
18
19
import (
20
"context"
21
"net/http"
22
"strconv"
23
"strings"
24
"time"
25
26
. "github.com/onsi/ginkgo/v2"
27
. "github.com/onsi/gomega"
28
internalapi "k8s.io/cri-api/pkg/apis"
29
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
30
31
"sigs.k8s.io/cri-tools/pkg/common"
32
"sigs.k8s.io/cri-tools/pkg/framework"
33
)
34
35
var _ = framework.KubeDescribe("Networking", func() {
36
f := framework.NewDefaultCRIFramework()
37
38
var (
39
rc internalapi.RuntimeService
40
ic internalapi.ImageManagerService
41
)
42
43
BeforeEach(func() {
44
rc = f.CRIClient.CRIRuntimeClient
45
ic = f.CRIClient.CRIImageClient
46
})
47
48
Context("runtime should support networking", func() {
49
var podID string
50
51
AfterEach(func(ctx SpecContext) {
52
By("stop PodSandbox")
53
Expect(rc.StopPodSandbox(ctx, podID)).NotTo(HaveOccurred())
54
By("delete PodSandbox")
55
Expect(rc.RemovePodSandbox(ctx, podID)).NotTo(HaveOccurred())
56
})
57
58
It("runtime should support DNS config [Conformance]", func(ctx SpecContext) {
59
By("create a PodSandbox with DNS config")
60
61
var podConfig *runtimeapi.PodSandboxConfig
62
63
podID, podConfig = createPodSandWithDNSConfig(ctx, rc)
64
65
By("create container")
66
67
containerID := framework.CreateDefaultContainer(ctx, rc, ic, podID, podConfig, "container-for-DNS-config-test-")
68
69
By("start container")
70
startContainer(ctx, rc, containerID)
71
72
By("check DNS config")
73
74
expectedContent := getDNSConfigContent
75
checkDNSConfig(ctx, rc, containerID, expectedContent)
76
})
77
78
It("runtime should support set hostname [Conformance]", func(ctx SpecContext) {
79
By("create a PodSandbox with hostname")
80
81
var podConfig *runtimeapi.PodSandboxConfig
82
83
const testHostname = "test-hostname"
84
85
podID, podConfig = createPodSandWithHostname(ctx, rc, testHostname)
86
87
By("create container")
88
89
containerID := framework.CreateDefaultContainer(ctx, rc, ic, podID, podConfig, "container-for-hostname-test-")
90
91
By("start container")
92
startContainer(ctx, rc, containerID)
93
94
By("check hostname")
95
checkHostname(ctx, rc, containerID, testHostname)
96
})
97
98
It("runtime should support port mapping with only container port [Conformance]", func(ctx SpecContext) {
99
By("create a PodSandbox with container port mapping")
100
101
var podConfig *runtimeapi.PodSandboxConfig
102
103
portMappings := []*runtimeapi.PortMapping{
104
{
105
ContainerPort: webServerContainerPort,
106
},
107
}
108
podID, podConfig = createPodSandboxWithPortMapping(ctx, rc, portMappings, false)
109
110
By("create a web server container")
111
112
containerID := createWebServerContainer(ctx, rc, ic, podID, podConfig, "container-for-container-port")
113
114
By("start the web server container")
115
startContainer(ctx, rc, containerID)
116
117
By("check the port mapping with only container port")
118
checkMainPage(ctx, rc, podID, 0, webServerContainerPort)
119
})
120
121
It("runtime should support port mapping with host port and container port [Conformance]", func(ctx SpecContext) {
122
By("create a PodSandbox with host port and container port mapping")
123
124
var podConfig *runtimeapi.PodSandboxConfig
125
126
portMappings := []*runtimeapi.PortMapping{
127
{
128
ContainerPort: webServerContainerPort,
129
HostPort: webServerHostPortForPortMapping,
130
},
131
}
132
podID, podConfig = createPodSandboxWithPortMapping(ctx, rc, portMappings, false)
133
134
By("create a web server container")
135
136
containerID := createWebServerContainer(ctx, rc, ic, podID, podConfig, "container-for-host-port")
137
138
By("start the web server container")
139
startContainer(ctx, rc, containerID)
140
141
By("check the port mapping with host port and container port")
142
checkMainPage(ctx, rc, "", webServerHostPortForPortMapping, 0)
143
})
144
})
145
})
146
147
// createPodSandWithHostname create a PodSandbox with hostname.
148
func createPodSandWithHostname(ctx context.Context, c internalapi.RuntimeService, hostname string) (string, *runtimeapi.PodSandboxConfig) {
149
podSandboxName := "create-PodSandbox-with-hostname" + framework.NewUUID()
150
uid := framework.DefaultUIDPrefix + framework.NewUUID()
151
namespace := framework.DefaultNamespacePrefix + framework.NewUUID()
152
config := &runtimeapi.PodSandboxConfig{
153
Metadata: framework.BuildPodSandboxMetadata(podSandboxName, uid, namespace, framework.DefaultAttempt),
154
Hostname: hostname,
155
Labels: framework.DefaultPodLabels,
156
Linux: &runtimeapi.LinuxPodSandboxConfig{
157
CgroupParent: common.GetCgroupParent(ctx, c),
158
},
159
}
160
161
podID := framework.RunPodSandbox(ctx, c, config)
162
163
return podID, config
164
}
165
166
// createPodSandWithDNSConfig create a PodSandbox with DNS config.
167
func createPodSandWithDNSConfig(ctx context.Context, c internalapi.RuntimeService) (string, *runtimeapi.PodSandboxConfig) {
168
podSandboxName := "create-PodSandbox-with-DNS-config" + framework.NewUUID()
169
uid := framework.DefaultUIDPrefix + framework.NewUUID()
170
namespace := framework.DefaultNamespacePrefix + framework.NewUUID()
171
config := &runtimeapi.PodSandboxConfig{
172
Metadata: framework.BuildPodSandboxMetadata(podSandboxName, uid, namespace, framework.DefaultAttempt),
173
DnsConfig: &runtimeapi.DNSConfig{
174
Servers: []string{defaultDNSServer},
175
Searches: []string{defaultDNSSearch},
176
Options: []string{defaultDNSOption},
177
},
178
Linux: &runtimeapi.LinuxPodSandboxConfig{
179
CgroupParent: common.GetCgroupParent(ctx, c),
180
},
181
Labels: framework.DefaultPodLabels,
182
}
183
184
podID := framework.RunPodSandbox(ctx, c, config)
185
186
return podID, config
187
}
188
189
// createPodSandboxWithPortMapping create a PodSandbox with port mapping.
190
func createPodSandboxWithPortMapping(ctx context.Context, c internalapi.RuntimeService, portMappings []*runtimeapi.PortMapping, hostNet bool) (string, *runtimeapi.PodSandboxConfig) {
191
podSandboxName := "create-PodSandbox-with-port-mapping" + framework.NewUUID()
192
uid := framework.DefaultUIDPrefix + framework.NewUUID()
193
namespace := framework.DefaultNamespacePrefix + framework.NewUUID()
194
195
config := &runtimeapi.PodSandboxConfig{
196
Metadata: framework.BuildPodSandboxMetadata(podSandboxName, uid, namespace, framework.DefaultAttempt),
197
PortMappings: portMappings,
198
Linux: &runtimeapi.LinuxPodSandboxConfig{
199
CgroupParent: common.GetCgroupParent(ctx, c),
200
},
201
Labels: framework.DefaultPodLabels,
202
}
203
if hostNet {
204
config.Linux.SecurityContext = &runtimeapi.LinuxSandboxSecurityContext{
205
NamespaceOptions: &runtimeapi.NamespaceOption{
206
Network: runtimeapi.NamespaceMode_NODE,
207
},
208
}
209
}
210
211
podID := framework.RunPodSandbox(ctx, c, config)
212
213
return podID, config
214
}
215
216
// checkHostname checks the container hostname.
217
func checkHostname(ctx context.Context, c internalapi.RuntimeService, containerID, hostname string) {
218
By("get the current hostname via execSync")
219
220
stdout, stderr, err := c.ExecSync(ctx, containerID, getHostnameCmd, time.Duration(defaultExecSyncTimeout)*time.Second)
221
framework.ExpectNoError(err, "failed to execSync in container %q", containerID)
222
Expect(strings.EqualFold(strings.TrimSpace(string(stdout)), hostname)).To(BeTrue())
223
Expect(string(stderr)).To(BeEmpty(), "The stderr should be empty.")
224
framework.Logf("check hostname succeed")
225
}
226
227
// checkDNSConfig checks the content of /etc/resolv.conf.
228
func checkDNSConfig(ctx context.Context, c internalapi.RuntimeService, containerID string, expectedContent []string) {
229
By("get the current dns config via execSync")
230
231
stdout, stderr, err := c.ExecSync(ctx, containerID, getDNSConfigCmd, time.Duration(defaultExecSyncTimeout)*time.Second)
232
framework.ExpectNoError(err, "failed to execSync in container %q", containerID)
233
234
for _, content := range expectedContent {
235
Expect(string(stdout)).To(ContainSubstring(content), "The stdout output of execSync should contain %q", content)
236
}
237
238
Expect(string(stderr)).To(BeEmpty(), "The stderr should be empty.")
239
framework.Logf("check DNS config succeed")
240
}
241
242
// createWebServerContainer creates a container running a web server.
243
func createWebServerContainer(ctx context.Context, rc internalapi.RuntimeService, ic internalapi.ImageManagerService, podID string, podConfig *runtimeapi.PodSandboxConfig, prefix string) string {
244
containerName := prefix + framework.NewUUID()
245
containerConfig := &runtimeapi.ContainerConfig{
246
Metadata: framework.BuildContainerMetadata(containerName, framework.DefaultAttempt),
247
Image: &runtimeapi.ImageSpec{Image: webServerImage},
248
Linux: &runtimeapi.LinuxContainerConfig{},
249
}
250
251
return framework.CreateContainer(ctx, rc, ic, containerConfig, podID, podConfig)
252
}
253
254
// createHostNetWebServerContainer creates a web server container using webServerHostNetContainerPort.
255
func createHostNetWebServerContainer(ctx context.Context, rc internalapi.RuntimeService, ic internalapi.ImageManagerService, podID string, podConfig *runtimeapi.PodSandboxConfig, prefix string) string {
256
containerName := prefix + framework.NewUUID()
257
containerConfig := &runtimeapi.ContainerConfig{
258
Metadata: framework.BuildContainerMetadata(containerName, framework.DefaultAttempt),
259
Image: &runtimeapi.ImageSpec{Image: hostNetWebServerImage},
260
Linux: &runtimeapi.LinuxContainerConfig{},
261
}
262
263
return framework.CreateContainer(ctx, rc, ic, containerConfig, podID, podConfig)
264
}
265
266
// checkMainPage check if the we can get the main page of the pod via given IP:port.
267
func checkMainPage(ctx context.Context, c internalapi.RuntimeService, podID string, hostPort, containerPort int32) {
268
By("get the IP:port needed to be checked")
269
270
url := "http://"
271
if hostPort != 0 {
272
url += "127.0.0.1:" + strconv.Itoa(int(hostPort))
273
} else {
274
status := getPodSandboxStatus(ctx, c, podID)
275
Expect(status.GetNetwork()).NotTo(BeNil(), "The network in status should not be nil.")
276
Expect(status.GetNetwork().GetIp()).NotTo(BeNil(), "The IP should not be nil.")
277
url += status.GetNetwork().GetIp() + ":" + strconv.Itoa(int(containerPort))
278
}
279
280
framework.Logf("the IP:port is " + url)
281
282
By("check the content of " + url)
283
284
respChan := make(chan *http.Response, 1)
285
defer close(respChan)
286
287
Eventually(func() error {
288
tctx, cancel := context.WithTimeout(ctx, time.Minute)
289
defer cancel()
290
291
req, err := http.NewRequestWithContext(tctx, http.MethodGet, url, http.NoBody)
292
if err != nil {
293
return err
294
}
295
296
resp, err := http.DefaultClient.Do(req)
297
if err != nil {
298
return err
299
}
300
defer resp.Body.Close()
301
302
respChan <- resp
303
304
return nil
305
}, time.Minute, time.Second).Should(Succeed())
306
307
resp := <-respChan
308
Expect(resp.StatusCode).To(Equal(200), "The status code of response should be 200.")
309
framework.Logf("check port mapping succeed")
310
}