github.com/kubernetes-incubator/kube-aws@v0.16.4/pkg/api/gpu.go (about)

     1  package api
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"github.com/kubernetes-incubator/kube-aws/logger"
     7  	"strings"
     8  )
     9  
    10  var GPUEnabledInstanceFamily = []string{"p2", "p3", "g2", "g3"}
    11  
    12  type Gpu struct {
    13  	Nvidia NvidiaSetting `yaml:"nvidia"`
    14  }
    15  
    16  type NvidiaSetting struct {
    17  	Enabled bool   `yaml:"enabled,omitempty"`
    18  	Version string `yaml:"version,omitempty"`
    19  }
    20  
    21  func isGpuEnabledInstanceType(instanceType string) bool {
    22  	for _, family := range GPUEnabledInstanceFamily {
    23  		if strings.HasPrefix(instanceType, family) {
    24  			return true
    25  		}
    26  	}
    27  	return false
    28  }
    29  
    30  func newDefaultGpu() Gpu {
    31  	return Gpu{
    32  		Nvidia: NvidiaSetting{
    33  			Enabled: false,
    34  			Version: "",
    35  		},
    36  	}
    37  }
    38  
    39  // This function is used when rendering cloud-config-worker
    40  func (c NvidiaSetting) IsEnabledOn(instanceType string) bool {
    41  	return isGpuEnabledInstanceType(instanceType) && c.Enabled
    42  }
    43  
    44  func (c Gpu) Validate(instanceType string, experimentalGpuSupportEnabled bool) error {
    45  	if c.Nvidia.Enabled && !isGpuEnabledInstanceType(instanceType) {
    46  		return errors.New(fmt.Sprintf("instance type %v doesn't support GPU. You can enable Nvidia driver intallation support only when use %v instance family.", instanceType, GPUEnabledInstanceFamily))
    47  	}
    48  	if !c.Nvidia.Enabled && !experimentalGpuSupportEnabled && isGpuEnabledInstanceType(instanceType) {
    49  		logger.Warnf("Nvidia GPU driver intallation is disabled although instance type %v does support GPU.  You have to install Nvidia GPU driver by yourself to schedule gpu resource.\n", instanceType)
    50  	}
    51  	if c.Nvidia.Enabled && experimentalGpuSupportEnabled {
    52  		return errors.New(`Only one of gpu.nvidia.enabled and experimental.gpuSupport.enabled are allowed at one time.`)
    53  	}
    54  	if c.Nvidia.Enabled && len(c.Nvidia.Version) == 0 {
    55  		return errors.New(`gpu.nvidia.version must not be empty when gpu.nvidia is enabled.`)
    56  	}
    57  
    58  	return nil
    59  }