Skip to content

Latest commit

 

History

History
310 lines (237 loc) · 10.7 KB

File metadata and controls

310 lines (237 loc) · 10.7 KB
 
Apr 6, 2017
Apr 6, 2017
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 (
Dec 12, 2022
Dec 12, 2022
20
"context"
Apr 6, 2017
Apr 6, 2017
21
"net/http"
22
"strconv"
Sep 24, 2019
Sep 24, 2019
23
"strings"
Apr 6, 2017
Apr 6, 2017
24
"time"
25
Jul 23, 2024
Jul 23, 2024
26
. "github.com/onsi/ginkgo/v2"
27
. "github.com/onsi/gomega"
Jun 27, 2019
Jun 27, 2019
28
internalapi "k8s.io/cri-api/pkg/apis"
Jan 26, 2022
Jan 26, 2022
29
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
Jul 23, 2024
Jul 23, 2024
30
May 7, 2024
May 7, 2024
31
"sigs.k8s.io/cri-tools/pkg/common"
32
"sigs.k8s.io/cri-tools/pkg/framework"
Apr 6, 2017
Apr 6, 2017
33
)
34
35
var _ = framework.KubeDescribe("Networking", func() {
36
f := framework.NewDefaultCRIFramework()
37
Feb 18, 2026
Feb 18, 2026
38
var (
39
rc internalapi.RuntimeService
40
ic internalapi.ImageManagerService
41
)
Apr 6, 2017
Apr 6, 2017
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
Mar 12, 2026
Mar 12, 2026
51
AfterEach(func(ctx SpecContext) {
Apr 6, 2017
Apr 6, 2017
52
By("stop PodSandbox")
Mar 12, 2026
Mar 12, 2026
53
Expect(rc.StopPodSandbox(ctx, podID)).NotTo(HaveOccurred())
Apr 6, 2017
Apr 6, 2017
54
By("delete PodSandbox")
Mar 12, 2026
Mar 12, 2026
55
Expect(rc.RemovePodSandbox(ctx, podID)).NotTo(HaveOccurred())
Apr 6, 2017
Apr 6, 2017
56
})
57
Mar 12, 2026
Mar 12, 2026
58
It("runtime should support DNS config [Conformance]", func(ctx SpecContext) {
Apr 18, 2017
Apr 18, 2017
59
By("create a PodSandbox with DNS config")
Feb 18, 2026
Feb 18, 2026
60
Apr 6, 2017
Apr 6, 2017
61
var podConfig *runtimeapi.PodSandboxConfig
Feb 18, 2026
Feb 18, 2026
62
Mar 12, 2026
Mar 12, 2026
63
podID, podConfig = createPodSandWithDNSConfig(ctx, rc)
Apr 6, 2017
Apr 6, 2017
64
65
By("create container")
Feb 18, 2026
Feb 18, 2026
66
Mar 12, 2026
Mar 12, 2026
67
containerID := framework.CreateDefaultContainer(ctx, rc, ic, podID, podConfig, "container-for-DNS-config-test-")
Apr 6, 2017
Apr 6, 2017
68
69
By("start container")
Mar 12, 2026
Mar 12, 2026
70
startContainer(ctx, rc, containerID)
Apr 6, 2017
Apr 6, 2017
71
72
By("check DNS config")
Feb 18, 2026
Feb 18, 2026
73
Aug 30, 2019
Aug 30, 2019
74
expectedContent := getDNSConfigContent
Mar 12, 2026
Mar 12, 2026
75
checkDNSConfig(ctx, rc, containerID, expectedContent)
Apr 6, 2017
Apr 6, 2017
76
})
77
Mar 12, 2026
Mar 12, 2026
78
It("runtime should support set hostname [Conformance]", func(ctx SpecContext) {
Sep 24, 2019
Sep 24, 2019
79
By("create a PodSandbox with hostname")
Feb 18, 2026
Feb 18, 2026
80
Sep 24, 2019
Sep 24, 2019
81
var podConfig *runtimeapi.PodSandboxConfig
Feb 18, 2026
Feb 18, 2026
82
Sep 24, 2019
Sep 24, 2019
83
const testHostname = "test-hostname"
Feb 18, 2026
Feb 18, 2026
84
Mar 12, 2026
Mar 12, 2026
85
podID, podConfig = createPodSandWithHostname(ctx, rc, testHostname)
Sep 24, 2019
Sep 24, 2019
86
87
By("create container")
Feb 18, 2026
Feb 18, 2026
88
Mar 12, 2026
Mar 12, 2026
89
containerID := framework.CreateDefaultContainer(ctx, rc, ic, podID, podConfig, "container-for-hostname-test-")
Sep 24, 2019
Sep 24, 2019
90
91
By("start container")
Mar 12, 2026
Mar 12, 2026
92
startContainer(ctx, rc, containerID)
Sep 24, 2019
Sep 24, 2019
93
94
By("check hostname")
Mar 12, 2026
Mar 12, 2026
95
checkHostname(ctx, rc, containerID, testHostname)
Sep 24, 2019
Sep 24, 2019
96
})
97
Mar 12, 2026
Mar 12, 2026
98
It("runtime should support port mapping with only container port [Conformance]", func(ctx SpecContext) {
Dec 4, 2023
Dec 4, 2023
99
By("create a PodSandbox with container port mapping")
Feb 18, 2026
Feb 18, 2026
100
Apr 6, 2017
Apr 6, 2017
101
var podConfig *runtimeapi.PodSandboxConfig
Feb 18, 2026
Feb 18, 2026
102
Apr 6, 2017
Apr 6, 2017
103
portMappings := []*runtimeapi.PortMapping{
104
{
Jan 30, 2019
Jan 30, 2019
105
ContainerPort: webServerContainerPort,
Apr 6, 2017
Apr 6, 2017
106
},
107
}
Mar 12, 2026
Mar 12, 2026
108
podID, podConfig = createPodSandboxWithPortMapping(ctx, rc, portMappings, false)
Apr 6, 2017
Apr 6, 2017
109
Jan 30, 2019
Jan 30, 2019
110
By("create a web server container")
Feb 18, 2026
Feb 18, 2026
111
Mar 12, 2026
Mar 12, 2026
112
containerID := createWebServerContainer(ctx, rc, ic, podID, podConfig, "container-for-container-port")
Apr 6, 2017
Apr 6, 2017
113
Jan 30, 2019
Jan 30, 2019
114
By("start the web server container")
Mar 12, 2026
Mar 12, 2026
115
startContainer(ctx, rc, containerID)
Apr 6, 2017
Apr 6, 2017
116
117
By("check the port mapping with only container port")
Mar 12, 2026
Mar 12, 2026
118
checkMainPage(ctx, rc, podID, 0, webServerContainerPort)
Apr 6, 2017
Apr 6, 2017
119
})
120
Mar 12, 2026
Mar 12, 2026
121
It("runtime should support port mapping with host port and container port [Conformance]", func(ctx SpecContext) {
Dec 4, 2023
Dec 4, 2023
122
By("create a PodSandbox with host port and container port mapping")
Feb 18, 2026
Feb 18, 2026
123
Apr 6, 2017
Apr 6, 2017
124
var podConfig *runtimeapi.PodSandboxConfig
Feb 18, 2026
Feb 18, 2026
125
Apr 6, 2017
Apr 6, 2017
126
portMappings := []*runtimeapi.PortMapping{
127
{
Jan 30, 2019
Jan 30, 2019
128
ContainerPort: webServerContainerPort,
129
HostPort: webServerHostPortForPortMapping,
Apr 6, 2017
Apr 6, 2017
130
},
131
}
Mar 12, 2026
Mar 12, 2026
132
podID, podConfig = createPodSandboxWithPortMapping(ctx, rc, portMappings, false)
Apr 6, 2017
Apr 6, 2017
133
Jan 30, 2019
Jan 30, 2019
134
By("create a web server container")
Feb 18, 2026
Feb 18, 2026
135
Mar 12, 2026
Mar 12, 2026
136
containerID := createWebServerContainer(ctx, rc, ic, podID, podConfig, "container-for-host-port")
Apr 6, 2017
Apr 6, 2017
137
Jan 30, 2019
Jan 30, 2019
138
By("start the web server container")
Mar 12, 2026
Mar 12, 2026
139
startContainer(ctx, rc, containerID)
Apr 6, 2017
Apr 6, 2017
140
141
By("check the port mapping with host port and container port")
Mar 12, 2026
Mar 12, 2026
142
checkMainPage(ctx, rc, "", webServerHostPortForPortMapping, 0)
Apr 6, 2017
Apr 6, 2017
143
})
144
})
145
})
146
Sep 24, 2019
Sep 24, 2019
147
// createPodSandWithHostname create a PodSandbox with hostname.
Mar 12, 2026
Mar 12, 2026
148
func createPodSandWithHostname(ctx context.Context, c internalapi.RuntimeService, hostname string) (string, *runtimeapi.PodSandboxConfig) {
Sep 24, 2019
Sep 24, 2019
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,
Dec 5, 2023
Dec 5, 2023
156
Linux: &runtimeapi.LinuxPodSandboxConfig{
Mar 12, 2026
Mar 12, 2026
157
CgroupParent: common.GetCgroupParent(ctx, c),
Dec 5, 2023
Dec 5, 2023
158
},
Sep 24, 2019
Sep 24, 2019
159
}
160
Mar 12, 2026
Mar 12, 2026
161
podID := framework.RunPodSandbox(ctx, c, config)
Jan 27, 2025
Jan 27, 2025
162
Sep 24, 2019
Sep 24, 2019
163
return podID, config
164
}
165
Apr 18, 2017
Apr 18, 2017
166
// createPodSandWithDNSConfig create a PodSandbox with DNS config.
Mar 12, 2026
Mar 12, 2026
167
func createPodSandWithDNSConfig(ctx context.Context, c internalapi.RuntimeService) (string, *runtimeapi.PodSandboxConfig) {
Apr 6, 2017
Apr 6, 2017
168
podSandboxName := "create-PodSandbox-with-DNS-config" + framework.NewUUID()
May 9, 2017
May 9, 2017
169
uid := framework.DefaultUIDPrefix + framework.NewUUID()
170
namespace := framework.DefaultNamespacePrefix + framework.NewUUID()
Apr 6, 2017
Apr 6, 2017
171
config := &runtimeapi.PodSandboxConfig{
May 9, 2017
May 9, 2017
172
Metadata: framework.BuildPodSandboxMetadata(podSandboxName, uid, namespace, framework.DefaultAttempt),
Apr 6, 2017
Apr 6, 2017
173
DnsConfig: &runtimeapi.DNSConfig{
174
Servers: []string{defaultDNSServer},
175
Searches: []string{defaultDNSSearch},
Apr 6, 2017
Apr 6, 2017
176
Options: []string{defaultDNSOption},
Apr 6, 2017
Apr 6, 2017
177
},
Dec 5, 2023
Dec 5, 2023
178
Linux: &runtimeapi.LinuxPodSandboxConfig{
Mar 12, 2026
Mar 12, 2026
179
CgroupParent: common.GetCgroupParent(ctx, c),
Dec 5, 2023
Dec 5, 2023
180
},
Jan 30, 2019
Jan 30, 2019
181
Labels: framework.DefaultPodLabels,
Apr 6, 2017
Apr 6, 2017
182
}
183
Mar 12, 2026
Mar 12, 2026
184
podID := framework.RunPodSandbox(ctx, c, config)
Jan 27, 2025
Jan 27, 2025
185
Apr 6, 2017
Apr 6, 2017
186
return podID, config
187
}
188
Apr 18, 2017
Apr 18, 2017
189
// createPodSandboxWithPortMapping create a PodSandbox with port mapping.
Mar 12, 2026
Mar 12, 2026
190
func createPodSandboxWithPortMapping(ctx context.Context, c internalapi.RuntimeService, portMappings []*runtimeapi.PortMapping, hostNet bool) (string, *runtimeapi.PodSandboxConfig) {
Apr 6, 2017
Apr 6, 2017
191
podSandboxName := "create-PodSandbox-with-port-mapping" + framework.NewUUID()
May 9, 2017
May 9, 2017
192
uid := framework.DefaultUIDPrefix + framework.NewUUID()
193
namespace := framework.DefaultNamespacePrefix + framework.NewUUID()
Jan 27, 2025
Jan 27, 2025
194
Apr 6, 2017
Apr 6, 2017
195
config := &runtimeapi.PodSandboxConfig{
May 9, 2017
May 9, 2017
196
Metadata: framework.BuildPodSandboxMetadata(podSandboxName, uid, namespace, framework.DefaultAttempt),
Apr 6, 2017
Apr 6, 2017
197
PortMappings: portMappings,
Dec 5, 2023
Dec 5, 2023
198
Linux: &runtimeapi.LinuxPodSandboxConfig{
Mar 12, 2026
Mar 12, 2026
199
CgroupParent: common.GetCgroupParent(ctx, c),
Dec 5, 2023
Dec 5, 2023
200
},
201
Labels: framework.DefaultPodLabels,
Apr 6, 2017
Apr 6, 2017
202
}
Apr 17, 2018
Apr 17, 2018
203
if hostNet {
204
config.Linux.SecurityContext = &runtimeapi.LinuxSandboxSecurityContext{
205
NamespaceOptions: &runtimeapi.NamespaceOption{
206
Network: runtimeapi.NamespaceMode_NODE,
207
},
208
}
209
}
Apr 6, 2017
Apr 6, 2017
210
Mar 12, 2026
Mar 12, 2026
211
podID := framework.RunPodSandbox(ctx, c, config)
Jan 27, 2025
Jan 27, 2025
212
Apr 6, 2017
Apr 6, 2017
213
return podID, config
214
}
215
Sep 24, 2019
Sep 24, 2019
216
// checkHostname checks the container hostname.
Mar 12, 2026
Mar 12, 2026
217
func checkHostname(ctx context.Context, c internalapi.RuntimeService, containerID, hostname string) {
Sep 24, 2019
Sep 24, 2019
218
By("get the current hostname via execSync")
Jan 27, 2025
Jan 27, 2025
219
Mar 12, 2026
Mar 12, 2026
220
stdout, stderr, err := c.ExecSync(ctx, containerID, getHostnameCmd, time.Duration(defaultExecSyncTimeout)*time.Second)
Sep 24, 2019
Sep 24, 2019
221
framework.ExpectNoError(err, "failed to execSync in container %q", containerID)
222
Expect(strings.EqualFold(strings.TrimSpace(string(stdout)), hostname)).To(BeTrue())
Feb 22, 2022
Feb 22, 2022
223
Expect(string(stderr)).To(BeEmpty(), "The stderr should be empty.")
Sep 24, 2019
Sep 24, 2019
224
framework.Logf("check hostname succeed")
225
}
226
Apr 6, 2017
Apr 6, 2017
227
// checkDNSConfig checks the content of /etc/resolv.conf.
Mar 12, 2026
Mar 12, 2026
228
func checkDNSConfig(ctx context.Context, c internalapi.RuntimeService, containerID string, expectedContent []string) {
Jan 30, 2019
Jan 30, 2019
229
By("get the current dns config via execSync")
Jan 27, 2025
Jan 27, 2025
230
Mar 12, 2026
Mar 12, 2026
231
stdout, stderr, err := c.ExecSync(ctx, containerID, getDNSConfigCmd, time.Duration(defaultExecSyncTimeout)*time.Second)
Apr 6, 2017
Apr 6, 2017
232
framework.ExpectNoError(err, "failed to execSync in container %q", containerID)
Jan 27, 2025
Jan 27, 2025
233
Apr 6, 2017
Apr 6, 2017
234
for _, content := range expectedContent {
235
Expect(string(stdout)).To(ContainSubstring(content), "The stdout output of execSync should contain %q", content)
236
}
Jan 27, 2025
Jan 27, 2025
237
Feb 22, 2022
Feb 22, 2022
238
Expect(string(stderr)).To(BeEmpty(), "The stderr should be empty.")
Apr 6, 2017
Apr 6, 2017
239
framework.Logf("check DNS config succeed")
240
}
241
Jul 22, 2024
Jul 22, 2024
242
// createWebServerContainer creates a container running a web server.
Mar 12, 2026
Mar 12, 2026
243
func createWebServerContainer(ctx context.Context, rc internalapi.RuntimeService, ic internalapi.ImageManagerService, podID string, podConfig *runtimeapi.PodSandboxConfig, prefix string) string {
Apr 6, 2017
Apr 6, 2017
244
containerName := prefix + framework.NewUUID()
245
containerConfig := &runtimeapi.ContainerConfig{
May 10, 2017
May 10, 2017
246
Metadata: framework.BuildContainerMetadata(containerName, framework.DefaultAttempt),
Jan 30, 2019
Jan 30, 2019
247
Image: &runtimeapi.ImageSpec{Image: webServerImage},
Apr 6, 2017
Apr 6, 2017
248
Linux: &runtimeapi.LinuxContainerConfig{},
249
}
Jan 27, 2025
Jan 27, 2025
250
Mar 12, 2026
Mar 12, 2026
251
return framework.CreateContainer(ctx, rc, ic, containerConfig, podID, podConfig)
Apr 17, 2018
Apr 17, 2018
252
}
253
Jan 30, 2019
Jan 30, 2019
254
// createHostNetWebServerContainer creates a web server container using webServerHostNetContainerPort.
Mar 12, 2026
Mar 12, 2026
255
func createHostNetWebServerContainer(ctx context.Context, rc internalapi.RuntimeService, ic internalapi.ImageManagerService, podID string, podConfig *runtimeapi.PodSandboxConfig, prefix string) string {
Apr 17, 2018
Apr 17, 2018
256
containerName := prefix + framework.NewUUID()
257
containerConfig := &runtimeapi.ContainerConfig{
258
Metadata: framework.BuildContainerMetadata(containerName, framework.DefaultAttempt),
Jan 30, 2019
Jan 30, 2019
259
Image: &runtimeapi.ImageSpec{Image: hostNetWebServerImage},
Apr 17, 2018
Apr 17, 2018
260
Linux: &runtimeapi.LinuxContainerConfig{},
261
}
Jan 27, 2025
Jan 27, 2025
262
Mar 12, 2026
Mar 12, 2026
263
return framework.CreateContainer(ctx, rc, ic, containerConfig, podID, podConfig)
Apr 6, 2017
Apr 6, 2017
264
}
265
Oct 3, 2018
Oct 3, 2018
266
// checkMainPage check if the we can get the main page of the pod via given IP:port.
Mar 12, 2026
Mar 12, 2026
267
func checkMainPage(ctx context.Context, c internalapi.RuntimeService, podID string, hostPort, containerPort int32) {
Apr 6, 2017
Apr 6, 2017
268
By("get the IP:port needed to be checked")
Apr 24, 2017
Apr 24, 2017
269
Apr 6, 2017
Apr 6, 2017
270
url := "http://"
Feb 20, 2018
Feb 20, 2018
271
if hostPort != 0 {
272
url += "127.0.0.1:" + strconv.Itoa(int(hostPort))
Apr 24, 2017
Apr 24, 2017
273
} else {
Mar 12, 2026
Mar 12, 2026
274
status := getPodSandboxStatus(ctx, c, podID)
Apr 6, 2017
Apr 6, 2017
275
Expect(status.GetNetwork()).NotTo(BeNil(), "The network in status should not be nil.")
Aug 11, 2025
Aug 11, 2025
276
Expect(status.GetNetwork().GetIp()).NotTo(BeNil(), "The IP should not be nil.")
277
url += status.GetNetwork().GetIp() + ":" + strconv.Itoa(int(containerPort))
Apr 6, 2017
Apr 6, 2017
278
}
Jan 27, 2025
Jan 27, 2025
279
Apr 6, 2017
Apr 6, 2017
280
framework.Logf("the IP:port is " + url)
Apr 6, 2017
Apr 6, 2017
281
282
By("check the content of " + url)
Apr 24, 2017
Apr 24, 2017
283
Jul 24, 2024
Jul 24, 2024
284
respChan := make(chan *http.Response, 1)
285
defer close(respChan)
286
Apr 24, 2017
Apr 24, 2017
287
Eventually(func() error {
Mar 12, 2026
Mar 12, 2026
288
tctx, cancel := context.WithTimeout(ctx, time.Minute)
Jul 24, 2024
Jul 24, 2024
289
defer cancel()
290
Mar 12, 2026
Mar 12, 2026
291
req, err := http.NewRequestWithContext(tctx, http.MethodGet, url, http.NoBody)
Jul 24, 2024
Jul 24, 2024
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()
Nov 18, 2025
Nov 18, 2025
301
Jul 24, 2024
Jul 24, 2024
302
respChan <- resp
Jan 27, 2025
Jan 27, 2025
303
Jul 24, 2024
Jul 24, 2024
304
return nil
Dec 9, 2024
Dec 9, 2024
305
}, time.Minute, time.Second).Should(Succeed())
Apr 24, 2017
Apr 24, 2017
306
Jul 24, 2024
Jul 24, 2024
307
resp := <-respChan
Apr 6, 2017
Apr 6, 2017
308
Expect(resp.StatusCode).To(Equal(200), "The status code of response should be 200.")
309
framework.Logf("check port mapping succeed")
310
}