Skip to content

Commit 7e16211

Browse files
committed
docker:send GPU instance config only when GPUs are present
1 parent 8a00dfb commit 7e16211

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

ecs-init/docker/docker.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,11 @@ func (c *Client) LoadEnvVars() map[string]string {
253253
envVariables := make(map[string]string)
254254
// merge in instance-specific environment variables
255255
for envKey, envValue := range c.loadCustomInstanceEnvVars() {
256+
if envKey == config.GPUSupportEnvVar && envValue == "true" {
257+
if !nvidiaGPUDevicesPresent() {
258+
continue
259+
}
260+
}
256261
envVariables[envKey] = envValue
257262
}
258263

ecs-init/docker/docker_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,13 @@ func TestGetInstanceConfig(t *testing.T) {
482482
defer mockCtrl.Finish()
483483

484484
envFile := "\nECS_ENABLE_GPU_SUPPORT=true\n"
485+
defer func() {
486+
MatchFilePatternForGPU = FilePatternMatchForGPU
487+
}()
488+
MatchFilePatternForGPU = func(pattern string) ([]string, error) {
489+
// GPU device present
490+
return []string{"/dev/nvidia0"}, nil
491+
}
485492

486493
mockFS := NewMockfileSystem(mockCtrl)
487494

@@ -500,6 +507,38 @@ func TestGetInstanceConfig(t *testing.T) {
500507
expectKey("ECS_ENABLE_GPU_SUPPORT=true", envVariables, t)
501508
}
502509

510+
func TestGetNonGPUInstanceConfig(t *testing.T) {
511+
mockCtrl := gomock.NewController(t)
512+
defer mockCtrl.Finish()
513+
514+
envFile := "\nECS_ENABLE_GPU_SUPPORT=true\n"
515+
defer func() {
516+
MatchFilePatternForGPU = FilePatternMatchForGPU
517+
}()
518+
MatchFilePatternForGPU = func(pattern string) ([]string, error) {
519+
// matches is nil
520+
return nil, nil
521+
}
522+
523+
mockFS := NewMockfileSystem(mockCtrl)
524+
525+
mockFS.EXPECT().ReadFile(config.InstanceConfigFile()).Return([]byte(envFile), nil)
526+
mockFS.EXPECT().ReadFile(config.AgentConfigFile()).Return(nil, errors.New("not found"))
527+
528+
client := &Client{
529+
fs: mockFS,
530+
}
531+
cfg := client.getContainerConfig()
532+
533+
envVariables := make(map[string]struct{})
534+
for _, envVar := range cfg.Env {
535+
envVariables[envVar] = struct{}{}
536+
}
537+
if _, ok := envVariables["ECS_ENABLE_GPU_SUPPORT=true"]; ok {
538+
t.Errorf("Expected ECS_ENABLE_GPU_SUPPORT=true to be not present")
539+
}
540+
}
541+
503542
func TestGetConfigOverrides(t *testing.T) {
504543
mockCtrl := gomock.NewController(t)
505544
defer mockCtrl.Finish()

0 commit comments

Comments
 (0)