Skip to content

Commit 4917a42

Browse files
support RuntimeClass.handler, will useful like nvidia isn't the default runtime
Signed-off-by: zhangguanzhang <zhangguanzhang@qq.com>
1 parent b138f52 commit 4917a42

File tree

10 files changed

+231
-39
lines changed

10 files changed

+231
-39
lines changed

core/container_create.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ func (ds *dockerService) CreateContainer(
6767
containerName := makeContainerName(sandboxConfig, config)
6868
mounts := config.GetMounts()
6969
terminationMessagePath, _ := config.Annotations["io.kubernetes.container.terminationMessagePath"]
70+
71+
sandboxInfo, err := ds.client.InspectContainer(r.GetPodSandboxId())
72+
if err != nil {
73+
return nil, fmt.Errorf("unable to get container's sandbox ID: %v", err)
74+
}
7075
createConfig := dockerbackend.ContainerCreateConfig{
7176
Name: containerName,
7277
Config: &container.Config{
@@ -91,6 +96,7 @@ func (ds *dockerService) CreateContainer(
9196
RestartPolicy: container.RestartPolicy{
9297
Name: "no",
9398
},
99+
Runtime: sandboxInfo.HostConfig.Runtime,
94100
},
95101
}
96102

core/container_test.go

Lines changed: 87 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,15 @@ func TestConcurrentlyCreateAndDeleteContainers(t *testing.T) {
6767
podName, namespace := "foo", "bar"
6868
containerName, image := "sidecar", "logger"
6969

70+
type podInfo struct {
71+
ContainerId string
72+
SandboxID string
73+
}
74+
7075
const count = 20
7176
configs := make([]*runtimeapi.ContainerConfig, 0, count)
7277
sConfigs := make([]*runtimeapi.PodSandboxConfig, 0, count)
78+
7379
for i := 0; i < count; i++ {
7480
s := makeSandboxConfig(fmt.Sprintf("%s%d", podName, i),
7581
fmt.Sprintf("%s%d", namespace, i), fmt.Sprintf("%d", i), 0)
@@ -80,8 +86,8 @@ func TestConcurrentlyCreateAndDeleteContainers(t *testing.T) {
8086
configs = append(configs, c)
8187
}
8288

83-
containerIDs := make(
84-
chan string,
89+
podInfos := make(
90+
chan podInfo,
8591
len(configs),
8692
) // make channel non-blocking to simulate concurrent containers creation
8793

@@ -94,39 +100,64 @@ func TestConcurrentlyCreateAndDeleteContainers(t *testing.T) {
94100

95101
go func() {
96102
creationWg.Wait()
97-
close(containerIDs)
103+
close(podInfos)
98104
}()
99105
for i := range configs {
100106
go func(i int) {
101107
defer creationWg.Done()
102-
// We don't care about the sandbox id; pass a bogus one.
103-
sandboxID := fmt.Sprintf("sandboxid%d", i)
108+
109+
runSandboxResp, err := ds.RunPodSandbox(getTestCTX(), &runtimeapi.RunPodSandboxRequest{
110+
Config: sConfigs[i],
111+
})
112+
if err != nil {
113+
t.Errorf("RunPodSandbox: %v", err)
114+
return
115+
}
116+
104117
req := &runtimeapi.CreateContainerRequest{
105-
PodSandboxId: sandboxID,
118+
PodSandboxId: runSandboxResp.PodSandboxId,
106119
Config: configs[i],
107120
SandboxConfig: sConfigs[i],
108121
}
122+
109123
createResp, err := ds.CreateContainer(getTestCTX(), req)
110124
if err != nil {
111125
t.Errorf("CreateContainer: %v", err)
112126
return
113127
}
114-
containerIDs <- createResp.ContainerId
128+
podInfos <- podInfo{
129+
ContainerId: createResp.ContainerId,
130+
SandboxID: runSandboxResp.PodSandboxId,
131+
}
115132
}(i)
116133
}
117134

118-
for containerID := range containerIDs {
135+
for pod := range podInfos {
119136
deletionWg.Add(1)
120-
go func(id string) {
137+
go func(i podInfo) {
121138
defer deletionWg.Done()
122139
_, err := ds.RemoveContainer(
123140
getTestCTX(),
124-
&runtimeapi.RemoveContainerRequest{ContainerId: id},
141+
&runtimeapi.RemoveContainerRequest{ContainerId: i.ContainerId},
125142
)
126143
if err != nil {
127144
t.Errorf("RemoveContainer: %v", err)
128145
}
129-
}(containerID)
146+
_, err = ds.StopPodSandbox(
147+
getTestCTX(),
148+
&runtimeapi.StopPodSandboxRequest{PodSandboxId: i.SandboxID},
149+
)
150+
if err != nil {
151+
t.Errorf("StopPodSandbox: %v", err)
152+
}
153+
_, err = ds.RemovePodSandbox(
154+
getTestCTX(),
155+
&runtimeapi.RemovePodSandboxRequest{PodSandboxId: i.SandboxID},
156+
)
157+
if err != nil {
158+
t.Errorf("RemovePodSandbox: %v", err)
159+
}
160+
}(pod)
130161
}
131162
deletionWg.Wait()
132163
}
@@ -155,10 +186,15 @@ func TestListContainers(t *testing.T) {
155186
state := runtimeapi.ContainerState_CONTAINER_RUNNING
156187
var createdAt int64 = fakeClock.Now().UnixNano()
157188
for i := range configs {
158-
// We don't care about the sandbox id; pass a bogus one.
159-
sandboxID := fmt.Sprintf("sandboxid%d", i)
189+
runSandboxResp, err := ds.RunPodSandbox(getTestCTX(), &runtimeapi.RunPodSandboxRequest{
190+
Config: sConfigs[i],
191+
})
192+
if err != nil {
193+
t.Errorf("RunPodSandbox: %v", err)
194+
return
195+
}
160196
req := &runtimeapi.CreateContainerRequest{
161-
PodSandboxId: sandboxID,
197+
PodSandboxId: runSandboxResp.PodSandboxId,
162198
Config: configs[i],
163199
SandboxConfig: sConfigs[i],
164200
}
@@ -174,7 +210,7 @@ func TestListContainers(t *testing.T) {
174210
expected = append([]*runtimeapi.Container{{
175211
Metadata: configs[i].Metadata,
176212
Id: id,
177-
PodSandboxId: sandboxID,
213+
PodSandboxId: runSandboxResp.PodSandboxId,
178214
State: state,
179215
CreatedAt: createdAt,
180216
Image: configs[i].Image,
@@ -226,12 +262,20 @@ func TestContainerStatus(t *testing.T) {
226262

227263
fDocker.InjectImages([]dockertypes.ImageSummary{{ID: imageName}})
228264

265+
runSandboxResp, err := ds.RunPodSandbox(getTestCTX(), &runtimeapi.RunPodSandboxRequest{
266+
Config: sConfig,
267+
})
268+
if err != nil {
269+
t.Errorf("RunPodSandbox: %v", err)
270+
return
271+
}
272+
229273
// Create the container.
230274
fClock.SetTime(time.Now().Add(-1 * time.Hour))
231275
expected.CreatedAt = fClock.Now().UnixNano()
232276

233277
req := &runtimeapi.CreateContainerRequest{
234-
PodSandboxId: sandboxID,
278+
PodSandboxId: runSandboxResp.PodSandboxId,
235279
Config: config,
236280
SandboxConfig: sConfig,
237281
}
@@ -243,7 +287,7 @@ func TestContainerStatus(t *testing.T) {
243287
c, err := fDocker.InspectContainer(id)
244288
require.NoError(t, err)
245289
assert.Equal(t, c.Config.Labels[containerTypeLabelKey], containerTypeLabelContainer)
246-
assert.Equal(t, c.Config.Labels[sandboxIDLabelKey], sandboxID)
290+
assert.Equal(t, c.Config.Labels[sandboxIDLabelKey], runSandboxResp.PodSandboxId)
247291

248292
// Set the id manually since we don't know the id until it's created.
249293
expected.Id = id
@@ -309,8 +353,16 @@ func TestContainerLogPath(t *testing.T) {
309353
config := makeContainerConfig(sConfig, "pause", "iamimage", 0, nil, nil)
310354
config.LogPath = containerLogPath
311355

356+
runSandboxResp, err := ds.RunPodSandbox(getTestCTX(), &runtimeapi.RunPodSandboxRequest{
357+
Config: sConfig,
358+
})
359+
if err != nil {
360+
t.Errorf("RunPodSandbox: %v", err)
361+
return
362+
}
363+
312364
req := &runtimeapi.CreateContainerRequest{
313-
PodSandboxId: sandboxID,
365+
PodSandboxId: runSandboxResp.PodSandboxId,
314366
Config: config,
315367
SandboxConfig: sConfig,
316368
}
@@ -371,43 +423,54 @@ func TestContainerCreationConflict(t *testing.T) {
371423
noContainerError := fmt.Errorf("Error response from daemon: No such container: %s", containerID)
372424
randomError := fmt.Errorf("random error")
373425

426+
// sandBox run called "inspect_image", "pull", "create", "start", "inspect_container",
427+
sandBoxCalls := []string{"inspect_image", "pull", "create", "start", "inspect_container"}
374428
for desc, test := range map[string]struct {
375429
createError error
376430
removeError error
377431
expectError error
378432
expectCalls []string
379433
expectFields int
380434
}{
435+
// sandBox run called "inspect_image", "pull", "create", "start", "inspect_container",
381436
"no create error": {
382-
expectCalls: []string{"create"},
437+
expectCalls: append(sandBoxCalls, []string{"create"}...),
383438
expectFields: 6,
384439
},
385440
"random create error": {
386441
createError: randomError,
387442
expectError: randomError,
388-
expectCalls: []string{"create"},
443+
expectCalls: append(sandBoxCalls, []string{"create"}...),
389444
},
390445
"conflict create error with successful remove": {
391446
createError: conflictError,
392447
expectError: conflictError,
393-
expectCalls: []string{"create", "remove"},
448+
expectCalls: append(sandBoxCalls, []string{"create", "remove"}...),
394449
},
395450
"conflict create error with random remove error": {
396451
createError: conflictError,
397452
removeError: randomError,
398453
expectError: conflictError,
399-
expectCalls: []string{"create", "remove"},
454+
expectCalls: append(sandBoxCalls, []string{"create", "remove"}...),
400455
},
401456
"conflict create error with no such container remove error": {
402457
createError: conflictError,
403458
removeError: noContainerError,
404-
expectCalls: []string{"create", "remove", "create"},
459+
expectCalls: append(sandBoxCalls, []string{"create", "remove", "create"}...),
405460
expectFields: 7,
406461
},
407462
} {
408463
t.Logf("TestCase: %s", desc)
409464
ds, fDocker, _ := newTestDockerService()
410465

466+
runSandboxResp, err := ds.RunPodSandbox(getTestCTX(), &runtimeapi.RunPodSandboxRequest{
467+
Config: sConfig,
468+
})
469+
if err != nil {
470+
require.EqualError(t, err, test.expectError.Error())
471+
continue
472+
}
473+
411474
if test.createError != nil {
412475
fDocker.InjectError("create", test.createError)
413476
}
@@ -416,7 +479,7 @@ func TestContainerCreationConflict(t *testing.T) {
416479
}
417480

418481
req := &runtimeapi.CreateContainerRequest{
419-
PodSandboxId: sandboxID,
482+
PodSandboxId: runSandboxResp.PodSandboxId,
420483
Config: config,
421484
SandboxConfig: sConfig,
422485
}

core/docker_service.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ type dockerService struct {
286286
// methods for more info).
287287
containerCleanupInfos map[string]*containerCleanupInfo
288288
cleanupInfosLock sync.RWMutex
289+
290+
// runtimeInfoLock sync.RWMutex
289291
}
290292

291293
type dockerServiceAlpha struct {

core/docker_service_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"errors"
2121
"math/rand"
2222
"runtime"
23+
"sync"
2324
"testing"
2425
"time"
2526

@@ -46,26 +47,33 @@ func newTestNetworkPlugin(t *testing.T) *nettest.MockNetworkPlugin {
4647
}
4748

4849
type mockCheckpointManager struct {
50+
lock sync.Mutex
4951
checkpoint map[string]*PodSandboxCheckpoint
5052
}
5153

5254
func (ckm *mockCheckpointManager) CreateCheckpoint(
5355
checkpointKey string,
5456
checkpoint store.Checkpoint,
5557
) error {
58+
ckm.lock.Lock()
5659
ckm.checkpoint[checkpointKey] = checkpoint.(*PodSandboxCheckpoint)
60+
ckm.lock.Unlock()
5761
return nil
5862
}
5963

6064
func (ckm *mockCheckpointManager) GetCheckpoint(
6165
checkpointKey string,
6266
checkpoint store.Checkpoint,
6367
) error {
68+
ckm.lock.Lock()
69+
defer ckm.lock.Unlock()
6470
*(checkpoint.(*PodSandboxCheckpoint)) = *(ckm.checkpoint[checkpointKey])
6571
return nil
6672
}
6773

6874
func (ckm *mockCheckpointManager) RemoveCheckpoint(checkpointKey string) error {
75+
ckm.lock.Lock()
76+
defer ckm.lock.Unlock()
6977
_, ok := ckm.checkpoint[checkpointKey]
7078
if ok {
7179
delete(ckm.checkpoint, "moo")
@@ -75,14 +83,19 @@ func (ckm *mockCheckpointManager) RemoveCheckpoint(checkpointKey string) error {
7583

7684
func (ckm *mockCheckpointManager) ListCheckpoints() ([]string, error) {
7785
var keys []string
86+
ckm.lock.Lock()
87+
defer ckm.lock.Unlock()
7888
for key := range ckm.checkpoint {
7989
keys = append(keys, key)
8090
}
8191
return keys, nil
8292
}
8393

8494
func newMockCheckpointManager() store.CheckpointManager {
85-
return &mockCheckpointManager{checkpoint: make(map[string]*PodSandboxCheckpoint)}
95+
return &mockCheckpointManager{
96+
checkpoint: make(map[string]*PodSandboxCheckpoint),
97+
lock: sync.Mutex{},
98+
}
8699
}
87100

88101
func newTestDockerService() (*dockerService, *libdocker.FakeDockerClient, *clock.FakeClock) {

core/sandbox_helpers.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,24 @@ var (
5858
defaultSandboxGracePeriod = time.Duration(10) * time.Second
5959
)
6060

61+
// check Runtime correct
62+
func (ds *dockerService) IsRuntimeConfigured(runtime string) error {
63+
info, err := ds.getDockerInfo()
64+
if err != nil {
65+
return fmt.Errorf("failed to get docker info: %v", err)
66+
}
67+
68+
// ds.runtimeInfoLock.RLock()
69+
for r := range info.Runtimes {
70+
if r == runtime {
71+
return nil
72+
}
73+
}
74+
// ds.runtimeInfoLock.RUnlock()
75+
76+
return fmt.Errorf("no runtime for %q is configured", runtime)
77+
}
78+
6179
// Returns whether the sandbox network is ready, and whether the sandbox is known
6280
func (ds *dockerService) getNetworkReady(podSandboxID string) (bool, bool) {
6381
ds.networkReadyLock.Lock()

0 commit comments

Comments
 (0)