mirror of
				https://github.com/moby/moby.git
				synced 2022-11-09 12:21:53 -05:00 
			
		
		
		
	Add DeviceRequests to HostConfig to support NVIDIA GPUs
This patch hard-codes support for NVIDIA GPUs. In a future patch it should move out into its own Device Plugin. Signed-off-by: Tibor Vass <tibor@docker.com>
This commit is contained in:
		
							parent
							
								
									36d2c8b48e
								
							
						
					
					
						commit
						8f936ae8cf
					
				
					 10 changed files with 529 additions and 8 deletions
				
			
		
							
								
								
									
										23
									
								
								pkg/capabilities/caps.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								pkg/capabilities/caps.go
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,23 @@
 | 
			
		|||
// Package capabilities allows to generically handle capabilities.
 | 
			
		||||
package capabilities
 | 
			
		||||
 | 
			
		||||
// Set represents a set of capabilities.
 | 
			
		||||
type Set map[string]struct{}
 | 
			
		||||
 | 
			
		||||
// Match tries to match set with caps, which is an OR list of AND lists of capabilities.
 | 
			
		||||
// The matched AND list of capabilities is returned; or nil if none are matched.
 | 
			
		||||
func (set Set) Match(caps [][]string) []string {
 | 
			
		||||
	if set == nil {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
anyof:
 | 
			
		||||
	for _, andList := range caps {
 | 
			
		||||
		for _, cap := range andList {
 | 
			
		||||
			if _, ok := set[cap]; !ok {
 | 
			
		||||
				continue anyof
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		return andList
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										72
									
								
								pkg/capabilities/caps_test.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										72
									
								
								pkg/capabilities/caps_test.go
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,72 @@
 | 
			
		|||
package capabilities
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"testing"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestMatch(t *testing.T) {
 | 
			
		||||
	set := Set{
 | 
			
		||||
		"foo": struct{}{},
 | 
			
		||||
		"bar": struct{}{},
 | 
			
		||||
	}
 | 
			
		||||
	type testcase struct {
 | 
			
		||||
		caps     [][]string
 | 
			
		||||
		expected []string
 | 
			
		||||
	}
 | 
			
		||||
	var testcases = []testcase{
 | 
			
		||||
		// matches
 | 
			
		||||
		{
 | 
			
		||||
			caps:     [][]string{{}},
 | 
			
		||||
			expected: []string{},
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			caps:     [][]string{{"foo"}},
 | 
			
		||||
			expected: []string{"foo"},
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			caps:     [][]string{{"bar"}, {"foo"}},
 | 
			
		||||
			expected: []string{"bar"},
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			caps:     [][]string{{"foo", "bar"}},
 | 
			
		||||
			expected: []string{"foo", "bar"},
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			caps:     [][]string{{"qux"}, {"foo"}},
 | 
			
		||||
			expected: []string{"foo"},
 | 
			
		||||
		},
 | 
			
		||||
		{
 | 
			
		||||
			caps:     [][]string{{"foo", "bar"}, {"baz"}, {"bar"}},
 | 
			
		||||
			expected: []string{"foo", "bar"},
 | 
			
		||||
		},
 | 
			
		||||
 | 
			
		||||
		// non matches
 | 
			
		||||
		{caps: nil},
 | 
			
		||||
		{caps: [][]string{}},
 | 
			
		||||
		{caps: [][]string{{"qux"}}},
 | 
			
		||||
		{caps: [][]string{{"foo", "bar", "qux"}}},
 | 
			
		||||
		{caps: [][]string{{"qux"}, {"baz"}}},
 | 
			
		||||
		{caps: [][]string{{"foo", "baz"}}},
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, m := range testcases {
 | 
			
		||||
		t.Run(fmt.Sprintf("%v", m.caps), func(t *testing.T) {
 | 
			
		||||
			selected := set.Match(m.caps)
 | 
			
		||||
			if m.expected == nil || selected == nil {
 | 
			
		||||
				if m.expected == nil && selected == nil {
 | 
			
		||||
					return
 | 
			
		||||
				}
 | 
			
		||||
				t.Fatalf("selected = %v, expected = %v", selected, m.expected)
 | 
			
		||||
			}
 | 
			
		||||
			if len(selected) != len(m.expected) {
 | 
			
		||||
				t.Fatalf("len(selected) = %d, len(expected) = %d", len(selected), len(m.expected))
 | 
			
		||||
			}
 | 
			
		||||
			for i, s := range selected {
 | 
			
		||||
				if m.expected[i] != s {
 | 
			
		||||
					t.Fatalf("selected[%d] = %s, expected[%d] = %s", i, s, i, m.expected[i])
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
		})
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue